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"
30 struct unixuid_private {
31 struct wbc_context *wbc_ctx;
32 struct unix_sec_ctx *last_sec_ctx;
33 struct security_token *last_token;
46 pull the current security context into a unix_sec_ctx
48 static struct unix_sec_ctx *save_unix_security(TALLOC_CTX *mem_ctx)
50 struct unix_sec_ctx *sec = talloc(mem_ctx, struct unix_sec_ctx);
56 sec->ngroups = getgroups(0, NULL);
57 if (sec->ngroups == -1) {
61 sec->groups = talloc_array(sec, gid_t, sec->ngroups);
62 if (sec->groups == NULL) {
67 if (getgroups(sec->ngroups, sec->groups) != sec->ngroups) {
76 set the current security context from a unix_sec_ctx
78 static NTSTATUS set_unix_security(struct unix_sec_ctx *sec)
82 if (setgroups(sec->ngroups, sec->groups) != 0) {
83 return NT_STATUS_ACCESS_DENIED;
85 if (setegid(sec->gid) != 0) {
86 return NT_STATUS_ACCESS_DENIED;
88 if (seteuid(sec->uid) != 0) {
89 return NT_STATUS_ACCESS_DENIED;
95 form a unix_sec_ctx from the current security_token
97 static NTSTATUS nt_token_to_unix_security(struct ntvfs_module_context *ntvfs,
98 struct ntvfs_request *req,
99 struct security_token *token,
100 struct unix_sec_ctx **sec)
102 struct unixuid_private *priv = ntvfs->private_data;
105 struct id_mapping *ids;
106 struct composite_context *ctx;
107 *sec = talloc(req, struct unix_sec_ctx);
109 /* we can't do unix security without a user and group */
110 if (token->num_sids < 2) {
111 return NT_STATUS_ACCESS_DENIED;
114 ids = talloc_array(req, struct id_mapping, token->num_sids);
115 NT_STATUS_HAVE_NO_MEMORY(ids);
117 ids[0].unixid = NULL;
118 ids[0].sid = token->user_sid;
119 ids[0].status = NT_STATUS_NONE_MAPPED;
121 ids[1].unixid = NULL;
122 ids[1].sid = token->group_sid;
123 ids[1].status = NT_STATUS_NONE_MAPPED;
125 (*sec)->ngroups = token->num_sids - 2;
126 (*sec)->groups = talloc_array(*sec, gid_t, (*sec)->ngroups);
127 NT_STATUS_HAVE_NO_MEMORY((*sec)->groups);
129 for (i=0;i<(*sec)->ngroups;i++) {
130 ids[i+2].unixid = NULL;
131 ids[i+2].sid = token->sids[i+2];
132 ids[i+2].status = NT_STATUS_NONE_MAPPED;
135 ctx = wbc_sids_to_xids_send(priv->wbc_ctx, ids, token->num_sids, ids);
136 NT_STATUS_HAVE_NO_MEMORY(ctx);
138 status = wbc_sids_to_xids_recv(ctx, &ids);
139 NT_STATUS_NOT_OK_RETURN(status);
141 if (ids[0].unixid->type == ID_TYPE_BOTH ||
142 ids[0].unixid->type == ID_TYPE_UID) {
143 (*sec)->uid = ids[0].unixid->id;
145 return NT_STATUS_INVALID_SID;
148 if (ids[1].unixid->type == ID_TYPE_BOTH ||
149 ids[1].unixid->type == ID_TYPE_GID) {
150 (*sec)->gid = ids[1].unixid->id;
152 return NT_STATUS_INVALID_SID;
155 for (i=0;i<(*sec)->ngroups;i++) {
156 if (ids[i+2].unixid->type == ID_TYPE_BOTH ||
157 ids[i+2].unixid->type == ID_TYPE_GID) {
158 (*sec)->groups[i] = ids[i+2].unixid->id;
160 return NT_STATUS_INVALID_SID;
168 setup our unix security context according to the session authentication info
170 static NTSTATUS unixuid_setup_security(struct ntvfs_module_context *ntvfs,
171 struct ntvfs_request *req, struct unix_sec_ctx **sec)
173 struct unixuid_private *private = ntvfs->private_data;
174 struct security_token *token;
175 struct unix_sec_ctx *newsec;
178 if (req->session_info == NULL) {
179 return NT_STATUS_ACCESS_DENIED;
182 token = req->session_info->security_token;
184 *sec = save_unix_security(ntvfs);
186 return NT_STATUS_NO_MEMORY;
189 if (token == private->last_token) {
190 newsec = private->last_sec_ctx;
192 status = nt_token_to_unix_security(ntvfs, req, token, &newsec);
193 if (!NT_STATUS_IS_OK(status)) {
197 if (private->last_sec_ctx) {
198 talloc_free(private->last_sec_ctx);
200 private->last_sec_ctx = newsec;
201 private->last_token = token;
202 talloc_steal(private, newsec);
205 status = set_unix_security(newsec);
206 if (!NT_STATUS_IS_OK(status)) {
215 this pass through macro operates on request contexts
217 #define PASS_THRU_REQ(ntvfs, req, op, args) do { \
219 struct unix_sec_ctx *sec; \
220 status = unixuid_setup_security(ntvfs, req, &sec); \
221 NT_STATUS_NOT_OK_RETURN(status); \
222 status = ntvfs_next_##op args; \
223 status2 = set_unix_security(sec); \
225 if (!NT_STATUS_IS_OK(status2)) smb_panic("Unable to reset security context"); \
231 connect to a share - used when a tree_connect operation comes in.
233 static NTSTATUS unixuid_connect(struct ntvfs_module_context *ntvfs,
234 struct ntvfs_request *req, const char *sharename)
236 struct unixuid_private *private;
239 private = talloc(ntvfs, struct unixuid_private);
241 return NT_STATUS_NO_MEMORY;
244 private->wbc_ctx = wbc_init(private, ntvfs->ctx->msg_ctx,
245 ntvfs->ctx->event_ctx);
246 if (private->wbc_ctx == NULL) {
247 talloc_free(private);
248 return NT_STATUS_INTERNAL_ERROR;
251 ntvfs->private_data = private;
252 private->last_sec_ctx = NULL;
253 private->last_token = NULL;
255 /* we don't use PASS_THRU_REQ here, as the connect operation runs with
256 root privileges. This allows the backends to setup any database
257 links they might need during the connect. */
258 status = ntvfs_next_connect(ntvfs, req, sharename);
264 disconnect from a share
266 static NTSTATUS unixuid_disconnect(struct ntvfs_module_context *ntvfs)
268 struct unixuid_private *private = ntvfs->private_data;
271 talloc_free(private);
272 ntvfs->private_data = NULL;
274 status = ntvfs_next_disconnect(ntvfs);
283 static NTSTATUS unixuid_unlink(struct ntvfs_module_context *ntvfs,
284 struct ntvfs_request *req,
285 union smb_unlink *unl)
289 PASS_THRU_REQ(ntvfs, req, unlink, (ntvfs, req, unl));
297 static NTSTATUS unixuid_ioctl(struct ntvfs_module_context *ntvfs,
298 struct ntvfs_request *req, union smb_ioctl *io)
302 PASS_THRU_REQ(ntvfs, req, ioctl, (ntvfs, req, io));
308 check if a directory exists
310 static NTSTATUS unixuid_chkpath(struct ntvfs_module_context *ntvfs,
311 struct ntvfs_request *req,
312 union smb_chkpath *cp)
316 PASS_THRU_REQ(ntvfs, req, chkpath, (ntvfs, req, cp));
322 return info on a pathname
324 static NTSTATUS unixuid_qpathinfo(struct ntvfs_module_context *ntvfs,
325 struct ntvfs_request *req, union smb_fileinfo *info)
329 PASS_THRU_REQ(ntvfs, req, qpathinfo, (ntvfs, req, info));
335 query info on a open file
337 static NTSTATUS unixuid_qfileinfo(struct ntvfs_module_context *ntvfs,
338 struct ntvfs_request *req, union smb_fileinfo *info)
342 PASS_THRU_REQ(ntvfs, req, qfileinfo, (ntvfs, req, info));
349 set info on a pathname
351 static NTSTATUS unixuid_setpathinfo(struct ntvfs_module_context *ntvfs,
352 struct ntvfs_request *req, union smb_setfileinfo *st)
356 PASS_THRU_REQ(ntvfs, req, setpathinfo, (ntvfs, req, st));
364 static NTSTATUS unixuid_open(struct ntvfs_module_context *ntvfs,
365 struct ntvfs_request *req, union smb_open *io)
369 PASS_THRU_REQ(ntvfs, req, open, (ntvfs, req, io));
377 static NTSTATUS unixuid_mkdir(struct ntvfs_module_context *ntvfs,
378 struct ntvfs_request *req, union smb_mkdir *md)
382 PASS_THRU_REQ(ntvfs, req, mkdir, (ntvfs, req, md));
390 static NTSTATUS unixuid_rmdir(struct ntvfs_module_context *ntvfs,
391 struct ntvfs_request *req, struct smb_rmdir *rd)
395 PASS_THRU_REQ(ntvfs, req, rmdir, (ntvfs, req, rd));
401 rename a set of files
403 static NTSTATUS unixuid_rename(struct ntvfs_module_context *ntvfs,
404 struct ntvfs_request *req, union smb_rename *ren)
408 PASS_THRU_REQ(ntvfs, req, rename, (ntvfs, req, ren));
416 static NTSTATUS unixuid_copy(struct ntvfs_module_context *ntvfs,
417 struct ntvfs_request *req, struct smb_copy *cp)
421 PASS_THRU_REQ(ntvfs, req, copy, (ntvfs, req, cp));
429 static NTSTATUS unixuid_read(struct ntvfs_module_context *ntvfs,
430 struct ntvfs_request *req, union smb_read *rd)
434 PASS_THRU_REQ(ntvfs, req, read, (ntvfs, req, rd));
442 static NTSTATUS unixuid_write(struct ntvfs_module_context *ntvfs,
443 struct ntvfs_request *req, union smb_write *wr)
447 PASS_THRU_REQ(ntvfs, req, write, (ntvfs, req, wr));
455 static NTSTATUS unixuid_seek(struct ntvfs_module_context *ntvfs,
456 struct ntvfs_request *req,
461 PASS_THRU_REQ(ntvfs, req, seek, (ntvfs, req, io));
469 static NTSTATUS unixuid_flush(struct ntvfs_module_context *ntvfs,
470 struct ntvfs_request *req,
475 PASS_THRU_REQ(ntvfs, req, flush, (ntvfs, req, io));
483 static NTSTATUS unixuid_close(struct ntvfs_module_context *ntvfs,
484 struct ntvfs_request *req, union smb_close *io)
488 PASS_THRU_REQ(ntvfs, req, close, (ntvfs, req, io));
496 static NTSTATUS unixuid_exit(struct ntvfs_module_context *ntvfs,
497 struct ntvfs_request *req)
501 PASS_THRU_REQ(ntvfs, req, exit, (ntvfs, req));
507 logoff - closing files
509 static NTSTATUS unixuid_logoff(struct ntvfs_module_context *ntvfs,
510 struct ntvfs_request *req)
512 struct unixuid_private *private = ntvfs->private_data;
515 PASS_THRU_REQ(ntvfs, req, logoff, (ntvfs, req));
517 private->last_token = NULL;
525 static NTSTATUS unixuid_async_setup(struct ntvfs_module_context *ntvfs,
526 struct ntvfs_request *req,
531 PASS_THRU_REQ(ntvfs, req, async_setup, (ntvfs, req, private));
537 cancel an async request
539 static NTSTATUS unixuid_cancel(struct ntvfs_module_context *ntvfs,
540 struct ntvfs_request *req)
544 PASS_THRU_REQ(ntvfs, req, cancel, (ntvfs, req));
552 static NTSTATUS unixuid_notify(struct ntvfs_module_context *ntvfs,
553 struct ntvfs_request *req, union smb_notify *info)
557 PASS_THRU_REQ(ntvfs, req, notify, (ntvfs, req, info));
565 static NTSTATUS unixuid_lock(struct ntvfs_module_context *ntvfs,
566 struct ntvfs_request *req, union smb_lock *lck)
570 PASS_THRU_REQ(ntvfs, req, lock, (ntvfs, req, lck));
576 set info on a open file
578 static NTSTATUS unixuid_setfileinfo(struct ntvfs_module_context *ntvfs,
579 struct ntvfs_request *req,
580 union smb_setfileinfo *info)
584 PASS_THRU_REQ(ntvfs, req, setfileinfo, (ntvfs, req, info));
591 return filesystem space info
593 static NTSTATUS unixuid_fsinfo(struct ntvfs_module_context *ntvfs,
594 struct ntvfs_request *req, union smb_fsinfo *fs)
598 PASS_THRU_REQ(ntvfs, req, fsinfo, (ntvfs, req, fs));
604 return print queue info
606 static NTSTATUS unixuid_lpq(struct ntvfs_module_context *ntvfs,
607 struct ntvfs_request *req, union smb_lpq *lpq)
611 PASS_THRU_REQ(ntvfs, req, lpq, (ntvfs, req, lpq));
617 list files in a directory matching a wildcard pattern
619 static NTSTATUS unixuid_search_first(struct ntvfs_module_context *ntvfs,
620 struct ntvfs_request *req, union smb_search_first *io,
621 void *search_private,
622 bool (*callback)(void *, const union smb_search_data *))
626 PASS_THRU_REQ(ntvfs, req, search_first, (ntvfs, req, io, search_private, callback));
631 /* continue a search */
632 static NTSTATUS unixuid_search_next(struct ntvfs_module_context *ntvfs,
633 struct ntvfs_request *req, union smb_search_next *io,
634 void *search_private,
635 bool (*callback)(void *, const union smb_search_data *))
639 PASS_THRU_REQ(ntvfs, req, search_next, (ntvfs, req, io, search_private, callback));
645 static NTSTATUS unixuid_search_close(struct ntvfs_module_context *ntvfs,
646 struct ntvfs_request *req, union smb_search_close *io)
650 PASS_THRU_REQ(ntvfs, req, search_close, (ntvfs, req, io));
655 /* SMBtrans - not used on file shares */
656 static NTSTATUS unixuid_trans(struct ntvfs_module_context *ntvfs,
657 struct ntvfs_request *req, struct smb_trans2 *trans2)
661 PASS_THRU_REQ(ntvfs, req, trans, (ntvfs, req, trans2));
667 initialise the unixuid backend, registering ourselves with the ntvfs subsystem
669 NTSTATUS ntvfs_unixuid_init(void)
672 struct ntvfs_ops ops;
673 NTVFS_CURRENT_CRITICAL_SIZES(vers);
677 /* fill in all the operations */
678 ops.connect = unixuid_connect;
679 ops.disconnect = unixuid_disconnect;
680 ops.unlink = unixuid_unlink;
681 ops.chkpath = unixuid_chkpath;
682 ops.qpathinfo = unixuid_qpathinfo;
683 ops.setpathinfo = unixuid_setpathinfo;
684 ops.open = unixuid_open;
685 ops.mkdir = unixuid_mkdir;
686 ops.rmdir = unixuid_rmdir;
687 ops.rename = unixuid_rename;
688 ops.copy = unixuid_copy;
689 ops.ioctl = unixuid_ioctl;
690 ops.read = unixuid_read;
691 ops.write = unixuid_write;
692 ops.seek = unixuid_seek;
693 ops.flush = unixuid_flush;
694 ops.close = unixuid_close;
695 ops.exit = unixuid_exit;
696 ops.lock = unixuid_lock;
697 ops.setfileinfo = unixuid_setfileinfo;
698 ops.qfileinfo = unixuid_qfileinfo;
699 ops.fsinfo = unixuid_fsinfo;
700 ops.lpq = unixuid_lpq;
701 ops.search_first = unixuid_search_first;
702 ops.search_next = unixuid_search_next;
703 ops.search_close = unixuid_search_close;
704 ops.trans = unixuid_trans;
705 ops.logoff = unixuid_logoff;
706 ops.async_setup = unixuid_async_setup;
707 ops.cancel = unixuid_cancel;
708 ops.notify = unixuid_notify;
710 ops.name = "unixuid";
712 /* we register under all 3 backend types, as we are not type specific */
713 ops.type = NTVFS_DISK;
714 ret = ntvfs_register(&ops, &vers);
715 if (!NT_STATUS_IS_OK(ret)) goto failed;
717 ops.type = NTVFS_PRINT;
718 ret = ntvfs_register(&ops, &vers);
719 if (!NT_STATUS_IS_OK(ret)) goto failed;
721 ops.type = NTVFS_IPC;
722 ret = ntvfs_register(&ops, &vers);
723 if (!NT_STATUS_IS_OK(ret)) goto failed;