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 "auth/auth.h"
27 struct unixuid_private {
29 struct unix_sec_ctx *last_sec_ctx;
30 struct nt_user_token *last_token;
35 map a sid to a unix uid
37 static NTSTATUS sid_to_unixuid(struct ntvfs_module_context *ntvfs,
38 struct smbsrv_request *req, struct dom_sid *sid, uid_t *uid)
40 struct unixuid_private *private = ntvfs->private_data;
41 const char *attrs[] = { "sAMAccountName", "unixID", "unixName", "sAMAccountType", NULL };
45 struct ldb_message **res;
50 sidstr = dom_sid_string(ctx, sid);
52 ret = samdb_search(private->samctx, ctx, NULL, &res, attrs, "objectSid=%s", sidstr);
54 DEBUG(0,("sid_to_unixuid: unable to find sam record for sid %s\n", sidstr));
56 return NT_STATUS_ACCESS_DENIED;
59 /* make sure its a user, not a group */
60 atype = samdb_result_uint(res[0], "sAMAccountType", 0);
61 if (atype && (!(atype & ATYPE_ACCOUNT))) {
62 DEBUG(0,("sid_to_unixuid: sid %s is not an account!\n", sidstr));
64 return NT_STATUS_ACCESS_DENIED;
67 /* first try to get the uid directly */
68 s = samdb_result_string(res[0], "unixID", NULL);
70 *uid = strtoul(s, NULL, 0);
75 /* next try via the UnixName attribute */
76 s = samdb_result_string(res[0], "unixName", NULL);
78 struct passwd *pwd = getpwnam(s);
80 DEBUG(0,("unixName %s for sid %s does not exist as a local user\n", s, sidstr));
82 return NT_STATUS_ACCESS_DENIED;
89 /* finally try via the sAMAccountName attribute */
90 s = samdb_result_string(res[0], "sAMAccountName", NULL);
92 struct passwd *pwd = getpwnam(s);
94 DEBUG(0,("sAMAccountName '%s' for sid %s does not exist as a local user\n", s, sidstr));
96 return NT_STATUS_ACCESS_DENIED;
103 DEBUG(0,("sid_to_unixuid: no unixID, unixName or sAMAccountName for sid %s\n", sidstr));
106 return NT_STATUS_ACCESS_DENIED;
111 map a sid to a unix gid
113 static NTSTATUS sid_to_unixgid(struct ntvfs_module_context *ntvfs,
114 struct smbsrv_request *req, struct dom_sid *sid, gid_t *gid)
116 struct unixuid_private *private = ntvfs->private_data;
117 const char *attrs[] = { "sAMAccountName", "unixID", "unixName", "sAMAccountType", NULL };
121 struct ldb_message **res;
125 ctx = talloc(req, 0);
126 sidstr = dom_sid_string(ctx, sid);
128 ret = samdb_search(private->samctx, ctx, NULL, &res, attrs, "objectSid=%s", sidstr);
130 DEBUG(0,("sid_to_unixgid: unable to find sam record for sid %s\n", sidstr));
132 return NT_STATUS_ACCESS_DENIED;
135 /* make sure its not a user */
136 atype = samdb_result_uint(res[0], "sAMAccountType", 0);
137 if (atype && atype == ATYPE_NORMAL_ACCOUNT) {
138 DEBUG(0,("sid_to_unixgid: sid %s is a ATYPE_NORMAL_ACCOUNT\n", sidstr));
140 return NT_STATUS_ACCESS_DENIED;
143 /* first try to get the gid directly */
144 s = samdb_result_string(res[0], "unixID", NULL);
146 *gid = strtoul(s, NULL, 0);
151 /* next try via the UnixName attribute */
152 s = samdb_result_string(res[0], "unixName", NULL);
154 struct group *grp = getgrnam(s);
156 DEBUG(0,("unixName '%s' for sid %s does not exist as a local group\n", s, sidstr));
158 return NT_STATUS_ACCESS_DENIED;
165 /* finally try via the sAMAccountName attribute */
166 s = samdb_result_string(res[0], "sAMAccountName", NULL);
168 struct group *grp = getgrnam(s);
170 DEBUG(0,("sAMAccountName '%s' for sid %s does not exist as a local group\n", s, sidstr));
172 return NT_STATUS_ACCESS_DENIED;
179 DEBUG(0,("sid_to_unixgid: no unixID, unixName or sAMAccountName for sid %s\n", sidstr));
182 return NT_STATUS_ACCESS_DENIED;
185 struct unix_sec_ctx {
193 pull the current security context into a unix_sec_ctx
195 static struct unix_sec_ctx *save_unix_security(TALLOC_CTX *mem_ctx)
197 struct unix_sec_ctx *sec = talloc_p(mem_ctx, struct unix_sec_ctx);
201 sec->uid = geteuid();
202 sec->gid = getegid();
203 sec->ngroups = getgroups(0, NULL);
204 if (sec->ngroups == -1) {
208 sec->groups = talloc_array_p(sec, gid_t, sec->ngroups);
209 if (sec->groups == NULL) {
214 if (getgroups(sec->ngroups, sec->groups) != sec->ngroups) {
223 set the current security context from a unix_sec_ctx
225 static NTSTATUS set_unix_security(struct unix_sec_ctx *sec)
229 if (setgroups(sec->ngroups, sec->groups) != 0) {
230 return NT_STATUS_ACCESS_DENIED;
232 if (setegid(sec->gid) != 0) {
233 return NT_STATUS_ACCESS_DENIED;
235 if (seteuid(sec->uid) != 0) {
236 return NT_STATUS_ACCESS_DENIED;
242 form a unix_sec_ctx from the current nt_user_token
244 static NTSTATUS nt_token_to_unix_security(struct ntvfs_module_context *ntvfs,
245 struct smbsrv_request *req,
246 struct nt_user_token *token,
247 struct unix_sec_ctx **sec)
251 *sec = talloc_p(req, struct unix_sec_ctx);
253 /* we can't do unix security without a user and group */
254 if (token->num_sids < 2) {
255 return NT_STATUS_ACCESS_DENIED;
258 status = sid_to_unixuid(ntvfs, req, token->user_sids[0], &(*sec)->uid);
259 if (!NT_STATUS_IS_OK(status)) {
263 status = sid_to_unixgid(ntvfs, req, token->user_sids[1], &(*sec)->gid);
264 if (!NT_STATUS_IS_OK(status)) {
268 (*sec)->ngroups = token->num_sids - 2;
269 (*sec)->groups = talloc_array_p(*sec, gid_t, (*sec)->ngroups);
270 if ((*sec)->groups == NULL) {
271 return NT_STATUS_NO_MEMORY;
274 for (i=0;i<(*sec)->ngroups;i++) {
275 status = sid_to_unixgid(ntvfs, req, token->user_sids[i+2], &(*sec)->groups[i]);
276 if (!NT_STATUS_IS_OK(status)) {
285 setup our unix security context according to the session authentication info
287 static NTSTATUS unixuid_setup_security(struct ntvfs_module_context *ntvfs,
288 struct smbsrv_request *req, struct unix_sec_ctx **sec)
290 struct unixuid_private *private = ntvfs->private_data;
291 struct nt_user_token *token = req->session->session_info->nt_user_token;
292 void *ctx = talloc(req, 0);
293 struct unix_sec_ctx *newsec;
296 *sec = save_unix_security(req);
298 return NT_STATUS_NO_MEMORY;
301 if (req->session->session_info->nt_user_token == private->last_token) {
302 newsec = private->last_sec_ctx;
304 status = nt_token_to_unix_security(ntvfs, req, token, &newsec);
305 if (!NT_STATUS_IS_OK(status)) {
309 if (private->last_sec_ctx) {
310 talloc_free(private->last_sec_ctx);
312 private->last_sec_ctx = newsec;
313 private->last_token = req->session->session_info->nt_user_token;
314 talloc_steal(private, newsec);
317 status = set_unix_security(newsec);
318 if (!NT_STATUS_IS_OK(status)) {
329 this pass through macro operates on request contexts
331 #define PASS_THRU_REQ(ntvfs, req, op, args) do { \
333 struct unix_sec_ctx *sec; \
334 status = unixuid_setup_security(ntvfs, req, &sec); \
335 if (NT_STATUS_IS_OK(status)) status = ntvfs_next_##op args; \
336 status2 = set_unix_security(sec); \
337 if (!NT_STATUS_IS_OK(status2)) smb_panic("Unable to reset security context"); \
343 connect to a share - used when a tree_connect operation comes in.
345 static NTSTATUS unixuid_connect(struct ntvfs_module_context *ntvfs,
346 struct smbsrv_request *req, const char *sharename)
348 struct unixuid_private *private;
351 private = talloc_p(req->tcon, struct unixuid_private);
353 return NT_STATUS_NO_MEMORY;
356 private->samctx = samdb_connect(private);
357 if (private->samctx == NULL) {
358 return NT_STATUS_INTERNAL_DB_CORRUPTION;
361 ntvfs->private_data = private;
362 private->last_sec_ctx = NULL;
363 private->last_token = NULL;
365 /* we don't use PASS_THRU_REQ here, as the connect operation runs with
366 root privileges. This allows the backends to setup any database
367 links they might need during the connect. */
368 status = ntvfs_next_connect(ntvfs, req, sharename);
374 disconnect from a share
376 static NTSTATUS unixuid_disconnect(struct ntvfs_module_context *ntvfs,
377 struct smbsrv_tcon *tcon)
379 struct unixuid_private *private = ntvfs->private_data;
382 talloc_free(private);
384 status = ntvfs_next_disconnect(ntvfs, tcon);
393 static NTSTATUS unixuid_unlink(struct ntvfs_module_context *ntvfs,
394 struct smbsrv_request *req, struct smb_unlink *unl)
398 PASS_THRU_REQ(ntvfs, req, unlink, (ntvfs, req, unl));
406 static NTSTATUS unixuid_ioctl(struct ntvfs_module_context *ntvfs,
407 struct smbsrv_request *req, union smb_ioctl *io)
411 PASS_THRU_REQ(ntvfs, req, ioctl, (ntvfs, req, io));
417 check if a directory exists
419 static NTSTATUS unixuid_chkpath(struct ntvfs_module_context *ntvfs,
420 struct smbsrv_request *req, struct smb_chkpath *cp)
424 PASS_THRU_REQ(ntvfs, req, chkpath, (ntvfs, req, cp));
430 return info on a pathname
432 static NTSTATUS unixuid_qpathinfo(struct ntvfs_module_context *ntvfs,
433 struct smbsrv_request *req, union smb_fileinfo *info)
437 PASS_THRU_REQ(ntvfs, req, qpathinfo, (ntvfs, req, info));
443 query info on a open file
445 static NTSTATUS unixuid_qfileinfo(struct ntvfs_module_context *ntvfs,
446 struct smbsrv_request *req, union smb_fileinfo *info)
450 PASS_THRU_REQ(ntvfs, req, qfileinfo, (ntvfs, req, info));
457 set info on a pathname
459 static NTSTATUS unixuid_setpathinfo(struct ntvfs_module_context *ntvfs,
460 struct smbsrv_request *req, union smb_setfileinfo *st)
464 PASS_THRU_REQ(ntvfs, req, setpathinfo, (ntvfs, req, st));
472 static NTSTATUS unixuid_openfile(struct ntvfs_module_context *ntvfs,
473 struct smbsrv_request *req, union smb_open *io)
477 PASS_THRU_REQ(ntvfs, req, openfile, (ntvfs, req, io));
485 static NTSTATUS unixuid_mkdir(struct ntvfs_module_context *ntvfs,
486 struct smbsrv_request *req, union smb_mkdir *md)
490 PASS_THRU_REQ(ntvfs, req, mkdir, (ntvfs, req, md));
498 static NTSTATUS unixuid_rmdir(struct ntvfs_module_context *ntvfs,
499 struct smbsrv_request *req, struct smb_rmdir *rd)
503 PASS_THRU_REQ(ntvfs, req, rmdir, (ntvfs, req, rd));
509 rename a set of files
511 static NTSTATUS unixuid_rename(struct ntvfs_module_context *ntvfs,
512 struct smbsrv_request *req, union smb_rename *ren)
516 PASS_THRU_REQ(ntvfs, req, rename, (ntvfs, req, ren));
524 static NTSTATUS unixuid_copy(struct ntvfs_module_context *ntvfs,
525 struct smbsrv_request *req, struct smb_copy *cp)
529 PASS_THRU_REQ(ntvfs, req, copy, (ntvfs, req, cp));
537 static NTSTATUS unixuid_read(struct ntvfs_module_context *ntvfs,
538 struct smbsrv_request *req, union smb_read *rd)
542 PASS_THRU_REQ(ntvfs, req, read, (ntvfs, req, rd));
550 static NTSTATUS unixuid_write(struct ntvfs_module_context *ntvfs,
551 struct smbsrv_request *req, union smb_write *wr)
555 PASS_THRU_REQ(ntvfs, req, write, (ntvfs, req, wr));
563 static NTSTATUS unixuid_seek(struct ntvfs_module_context *ntvfs,
564 struct smbsrv_request *req, struct smb_seek *io)
568 PASS_THRU_REQ(ntvfs, req, seek, (ntvfs, req, io));
576 static NTSTATUS unixuid_flush(struct ntvfs_module_context *ntvfs,
577 struct smbsrv_request *req, struct smb_flush *io)
581 PASS_THRU_REQ(ntvfs, req, flush, (ntvfs, req, io));
589 static NTSTATUS unixuid_close(struct ntvfs_module_context *ntvfs,
590 struct smbsrv_request *req, union smb_close *io)
594 PASS_THRU_REQ(ntvfs, req, close, (ntvfs, req, io));
602 static NTSTATUS unixuid_exit(struct ntvfs_module_context *ntvfs,
603 struct smbsrv_request *req)
607 PASS_THRU_REQ(ntvfs, req, exit, (ntvfs, req));
613 logoff - closing files
615 static NTSTATUS unixuid_logoff(struct ntvfs_module_context *ntvfs,
616 struct smbsrv_request *req)
618 struct unixuid_private *private = ntvfs->private_data;
621 PASS_THRU_REQ(ntvfs, req, logoff, (ntvfs, req));
623 private->last_token = NULL;
631 static NTSTATUS unixuid_async_setup(struct ntvfs_module_context *ntvfs,
632 struct smbsrv_request *req,
637 PASS_THRU_REQ(ntvfs, req, async_setup, (ntvfs, req, private));
645 static NTSTATUS unixuid_lock(struct ntvfs_module_context *ntvfs,
646 struct smbsrv_request *req, union smb_lock *lck)
650 PASS_THRU_REQ(ntvfs, req, lock, (ntvfs, req, lck));
656 set info on a open file
658 static NTSTATUS unixuid_setfileinfo(struct ntvfs_module_context *ntvfs,
659 struct smbsrv_request *req,
660 union smb_setfileinfo *info)
664 PASS_THRU_REQ(ntvfs, req, setfileinfo, (ntvfs, req, info));
671 return filesystem space info
673 static NTSTATUS unixuid_fsinfo(struct ntvfs_module_context *ntvfs,
674 struct smbsrv_request *req, union smb_fsinfo *fs)
678 PASS_THRU_REQ(ntvfs, req, fsinfo, (ntvfs, req, fs));
684 return print queue info
686 static NTSTATUS unixuid_lpq(struct ntvfs_module_context *ntvfs,
687 struct smbsrv_request *req, union smb_lpq *lpq)
691 PASS_THRU_REQ(ntvfs, req, lpq, (ntvfs, req, lpq));
697 list files in a directory matching a wildcard pattern
699 static NTSTATUS unixuid_search_first(struct ntvfs_module_context *ntvfs,
700 struct smbsrv_request *req, union smb_search_first *io,
701 void *search_private,
702 BOOL (*callback)(void *, union smb_search_data *))
706 PASS_THRU_REQ(ntvfs, req, search_first, (ntvfs, req, io, search_private, callback));
711 /* continue a search */
712 static NTSTATUS unixuid_search_next(struct ntvfs_module_context *ntvfs,
713 struct smbsrv_request *req, union smb_search_next *io,
714 void *search_private,
715 BOOL (*callback)(void *, union smb_search_data *))
719 PASS_THRU_REQ(ntvfs, req, search_next, (ntvfs, req, io, search_private, callback));
725 static NTSTATUS unixuid_search_close(struct ntvfs_module_context *ntvfs,
726 struct smbsrv_request *req, union smb_search_close *io)
730 PASS_THRU_REQ(ntvfs, req, search_close, (ntvfs, req, io));
735 /* SMBtrans - not used on file shares */
736 static NTSTATUS unixuid_trans(struct ntvfs_module_context *ntvfs,
737 struct smbsrv_request *req, struct smb_trans2 *trans2)
741 PASS_THRU_REQ(ntvfs, req, trans, (ntvfs, req, trans2));
747 initialise the unixuid backend, registering ourselves with the ntvfs subsystem
749 NTSTATUS ntvfs_unixuid_init(void)
752 struct ntvfs_ops ops;
756 /* fill in all the operations */
757 ops.connect = unixuid_connect;
758 ops.disconnect = unixuid_disconnect;
759 ops.unlink = unixuid_unlink;
760 ops.chkpath = unixuid_chkpath;
761 ops.qpathinfo = unixuid_qpathinfo;
762 ops.setpathinfo = unixuid_setpathinfo;
763 ops.openfile = unixuid_openfile;
764 ops.mkdir = unixuid_mkdir;
765 ops.rmdir = unixuid_rmdir;
766 ops.rename = unixuid_rename;
767 ops.copy = unixuid_copy;
768 ops.ioctl = unixuid_ioctl;
769 ops.read = unixuid_read;
770 ops.write = unixuid_write;
771 ops.seek = unixuid_seek;
772 ops.flush = unixuid_flush;
773 ops.close = unixuid_close;
774 ops.exit = unixuid_exit;
775 ops.lock = unixuid_lock;
776 ops.setfileinfo = unixuid_setfileinfo;
777 ops.qfileinfo = unixuid_qfileinfo;
778 ops.fsinfo = unixuid_fsinfo;
779 ops.lpq = unixuid_lpq;
780 ops.search_first = unixuid_search_first;
781 ops.search_next = unixuid_search_next;
782 ops.search_close = unixuid_search_close;
783 ops.trans = unixuid_trans;
784 ops.logoff = unixuid_logoff;
785 ops.async_setup = unixuid_async_setup;
787 ops.name = "unixuid";
789 /* we register under all 3 backend types, as we are not type specific */
790 ops.type = NTVFS_DISK;
791 ret = register_backend("ntvfs", &ops);
792 if (!NT_STATUS_IS_OK(ret)) goto failed;
794 ops.type = NTVFS_PRINT;
795 ret = register_backend("ntvfs", &ops);
796 if (!NT_STATUS_IS_OK(ret)) goto failed;
798 ops.type = NTVFS_IPC;
799 ret = register_backend("ntvfs", &ops);
800 if (!NT_STATUS_IS_OK(ret)) goto failed;