r5037: got rid of all of the TALLOC_DEPRECATED stuff. My apologies for the
[ira/wip.git] / source4 / ntvfs / unixuid / vfs_unixuid.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    a pass-thru NTVFS module to setup a security context using unix
5    uid/gid
6
7    Copyright (C) Andrew Tridgell 2004
8
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.
13    
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.
18    
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.
22 */
23
24 #include "includes.h"
25 #include "auth/auth.h"
26 #include "libcli/security/security.h"
27 #include "smb_server/smb_server.h"
28
29 struct unixuid_private {
30         struct sidmap_context *sidmap;
31         struct unix_sec_ctx *last_sec_ctx;
32         struct security_token *last_token;
33 };
34
35
36
37 struct unix_sec_ctx {
38         uid_t uid;
39         gid_t gid;
40         uint_t ngroups;
41         gid_t *groups;
42 };
43
44 /*
45   pull the current security context into a unix_sec_ctx
46 */
47 static struct unix_sec_ctx *save_unix_security(TALLOC_CTX *mem_ctx)
48 {
49         struct unix_sec_ctx *sec = talloc(mem_ctx, struct unix_sec_ctx);
50         if (sec == NULL) {
51                 return NULL;
52         }
53         sec->uid = geteuid();
54         sec->gid = getegid();
55         sec->ngroups = getgroups(0, NULL);
56         if (sec->ngroups == -1) {
57                 talloc_free(sec);
58                 return NULL;
59         }
60         sec->groups = talloc_array(sec, gid_t, sec->ngroups);
61         if (sec->groups == NULL) {
62                 talloc_free(sec);
63                 return NULL;
64         }
65
66         if (getgroups(sec->ngroups, sec->groups) != sec->ngroups) {
67                 talloc_free(sec);
68                 return NULL;
69         }
70
71         return sec;
72 }
73
74 /*
75   set the current security context from a unix_sec_ctx
76 */
77 static NTSTATUS set_unix_security(struct unix_sec_ctx *sec)
78 {
79         seteuid(0);
80
81         if (setgroups(sec->ngroups, sec->groups) != 0) {
82                 return NT_STATUS_ACCESS_DENIED;
83         }
84         if (setegid(sec->gid) != 0) {
85                 return NT_STATUS_ACCESS_DENIED;
86         }
87         if (seteuid(sec->uid) != 0) {
88                 return NT_STATUS_ACCESS_DENIED;
89         }
90         return NT_STATUS_OK;
91 }
92
93 /*
94   form a unix_sec_ctx from the current security_token
95 */
96 static NTSTATUS nt_token_to_unix_security(struct ntvfs_module_context *ntvfs,
97                                           struct smbsrv_request *req,
98                                           struct security_token *token,
99                                           struct unix_sec_ctx **sec)
100 {
101         struct unixuid_private *private = ntvfs->private_data;
102         int i;
103         NTSTATUS status;
104         *sec = talloc(req, struct unix_sec_ctx);
105
106         /* we can't do unix security without a user and group */
107         if (token->num_sids < 2) {
108                 return NT_STATUS_ACCESS_DENIED;
109         }
110
111         status = sidmap_sid_to_unixuid(private->sidmap, 
112                                        token->user_sid, &(*sec)->uid);
113         if (!NT_STATUS_IS_OK(status)) {
114                 return status;
115         }
116
117         status = sidmap_sid_to_unixgid(private->sidmap, 
118                                        token->group_sid, &(*sec)->gid);
119         if (!NT_STATUS_IS_OK(status)) {
120                 return status;
121         }
122
123         (*sec)->ngroups = token->num_sids - 2;
124         (*sec)->groups = talloc_array(*sec, gid_t, (*sec)->ngroups);
125         if ((*sec)->groups == NULL) {
126                 return NT_STATUS_NO_MEMORY;
127         }
128
129         for (i=0;i<(*sec)->ngroups;i++) {
130                 status = sidmap_sid_to_unixgid(private->sidmap, 
131                                                token->sids[i+2], &(*sec)->groups[i]);
132                 if (!NT_STATUS_IS_OK(status)) {
133                         return status;
134                 }
135         }
136
137         return NT_STATUS_OK;
138 }
139
140 /*
141   setup our unix security context according to the session authentication info
142 */
143 static NTSTATUS unixuid_setup_security(struct ntvfs_module_context *ntvfs,
144                                        struct smbsrv_request *req, struct unix_sec_ctx **sec)
145 {
146         struct unixuid_private *private = ntvfs->private_data;
147         struct security_token *token = req->session->session_info->security_token;
148         void *ctx = talloc_new(req);
149         struct unix_sec_ctx *newsec;
150         NTSTATUS status;
151
152         if (req->session == NULL) {
153                 return NT_STATUS_ACCESS_DENIED;
154         }
155
156         *sec = save_unix_security(req);
157         if (*sec == NULL) {
158                 return NT_STATUS_NO_MEMORY;
159         }
160
161         if (req->session->session_info->security_token == private->last_token) {
162                 newsec = private->last_sec_ctx;
163         } else {
164                 status = nt_token_to_unix_security(ntvfs, req, token, &newsec);
165                 if (!NT_STATUS_IS_OK(status)) {
166                         talloc_free(ctx);
167                         return status;
168                 }
169                 if (private->last_sec_ctx) {
170                         talloc_free(private->last_sec_ctx);
171                 }
172                 private->last_sec_ctx = newsec;
173                 private->last_token = req->session->session_info->security_token;
174                 talloc_steal(private, newsec);
175         }
176
177         status = set_unix_security(newsec);
178         if (!NT_STATUS_IS_OK(status)) {
179                 talloc_free(ctx);
180                 return status;
181         }
182
183         talloc_free(ctx);
184
185         return NT_STATUS_OK;
186 }
187
188 /*
189   this pass through macro operates on request contexts
190 */
191 #define PASS_THRU_REQ(ntvfs, req, op, args) do { \
192         NTSTATUS status2; \
193         struct unix_sec_ctx *sec; \
194         status = unixuid_setup_security(ntvfs, req, &sec); \
195         if (NT_STATUS_IS_OK(status)) status = ntvfs_next_##op args; \
196         status2 = set_unix_security(sec); \
197         if (!NT_STATUS_IS_OK(status2)) smb_panic("Unable to reset security context"); \
198 } while (0)
199
200
201
202 /*
203   connect to a share - used when a tree_connect operation comes in.
204 */
205 static NTSTATUS unixuid_connect(struct ntvfs_module_context *ntvfs,
206                                 struct smbsrv_request *req, const char *sharename)
207 {
208         struct unixuid_private *private;
209         NTSTATUS status;
210
211         private = talloc(req->tcon, struct unixuid_private);
212         if (!private) {
213                 return NT_STATUS_NO_MEMORY;
214         }
215
216         private->sidmap = sidmap_open(private);
217         if (private->sidmap == NULL) {
218                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
219         }
220
221         ntvfs->private_data = private;
222         private->last_sec_ctx = NULL;
223         private->last_token = NULL;
224
225         /* we don't use PASS_THRU_REQ here, as the connect operation runs with 
226            root privileges. This allows the backends to setup any database
227            links they might need during the connect. */
228         status = ntvfs_next_connect(ntvfs, req, sharename);
229
230         return status;
231 }
232
233 /*
234   disconnect from a share
235 */
236 static NTSTATUS unixuid_disconnect(struct ntvfs_module_context *ntvfs,
237                                    struct smbsrv_tcon *tcon)
238 {
239         struct unixuid_private *private = ntvfs->private_data;
240         NTSTATUS status;
241
242         talloc_free(private);
243
244         status = ntvfs_next_disconnect(ntvfs, tcon);
245  
246         return status;
247 }
248
249
250 /*
251   delete a file
252 */
253 static NTSTATUS unixuid_unlink(struct ntvfs_module_context *ntvfs,
254                               struct smbsrv_request *req, struct smb_unlink *unl)
255 {
256         NTSTATUS status;
257
258         PASS_THRU_REQ(ntvfs, req, unlink, (ntvfs, req, unl));
259
260         return status;
261 }
262
263 /*
264   ioctl interface
265 */
266 static NTSTATUS unixuid_ioctl(struct ntvfs_module_context *ntvfs,
267                              struct smbsrv_request *req, union smb_ioctl *io)
268 {
269         NTSTATUS status;
270
271         PASS_THRU_REQ(ntvfs, req, ioctl, (ntvfs, req, io));
272
273         return status;
274 }
275
276 /*
277   check if a directory exists
278 */
279 static NTSTATUS unixuid_chkpath(struct ntvfs_module_context *ntvfs,
280                                struct smbsrv_request *req, struct smb_chkpath *cp)
281 {
282         NTSTATUS status;
283
284         PASS_THRU_REQ(ntvfs, req, chkpath, (ntvfs, req, cp));
285
286         return status;
287 }
288
289 /*
290   return info on a pathname
291 */
292 static NTSTATUS unixuid_qpathinfo(struct ntvfs_module_context *ntvfs,
293                                  struct smbsrv_request *req, union smb_fileinfo *info)
294 {
295         NTSTATUS status;
296
297         PASS_THRU_REQ(ntvfs, req, qpathinfo, (ntvfs, req, info));
298
299         return status;
300 }
301
302 /*
303   query info on a open file
304 */
305 static NTSTATUS unixuid_qfileinfo(struct ntvfs_module_context *ntvfs,
306                                  struct smbsrv_request *req, union smb_fileinfo *info)
307 {
308         NTSTATUS status;
309
310         PASS_THRU_REQ(ntvfs, req, qfileinfo, (ntvfs, req, info));
311
312         return status;
313 }
314
315
316 /*
317   set info on a pathname
318 */
319 static NTSTATUS unixuid_setpathinfo(struct ntvfs_module_context *ntvfs,
320                                    struct smbsrv_request *req, union smb_setfileinfo *st)
321 {
322         NTSTATUS status;
323
324         PASS_THRU_REQ(ntvfs, req, setpathinfo, (ntvfs, req, st));
325
326         return status;
327 }
328
329 /*
330   open a file
331 */
332 static NTSTATUS unixuid_openfile(struct ntvfs_module_context *ntvfs,
333                                  struct smbsrv_request *req, union smb_open *io)
334 {
335         NTSTATUS status;
336
337         PASS_THRU_REQ(ntvfs, req, openfile, (ntvfs, req, io));
338
339         return status;
340 }
341
342 /*
343   create a directory
344 */
345 static NTSTATUS unixuid_mkdir(struct ntvfs_module_context *ntvfs,
346                              struct smbsrv_request *req, union smb_mkdir *md)
347 {
348         NTSTATUS status;
349
350         PASS_THRU_REQ(ntvfs, req, mkdir, (ntvfs, req, md));
351
352         return status;
353 }
354
355 /*
356   remove a directory
357 */
358 static NTSTATUS unixuid_rmdir(struct ntvfs_module_context *ntvfs,
359                              struct smbsrv_request *req, struct smb_rmdir *rd)
360 {
361         NTSTATUS status;
362
363         PASS_THRU_REQ(ntvfs, req, rmdir, (ntvfs, req, rd));
364
365         return status;
366 }
367
368 /*
369   rename a set of files
370 */
371 static NTSTATUS unixuid_rename(struct ntvfs_module_context *ntvfs,
372                               struct smbsrv_request *req, union smb_rename *ren)
373 {
374         NTSTATUS status;
375
376         PASS_THRU_REQ(ntvfs, req, rename, (ntvfs, req, ren));
377
378         return status;
379 }
380
381 /*
382   copy a set of files
383 */
384 static NTSTATUS unixuid_copy(struct ntvfs_module_context *ntvfs,
385                             struct smbsrv_request *req, struct smb_copy *cp)
386 {
387         NTSTATUS status;
388
389         PASS_THRU_REQ(ntvfs, req, copy, (ntvfs, req, cp));
390
391         return status;
392 }
393
394 /*
395   read from a file
396 */
397 static NTSTATUS unixuid_read(struct ntvfs_module_context *ntvfs,
398                             struct smbsrv_request *req, union smb_read *rd)
399 {
400         NTSTATUS status;
401
402         PASS_THRU_REQ(ntvfs, req, read, (ntvfs, req, rd));
403
404         return status;
405 }
406
407 /*
408   write to a file
409 */
410 static NTSTATUS unixuid_write(struct ntvfs_module_context *ntvfs,
411                              struct smbsrv_request *req, union smb_write *wr)
412 {
413         NTSTATUS status;
414
415         PASS_THRU_REQ(ntvfs, req, write, (ntvfs, req, wr));
416
417         return status;
418 }
419
420 /*
421   seek in a file
422 */
423 static NTSTATUS unixuid_seek(struct ntvfs_module_context *ntvfs,
424                             struct smbsrv_request *req, struct smb_seek *io)
425 {
426         NTSTATUS status;
427
428         PASS_THRU_REQ(ntvfs, req, seek, (ntvfs, req, io));
429
430         return status;
431 }
432
433 /*
434   flush a file
435 */
436 static NTSTATUS unixuid_flush(struct ntvfs_module_context *ntvfs,
437                              struct smbsrv_request *req, struct smb_flush *io)
438 {
439         NTSTATUS status;
440
441         PASS_THRU_REQ(ntvfs, req, flush, (ntvfs, req, io));
442
443         return status;
444 }
445
446 /*
447   close a file
448 */
449 static NTSTATUS unixuid_close(struct ntvfs_module_context *ntvfs,
450                              struct smbsrv_request *req, union smb_close *io)
451 {
452         NTSTATUS status;
453
454         PASS_THRU_REQ(ntvfs, req, close, (ntvfs, req, io));
455
456         return status;
457 }
458
459 /*
460   exit - closing files
461 */
462 static NTSTATUS unixuid_exit(struct ntvfs_module_context *ntvfs,
463                             struct smbsrv_request *req)
464 {
465         NTSTATUS status;
466
467         PASS_THRU_REQ(ntvfs, req, exit, (ntvfs, req));
468
469         return status;
470 }
471
472 /*
473   logoff - closing files
474 */
475 static NTSTATUS unixuid_logoff(struct ntvfs_module_context *ntvfs,
476                               struct smbsrv_request *req)
477 {
478         struct unixuid_private *private = ntvfs->private_data;
479         NTSTATUS status;
480
481         PASS_THRU_REQ(ntvfs, req, logoff, (ntvfs, req));
482
483         private->last_token = NULL;
484
485         return status;
486 }
487
488 /*
489   async setup
490 */
491 static NTSTATUS unixuid_async_setup(struct ntvfs_module_context *ntvfs,
492                                     struct smbsrv_request *req, 
493                                     void *private)
494 {
495         NTSTATUS status;
496
497         PASS_THRU_REQ(ntvfs, req, async_setup, (ntvfs, req, private));
498
499         return status;
500 }
501
502 /*
503   cancel an async request
504 */
505 static NTSTATUS unixuid_cancel(struct ntvfs_module_context *ntvfs,
506                                struct smbsrv_request *req)
507 {
508         NTSTATUS status;
509
510         PASS_THRU_REQ(ntvfs, req, cancel, (ntvfs, req));
511
512         return status;
513 }
514
515 /*
516   lock a byte range
517 */
518 static NTSTATUS unixuid_lock(struct ntvfs_module_context *ntvfs,
519                             struct smbsrv_request *req, union smb_lock *lck)
520 {
521         NTSTATUS status;
522
523         PASS_THRU_REQ(ntvfs, req, lock, (ntvfs, req, lck));
524
525         return status;
526 }
527
528 /*
529   set info on a open file
530 */
531 static NTSTATUS unixuid_setfileinfo(struct ntvfs_module_context *ntvfs,
532                                    struct smbsrv_request *req, 
533                                    union smb_setfileinfo *info)
534 {
535         NTSTATUS status;
536
537         PASS_THRU_REQ(ntvfs, req, setfileinfo, (ntvfs, req, info));
538
539         return status;
540 }
541
542
543 /*
544   return filesystem space info
545 */
546 static NTSTATUS unixuid_fsinfo(struct ntvfs_module_context *ntvfs,
547                               struct smbsrv_request *req, union smb_fsinfo *fs)
548 {
549         NTSTATUS status;
550
551         PASS_THRU_REQ(ntvfs, req, fsinfo, (ntvfs, req, fs));
552
553         return status;
554 }
555
556 /*
557   return print queue info
558 */
559 static NTSTATUS unixuid_lpq(struct ntvfs_module_context *ntvfs,
560                            struct smbsrv_request *req, union smb_lpq *lpq)
561 {
562         NTSTATUS status;
563
564         PASS_THRU_REQ(ntvfs, req, lpq, (ntvfs, req, lpq));
565
566         return status;
567 }
568
569 /* 
570    list files in a directory matching a wildcard pattern
571 */
572 static NTSTATUS unixuid_search_first(struct ntvfs_module_context *ntvfs,
573                                     struct smbsrv_request *req, union smb_search_first *io, 
574                                     void *search_private, 
575                                     BOOL (*callback)(void *, union smb_search_data *))
576 {
577         NTSTATUS status;
578
579         PASS_THRU_REQ(ntvfs, req, search_first, (ntvfs, req, io, search_private, callback));
580
581         return status;
582 }
583
584 /* continue a search */
585 static NTSTATUS unixuid_search_next(struct ntvfs_module_context *ntvfs,
586                                    struct smbsrv_request *req, union smb_search_next *io, 
587                                    void *search_private, 
588                                    BOOL (*callback)(void *, union smb_search_data *))
589 {
590         NTSTATUS status;
591
592         PASS_THRU_REQ(ntvfs, req, search_next, (ntvfs, req, io, search_private, callback));
593
594         return status;
595 }
596
597 /* close a search */
598 static NTSTATUS unixuid_search_close(struct ntvfs_module_context *ntvfs,
599                                     struct smbsrv_request *req, union smb_search_close *io)
600 {
601         NTSTATUS status;
602
603         PASS_THRU_REQ(ntvfs, req, search_close, (ntvfs, req, io));
604
605         return status;
606 }
607
608 /* SMBtrans - not used on file shares */
609 static NTSTATUS unixuid_trans(struct ntvfs_module_context *ntvfs,
610                              struct smbsrv_request *req, struct smb_trans2 *trans2)
611 {
612         NTSTATUS status;
613
614         PASS_THRU_REQ(ntvfs, req, trans, (ntvfs, req, trans2));
615
616         return status;
617 }
618
619 /*
620   initialise the unixuid backend, registering ourselves with the ntvfs subsystem
621  */
622 NTSTATUS ntvfs_unixuid_init(void)
623 {
624         NTSTATUS ret;
625         struct ntvfs_ops ops;
626
627         ZERO_STRUCT(ops);
628
629         /* fill in all the operations */
630         ops.connect = unixuid_connect;
631         ops.disconnect = unixuid_disconnect;
632         ops.unlink = unixuid_unlink;
633         ops.chkpath = unixuid_chkpath;
634         ops.qpathinfo = unixuid_qpathinfo;
635         ops.setpathinfo = unixuid_setpathinfo;
636         ops.openfile = unixuid_openfile;
637         ops.mkdir = unixuid_mkdir;
638         ops.rmdir = unixuid_rmdir;
639         ops.rename = unixuid_rename;
640         ops.copy = unixuid_copy;
641         ops.ioctl = unixuid_ioctl;
642         ops.read = unixuid_read;
643         ops.write = unixuid_write;
644         ops.seek = unixuid_seek;
645         ops.flush = unixuid_flush;      
646         ops.close = unixuid_close;
647         ops.exit = unixuid_exit;
648         ops.lock = unixuid_lock;
649         ops.setfileinfo = unixuid_setfileinfo;
650         ops.qfileinfo = unixuid_qfileinfo;
651         ops.fsinfo = unixuid_fsinfo;
652         ops.lpq = unixuid_lpq;
653         ops.search_first = unixuid_search_first;
654         ops.search_next = unixuid_search_next;
655         ops.search_close = unixuid_search_close;
656         ops.trans = unixuid_trans;
657         ops.logoff = unixuid_logoff;
658         ops.async_setup = unixuid_async_setup;
659         ops.cancel = unixuid_cancel;
660
661         ops.name = "unixuid";
662
663         /* we register under all 3 backend types, as we are not type specific */
664         ops.type = NTVFS_DISK;  
665         ret = ntvfs_register(&ops);
666         if (!NT_STATUS_IS_OK(ret)) goto failed;
667
668         ops.type = NTVFS_PRINT; 
669         ret = ntvfs_register(&ops);
670         if (!NT_STATUS_IS_OK(ret)) goto failed;
671
672         ops.type = NTVFS_IPC;   
673         ret = ntvfs_register(&ops);
674         if (!NT_STATUS_IS_OK(ret)) goto failed;
675         
676 failed:
677         return ret;
678 }