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 struct unixuid_private {
33 struct wbc_context *wbc_ctx;
34 struct unix_sec_ctx *last_sec_ctx;
35 struct security_token *last_token;
48 pull the current security context into a unix_sec_ctx
50 static struct unix_sec_ctx *save_unix_security(TALLOC_CTX *mem_ctx)
52 struct unix_sec_ctx *sec = talloc(mem_ctx, struct unix_sec_ctx);
58 sec->ngroups = getgroups(0, NULL);
59 if (sec->ngroups == -1) {
63 sec->groups = talloc_array(sec, gid_t, sec->ngroups);
64 if (sec->groups == NULL) {
69 if (getgroups(sec->ngroups, sec->groups) != sec->ngroups) {
78 set the current security context from a unix_sec_ctx
80 static NTSTATUS set_unix_security(struct unix_sec_ctx *sec)
84 if (setgroups(sec->ngroups, sec->groups) != 0) {
85 return NT_STATUS_ACCESS_DENIED;
87 if (setegid(sec->gid) != 0) {
88 return NT_STATUS_ACCESS_DENIED;
90 if (seteuid(sec->uid) != 0) {
91 return NT_STATUS_ACCESS_DENIED;
96 static int unixuid_nesting_level;
99 called at the start and end of a tevent nesting loop. Needs to save/restore
100 unix security context
102 static int unixuid_event_nesting_hook(struct tevent_context *ev,
107 const char *location)
109 struct unix_sec_ctx *sec_ctx;
111 if (unixuid_nesting_level == 0) {
112 /* we don't need to do anything unless we are nested
113 inside of a call in this module */
118 sec_ctx = save_unix_security(ev);
119 if (sec_ctx == NULL) {
120 DEBUG(0,("%s: Failed to save security context\n", location));
123 *(struct unix_sec_ctx **)stack_ptr = sec_ctx;
124 if (seteuid(0) != 0 || setegid(0) != 0) {
125 DEBUG(0,("%s: Failed to change to root\n", location));
129 /* called when we come out of a nesting level */
132 sec_ctx = *(struct unix_sec_ctx **)stack_ptr;
133 if (sec_ctx == NULL) {
134 /* this happens the first time this function
135 is called, as we install the hook while
136 inside an event in unixuid_connect() */
140 sec_ctx = talloc_get_type_abort(sec_ctx, struct unix_sec_ctx);
141 status = set_unix_security(sec_ctx);
142 talloc_free(sec_ctx);
143 if (!NT_STATUS_IS_OK(status)) {
144 DEBUG(0,("%s: Failed to revert security context (%s)\n",
145 location, nt_errstr(status)));
155 form a unix_sec_ctx from the current security_token
157 static NTSTATUS nt_token_to_unix_security(struct ntvfs_module_context *ntvfs,
158 struct ntvfs_request *req,
159 struct security_token *token,
160 struct unix_sec_ctx **sec)
162 struct unixuid_private *priv = ntvfs->private_data;
165 struct id_mapping *ids;
166 struct composite_context *ctx;
167 *sec = talloc(req, struct unix_sec_ctx);
169 /* we can't do unix security without a user and group */
170 if (token->num_sids < 2) {
171 return NT_STATUS_ACCESS_DENIED;
174 ids = talloc_array(req, struct id_mapping, token->num_sids);
175 NT_STATUS_HAVE_NO_MEMORY(ids);
177 ids[0].unixid = NULL;
178 ids[0].sid = token->user_sid;
179 ids[0].status = NT_STATUS_NONE_MAPPED;
181 ids[1].unixid = NULL;
182 ids[1].sid = token->group_sid;
183 ids[1].status = NT_STATUS_NONE_MAPPED;
185 (*sec)->ngroups = token->num_sids - 2;
186 (*sec)->groups = talloc_array(*sec, gid_t, (*sec)->ngroups);
187 NT_STATUS_HAVE_NO_MEMORY((*sec)->groups);
189 for (i=0;i<(*sec)->ngroups;i++) {
190 ids[i+2].unixid = NULL;
191 ids[i+2].sid = token->sids[i+2];
192 ids[i+2].status = NT_STATUS_NONE_MAPPED;
195 ctx = wbc_sids_to_xids_send(priv->wbc_ctx, ids, token->num_sids, ids);
196 NT_STATUS_HAVE_NO_MEMORY(ctx);
198 status = wbc_sids_to_xids_recv(ctx, &ids);
199 NT_STATUS_NOT_OK_RETURN(status);
201 if (ids[0].unixid->type == ID_TYPE_BOTH ||
202 ids[0].unixid->type == ID_TYPE_UID) {
203 (*sec)->uid = ids[0].unixid->id;
205 return NT_STATUS_INVALID_SID;
208 if (ids[1].unixid->type == ID_TYPE_BOTH ||
209 ids[1].unixid->type == ID_TYPE_GID) {
210 (*sec)->gid = ids[1].unixid->id;
212 return NT_STATUS_INVALID_SID;
215 for (i=0;i<(*sec)->ngroups;i++) {
216 if (ids[i+2].unixid->type == ID_TYPE_BOTH ||
217 ids[i+2].unixid->type == ID_TYPE_GID) {
218 (*sec)->groups[i] = ids[i+2].unixid->id;
220 return NT_STATUS_INVALID_SID;
228 setup our unix security context according to the session authentication info
230 static NTSTATUS unixuid_setup_security(struct ntvfs_module_context *ntvfs,
231 struct ntvfs_request *req, struct unix_sec_ctx **sec)
233 struct unixuid_private *priv = ntvfs->private_data;
234 struct security_token *token;
235 struct unix_sec_ctx *newsec;
238 if (req->session_info == NULL) {
239 return NT_STATUS_ACCESS_DENIED;
242 token = req->session_info->security_token;
244 *sec = save_unix_security(ntvfs);
246 return NT_STATUS_NO_MEMORY;
249 if (token == priv->last_token) {
250 newsec = priv->last_sec_ctx;
252 status = nt_token_to_unix_security(ntvfs, req, token, &newsec);
253 if (!NT_STATUS_IS_OK(status)) {
257 if (priv->last_sec_ctx) {
258 talloc_free(priv->last_sec_ctx);
260 priv->last_sec_ctx = newsec;
261 priv->last_token = token;
262 talloc_steal(priv, newsec);
265 status = set_unix_security(newsec);
266 if (!NT_STATUS_IS_OK(status)) {
275 this pass through macro operates on request contexts
277 #define PASS_THRU_REQ(ntvfs, req, op, args) do { \
279 struct unix_sec_ctx *sec; \
280 status = unixuid_setup_security(ntvfs, req, &sec); \
281 NT_STATUS_NOT_OK_RETURN(status); \
282 unixuid_nesting_level++; \
283 status = ntvfs_next_##op args; \
284 unixuid_nesting_level--; \
285 status2 = set_unix_security(sec); \
287 if (!NT_STATUS_IS_OK(status2)) smb_panic("Unable to reset security context"); \
293 connect to a share - used when a tree_connect operation comes in.
295 static NTSTATUS unixuid_connect(struct ntvfs_module_context *ntvfs,
296 struct ntvfs_request *req, union smb_tcon *tcon)
298 struct unixuid_private *priv;
301 priv = talloc(ntvfs, struct unixuid_private);
303 return NT_STATUS_NO_MEMORY;
306 priv->wbc_ctx = wbc_init(priv, ntvfs->ctx->msg_ctx,
307 ntvfs->ctx->event_ctx);
308 if (priv->wbc_ctx == NULL) {
310 return NT_STATUS_INTERNAL_ERROR;
313 priv->last_sec_ctx = NULL;
314 priv->last_token = NULL;
315 ntvfs->private_data = priv;
317 tevent_loop_set_nesting_hook(ntvfs->ctx->event_ctx,
318 unixuid_event_nesting_hook,
319 &unixuid_nesting_level);
321 /* we don't use PASS_THRU_REQ here, as the connect operation runs with
322 root privileges. This allows the backends to setup any database
323 links they might need during the connect. */
324 status = ntvfs_next_connect(ntvfs, req, tcon);
330 disconnect from a share
332 static NTSTATUS unixuid_disconnect(struct ntvfs_module_context *ntvfs)
334 struct unixuid_private *priv = ntvfs->private_data;
338 ntvfs->private_data = NULL;
340 status = ntvfs_next_disconnect(ntvfs);
349 static NTSTATUS unixuid_unlink(struct ntvfs_module_context *ntvfs,
350 struct ntvfs_request *req,
351 union smb_unlink *unl)
355 PASS_THRU_REQ(ntvfs, req, unlink, (ntvfs, req, unl));
363 static NTSTATUS unixuid_ioctl(struct ntvfs_module_context *ntvfs,
364 struct ntvfs_request *req, union smb_ioctl *io)
368 PASS_THRU_REQ(ntvfs, req, ioctl, (ntvfs, req, io));
374 check if a directory exists
376 static NTSTATUS unixuid_chkpath(struct ntvfs_module_context *ntvfs,
377 struct ntvfs_request *req,
378 union smb_chkpath *cp)
382 PASS_THRU_REQ(ntvfs, req, chkpath, (ntvfs, req, cp));
388 return info on a pathname
390 static NTSTATUS unixuid_qpathinfo(struct ntvfs_module_context *ntvfs,
391 struct ntvfs_request *req, union smb_fileinfo *info)
395 PASS_THRU_REQ(ntvfs, req, qpathinfo, (ntvfs, req, info));
401 query info on a open file
403 static NTSTATUS unixuid_qfileinfo(struct ntvfs_module_context *ntvfs,
404 struct ntvfs_request *req, union smb_fileinfo *info)
408 PASS_THRU_REQ(ntvfs, req, qfileinfo, (ntvfs, req, info));
415 set info on a pathname
417 static NTSTATUS unixuid_setpathinfo(struct ntvfs_module_context *ntvfs,
418 struct ntvfs_request *req, union smb_setfileinfo *st)
422 PASS_THRU_REQ(ntvfs, req, setpathinfo, (ntvfs, req, st));
430 static NTSTATUS unixuid_open(struct ntvfs_module_context *ntvfs,
431 struct ntvfs_request *req, union smb_open *io)
435 PASS_THRU_REQ(ntvfs, req, open, (ntvfs, req, io));
443 static NTSTATUS unixuid_mkdir(struct ntvfs_module_context *ntvfs,
444 struct ntvfs_request *req, union smb_mkdir *md)
448 PASS_THRU_REQ(ntvfs, req, mkdir, (ntvfs, req, md));
456 static NTSTATUS unixuid_rmdir(struct ntvfs_module_context *ntvfs,
457 struct ntvfs_request *req, struct smb_rmdir *rd)
461 PASS_THRU_REQ(ntvfs, req, rmdir, (ntvfs, req, rd));
467 rename a set of files
469 static NTSTATUS unixuid_rename(struct ntvfs_module_context *ntvfs,
470 struct ntvfs_request *req, union smb_rename *ren)
474 PASS_THRU_REQ(ntvfs, req, rename, (ntvfs, req, ren));
482 static NTSTATUS unixuid_copy(struct ntvfs_module_context *ntvfs,
483 struct ntvfs_request *req, struct smb_copy *cp)
487 PASS_THRU_REQ(ntvfs, req, copy, (ntvfs, req, cp));
495 static NTSTATUS unixuid_read(struct ntvfs_module_context *ntvfs,
496 struct ntvfs_request *req, union smb_read *rd)
500 PASS_THRU_REQ(ntvfs, req, read, (ntvfs, req, rd));
508 static NTSTATUS unixuid_write(struct ntvfs_module_context *ntvfs,
509 struct ntvfs_request *req, union smb_write *wr)
513 PASS_THRU_REQ(ntvfs, req, write, (ntvfs, req, wr));
521 static NTSTATUS unixuid_seek(struct ntvfs_module_context *ntvfs,
522 struct ntvfs_request *req,
527 PASS_THRU_REQ(ntvfs, req, seek, (ntvfs, req, io));
535 static NTSTATUS unixuid_flush(struct ntvfs_module_context *ntvfs,
536 struct ntvfs_request *req,
541 PASS_THRU_REQ(ntvfs, req, flush, (ntvfs, req, io));
549 static NTSTATUS unixuid_close(struct ntvfs_module_context *ntvfs,
550 struct ntvfs_request *req, union smb_close *io)
554 PASS_THRU_REQ(ntvfs, req, close, (ntvfs, req, io));
562 static NTSTATUS unixuid_exit(struct ntvfs_module_context *ntvfs,
563 struct ntvfs_request *req)
567 PASS_THRU_REQ(ntvfs, req, exit, (ntvfs, req));
573 logoff - closing files
575 static NTSTATUS unixuid_logoff(struct ntvfs_module_context *ntvfs,
576 struct ntvfs_request *req)
578 struct unixuid_private *priv = ntvfs->private_data;
581 PASS_THRU_REQ(ntvfs, req, logoff, (ntvfs, req));
583 priv->last_token = NULL;
591 static NTSTATUS unixuid_async_setup(struct ntvfs_module_context *ntvfs,
592 struct ntvfs_request *req,
597 PASS_THRU_REQ(ntvfs, req, async_setup, (ntvfs, req, private_data));
603 cancel an async request
605 static NTSTATUS unixuid_cancel(struct ntvfs_module_context *ntvfs,
606 struct ntvfs_request *req)
610 PASS_THRU_REQ(ntvfs, req, cancel, (ntvfs, req));
618 static NTSTATUS unixuid_notify(struct ntvfs_module_context *ntvfs,
619 struct ntvfs_request *req, union smb_notify *info)
623 PASS_THRU_REQ(ntvfs, req, notify, (ntvfs, req, info));
631 static NTSTATUS unixuid_lock(struct ntvfs_module_context *ntvfs,
632 struct ntvfs_request *req, union smb_lock *lck)
636 PASS_THRU_REQ(ntvfs, req, lock, (ntvfs, req, lck));
642 set info on a open file
644 static NTSTATUS unixuid_setfileinfo(struct ntvfs_module_context *ntvfs,
645 struct ntvfs_request *req,
646 union smb_setfileinfo *info)
650 PASS_THRU_REQ(ntvfs, req, setfileinfo, (ntvfs, req, info));
657 return filesystem space info
659 static NTSTATUS unixuid_fsinfo(struct ntvfs_module_context *ntvfs,
660 struct ntvfs_request *req, union smb_fsinfo *fs)
664 PASS_THRU_REQ(ntvfs, req, fsinfo, (ntvfs, req, fs));
670 return print queue info
672 static NTSTATUS unixuid_lpq(struct ntvfs_module_context *ntvfs,
673 struct ntvfs_request *req, union smb_lpq *lpq)
677 PASS_THRU_REQ(ntvfs, req, lpq, (ntvfs, req, lpq));
683 list files in a directory matching a wildcard pattern
685 static NTSTATUS unixuid_search_first(struct ntvfs_module_context *ntvfs,
686 struct ntvfs_request *req, union smb_search_first *io,
687 void *search_private,
688 bool (*callback)(void *, const union smb_search_data *))
692 PASS_THRU_REQ(ntvfs, req, search_first, (ntvfs, req, io, search_private, callback));
697 /* continue a search */
698 static NTSTATUS unixuid_search_next(struct ntvfs_module_context *ntvfs,
699 struct ntvfs_request *req, union smb_search_next *io,
700 void *search_private,
701 bool (*callback)(void *, const union smb_search_data *))
705 PASS_THRU_REQ(ntvfs, req, search_next, (ntvfs, req, io, search_private, callback));
711 static NTSTATUS unixuid_search_close(struct ntvfs_module_context *ntvfs,
712 struct ntvfs_request *req, union smb_search_close *io)
716 PASS_THRU_REQ(ntvfs, req, search_close, (ntvfs, req, io));
721 /* SMBtrans - not used on file shares */
722 static NTSTATUS unixuid_trans(struct ntvfs_module_context *ntvfs,
723 struct ntvfs_request *req, struct smb_trans2 *trans2)
727 PASS_THRU_REQ(ntvfs, req, trans, (ntvfs, req, trans2));
733 initialise the unixuid backend, registering ourselves with the ntvfs subsystem
735 NTSTATUS ntvfs_unixuid_init(void)
738 struct ntvfs_ops ops;
739 NTVFS_CURRENT_CRITICAL_SIZES(vers);
743 /* fill in all the operations */
744 ops.connect = unixuid_connect;
745 ops.disconnect = unixuid_disconnect;
746 ops.unlink = unixuid_unlink;
747 ops.chkpath = unixuid_chkpath;
748 ops.qpathinfo = unixuid_qpathinfo;
749 ops.setpathinfo = unixuid_setpathinfo;
750 ops.open = unixuid_open;
751 ops.mkdir = unixuid_mkdir;
752 ops.rmdir = unixuid_rmdir;
753 ops.rename = unixuid_rename;
754 ops.copy = unixuid_copy;
755 ops.ioctl = unixuid_ioctl;
756 ops.read = unixuid_read;
757 ops.write = unixuid_write;
758 ops.seek = unixuid_seek;
759 ops.flush = unixuid_flush;
760 ops.close = unixuid_close;
761 ops.exit = unixuid_exit;
762 ops.lock = unixuid_lock;
763 ops.setfileinfo = unixuid_setfileinfo;
764 ops.qfileinfo = unixuid_qfileinfo;
765 ops.fsinfo = unixuid_fsinfo;
766 ops.lpq = unixuid_lpq;
767 ops.search_first = unixuid_search_first;
768 ops.search_next = unixuid_search_next;
769 ops.search_close = unixuid_search_close;
770 ops.trans = unixuid_trans;
771 ops.logoff = unixuid_logoff;
772 ops.async_setup = unixuid_async_setup;
773 ops.cancel = unixuid_cancel;
774 ops.notify = unixuid_notify;
776 ops.name = "unixuid";
778 /* we register under all 3 backend types, as we are not type specific */
779 ops.type = NTVFS_DISK;
780 ret = ntvfs_register(&ops, &vers);
781 if (!NT_STATUS_IS_OK(ret)) goto failed;
783 ops.type = NTVFS_PRINT;
784 ret = ntvfs_register(&ops, &vers);
785 if (!NT_STATUS_IS_OK(ret)) goto failed;
787 ops.type = NTVFS_IPC;
788 ret = ntvfs_register(&ops, &vers);
789 if (!NT_STATUS_IS_OK(ret)) goto failed;