More scalable print tdb fixes.
[samba.git] / source3 / lib / util_smbd.c
1 /*
2    Unix SMB/CIFS implementation.
3    Samba utility functions, used in smbd only
4    Copyright (C) Andrew Tridgell 2002
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 /* 
24    This function requires sys_getgrouplist - which is only
25    available in smbd due to it's use of become_root() in a 
26    legacy systems hack.
27 */
28
29 /*
30   return a full list of groups for a user
31
32   returns the number of groups the user is a member of. The return will include the
33   users primary group.
34
35   remember to free the resulting gid_t array
36
37   NOTE! uses become_root() to gain correct priviages on systems
38   that lack a native getgroups() call (uses initgroups and getgroups)
39 */
40 int getgroups_user(const char *user, gid_t **groups)
41 {
42         struct passwd *pwd;
43         int ngrp, max_grp;
44
45         pwd = getpwnam_alloc(user);
46         if (!pwd) return -1;
47
48         max_grp = groups_max();
49         (*groups) = (gid_t *)malloc(sizeof(gid_t) * max_grp);
50         if (! *groups) {
51                 passwd_free(&pwd);
52                 errno = ENOMEM;
53                 return -1;
54         }
55
56         ngrp = sys_getgrouplist(user, pwd->pw_gid, *groups, &max_grp);
57         if (ngrp <= 0) {
58                 passwd_free(&pwd);
59                 free(*groups);
60                 return ngrp;
61         }
62
63         passwd_free(&pwd);
64         return ngrp;
65 }