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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "system/filesys.h"
26 #include "system/passwd.h"
27 #include "auth/auth.h"
28 #include "smb_server/smb_server.h"
29 #include "ntvfs/ntvfs.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 == NULL) {
154 return NT_STATUS_ACCESS_DENIED;
157 token = req->session->session_info->security_token;
159 *sec = save_unix_security(req);
161 return NT_STATUS_NO_MEMORY;
164 if (req->session->session_info->security_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)) {
171 if (private->last_sec_ctx) {
172 talloc_free(private->last_sec_ctx);
174 private->last_sec_ctx = newsec;
175 private->last_token = req->session->session_info->security_token;
176 talloc_steal(private, newsec);
179 status = set_unix_security(newsec);
180 if (!NT_STATUS_IS_OK(status)) {
188 this pass through macro operates on request contexts
190 #define PASS_THRU_REQ(ntvfs, req, op, args) do { \
192 struct unix_sec_ctx *sec; \
193 status = unixuid_setup_security(ntvfs, req, &sec); \
194 if (NT_STATUS_IS_OK(status)) status = ntvfs_next_##op args; \
195 status2 = set_unix_security(sec); \
196 if (!NT_STATUS_IS_OK(status2)) smb_panic("Unable to reset security context"); \
202 connect to a share - used when a tree_connect operation comes in.
204 static NTSTATUS unixuid_connect(struct ntvfs_module_context *ntvfs,
205 struct ntvfs_request *req, const char *sharename)
207 struct unixuid_private *private;
210 private = talloc(ntvfs, struct unixuid_private);
212 return NT_STATUS_NO_MEMORY;
215 private->sidmap = sidmap_open(private);
216 if (private->sidmap == NULL) {
217 return NT_STATUS_INTERNAL_DB_CORRUPTION;
220 ntvfs->private_data = private;
221 private->last_sec_ctx = NULL;
222 private->last_token = NULL;
224 /* we don't use PASS_THRU_REQ here, as the connect operation runs with
225 root privileges. This allows the backends to setup any database
226 links they might need during the connect. */
227 status = ntvfs_next_connect(ntvfs, req, sharename);
233 disconnect from a share
235 static NTSTATUS unixuid_disconnect(struct ntvfs_module_context *ntvfs)
237 struct unixuid_private *private = ntvfs->private_data;
240 talloc_free(private);
241 ntvfs->private_data = NULL;
243 status = ntvfs_next_disconnect(ntvfs);
252 static NTSTATUS unixuid_unlink(struct ntvfs_module_context *ntvfs,
253 struct ntvfs_request *req,
254 union smb_unlink *unl)
258 PASS_THRU_REQ(ntvfs, req, unlink, (ntvfs, req, unl));
266 static NTSTATUS unixuid_ioctl(struct ntvfs_module_context *ntvfs,
267 struct ntvfs_request *req, union smb_ioctl *io)
271 PASS_THRU_REQ(ntvfs, req, ioctl, (ntvfs, req, io));
277 check if a directory exists
279 static NTSTATUS unixuid_chkpath(struct ntvfs_module_context *ntvfs,
280 struct ntvfs_request *req,
281 union smb_chkpath *cp)
285 PASS_THRU_REQ(ntvfs, req, chkpath, (ntvfs, req, cp));
291 return info on a pathname
293 static NTSTATUS unixuid_qpathinfo(struct ntvfs_module_context *ntvfs,
294 struct ntvfs_request *req, union smb_fileinfo *info)
298 PASS_THRU_REQ(ntvfs, req, qpathinfo, (ntvfs, req, info));
304 query info on a open file
306 static NTSTATUS unixuid_qfileinfo(struct ntvfs_module_context *ntvfs,
307 struct ntvfs_request *req, union smb_fileinfo *info)
311 PASS_THRU_REQ(ntvfs, req, qfileinfo, (ntvfs, req, info));
318 set info on a pathname
320 static NTSTATUS unixuid_setpathinfo(struct ntvfs_module_context *ntvfs,
321 struct ntvfs_request *req, union smb_setfileinfo *st)
325 PASS_THRU_REQ(ntvfs, req, setpathinfo, (ntvfs, req, st));
333 static NTSTATUS unixuid_open(struct ntvfs_module_context *ntvfs,
334 struct ntvfs_request *req, union smb_open *io)
338 PASS_THRU_REQ(ntvfs, req, open, (ntvfs, req, io));
346 static NTSTATUS unixuid_mkdir(struct ntvfs_module_context *ntvfs,
347 struct ntvfs_request *req, union smb_mkdir *md)
351 PASS_THRU_REQ(ntvfs, req, mkdir, (ntvfs, req, md));
359 static NTSTATUS unixuid_rmdir(struct ntvfs_module_context *ntvfs,
360 struct ntvfs_request *req, struct smb_rmdir *rd)
364 PASS_THRU_REQ(ntvfs, req, rmdir, (ntvfs, req, rd));
370 rename a set of files
372 static NTSTATUS unixuid_rename(struct ntvfs_module_context *ntvfs,
373 struct ntvfs_request *req, union smb_rename *ren)
377 PASS_THRU_REQ(ntvfs, req, rename, (ntvfs, req, ren));
385 static NTSTATUS unixuid_copy(struct ntvfs_module_context *ntvfs,
386 struct ntvfs_request *req, struct smb_copy *cp)
390 PASS_THRU_REQ(ntvfs, req, copy, (ntvfs, req, cp));
398 static NTSTATUS unixuid_read(struct ntvfs_module_context *ntvfs,
399 struct ntvfs_request *req, union smb_read *rd)
403 PASS_THRU_REQ(ntvfs, req, read, (ntvfs, req, rd));
411 static NTSTATUS unixuid_write(struct ntvfs_module_context *ntvfs,
412 struct ntvfs_request *req, union smb_write *wr)
416 PASS_THRU_REQ(ntvfs, req, write, (ntvfs, req, wr));
424 static NTSTATUS unixuid_seek(struct ntvfs_module_context *ntvfs,
425 struct ntvfs_request *req,
430 PASS_THRU_REQ(ntvfs, req, seek, (ntvfs, req, io));
438 static NTSTATUS unixuid_flush(struct ntvfs_module_context *ntvfs,
439 struct ntvfs_request *req,
444 PASS_THRU_REQ(ntvfs, req, flush, (ntvfs, req, io));
452 static NTSTATUS unixuid_close(struct ntvfs_module_context *ntvfs,
453 struct ntvfs_request *req, union smb_close *io)
457 PASS_THRU_REQ(ntvfs, req, close, (ntvfs, req, io));
465 static NTSTATUS unixuid_exit(struct ntvfs_module_context *ntvfs,
466 struct ntvfs_request *req)
470 PASS_THRU_REQ(ntvfs, req, exit, (ntvfs, req));
476 logoff - closing files
478 static NTSTATUS unixuid_logoff(struct ntvfs_module_context *ntvfs,
479 struct ntvfs_request *req)
481 struct unixuid_private *private = ntvfs->private_data;
484 PASS_THRU_REQ(ntvfs, req, logoff, (ntvfs, req));
486 private->last_token = NULL;
494 static NTSTATUS unixuid_async_setup(struct ntvfs_module_context *ntvfs,
495 struct ntvfs_request *req,
500 PASS_THRU_REQ(ntvfs, req, async_setup, (ntvfs, req, private));
506 cancel an async request
508 static NTSTATUS unixuid_cancel(struct ntvfs_module_context *ntvfs,
509 struct ntvfs_request *req)
513 PASS_THRU_REQ(ntvfs, req, cancel, (ntvfs, req));
521 static NTSTATUS unixuid_lock(struct ntvfs_module_context *ntvfs,
522 struct ntvfs_request *req, union smb_lock *lck)
526 PASS_THRU_REQ(ntvfs, req, lock, (ntvfs, req, lck));
532 set info on a open file
534 static NTSTATUS unixuid_setfileinfo(struct ntvfs_module_context *ntvfs,
535 struct ntvfs_request *req,
536 union smb_setfileinfo *info)
540 PASS_THRU_REQ(ntvfs, req, setfileinfo, (ntvfs, req, info));
547 return filesystem space info
549 static NTSTATUS unixuid_fsinfo(struct ntvfs_module_context *ntvfs,
550 struct ntvfs_request *req, union smb_fsinfo *fs)
554 PASS_THRU_REQ(ntvfs, req, fsinfo, (ntvfs, req, fs));
560 return print queue info
562 static NTSTATUS unixuid_lpq(struct ntvfs_module_context *ntvfs,
563 struct ntvfs_request *req, union smb_lpq *lpq)
567 PASS_THRU_REQ(ntvfs, req, lpq, (ntvfs, req, lpq));
573 list files in a directory matching a wildcard pattern
575 static NTSTATUS unixuid_search_first(struct ntvfs_module_context *ntvfs,
576 struct ntvfs_request *req, union smb_search_first *io,
577 void *search_private,
578 BOOL (*callback)(void *, union smb_search_data *))
582 PASS_THRU_REQ(ntvfs, req, search_first, (ntvfs, req, io, search_private, callback));
587 /* continue a search */
588 static NTSTATUS unixuid_search_next(struct ntvfs_module_context *ntvfs,
589 struct ntvfs_request *req, union smb_search_next *io,
590 void *search_private,
591 BOOL (*callback)(void *, union smb_search_data *))
595 PASS_THRU_REQ(ntvfs, req, search_next, (ntvfs, req, io, search_private, callback));
601 static NTSTATUS unixuid_search_close(struct ntvfs_module_context *ntvfs,
602 struct ntvfs_request *req, union smb_search_close *io)
606 PASS_THRU_REQ(ntvfs, req, search_close, (ntvfs, req, io));
611 /* SMBtrans - not used on file shares */
612 static NTSTATUS unixuid_trans(struct ntvfs_module_context *ntvfs,
613 struct ntvfs_request *req, struct smb_trans2 *trans2)
617 PASS_THRU_REQ(ntvfs, req, trans, (ntvfs, req, trans2));
623 initialise the unixuid backend, registering ourselves with the ntvfs subsystem
625 NTSTATUS ntvfs_unixuid_init(void)
628 struct ntvfs_ops ops;
632 /* fill in all the operations */
633 ops.connect = unixuid_connect;
634 ops.disconnect = unixuid_disconnect;
635 ops.unlink = unixuid_unlink;
636 ops.chkpath = unixuid_chkpath;
637 ops.qpathinfo = unixuid_qpathinfo;
638 ops.setpathinfo = unixuid_setpathinfo;
639 ops.open = unixuid_open;
640 ops.mkdir = unixuid_mkdir;
641 ops.rmdir = unixuid_rmdir;
642 ops.rename = unixuid_rename;
643 ops.copy = unixuid_copy;
644 ops.ioctl = unixuid_ioctl;
645 ops.read = unixuid_read;
646 ops.write = unixuid_write;
647 ops.seek = unixuid_seek;
648 ops.flush = unixuid_flush;
649 ops.close = unixuid_close;
650 ops.exit = unixuid_exit;
651 ops.lock = unixuid_lock;
652 ops.setfileinfo = unixuid_setfileinfo;
653 ops.qfileinfo = unixuid_qfileinfo;
654 ops.fsinfo = unixuid_fsinfo;
655 ops.lpq = unixuid_lpq;
656 ops.search_first = unixuid_search_first;
657 ops.search_next = unixuid_search_next;
658 ops.search_close = unixuid_search_close;
659 ops.trans = unixuid_trans;
660 ops.logoff = unixuid_logoff;
661 ops.async_setup = unixuid_async_setup;
662 ops.cancel = unixuid_cancel;
664 ops.name = "unixuid";
666 /* we register under all 3 backend types, as we are not type specific */
667 ops.type = NTVFS_DISK;
668 ret = ntvfs_register(&ops);
669 if (!NT_STATUS_IS_OK(ret)) goto failed;
671 ops.type = NTVFS_PRINT;
672 ret = ntvfs_register(&ops);
673 if (!NT_STATUS_IS_OK(ret)) goto failed;
675 ops.type = NTVFS_IPC;
676 ret = ntvfs_register(&ops);
677 if (!NT_STATUS_IS_OK(ret)) goto failed;