2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Simo Sorce 2001
5 Copyright (C) Jeremy Allison 2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 static void print_grent_list(struct sys_grent *glist)
27 DEBUG(100, ("print_grent_list: %x\n", glist ));
29 DEBUG(100,("glist: %x ", glist));
31 DEBUG(100,(": gr_name = (%x) %s ", glist->gr_name, glist->gr_name));
33 DEBUG(100,(": gr_passwd = (%x) %s ", glist->gr_passwd, glist->gr_passwd));
36 for (i = 0; glist->gr_mem[i]; i++)
37 DEBUG(100,(" : gr_mem[%d] = (%x) %s ", i, glist->gr_mem[i], glist->gr_mem[i]));
39 DEBUG(100,(": gr_next = %x\n", glist->next ));
42 DEBUG(100,("FINISHED !\n\n"));
46 /****************************************************************
47 Returns a single linked list of group entries.
48 Use grent_free() to free it after use.
49 ****************************************************************/
51 struct sys_grent * getgrent_list(void)
53 struct sys_grent *glist;
54 struct sys_grent *gent;
57 gent = (struct sys_grent *) malloc(sizeof(struct sys_grent));
59 DEBUG (0, ("Out of memory in getgrent_list!\n"));
62 memset(gent, '\0', sizeof(struct sys_grent));
77 if ((gent->gr_name = strdup(grp->gr_name)) == NULL)
81 if ((gent->gr_passwd = strdup(grp->gr_passwd)) == NULL)
84 gent->gr_gid = grp->gr_gid;
86 /* number of strings in gr_mem */
87 for (num = 0; grp->gr_mem[num]; num++)
90 /* alloc space for gr_mem string pointers */
91 if ((gent->gr_mem = (char **) malloc((num+1) * sizeof(char *))) == NULL)
94 memset(gent->gr_mem, '\0', (num+1) * sizeof(char *));
96 for (i=0; i < num; i++) {
97 if ((gent->gr_mem[i] = strdup(grp->gr_mem[i])) == NULL)
100 gent->gr_mem[num] = NULL;
104 gent->next = (struct sys_grent *) malloc(sizeof(struct sys_grent));
105 if (gent->next == NULL)
108 memset(gent, '\0', sizeof(struct sys_grent));
118 DEBUG(0, ("Out of memory in getgrent_list!\n"));
123 /****************************************************************
124 Free the single linked list of group entries made by
126 ****************************************************************/
128 void grent_free (struct sys_grent *glist)
131 struct sys_grent *prev;
133 SAFE_FREE(glist->gr_name);
134 SAFE_FREE(glist->gr_passwd);
137 for (i = 0; glist->gr_mem[i]; i++)
138 SAFE_FREE(glist->gr_mem[i]);
139 SAFE_FREE(glist->gr_mem);
147 /****************************************************************
148 Returns a single linked list of passwd entries.
149 Use pwent_free() to free it after use.
150 ****************************************************************/
152 struct sys_pwent * getpwent_list(void)
154 struct sys_pwent *plist;
155 struct sys_pwent *pent;
158 pent = (struct sys_pwent *) malloc(sizeof(struct sys_pwent));
160 DEBUG (0, ("Out of memory in getpwent_list!\n"));
167 while (pwd != NULL) {
168 memset(pent, '\0', sizeof(struct sys_pwent));
170 if ((pent->pw_name = strdup(pwd->pw_name)) == NULL)
173 if (pwd->pw_passwd) {
174 if ((pent->pw_passwd = strdup(pwd->pw_passwd)) == NULL)
177 pent->pw_uid = pwd->pw_uid;
178 pent->pw_gid = pwd->pw_gid;
180 if ((pent->pw_name = strdup(pwd->pw_gecos)) == NULL)
184 if ((pent->pw_name = strdup(pwd->pw_dir)) == NULL)
188 if ((pent->pw_name = strdup(pwd->pw_shell)) == NULL)
194 pent->next = (struct sys_pwent *) malloc(sizeof(struct sys_pwent));
195 if (pent->next == NULL)
207 DEBUG(0, ("Out of memory in getpwent_list!\n"));
212 /****************************************************************
213 Free the single linked list of passwd entries made by
215 ****************************************************************/
217 void pwent_free (struct sys_pwent *plist)
220 struct sys_pwent *prev;
222 SAFE_FREE(plist->pw_name);
223 SAFE_FREE(plist->pw_passwd);
224 SAFE_FREE(plist->pw_gecos);
225 SAFE_FREE(plist->pw_dir);
226 SAFE_FREE(plist->pw_shell);
234 /****************************************************************
235 Add the individual group users onto the list.
236 ****************************************************************/
238 static struct sys_userlist *add_members_to_userlist(struct sys_userlist *list_head, const struct group *grp)
242 /* Count the number of users. */
243 for (num_users = 0; grp->gr_mem[num_users]; num_users++)
246 for (i = 0; i < num_users; i++) {
247 struct sys_userlist *entry = (struct sys_userlist *)malloc(sizeof(*entry));
248 size_t len = strlen(grp->gr_mem[i])+1;
250 free_userlist(list_head);
253 entry->unix_name = (char *)malloc(len);
254 if (entry->unix_name == NULL) {
256 free_userlist(list_head);
259 safe_strcpy(entry->unix_name, grp->gr_mem[i],len);
260 DLIST_ADD(list_head, entry);
265 /****************************************************************
266 Get the list of UNIX users in a group.
267 We have to enumerate the /etc/group file as some UNIX getgrnam()
268 calls won't do that for us (notably Tru64 UNIX).
269 ****************************************************************/
271 struct sys_userlist *get_users_in_group(const char *gname)
273 struct sys_userlist *list_head = NULL;
278 enum SID_NAME_USE name_type;
280 /* No point using winbind if we can't split it in the
282 if (split_domain_and_name(gname, domain, groupname)) {
285 * If we're doing this via winbindd, don't do the
286 * entire group list enumeration as we know this is
287 * pointless (and slow).
290 if (winbind_lookup_name(domain, groupname, &sid, &name_type)
291 && name_type == SID_NAME_DOM_GRP) {
292 if ((gptr = (struct group *)getgrnam(gname)) == NULL)
294 return add_members_to_userlist(list_head, gptr);
299 while((gptr = getgrent()) != NULL) {
300 if (strequal(gname, gptr->gr_name)) {
301 list_head = add_members_to_userlist(list_head, gptr);
302 if (list_head == NULL)
310 /****************************************************************
311 Free list allocated above.
312 ****************************************************************/
314 void free_userlist(struct sys_userlist *list_head)
317 struct sys_userlist *old_head = list_head;
318 DLIST_REMOVE(list_head, list_head);
319 SAFE_FREE(old_head->unix_name);