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 "dsdb/samdb/samdb.h"
29 #include "param/param.h"
31 struct unixuid_private {
32 struct sidmap_context *sidmap;
33 struct unix_sec_ctx *last_sec_ctx;
34 struct security_token *last_token;
47 pull the current security context into a unix_sec_ctx
49 static struct unix_sec_ctx *save_unix_security(TALLOC_CTX *mem_ctx)
51 struct unix_sec_ctx *sec = talloc(mem_ctx, struct unix_sec_ctx);
57 sec->ngroups = getgroups(0, NULL);
58 if (sec->ngroups == -1) {
62 sec->groups = talloc_array(sec, gid_t, sec->ngroups);
63 if (sec->groups == NULL) {
68 if (getgroups(sec->ngroups, sec->groups) != sec->ngroups) {
77 set the current security context from a unix_sec_ctx
79 static NTSTATUS set_unix_security(struct unix_sec_ctx *sec)
83 if (setgroups(sec->ngroups, sec->groups) != 0) {
84 return NT_STATUS_ACCESS_DENIED;
86 if (setegid(sec->gid) != 0) {
87 return NT_STATUS_ACCESS_DENIED;
89 if (seteuid(sec->uid) != 0) {
90 return NT_STATUS_ACCESS_DENIED;
96 form a unix_sec_ctx from the current security_token
98 static NTSTATUS nt_token_to_unix_security(struct ntvfs_module_context *ntvfs,
99 struct ntvfs_request *req,
100 struct security_token *token,
101 struct unix_sec_ctx **sec)
103 struct unixuid_private *private = ntvfs->private_data;
106 *sec = talloc(req, struct unix_sec_ctx);
108 /* we can't do unix security without a user and group */
109 if (token->num_sids < 2) {
110 return NT_STATUS_ACCESS_DENIED;
113 status = sidmap_sid_to_unixuid(private->sidmap,
114 token->user_sid, &(*sec)->uid);
115 if (!NT_STATUS_IS_OK(status)) {
119 status = sidmap_sid_to_unixgid(private->sidmap,
120 token->group_sid, &(*sec)->gid);
121 if (!NT_STATUS_IS_OK(status)) {
125 (*sec)->ngroups = token->num_sids - 2;
126 (*sec)->groups = talloc_array(*sec, gid_t, (*sec)->ngroups);
127 if ((*sec)->groups == NULL) {
128 return NT_STATUS_NO_MEMORY;
131 for (i=0;i<(*sec)->ngroups;i++) {
132 status = sidmap_sid_to_unixgid(private->sidmap,
133 token->sids[i+2], &(*sec)->groups[i]);
134 if (!NT_STATUS_IS_OK(status)) {
143 setup our unix security context according to the session authentication info
145 static NTSTATUS unixuid_setup_security(struct ntvfs_module_context *ntvfs,
146 struct ntvfs_request *req, struct unix_sec_ctx **sec)
148 struct unixuid_private *private = ntvfs->private_data;
149 struct security_token *token;
150 struct unix_sec_ctx *newsec;
153 if (req->session_info == NULL) {
154 return NT_STATUS_ACCESS_DENIED;
157 token = req->session_info->security_token;
159 *sec = save_unix_security(ntvfs);
161 return NT_STATUS_NO_MEMORY;
164 if (token == private->last_token) {
165 newsec = private->last_sec_ctx;
167 status = nt_token_to_unix_security(ntvfs, req, token, &newsec);
168 if (!NT_STATUS_IS_OK(status)) {
172 if (private->last_sec_ctx) {
173 talloc_free(private->last_sec_ctx);
175 private->last_sec_ctx = newsec;
176 private->last_token = token;
177 talloc_steal(private, newsec);
180 status = set_unix_security(newsec);
181 if (!NT_STATUS_IS_OK(status)) {
190 this pass through macro operates on request contexts
192 #define PASS_THRU_REQ(ntvfs, req, op, args) do { \
194 struct unix_sec_ctx *sec; \
195 status = unixuid_setup_security(ntvfs, req, &sec); \
196 NT_STATUS_NOT_OK_RETURN(status); \
197 status = ntvfs_next_##op args; \
198 status2 = set_unix_security(sec); \
200 if (!NT_STATUS_IS_OK(status2)) smb_panic("Unable to reset security context"); \
206 connect to a share - used when a tree_connect operation comes in.
208 static NTSTATUS unixuid_connect(struct ntvfs_module_context *ntvfs,
209 struct ntvfs_request *req, const char *sharename)
211 struct unixuid_private *private;
214 private = talloc(ntvfs, struct unixuid_private);
216 return NT_STATUS_NO_MEMORY;
219 private->sidmap = sidmap_open(private, ntvfs->ctx->lp_ctx);
220 if (private->sidmap == NULL) {
221 return NT_STATUS_INTERNAL_DB_CORRUPTION;
224 ntvfs->private_data = private;
225 private->last_sec_ctx = NULL;
226 private->last_token = NULL;
228 /* we don't use PASS_THRU_REQ here, as the connect operation runs with
229 root privileges. This allows the backends to setup any database
230 links they might need during the connect. */
231 status = ntvfs_next_connect(ntvfs, req, sharename);
237 disconnect from a share
239 static NTSTATUS unixuid_disconnect(struct ntvfs_module_context *ntvfs)
241 struct unixuid_private *private = ntvfs->private_data;
244 talloc_free(private);
245 ntvfs->private_data = NULL;
247 status = ntvfs_next_disconnect(ntvfs);
256 static NTSTATUS unixuid_unlink(struct ntvfs_module_context *ntvfs,
257 struct ntvfs_request *req,
258 union smb_unlink *unl)
262 PASS_THRU_REQ(ntvfs, req, unlink, (ntvfs, req, unl));
270 static NTSTATUS unixuid_ioctl(struct ntvfs_module_context *ntvfs,
271 struct ntvfs_request *req, union smb_ioctl *io)
275 PASS_THRU_REQ(ntvfs, req, ioctl, (ntvfs, req, io));
281 check if a directory exists
283 static NTSTATUS unixuid_chkpath(struct ntvfs_module_context *ntvfs,
284 struct ntvfs_request *req,
285 union smb_chkpath *cp)
289 PASS_THRU_REQ(ntvfs, req, chkpath, (ntvfs, req, cp));
295 return info on a pathname
297 static NTSTATUS unixuid_qpathinfo(struct ntvfs_module_context *ntvfs,
298 struct ntvfs_request *req, union smb_fileinfo *info)
302 PASS_THRU_REQ(ntvfs, req, qpathinfo, (ntvfs, req, info));
308 query info on a open file
310 static NTSTATUS unixuid_qfileinfo(struct ntvfs_module_context *ntvfs,
311 struct ntvfs_request *req, union smb_fileinfo *info)
315 PASS_THRU_REQ(ntvfs, req, qfileinfo, (ntvfs, req, info));
322 set info on a pathname
324 static NTSTATUS unixuid_setpathinfo(struct ntvfs_module_context *ntvfs,
325 struct ntvfs_request *req, union smb_setfileinfo *st)
329 PASS_THRU_REQ(ntvfs, req, setpathinfo, (ntvfs, req, st));
337 static NTSTATUS unixuid_open(struct ntvfs_module_context *ntvfs,
338 struct ntvfs_request *req, union smb_open *io)
342 PASS_THRU_REQ(ntvfs, req, open, (ntvfs, req, io));
350 static NTSTATUS unixuid_mkdir(struct ntvfs_module_context *ntvfs,
351 struct ntvfs_request *req, union smb_mkdir *md)
355 PASS_THRU_REQ(ntvfs, req, mkdir, (ntvfs, req, md));
363 static NTSTATUS unixuid_rmdir(struct ntvfs_module_context *ntvfs,
364 struct ntvfs_request *req, struct smb_rmdir *rd)
368 PASS_THRU_REQ(ntvfs, req, rmdir, (ntvfs, req, rd));
374 rename a set of files
376 static NTSTATUS unixuid_rename(struct ntvfs_module_context *ntvfs,
377 struct ntvfs_request *req, union smb_rename *ren)
381 PASS_THRU_REQ(ntvfs, req, rename, (ntvfs, req, ren));
389 static NTSTATUS unixuid_copy(struct ntvfs_module_context *ntvfs,
390 struct ntvfs_request *req, struct smb_copy *cp)
394 PASS_THRU_REQ(ntvfs, req, copy, (ntvfs, req, cp));
402 static NTSTATUS unixuid_read(struct ntvfs_module_context *ntvfs,
403 struct ntvfs_request *req, union smb_read *rd)
407 PASS_THRU_REQ(ntvfs, req, read, (ntvfs, req, rd));
415 static NTSTATUS unixuid_write(struct ntvfs_module_context *ntvfs,
416 struct ntvfs_request *req, union smb_write *wr)
420 PASS_THRU_REQ(ntvfs, req, write, (ntvfs, req, wr));
428 static NTSTATUS unixuid_seek(struct ntvfs_module_context *ntvfs,
429 struct ntvfs_request *req,
434 PASS_THRU_REQ(ntvfs, req, seek, (ntvfs, req, io));
442 static NTSTATUS unixuid_flush(struct ntvfs_module_context *ntvfs,
443 struct ntvfs_request *req,
448 PASS_THRU_REQ(ntvfs, req, flush, (ntvfs, req, io));
456 static NTSTATUS unixuid_close(struct ntvfs_module_context *ntvfs,
457 struct ntvfs_request *req, union smb_close *io)
461 PASS_THRU_REQ(ntvfs, req, close, (ntvfs, req, io));
469 static NTSTATUS unixuid_exit(struct ntvfs_module_context *ntvfs,
470 struct ntvfs_request *req)
474 PASS_THRU_REQ(ntvfs, req, exit, (ntvfs, req));
480 logoff - closing files
482 static NTSTATUS unixuid_logoff(struct ntvfs_module_context *ntvfs,
483 struct ntvfs_request *req)
485 struct unixuid_private *private = ntvfs->private_data;
488 PASS_THRU_REQ(ntvfs, req, logoff, (ntvfs, req));
490 private->last_token = NULL;
498 static NTSTATUS unixuid_async_setup(struct ntvfs_module_context *ntvfs,
499 struct ntvfs_request *req,
504 PASS_THRU_REQ(ntvfs, req, async_setup, (ntvfs, req, private));
510 cancel an async request
512 static NTSTATUS unixuid_cancel(struct ntvfs_module_context *ntvfs,
513 struct ntvfs_request *req)
517 PASS_THRU_REQ(ntvfs, req, cancel, (ntvfs, req));
525 static NTSTATUS unixuid_notify(struct ntvfs_module_context *ntvfs,
526 struct ntvfs_request *req, union smb_notify *info)
530 PASS_THRU_REQ(ntvfs, req, notify, (ntvfs, req, info));
538 static NTSTATUS unixuid_lock(struct ntvfs_module_context *ntvfs,
539 struct ntvfs_request *req, union smb_lock *lck)
543 PASS_THRU_REQ(ntvfs, req, lock, (ntvfs, req, lck));
549 set info on a open file
551 static NTSTATUS unixuid_setfileinfo(struct ntvfs_module_context *ntvfs,
552 struct ntvfs_request *req,
553 union smb_setfileinfo *info)
557 PASS_THRU_REQ(ntvfs, req, setfileinfo, (ntvfs, req, info));
564 return filesystem space info
566 static NTSTATUS unixuid_fsinfo(struct ntvfs_module_context *ntvfs,
567 struct ntvfs_request *req, union smb_fsinfo *fs)
571 PASS_THRU_REQ(ntvfs, req, fsinfo, (ntvfs, req, fs));
577 return print queue info
579 static NTSTATUS unixuid_lpq(struct ntvfs_module_context *ntvfs,
580 struct ntvfs_request *req, union smb_lpq *lpq)
584 PASS_THRU_REQ(ntvfs, req, lpq, (ntvfs, req, lpq));
590 list files in a directory matching a wildcard pattern
592 static NTSTATUS unixuid_search_first(struct ntvfs_module_context *ntvfs,
593 struct ntvfs_request *req, union smb_search_first *io,
594 void *search_private,
595 bool (*callback)(void *, const union smb_search_data *))
599 PASS_THRU_REQ(ntvfs, req, search_first, (ntvfs, req, io, search_private, callback));
604 /* continue a search */
605 static NTSTATUS unixuid_search_next(struct ntvfs_module_context *ntvfs,
606 struct ntvfs_request *req, union smb_search_next *io,
607 void *search_private,
608 bool (*callback)(void *, const union smb_search_data *))
612 PASS_THRU_REQ(ntvfs, req, search_next, (ntvfs, req, io, search_private, callback));
618 static NTSTATUS unixuid_search_close(struct ntvfs_module_context *ntvfs,
619 struct ntvfs_request *req, union smb_search_close *io)
623 PASS_THRU_REQ(ntvfs, req, search_close, (ntvfs, req, io));
628 /* SMBtrans - not used on file shares */
629 static NTSTATUS unixuid_trans(struct ntvfs_module_context *ntvfs,
630 struct ntvfs_request *req, struct smb_trans2 *trans2)
634 PASS_THRU_REQ(ntvfs, req, trans, (ntvfs, req, trans2));
640 initialise the unixuid backend, registering ourselves with the ntvfs subsystem
642 NTSTATUS ntvfs_unixuid_init(void)
645 struct ntvfs_ops ops;
646 NTVFS_CURRENT_CRITICAL_SIZES(vers);
650 /* fill in all the operations */
651 ops.connect = unixuid_connect;
652 ops.disconnect = unixuid_disconnect;
653 ops.unlink = unixuid_unlink;
654 ops.chkpath = unixuid_chkpath;
655 ops.qpathinfo = unixuid_qpathinfo;
656 ops.setpathinfo = unixuid_setpathinfo;
657 ops.open = unixuid_open;
658 ops.mkdir = unixuid_mkdir;
659 ops.rmdir = unixuid_rmdir;
660 ops.rename = unixuid_rename;
661 ops.copy = unixuid_copy;
662 ops.ioctl = unixuid_ioctl;
663 ops.read = unixuid_read;
664 ops.write = unixuid_write;
665 ops.seek = unixuid_seek;
666 ops.flush = unixuid_flush;
667 ops.close = unixuid_close;
668 ops.exit = unixuid_exit;
669 ops.lock = unixuid_lock;
670 ops.setfileinfo = unixuid_setfileinfo;
671 ops.qfileinfo = unixuid_qfileinfo;
672 ops.fsinfo = unixuid_fsinfo;
673 ops.lpq = unixuid_lpq;
674 ops.search_first = unixuid_search_first;
675 ops.search_next = unixuid_search_next;
676 ops.search_close = unixuid_search_close;
677 ops.trans = unixuid_trans;
678 ops.logoff = unixuid_logoff;
679 ops.async_setup = unixuid_async_setup;
680 ops.cancel = unixuid_cancel;
681 ops.notify = unixuid_notify;
683 ops.name = "unixuid";
685 /* we register under all 3 backend types, as we are not type specific */
686 ops.type = NTVFS_DISK;
687 ret = ntvfs_register(&ops, &vers);
688 if (!NT_STATUS_IS_OK(ret)) goto failed;
690 ops.type = NTVFS_PRINT;
691 ret = ntvfs_register(&ops, &vers);
692 if (!NT_STATUS_IS_OK(ret)) goto failed;
694 ops.type = NTVFS_IPC;
695 ret = ntvfs_register(&ops, &vers);
696 if (!NT_STATUS_IS_OK(ret)) goto failed;