2 Unix SMB/CIFS implementation.
4 a pass-thru NTVFS module to setup a security context using unix
7 Copyright (C) Andrew Tridgell 2004
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "system/filesys.h"
25 #include "system/passwd.h"
26 #include "auth/auth.h"
27 #include "ntvfs/ntvfs.h"
28 #include "libcli/wbclient/wbclient.h"
29 #define TEVENT_DEPRECATED
32 #if defined(UID_WRAPPER)
33 #if !defined(UID_WRAPPER_REPLACE) && !defined(UID_WRAPPER_NOT_REPLACE)
34 #define UID_WRAPPER_REPLACE
35 #include "../uid_wrapper/uid_wrapper.h"
38 #define uwrap_enabled() 0
42 struct unixuid_private {
43 struct wbc_context *wbc_ctx;
44 struct unix_sec_ctx *last_sec_ctx;
45 struct security_token *last_token;
58 pull the current security context into a unix_sec_ctx
60 static struct unix_sec_ctx *save_unix_security(TALLOC_CTX *mem_ctx)
62 struct unix_sec_ctx *sec = talloc(mem_ctx, struct unix_sec_ctx);
68 sec->ngroups = getgroups(0, NULL);
69 if (sec->ngroups == -1) {
73 sec->groups = talloc_array(sec, gid_t, sec->ngroups);
74 if (sec->groups == NULL) {
79 if (getgroups(sec->ngroups, sec->groups) != sec->ngroups) {
88 set the current security context from a unix_sec_ctx
90 static NTSTATUS set_unix_security(struct unix_sec_ctx *sec)
94 if (setgroups(sec->ngroups, sec->groups) != 0) {
95 return NT_STATUS_ACCESS_DENIED;
97 if (setegid(sec->gid) != 0) {
98 return NT_STATUS_ACCESS_DENIED;
100 if (seteuid(sec->uid) != 0) {
101 return NT_STATUS_ACCESS_DENIED;
106 static int unixuid_nesting_level;
109 called at the start and end of a tevent nesting loop. Needs to save/restore
110 unix security context
112 static int unixuid_event_nesting_hook(struct tevent_context *ev,
117 const char *location)
119 struct unix_sec_ctx *sec_ctx;
121 if (unixuid_nesting_level == 0) {
122 /* we don't need to do anything unless we are nested
123 inside of a call in this module */
128 sec_ctx = save_unix_security(ev);
129 if (sec_ctx == NULL) {
130 DEBUG(0,("%s: Failed to save security context\n", location));
133 *(struct unix_sec_ctx **)stack_ptr = sec_ctx;
134 if (seteuid(0) != 0 || setegid(0) != 0) {
135 DEBUG(0,("%s: Failed to change to root\n", location));
139 /* called when we come out of a nesting level */
142 sec_ctx = *(struct unix_sec_ctx **)stack_ptr;
143 if (sec_ctx == NULL) {
144 /* this happens the first time this function
145 is called, as we install the hook while
146 inside an event in unixuid_connect() */
150 sec_ctx = talloc_get_type_abort(sec_ctx, struct unix_sec_ctx);
151 status = set_unix_security(sec_ctx);
152 talloc_free(sec_ctx);
153 if (!NT_STATUS_IS_OK(status)) {
154 DEBUG(0,("%s: Failed to revert security context (%s)\n",
155 location, nt_errstr(status)));
165 form a unix_sec_ctx from the current security_token
167 static NTSTATUS nt_token_to_unix_security(struct ntvfs_module_context *ntvfs,
168 struct ntvfs_request *req,
169 struct security_token *token,
170 struct unix_sec_ctx **sec)
172 struct unixuid_private *priv = ntvfs->private_data;
176 struct composite_context *ctx;
177 *sec = talloc(req, struct unix_sec_ctx);
179 /* we can't do unix security without a user and group */
180 if (token->num_sids < 2) {
181 return NT_STATUS_ACCESS_DENIED;
184 ids = talloc_array(req, struct id_map, token->num_sids);
185 NT_STATUS_HAVE_NO_MEMORY(ids);
187 (*sec)->ngroups = token->num_sids - 2;
188 (*sec)->groups = talloc_array(*sec, gid_t, (*sec)->ngroups);
189 NT_STATUS_HAVE_NO_MEMORY((*sec)->groups);
191 for (i=0;i<token->num_sids;i++) {
192 ZERO_STRUCT(ids[i].xid);
193 ids[i].sid = token->sids[i];
194 ids[i].status = ID_UNKNOWN;
197 ctx = wbc_sids_to_xids_send(priv->wbc_ctx, ids, token->num_sids, ids);
198 NT_STATUS_HAVE_NO_MEMORY(ctx);
200 status = wbc_sids_to_xids_recv(ctx, &ids);
201 NT_STATUS_NOT_OK_RETURN(status);
203 if (ids[0].xid.type == ID_TYPE_BOTH ||
204 ids[0].xid.type == ID_TYPE_UID) {
205 (*sec)->uid = ids[0].xid.id;
207 return NT_STATUS_INVALID_SID;
210 if (ids[1].xid.type == ID_TYPE_BOTH ||
211 ids[1].xid.type == ID_TYPE_GID) {
212 (*sec)->gid = ids[1].xid.id;
214 return NT_STATUS_INVALID_SID;
217 for (i=0;i<(*sec)->ngroups;i++) {
218 if (ids[i+2].xid.type == ID_TYPE_BOTH ||
219 ids[i+2].xid.type == ID_TYPE_GID) {
220 (*sec)->groups[i] = ids[i+2].xid.id;
222 return NT_STATUS_INVALID_SID;
230 setup our unix security context according to the session authentication info
232 static NTSTATUS unixuid_setup_security(struct ntvfs_module_context *ntvfs,
233 struct ntvfs_request *req, struct unix_sec_ctx **sec)
235 struct unixuid_private *priv = ntvfs->private_data;
236 struct security_token *token;
237 struct unix_sec_ctx *newsec;
240 /* If we are asked to set up, but have not had a successful
241 * session setup or tree connect, then these may not be filled
242 * in. ACCESS_DENIED is the right error code here */
243 if (req->session_info == NULL || priv == NULL) {
244 return NT_STATUS_ACCESS_DENIED;
247 token = req->session_info->security_token;
249 *sec = save_unix_security(ntvfs);
251 return NT_STATUS_NO_MEMORY;
254 if (token == priv->last_token) {
255 newsec = priv->last_sec_ctx;
257 status = nt_token_to_unix_security(ntvfs, req, token, &newsec);
258 if (!NT_STATUS_IS_OK(status)) {
262 if (priv->last_sec_ctx) {
263 talloc_free(priv->last_sec_ctx);
265 priv->last_sec_ctx = newsec;
266 priv->last_token = token;
267 talloc_steal(priv, newsec);
270 status = set_unix_security(newsec);
271 if (!NT_STATUS_IS_OK(status)) {
280 this pass through macro operates on request contexts
282 #define PASS_THRU_REQ(ntvfs, req, op, args) do { \
284 struct unix_sec_ctx *sec; \
285 status = unixuid_setup_security(ntvfs, req, &sec); \
286 NT_STATUS_NOT_OK_RETURN(status); \
287 unixuid_nesting_level++; \
288 status = ntvfs_next_##op args; \
289 unixuid_nesting_level--; \
290 status2 = set_unix_security(sec); \
292 if (!NT_STATUS_IS_OK(status2)) smb_panic("Unable to reset security context"); \
298 connect to a share - used when a tree_connect operation comes in.
300 static NTSTATUS unixuid_connect(struct ntvfs_module_context *ntvfs,
301 struct ntvfs_request *req, union smb_tcon *tcon)
303 struct unixuid_private *priv;
306 priv = talloc(ntvfs, struct unixuid_private);
308 return NT_STATUS_NO_MEMORY;
311 priv->wbc_ctx = wbc_init(priv, ntvfs->ctx->msg_ctx,
312 ntvfs->ctx->event_ctx);
313 if (priv->wbc_ctx == NULL) {
315 return NT_STATUS_INTERNAL_ERROR;
318 priv->last_sec_ctx = NULL;
319 priv->last_token = NULL;
320 ntvfs->private_data = priv;
322 tevent_loop_set_nesting_hook(ntvfs->ctx->event_ctx,
323 unixuid_event_nesting_hook,
324 &unixuid_nesting_level);
326 /* we don't use PASS_THRU_REQ here, as the connect operation runs with
327 root privileges. This allows the backends to setup any database
328 links they might need during the connect. */
329 status = ntvfs_next_connect(ntvfs, req, tcon);
335 disconnect from a share
337 static NTSTATUS unixuid_disconnect(struct ntvfs_module_context *ntvfs)
339 struct unixuid_private *priv = ntvfs->private_data;
343 ntvfs->private_data = NULL;
345 status = ntvfs_next_disconnect(ntvfs);
354 static NTSTATUS unixuid_unlink(struct ntvfs_module_context *ntvfs,
355 struct ntvfs_request *req,
356 union smb_unlink *unl)
360 PASS_THRU_REQ(ntvfs, req, unlink, (ntvfs, req, unl));
368 static NTSTATUS unixuid_ioctl(struct ntvfs_module_context *ntvfs,
369 struct ntvfs_request *req, union smb_ioctl *io)
373 PASS_THRU_REQ(ntvfs, req, ioctl, (ntvfs, req, io));
379 check if a directory exists
381 static NTSTATUS unixuid_chkpath(struct ntvfs_module_context *ntvfs,
382 struct ntvfs_request *req,
383 union smb_chkpath *cp)
387 PASS_THRU_REQ(ntvfs, req, chkpath, (ntvfs, req, cp));
393 return info on a pathname
395 static NTSTATUS unixuid_qpathinfo(struct ntvfs_module_context *ntvfs,
396 struct ntvfs_request *req, union smb_fileinfo *info)
400 PASS_THRU_REQ(ntvfs, req, qpathinfo, (ntvfs, req, info));
406 query info on a open file
408 static NTSTATUS unixuid_qfileinfo(struct ntvfs_module_context *ntvfs,
409 struct ntvfs_request *req, union smb_fileinfo *info)
413 PASS_THRU_REQ(ntvfs, req, qfileinfo, (ntvfs, req, info));
420 set info on a pathname
422 static NTSTATUS unixuid_setpathinfo(struct ntvfs_module_context *ntvfs,
423 struct ntvfs_request *req, union smb_setfileinfo *st)
427 PASS_THRU_REQ(ntvfs, req, setpathinfo, (ntvfs, req, st));
435 static NTSTATUS unixuid_open(struct ntvfs_module_context *ntvfs,
436 struct ntvfs_request *req, union smb_open *io)
440 PASS_THRU_REQ(ntvfs, req, open, (ntvfs, req, io));
448 static NTSTATUS unixuid_mkdir(struct ntvfs_module_context *ntvfs,
449 struct ntvfs_request *req, union smb_mkdir *md)
453 PASS_THRU_REQ(ntvfs, req, mkdir, (ntvfs, req, md));
461 static NTSTATUS unixuid_rmdir(struct ntvfs_module_context *ntvfs,
462 struct ntvfs_request *req, struct smb_rmdir *rd)
466 PASS_THRU_REQ(ntvfs, req, rmdir, (ntvfs, req, rd));
472 rename a set of files
474 static NTSTATUS unixuid_rename(struct ntvfs_module_context *ntvfs,
475 struct ntvfs_request *req, union smb_rename *ren)
479 PASS_THRU_REQ(ntvfs, req, rename, (ntvfs, req, ren));
487 static NTSTATUS unixuid_copy(struct ntvfs_module_context *ntvfs,
488 struct ntvfs_request *req, struct smb_copy *cp)
492 PASS_THRU_REQ(ntvfs, req, copy, (ntvfs, req, cp));
500 static NTSTATUS unixuid_read(struct ntvfs_module_context *ntvfs,
501 struct ntvfs_request *req, union smb_read *rd)
505 PASS_THRU_REQ(ntvfs, req, read, (ntvfs, req, rd));
513 static NTSTATUS unixuid_write(struct ntvfs_module_context *ntvfs,
514 struct ntvfs_request *req, union smb_write *wr)
518 PASS_THRU_REQ(ntvfs, req, write, (ntvfs, req, wr));
526 static NTSTATUS unixuid_seek(struct ntvfs_module_context *ntvfs,
527 struct ntvfs_request *req,
532 PASS_THRU_REQ(ntvfs, req, seek, (ntvfs, req, io));
540 static NTSTATUS unixuid_flush(struct ntvfs_module_context *ntvfs,
541 struct ntvfs_request *req,
546 PASS_THRU_REQ(ntvfs, req, flush, (ntvfs, req, io));
554 static NTSTATUS unixuid_close(struct ntvfs_module_context *ntvfs,
555 struct ntvfs_request *req, union smb_close *io)
559 PASS_THRU_REQ(ntvfs, req, close, (ntvfs, req, io));
567 static NTSTATUS unixuid_exit(struct ntvfs_module_context *ntvfs,
568 struct ntvfs_request *req)
572 PASS_THRU_REQ(ntvfs, req, exit, (ntvfs, req));
578 logoff - closing files
580 static NTSTATUS unixuid_logoff(struct ntvfs_module_context *ntvfs,
581 struct ntvfs_request *req)
583 struct unixuid_private *priv = ntvfs->private_data;
586 PASS_THRU_REQ(ntvfs, req, logoff, (ntvfs, req));
588 priv->last_token = NULL;
596 static NTSTATUS unixuid_async_setup(struct ntvfs_module_context *ntvfs,
597 struct ntvfs_request *req,
602 PASS_THRU_REQ(ntvfs, req, async_setup, (ntvfs, req, private_data));
608 cancel an async request
610 static NTSTATUS unixuid_cancel(struct ntvfs_module_context *ntvfs,
611 struct ntvfs_request *req)
615 PASS_THRU_REQ(ntvfs, req, cancel, (ntvfs, req));
623 static NTSTATUS unixuid_notify(struct ntvfs_module_context *ntvfs,
624 struct ntvfs_request *req, union smb_notify *info)
628 PASS_THRU_REQ(ntvfs, req, notify, (ntvfs, req, info));
636 static NTSTATUS unixuid_lock(struct ntvfs_module_context *ntvfs,
637 struct ntvfs_request *req, union smb_lock *lck)
641 PASS_THRU_REQ(ntvfs, req, lock, (ntvfs, req, lck));
647 set info on a open file
649 static NTSTATUS unixuid_setfileinfo(struct ntvfs_module_context *ntvfs,
650 struct ntvfs_request *req,
651 union smb_setfileinfo *info)
655 PASS_THRU_REQ(ntvfs, req, setfileinfo, (ntvfs, req, info));
662 return filesystem space info
664 static NTSTATUS unixuid_fsinfo(struct ntvfs_module_context *ntvfs,
665 struct ntvfs_request *req, union smb_fsinfo *fs)
669 PASS_THRU_REQ(ntvfs, req, fsinfo, (ntvfs, req, fs));
675 return print queue info
677 static NTSTATUS unixuid_lpq(struct ntvfs_module_context *ntvfs,
678 struct ntvfs_request *req, union smb_lpq *lpq)
682 PASS_THRU_REQ(ntvfs, req, lpq, (ntvfs, req, lpq));
688 list files in a directory matching a wildcard pattern
690 static NTSTATUS unixuid_search_first(struct ntvfs_module_context *ntvfs,
691 struct ntvfs_request *req, union smb_search_first *io,
692 void *search_private,
693 bool (*callback)(void *, const union smb_search_data *))
697 PASS_THRU_REQ(ntvfs, req, search_first, (ntvfs, req, io, search_private, callback));
702 /* continue a search */
703 static NTSTATUS unixuid_search_next(struct ntvfs_module_context *ntvfs,
704 struct ntvfs_request *req, union smb_search_next *io,
705 void *search_private,
706 bool (*callback)(void *, const union smb_search_data *))
710 PASS_THRU_REQ(ntvfs, req, search_next, (ntvfs, req, io, search_private, callback));
716 static NTSTATUS unixuid_search_close(struct ntvfs_module_context *ntvfs,
717 struct ntvfs_request *req, union smb_search_close *io)
721 PASS_THRU_REQ(ntvfs, req, search_close, (ntvfs, req, io));
726 /* SMBtrans - not used on file shares */
727 static NTSTATUS unixuid_trans(struct ntvfs_module_context *ntvfs,
728 struct ntvfs_request *req, struct smb_trans2 *trans2)
732 PASS_THRU_REQ(ntvfs, req, trans, (ntvfs, req, trans2));
738 initialise the unixuid backend, registering ourselves with the ntvfs subsystem
740 NTSTATUS ntvfs_unixuid_init(void)
743 struct ntvfs_ops ops;
744 NTVFS_CURRENT_CRITICAL_SIZES(vers);
748 /* fill in all the operations */
749 ops.connect = unixuid_connect;
750 ops.disconnect = unixuid_disconnect;
751 ops.unlink = unixuid_unlink;
752 ops.chkpath = unixuid_chkpath;
753 ops.qpathinfo = unixuid_qpathinfo;
754 ops.setpathinfo = unixuid_setpathinfo;
755 ops.open = unixuid_open;
756 ops.mkdir = unixuid_mkdir;
757 ops.rmdir = unixuid_rmdir;
758 ops.rename = unixuid_rename;
759 ops.copy = unixuid_copy;
760 ops.ioctl = unixuid_ioctl;
761 ops.read = unixuid_read;
762 ops.write = unixuid_write;
763 ops.seek = unixuid_seek;
764 ops.flush = unixuid_flush;
765 ops.close = unixuid_close;
766 ops.exit = unixuid_exit;
767 ops.lock = unixuid_lock;
768 ops.setfileinfo = unixuid_setfileinfo;
769 ops.qfileinfo = unixuid_qfileinfo;
770 ops.fsinfo = unixuid_fsinfo;
771 ops.lpq = unixuid_lpq;
772 ops.search_first = unixuid_search_first;
773 ops.search_next = unixuid_search_next;
774 ops.search_close = unixuid_search_close;
775 ops.trans = unixuid_trans;
776 ops.logoff = unixuid_logoff;
777 ops.async_setup = unixuid_async_setup;
778 ops.cancel = unixuid_cancel;
779 ops.notify = unixuid_notify;
781 ops.name = "unixuid";
783 /* we register under all 3 backend types, as we are not type specific */
784 ops.type = NTVFS_DISK;
785 ret = ntvfs_register(&ops, &vers);
786 if (!NT_STATUS_IS_OK(ret)) goto failed;
788 ops.type = NTVFS_PRINT;
789 ret = ntvfs_register(&ops, &vers);
790 if (!NT_STATUS_IS_OK(ret)) goto failed;
792 ops.type = NTVFS_IPC;
793 ret = ntvfs_register(&ops, &vers);
794 if (!NT_STATUS_IS_OK(ret)) goto failed;