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.
26 struct unixuid_private {
28 struct unix_sec_ctx *last_sec_ctx;
29 struct nt_user_token *last_token;
34 map a sid to a unix uid
36 static NTSTATUS sid_to_unixuid(struct ntvfs_module_context *ntvfs,
37 struct smbsrv_request *req, struct dom_sid *sid, uid_t *uid)
39 struct unixuid_private *private = ntvfs->private_data;
40 const char *attrs[] = { "sAMAccountName", "unixID", "unixName", "sAMAccountType", NULL };
44 struct ldb_message **res;
49 sidstr = dom_sid_string(ctx, sid);
51 ret = samdb_search(private->samctx, ctx, NULL, &res, attrs, "objectSid=%s", sidstr);
53 DEBUG(0,("sid_to_unixuid: unable to find sam record for sid %s\n", sidstr));
55 return NT_STATUS_ACCESS_DENIED;
58 /* make sure its a user, not a group */
59 atype = samdb_result_uint(res[0], "sAMAccountType", 0);
60 if (atype && (!(atype & ATYPE_ACCOUNT))) {
61 DEBUG(0,("sid_to_unixuid: sid %s is not an account!\n", sidstr));
63 return NT_STATUS_ACCESS_DENIED;
66 /* first try to get the uid directly */
67 s = samdb_result_string(res[0], "unixID", NULL);
69 *uid = strtoul(s, NULL, 0);
74 /* next try via the UnixName attribute */
75 s = samdb_result_string(res[0], "unixName", NULL);
77 struct passwd *pwd = getpwnam(s);
79 DEBUG(0,("unixName %s for sid %s does not exist as a local user\n", s, sidstr));
81 return NT_STATUS_ACCESS_DENIED;
88 /* finally try via the sAMAccountName attribute */
89 s = samdb_result_string(res[0], "sAMAccountName", NULL);
91 struct passwd *pwd = getpwnam(s);
93 DEBUG(0,("sAMAccountName '%s' for sid %s does not exist as a local user\n", s, sidstr));
95 return NT_STATUS_ACCESS_DENIED;
102 DEBUG(0,("sid_to_unixuid: no unixID, unixName or sAMAccountName for sid %s\n", sidstr));
105 return NT_STATUS_ACCESS_DENIED;
110 map a sid to a unix gid
112 static NTSTATUS sid_to_unixgid(struct ntvfs_module_context *ntvfs,
113 struct smbsrv_request *req, struct dom_sid *sid, gid_t *gid)
115 struct unixuid_private *private = ntvfs->private_data;
116 const char *attrs[] = { "sAMAccountName", "unixID", "unixName", "sAMAccountType", NULL };
120 struct ldb_message **res;
124 ctx = talloc(req, 0);
125 sidstr = dom_sid_string(ctx, sid);
127 ret = samdb_search(private->samctx, ctx, NULL, &res, attrs, "objectSid=%s", sidstr);
129 DEBUG(0,("sid_to_unixgid: unable to find sam record for sid %s\n", sidstr));
131 return NT_STATUS_ACCESS_DENIED;
134 /* make sure its not a user */
135 atype = samdb_result_uint(res[0], "sAMAccountType", 0);
136 if (atype && atype == ATYPE_NORMAL_ACCOUNT) {
137 DEBUG(0,("sid_to_unixgid: sid %s is a ATYPE_NORMAL_ACCOUNT\n", sidstr));
139 return NT_STATUS_ACCESS_DENIED;
142 /* first try to get the gid directly */
143 s = samdb_result_string(res[0], "unixID", NULL);
145 *gid = strtoul(s, NULL, 0);
150 /* next try via the UnixName attribute */
151 s = samdb_result_string(res[0], "unixName", NULL);
153 struct group *grp = getgrnam(s);
155 DEBUG(0,("unixName '%s' for sid %s does not exist as a local group\n", s, sidstr));
157 return NT_STATUS_ACCESS_DENIED;
164 /* finally try via the sAMAccountName attribute */
165 s = samdb_result_string(res[0], "sAMAccountName", NULL);
167 struct group *grp = getgrnam(s);
169 DEBUG(0,("sAMAccountName '%s' for sid %s does not exist as a local group\n", s, sidstr));
171 return NT_STATUS_ACCESS_DENIED;
178 DEBUG(0,("sid_to_unixgid: no unixID, unixName or sAMAccountName for sid %s\n", sidstr));
181 return NT_STATUS_ACCESS_DENIED;
184 struct unix_sec_ctx {
192 pull the current security context into a unix_sec_ctx
194 static struct unix_sec_ctx *save_unix_security(TALLOC_CTX *mem_ctx)
196 struct unix_sec_ctx *sec = talloc_p(mem_ctx, struct unix_sec_ctx);
200 sec->uid = geteuid();
201 sec->gid = getegid();
202 sec->ngroups = getgroups(0, NULL);
203 if (sec->ngroups == -1) {
207 sec->groups = talloc_array_p(sec, gid_t, sec->ngroups);
208 if (sec->groups == NULL) {
213 if (getgroups(sec->ngroups, sec->groups) != sec->ngroups) {
222 set the current security context from a unix_sec_ctx
224 static NTSTATUS set_unix_security(struct unix_sec_ctx *sec)
228 if (setgroups(sec->ngroups, sec->groups) != 0) {
229 return NT_STATUS_ACCESS_DENIED;
231 if (setegid(sec->gid) != 0) {
232 return NT_STATUS_ACCESS_DENIED;
234 if (seteuid(sec->uid) != 0) {
235 return NT_STATUS_ACCESS_DENIED;
241 form a unix_sec_ctx from the current nt_user_token
243 static NTSTATUS nt_token_to_unix_security(struct ntvfs_module_context *ntvfs,
244 struct smbsrv_request *req,
245 struct nt_user_token *token,
246 struct unix_sec_ctx **sec)
250 *sec = talloc_p(req, struct unix_sec_ctx);
252 /* we can't do unix security without a user and group */
253 if (token->num_sids < 2) {
254 return NT_STATUS_ACCESS_DENIED;
257 status = sid_to_unixuid(ntvfs, req, token->user_sids[0], &(*sec)->uid);
258 if (!NT_STATUS_IS_OK(status)) {
262 status = sid_to_unixgid(ntvfs, req, token->user_sids[1], &(*sec)->gid);
263 if (!NT_STATUS_IS_OK(status)) {
267 (*sec)->ngroups = token->num_sids - 2;
268 (*sec)->groups = talloc_array_p(*sec, gid_t, (*sec)->ngroups);
269 if ((*sec)->groups == NULL) {
270 return NT_STATUS_NO_MEMORY;
273 for (i=0;i<(*sec)->ngroups;i++) {
274 status = sid_to_unixgid(ntvfs, req, token->user_sids[i+2], &(*sec)->groups[i]);
275 if (!NT_STATUS_IS_OK(status)) {
284 setup our unix security context according to the session authentication info
286 static NTSTATUS unixuid_setup_security(struct ntvfs_module_context *ntvfs,
287 struct smbsrv_request *req, struct unix_sec_ctx **sec)
289 struct unixuid_private *private = ntvfs->private_data;
290 struct nt_user_token *token = req->session->session_info->nt_user_token;
291 void *ctx = talloc(req, 0);
292 struct unix_sec_ctx *newsec;
295 *sec = save_unix_security(req);
297 return NT_STATUS_NO_MEMORY;
300 if (req->session->session_info->nt_user_token == private->last_token) {
301 newsec = private->last_sec_ctx;
303 status = nt_token_to_unix_security(ntvfs, req, token, &newsec);
304 if (!NT_STATUS_IS_OK(status)) {
308 if (private->last_sec_ctx) {
309 talloc_free(private->last_sec_ctx);
311 private->last_sec_ctx = newsec;
312 private->last_token = req->session->session_info->nt_user_token;
313 talloc_steal(private, newsec);
316 status = set_unix_security(newsec);
317 if (!NT_STATUS_IS_OK(status)) {
328 this pass through macro operates on request contexts
330 #define PASS_THRU_REQ(ntvfs, req, op, args) do { \
332 struct unix_sec_ctx *sec; \
333 status = unixuid_setup_security(ntvfs, req, &sec); \
334 if (NT_STATUS_IS_OK(status)) status = ntvfs_next_##op args; \
335 status2 = set_unix_security(sec); \
336 if (!NT_STATUS_IS_OK(status2)) smb_panic("Unable to reset security context"); \
342 connect to a share - used when a tree_connect operation comes in.
344 static NTSTATUS unixuid_connect(struct ntvfs_module_context *ntvfs,
345 struct smbsrv_request *req, const char *sharename)
347 struct unixuid_private *private;
350 private = talloc_p(req->tcon, struct unixuid_private);
352 return NT_STATUS_NO_MEMORY;
355 private->samctx = samdb_connect(private);
356 if (private->samctx == NULL) {
357 return NT_STATUS_INTERNAL_DB_CORRUPTION;
360 ntvfs->private_data = private;
361 private->last_sec_ctx = NULL;
362 private->last_token = NULL;
364 /* we don't use PASS_THRU_REQ here, as the connect operation runs with
365 root privileges. This allows the backends to setup any database
366 links they might need during the connect. */
367 status = ntvfs_next_connect(ntvfs, req, sharename);
373 disconnect from a share
375 static NTSTATUS unixuid_disconnect(struct ntvfs_module_context *ntvfs,
376 struct smbsrv_tcon *tcon)
378 struct unixuid_private *private = ntvfs->private_data;
381 talloc_free(private);
383 status = ntvfs_next_disconnect(ntvfs, tcon);
392 static NTSTATUS unixuid_unlink(struct ntvfs_module_context *ntvfs,
393 struct smbsrv_request *req, struct smb_unlink *unl)
397 PASS_THRU_REQ(ntvfs, req, unlink, (ntvfs, req, unl));
405 static NTSTATUS unixuid_ioctl(struct ntvfs_module_context *ntvfs,
406 struct smbsrv_request *req, union smb_ioctl *io)
410 PASS_THRU_REQ(ntvfs, req, ioctl, (ntvfs, req, io));
416 check if a directory exists
418 static NTSTATUS unixuid_chkpath(struct ntvfs_module_context *ntvfs,
419 struct smbsrv_request *req, struct smb_chkpath *cp)
423 PASS_THRU_REQ(ntvfs, req, chkpath, (ntvfs, req, cp));
429 return info on a pathname
431 static NTSTATUS unixuid_qpathinfo(struct ntvfs_module_context *ntvfs,
432 struct smbsrv_request *req, union smb_fileinfo *info)
436 PASS_THRU_REQ(ntvfs, req, qpathinfo, (ntvfs, req, info));
442 query info on a open file
444 static NTSTATUS unixuid_qfileinfo(struct ntvfs_module_context *ntvfs,
445 struct smbsrv_request *req, union smb_fileinfo *info)
449 PASS_THRU_REQ(ntvfs, req, qfileinfo, (ntvfs, req, info));
456 set info on a pathname
458 static NTSTATUS unixuid_setpathinfo(struct ntvfs_module_context *ntvfs,
459 struct smbsrv_request *req, union smb_setfileinfo *st)
463 PASS_THRU_REQ(ntvfs, req, setpathinfo, (ntvfs, req, st));
471 static NTSTATUS unixuid_open(struct ntvfs_module_context *ntvfs,
472 struct smbsrv_request *req, union smb_open *io)
476 PASS_THRU_REQ(ntvfs, req, open, (ntvfs, req, io));
484 static NTSTATUS unixuid_mkdir(struct ntvfs_module_context *ntvfs,
485 struct smbsrv_request *req, union smb_mkdir *md)
489 PASS_THRU_REQ(ntvfs, req, mkdir, (ntvfs, req, md));
497 static NTSTATUS unixuid_rmdir(struct ntvfs_module_context *ntvfs,
498 struct smbsrv_request *req, struct smb_rmdir *rd)
502 PASS_THRU_REQ(ntvfs, req, rmdir, (ntvfs, req, rd));
508 rename a set of files
510 static NTSTATUS unixuid_rename(struct ntvfs_module_context *ntvfs,
511 struct smbsrv_request *req, union smb_rename *ren)
515 PASS_THRU_REQ(ntvfs, req, rename, (ntvfs, req, ren));
523 static NTSTATUS unixuid_copy(struct ntvfs_module_context *ntvfs,
524 struct smbsrv_request *req, struct smb_copy *cp)
528 PASS_THRU_REQ(ntvfs, req, copy, (ntvfs, req, cp));
536 static NTSTATUS unixuid_read(struct ntvfs_module_context *ntvfs,
537 struct smbsrv_request *req, union smb_read *rd)
541 PASS_THRU_REQ(ntvfs, req, read, (ntvfs, req, rd));
549 static NTSTATUS unixuid_write(struct ntvfs_module_context *ntvfs,
550 struct smbsrv_request *req, union smb_write *wr)
554 PASS_THRU_REQ(ntvfs, req, write, (ntvfs, req, wr));
562 static NTSTATUS unixuid_seek(struct ntvfs_module_context *ntvfs,
563 struct smbsrv_request *req, struct smb_seek *io)
567 PASS_THRU_REQ(ntvfs, req, seek, (ntvfs, req, io));
575 static NTSTATUS unixuid_flush(struct ntvfs_module_context *ntvfs,
576 struct smbsrv_request *req, struct smb_flush *io)
580 PASS_THRU_REQ(ntvfs, req, flush, (ntvfs, req, io));
588 static NTSTATUS unixuid_close(struct ntvfs_module_context *ntvfs,
589 struct smbsrv_request *req, union smb_close *io)
593 PASS_THRU_REQ(ntvfs, req, close, (ntvfs, req, io));
601 static NTSTATUS unixuid_exit(struct ntvfs_module_context *ntvfs,
602 struct smbsrv_request *req)
606 PASS_THRU_REQ(ntvfs, req, exit, (ntvfs, req));
612 logoff - closing files
614 static NTSTATUS unixuid_logoff(struct ntvfs_module_context *ntvfs,
615 struct smbsrv_request *req)
617 struct unixuid_private *private = ntvfs->private_data;
620 PASS_THRU_REQ(ntvfs, req, logoff, (ntvfs, req));
622 private->last_token = NULL;
630 static NTSTATUS unixuid_async_setup(struct ntvfs_module_context *ntvfs,
631 struct smbsrv_request *req,
636 PASS_THRU_REQ(ntvfs, req, async_setup, (ntvfs, req, private));
644 static NTSTATUS unixuid_lock(struct ntvfs_module_context *ntvfs,
645 struct smbsrv_request *req, union smb_lock *lck)
649 PASS_THRU_REQ(ntvfs, req, lock, (ntvfs, req, lck));
655 set info on a open file
657 static NTSTATUS unixuid_setfileinfo(struct ntvfs_module_context *ntvfs,
658 struct smbsrv_request *req,
659 union smb_setfileinfo *info)
663 PASS_THRU_REQ(ntvfs, req, setfileinfo, (ntvfs, req, info));
670 return filesystem space info
672 static NTSTATUS unixuid_fsinfo(struct ntvfs_module_context *ntvfs,
673 struct smbsrv_request *req, union smb_fsinfo *fs)
677 PASS_THRU_REQ(ntvfs, req, fsinfo, (ntvfs, req, fs));
683 return print queue info
685 static NTSTATUS unixuid_lpq(struct ntvfs_module_context *ntvfs,
686 struct smbsrv_request *req, union smb_lpq *lpq)
690 PASS_THRU_REQ(ntvfs, req, lpq, (ntvfs, req, lpq));
696 list files in a directory matching a wildcard pattern
698 static NTSTATUS unixuid_search_first(struct ntvfs_module_context *ntvfs,
699 struct smbsrv_request *req, union smb_search_first *io,
700 void *search_private,
701 BOOL (*callback)(void *, union smb_search_data *))
705 PASS_THRU_REQ(ntvfs, req, search_first, (ntvfs, req, io, search_private, callback));
710 /* continue a search */
711 static NTSTATUS unixuid_search_next(struct ntvfs_module_context *ntvfs,
712 struct smbsrv_request *req, union smb_search_next *io,
713 void *search_private,
714 BOOL (*callback)(void *, union smb_search_data *))
718 PASS_THRU_REQ(ntvfs, req, search_next, (ntvfs, req, io, search_private, callback));
724 static NTSTATUS unixuid_search_close(struct ntvfs_module_context *ntvfs,
725 struct smbsrv_request *req, union smb_search_close *io)
729 PASS_THRU_REQ(ntvfs, req, search_close, (ntvfs, req, io));
734 /* SMBtrans - not used on file shares */
735 static NTSTATUS unixuid_trans(struct ntvfs_module_context *ntvfs,
736 struct smbsrv_request *req, struct smb_trans2 *trans2)
740 PASS_THRU_REQ(ntvfs, req, trans, (ntvfs, req, trans2));
746 initialise the unixuid backend, registering ourselves with the ntvfs subsystem
748 NTSTATUS ntvfs_unixuid_init(void)
751 struct ntvfs_ops ops;
755 /* fill in all the operations */
756 ops.connect = unixuid_connect;
757 ops.disconnect = unixuid_disconnect;
758 ops.unlink = unixuid_unlink;
759 ops.chkpath = unixuid_chkpath;
760 ops.qpathinfo = unixuid_qpathinfo;
761 ops.setpathinfo = unixuid_setpathinfo;
762 ops.open = unixuid_open;
763 ops.mkdir = unixuid_mkdir;
764 ops.rmdir = unixuid_rmdir;
765 ops.rename = unixuid_rename;
766 ops.copy = unixuid_copy;
767 ops.ioctl = unixuid_ioctl;
768 ops.read = unixuid_read;
769 ops.write = unixuid_write;
770 ops.seek = unixuid_seek;
771 ops.flush = unixuid_flush;
772 ops.close = unixuid_close;
773 ops.exit = unixuid_exit;
774 ops.lock = unixuid_lock;
775 ops.setfileinfo = unixuid_setfileinfo;
776 ops.qfileinfo = unixuid_qfileinfo;
777 ops.fsinfo = unixuid_fsinfo;
778 ops.lpq = unixuid_lpq;
779 ops.search_first = unixuid_search_first;
780 ops.search_next = unixuid_search_next;
781 ops.search_close = unixuid_search_close;
782 ops.trans = unixuid_trans;
783 ops.logoff = unixuid_logoff;
784 ops.async_setup = unixuid_async_setup;
786 ops.name = "unixuid";
788 /* we register under all 3 backend types, as we are not type specific */
789 ops.type = NTVFS_DISK;
790 ret = register_backend("ntvfs", &ops);
791 if (!NT_STATUS_IS_OK(ret)) goto failed;
793 ops.type = NTVFS_PRINT;
794 ret = register_backend("ntvfs", &ops);
795 if (!NT_STATUS_IS_OK(ret)) goto failed;
797 ops.type = NTVFS_IPC;
798 ret = register_backend("ntvfs", &ops);
799 if (!NT_STATUS_IS_OK(ret)) goto failed;