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 NTSTATUS ntvfs_unixuid_init(void);
44 struct unixuid_private {
45 struct wbc_context *wbc_ctx;
46 struct security_unix_token *last_sec_ctx;
47 struct security_token *last_token;
52 pull the current security context into a security_unix_token
54 static struct security_unix_token *save_unix_security(TALLOC_CTX *mem_ctx)
56 struct security_unix_token *sec = talloc(mem_ctx, struct security_unix_token);
62 sec->ngroups = getgroups(0, NULL);
63 if (sec->ngroups == -1) {
67 sec->groups = talloc_array(sec, gid_t, sec->ngroups);
68 if (sec->groups == NULL) {
73 if (getgroups(sec->ngroups, sec->groups) != sec->ngroups) {
82 set the current security context from a security_unix_token
84 static NTSTATUS set_unix_security(struct security_unix_token *sec)
88 if (setgroups(sec->ngroups, sec->groups) != 0) {
89 return NT_STATUS_ACCESS_DENIED;
91 if (setegid(sec->gid) != 0) {
92 return NT_STATUS_ACCESS_DENIED;
94 if (seteuid(sec->uid) != 0) {
95 return NT_STATUS_ACCESS_DENIED;
100 static int unixuid_nesting_level;
103 called at the start and end of a tevent nesting loop. Needs to save/restore
104 unix security context
106 static int unixuid_event_nesting_hook(struct tevent_context *ev,
111 const char *location)
113 struct security_unix_token *sec_ctx;
115 if (unixuid_nesting_level == 0) {
116 /* we don't need to do anything unless we are nested
117 inside of a call in this module */
122 sec_ctx = save_unix_security(ev);
123 if (sec_ctx == NULL) {
124 DEBUG(0,("%s: Failed to save security context\n", location));
127 *(struct security_unix_token **)stack_ptr = sec_ctx;
128 if (seteuid(0) != 0 || setegid(0) != 0) {
129 DEBUG(0,("%s: Failed to change to root\n", location));
133 /* called when we come out of a nesting level */
136 sec_ctx = *(struct security_unix_token **)stack_ptr;
137 if (sec_ctx == NULL) {
138 /* this happens the first time this function
139 is called, as we install the hook while
140 inside an event in unixuid_connect() */
144 sec_ctx = talloc_get_type_abort(sec_ctx, struct security_unix_token);
145 status = set_unix_security(sec_ctx);
146 talloc_free(sec_ctx);
147 if (!NT_STATUS_IS_OK(status)) {
148 DEBUG(0,("%s: Failed to revert security context (%s)\n",
149 location, nt_errstr(status)));
159 form a security_unix_token from the current security_token
161 static NTSTATUS nt_token_to_unix_security(struct ntvfs_module_context *ntvfs,
162 struct ntvfs_request *req,
163 struct security_token *token,
164 struct security_unix_token **sec)
166 struct unixuid_private *priv = ntvfs->private_data;
168 return security_token_to_unix_token(req,
174 setup our unix security context according to the session authentication info
176 static NTSTATUS unixuid_setup_security(struct ntvfs_module_context *ntvfs,
177 struct ntvfs_request *req, struct security_unix_token **sec)
179 struct unixuid_private *priv = ntvfs->private_data;
180 struct security_token *token;
181 struct security_unix_token *newsec;
184 /* If we are asked to set up, but have not had a successful
185 * session setup or tree connect, then these may not be filled
186 * in. ACCESS_DENIED is the right error code here */
187 if (req->session_info == NULL || priv == NULL) {
188 return NT_STATUS_ACCESS_DENIED;
191 token = req->session_info->security_token;
193 *sec = save_unix_security(ntvfs);
195 return NT_STATUS_NO_MEMORY;
198 if (token == priv->last_token) {
199 newsec = priv->last_sec_ctx;
201 status = nt_token_to_unix_security(ntvfs, req, token, &newsec);
202 if (!NT_STATUS_IS_OK(status)) {
206 if (priv->last_sec_ctx) {
207 talloc_free(priv->last_sec_ctx);
209 priv->last_sec_ctx = newsec;
210 priv->last_token = token;
211 talloc_steal(priv, newsec);
214 status = set_unix_security(newsec);
215 if (!NT_STATUS_IS_OK(status)) {
224 this pass through macro operates on request contexts
226 #define PASS_THRU_REQ(ntvfs, req, op, args) do { \
228 struct security_unix_token *sec; \
229 status = unixuid_setup_security(ntvfs, req, &sec); \
230 NT_STATUS_NOT_OK_RETURN(status); \
231 unixuid_nesting_level++; \
232 status = ntvfs_next_##op args; \
233 unixuid_nesting_level--; \
234 status2 = set_unix_security(sec); \
236 if (!NT_STATUS_IS_OK(status2)) smb_panic("Unable to reset security context"); \
242 connect to a share - used when a tree_connect operation comes in.
244 static NTSTATUS unixuid_connect(struct ntvfs_module_context *ntvfs,
245 struct ntvfs_request *req, union smb_tcon *tcon)
247 struct unixuid_private *priv;
250 priv = talloc(ntvfs, struct unixuid_private);
252 return NT_STATUS_NO_MEMORY;
255 priv->wbc_ctx = wbc_init(priv, ntvfs->ctx->msg_ctx,
256 ntvfs->ctx->event_ctx);
257 if (priv->wbc_ctx == NULL) {
259 return NT_STATUS_INTERNAL_ERROR;
262 priv->last_sec_ctx = NULL;
263 priv->last_token = NULL;
264 ntvfs->private_data = priv;
266 tevent_loop_set_nesting_hook(ntvfs->ctx->event_ctx,
267 unixuid_event_nesting_hook,
268 &unixuid_nesting_level);
270 /* we don't use PASS_THRU_REQ here, as the connect operation runs with
271 root privileges. This allows the backends to setup any database
272 links they might need during the connect. */
273 status = ntvfs_next_connect(ntvfs, req, tcon);
279 disconnect from a share
281 static NTSTATUS unixuid_disconnect(struct ntvfs_module_context *ntvfs)
283 struct unixuid_private *priv = ntvfs->private_data;
287 ntvfs->private_data = NULL;
289 status = ntvfs_next_disconnect(ntvfs);
298 static NTSTATUS unixuid_unlink(struct ntvfs_module_context *ntvfs,
299 struct ntvfs_request *req,
300 union smb_unlink *unl)
304 PASS_THRU_REQ(ntvfs, req, unlink, (ntvfs, req, unl));
312 static NTSTATUS unixuid_ioctl(struct ntvfs_module_context *ntvfs,
313 struct ntvfs_request *req, union smb_ioctl *io)
317 PASS_THRU_REQ(ntvfs, req, ioctl, (ntvfs, req, io));
323 check if a directory exists
325 static NTSTATUS unixuid_chkpath(struct ntvfs_module_context *ntvfs,
326 struct ntvfs_request *req,
327 union smb_chkpath *cp)
331 PASS_THRU_REQ(ntvfs, req, chkpath, (ntvfs, req, cp));
337 return info on a pathname
339 static NTSTATUS unixuid_qpathinfo(struct ntvfs_module_context *ntvfs,
340 struct ntvfs_request *req, union smb_fileinfo *info)
344 PASS_THRU_REQ(ntvfs, req, qpathinfo, (ntvfs, req, info));
350 query info on a open file
352 static NTSTATUS unixuid_qfileinfo(struct ntvfs_module_context *ntvfs,
353 struct ntvfs_request *req, union smb_fileinfo *info)
357 PASS_THRU_REQ(ntvfs, req, qfileinfo, (ntvfs, req, info));
364 set info on a pathname
366 static NTSTATUS unixuid_setpathinfo(struct ntvfs_module_context *ntvfs,
367 struct ntvfs_request *req, union smb_setfileinfo *st)
371 PASS_THRU_REQ(ntvfs, req, setpathinfo, (ntvfs, req, st));
379 static NTSTATUS unixuid_open(struct ntvfs_module_context *ntvfs,
380 struct ntvfs_request *req, union smb_open *io)
384 PASS_THRU_REQ(ntvfs, req, open, (ntvfs, req, io));
392 static NTSTATUS unixuid_mkdir(struct ntvfs_module_context *ntvfs,
393 struct ntvfs_request *req, union smb_mkdir *md)
397 PASS_THRU_REQ(ntvfs, req, mkdir, (ntvfs, req, md));
405 static NTSTATUS unixuid_rmdir(struct ntvfs_module_context *ntvfs,
406 struct ntvfs_request *req, struct smb_rmdir *rd)
410 PASS_THRU_REQ(ntvfs, req, rmdir, (ntvfs, req, rd));
416 rename a set of files
418 static NTSTATUS unixuid_rename(struct ntvfs_module_context *ntvfs,
419 struct ntvfs_request *req, union smb_rename *ren)
423 PASS_THRU_REQ(ntvfs, req, rename, (ntvfs, req, ren));
431 static NTSTATUS unixuid_copy(struct ntvfs_module_context *ntvfs,
432 struct ntvfs_request *req, struct smb_copy *cp)
436 PASS_THRU_REQ(ntvfs, req, copy, (ntvfs, req, cp));
444 static NTSTATUS unixuid_read(struct ntvfs_module_context *ntvfs,
445 struct ntvfs_request *req, union smb_read *rd)
449 PASS_THRU_REQ(ntvfs, req, read, (ntvfs, req, rd));
457 static NTSTATUS unixuid_write(struct ntvfs_module_context *ntvfs,
458 struct ntvfs_request *req, union smb_write *wr)
462 PASS_THRU_REQ(ntvfs, req, write, (ntvfs, req, wr));
470 static NTSTATUS unixuid_seek(struct ntvfs_module_context *ntvfs,
471 struct ntvfs_request *req,
476 PASS_THRU_REQ(ntvfs, req, seek, (ntvfs, req, io));
484 static NTSTATUS unixuid_flush(struct ntvfs_module_context *ntvfs,
485 struct ntvfs_request *req,
490 PASS_THRU_REQ(ntvfs, req, flush, (ntvfs, req, io));
498 static NTSTATUS unixuid_close(struct ntvfs_module_context *ntvfs,
499 struct ntvfs_request *req, union smb_close *io)
503 PASS_THRU_REQ(ntvfs, req, close, (ntvfs, req, io));
511 static NTSTATUS unixuid_exit(struct ntvfs_module_context *ntvfs,
512 struct ntvfs_request *req)
516 PASS_THRU_REQ(ntvfs, req, exit, (ntvfs, req));
522 logoff - closing files
524 static NTSTATUS unixuid_logoff(struct ntvfs_module_context *ntvfs,
525 struct ntvfs_request *req)
527 struct unixuid_private *priv = ntvfs->private_data;
530 PASS_THRU_REQ(ntvfs, req, logoff, (ntvfs, req));
532 priv->last_token = NULL;
540 static NTSTATUS unixuid_async_setup(struct ntvfs_module_context *ntvfs,
541 struct ntvfs_request *req,
546 PASS_THRU_REQ(ntvfs, req, async_setup, (ntvfs, req, private_data));
552 cancel an async request
554 static NTSTATUS unixuid_cancel(struct ntvfs_module_context *ntvfs,
555 struct ntvfs_request *req)
559 PASS_THRU_REQ(ntvfs, req, cancel, (ntvfs, req));
567 static NTSTATUS unixuid_notify(struct ntvfs_module_context *ntvfs,
568 struct ntvfs_request *req, union smb_notify *info)
572 PASS_THRU_REQ(ntvfs, req, notify, (ntvfs, req, info));
580 static NTSTATUS unixuid_lock(struct ntvfs_module_context *ntvfs,
581 struct ntvfs_request *req, union smb_lock *lck)
585 PASS_THRU_REQ(ntvfs, req, lock, (ntvfs, req, lck));
591 set info on a open file
593 static NTSTATUS unixuid_setfileinfo(struct ntvfs_module_context *ntvfs,
594 struct ntvfs_request *req,
595 union smb_setfileinfo *info)
599 PASS_THRU_REQ(ntvfs, req, setfileinfo, (ntvfs, req, info));
606 return filesystem space info
608 static NTSTATUS unixuid_fsinfo(struct ntvfs_module_context *ntvfs,
609 struct ntvfs_request *req, union smb_fsinfo *fs)
613 PASS_THRU_REQ(ntvfs, req, fsinfo, (ntvfs, req, fs));
619 return print queue info
621 static NTSTATUS unixuid_lpq(struct ntvfs_module_context *ntvfs,
622 struct ntvfs_request *req, union smb_lpq *lpq)
626 PASS_THRU_REQ(ntvfs, req, lpq, (ntvfs, req, lpq));
632 list files in a directory matching a wildcard pattern
634 static NTSTATUS unixuid_search_first(struct ntvfs_module_context *ntvfs,
635 struct ntvfs_request *req, union smb_search_first *io,
636 void *search_private,
637 bool (*callback)(void *, const union smb_search_data *))
641 PASS_THRU_REQ(ntvfs, req, search_first, (ntvfs, req, io, search_private, callback));
646 /* continue a search */
647 static NTSTATUS unixuid_search_next(struct ntvfs_module_context *ntvfs,
648 struct ntvfs_request *req, union smb_search_next *io,
649 void *search_private,
650 bool (*callback)(void *, const union smb_search_data *))
654 PASS_THRU_REQ(ntvfs, req, search_next, (ntvfs, req, io, search_private, callback));
660 static NTSTATUS unixuid_search_close(struct ntvfs_module_context *ntvfs,
661 struct ntvfs_request *req, union smb_search_close *io)
665 PASS_THRU_REQ(ntvfs, req, search_close, (ntvfs, req, io));
670 /* SMBtrans - not used on file shares */
671 static NTSTATUS unixuid_trans(struct ntvfs_module_context *ntvfs,
672 struct ntvfs_request *req, struct smb_trans2 *trans2)
676 PASS_THRU_REQ(ntvfs, req, trans, (ntvfs, req, trans2));
682 initialise the unixuid backend, registering ourselves with the ntvfs subsystem
684 NTSTATUS ntvfs_unixuid_init(void)
687 struct ntvfs_ops ops;
688 NTVFS_CURRENT_CRITICAL_SIZES(vers);
692 /* fill in all the operations */
693 ops.connect = unixuid_connect;
694 ops.disconnect = unixuid_disconnect;
695 ops.unlink = unixuid_unlink;
696 ops.chkpath = unixuid_chkpath;
697 ops.qpathinfo = unixuid_qpathinfo;
698 ops.setpathinfo = unixuid_setpathinfo;
699 ops.open = unixuid_open;
700 ops.mkdir = unixuid_mkdir;
701 ops.rmdir = unixuid_rmdir;
702 ops.rename = unixuid_rename;
703 ops.copy = unixuid_copy;
704 ops.ioctl = unixuid_ioctl;
705 ops.read = unixuid_read;
706 ops.write = unixuid_write;
707 ops.seek = unixuid_seek;
708 ops.flush = unixuid_flush;
709 ops.close = unixuid_close;
710 ops.exit = unixuid_exit;
711 ops.lock = unixuid_lock;
712 ops.setfileinfo = unixuid_setfileinfo;
713 ops.qfileinfo = unixuid_qfileinfo;
714 ops.fsinfo = unixuid_fsinfo;
715 ops.lpq = unixuid_lpq;
716 ops.search_first = unixuid_search_first;
717 ops.search_next = unixuid_search_next;
718 ops.search_close = unixuid_search_close;
719 ops.trans = unixuid_trans;
720 ops.logoff = unixuid_logoff;
721 ops.async_setup = unixuid_async_setup;
722 ops.cancel = unixuid_cancel;
723 ops.notify = unixuid_notify;
725 ops.name = "unixuid";
727 /* we register under all 3 backend types, as we are not type specific */
728 ops.type = NTVFS_DISK;
729 ret = ntvfs_register(&ops, &vers);
730 if (!NT_STATUS_IS_OK(ret)) goto failed;
732 ops.type = NTVFS_PRINT;
733 ret = ntvfs_register(&ops, &vers);
734 if (!NT_STATUS_IS_OK(ret)) goto failed;
736 ops.type = NTVFS_IPC;
737 ret = ntvfs_register(&ops, &vers);
738 if (!NT_STATUS_IS_OK(ret)) goto failed;