2 Unix SMB/Netbios implementation.
4 Samba utility functions
5 Copyright (C) Simo Sorce 2001
6 Copyright (C) Jeremy Allison 2001
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 static void print_grent_list(struct sys_grent
*glist
)
28 DEBUG(100, ("print_grent_list: %x\n", glist
));
30 DEBUG(100,("glist: %x ", glist
));
32 DEBUG(100,(": gr_name = (%x) %s ", glist
->gr_name
, glist
->gr_name
));
34 DEBUG(100,(": gr_passwd = (%x) %s ", glist
->gr_passwd
, glist
->gr_passwd
));
37 for (i
= 0; glist
->gr_mem
[i
]; i
++)
38 DEBUG(100,(" : gr_mem[%d] = (%x) %s ", i
, glist
->gr_mem
[i
], glist
->gr_mem
[i
]));
40 DEBUG(100,(": gr_next = %x\n", glist
->next
));
43 DEBUG(100,("FINISHED !\n\n"));
47 /****************************************************************
48 Returns a single linked list of group entries.
49 Use grent_free() to free it after use.
50 ****************************************************************/
52 struct sys_grent
* getgrent_list(void)
54 struct sys_grent
*glist
;
55 struct sys_grent
*gent
;
58 gent
= (struct sys_grent
*) malloc(sizeof(struct sys_grent
));
60 DEBUG (0, ("Out of memory in getgrent_list!\n"));
63 memset(gent
, '\0', sizeof(struct sys_grent
));
78 if ((gent
->gr_name
= strdup(grp
->gr_name
)) == NULL
)
82 if ((gent
->gr_passwd
= strdup(grp
->gr_passwd
)) == NULL
)
85 gent
->gr_gid
= grp
->gr_gid
;
87 /* number of strings in gr_mem */
88 for (num
= 0; grp
->gr_mem
[num
]; num
++)
91 /* alloc space for gr_mem string pointers */
92 if ((gent
->gr_mem
= (char **) malloc((num
+1) * sizeof(char *))) == NULL
)
95 memset(gent
->gr_mem
, '\0', (num
+1) * sizeof(char *));
97 for (i
=0; i
< num
; i
++) {
98 if ((gent
->gr_mem
[i
] = strdup(grp
->gr_mem
[i
])) == NULL
)
101 gent
->gr_mem
[num
] = NULL
;
105 gent
->next
= (struct sys_grent
*) malloc(sizeof(struct sys_grent
));
106 if (gent
->next
== NULL
)
109 memset(gent
, '\0', sizeof(struct sys_grent
));
119 DEBUG(0, ("Out of memory in getgrent_list!\n"));
124 /****************************************************************
125 Free the single linked list of group entries made by
127 ****************************************************************/
129 void grent_free (struct sys_grent
*glist
)
132 struct sys_grent
*prev
;
134 SAFE_FREE(glist
->gr_name
);
135 SAFE_FREE(glist
->gr_passwd
);
138 for (i
= 0; glist
->gr_mem
[i
]; i
++)
139 SAFE_FREE(glist
->gr_mem
[i
]);
140 SAFE_FREE(glist
->gr_mem
);
148 /****************************************************************
149 Returns a single linked list of passwd entries.
150 Use pwent_free() to free it after use.
151 ****************************************************************/
153 struct sys_pwent
* getpwent_list(void)
155 struct sys_pwent
*plist
;
156 struct sys_pwent
*pent
;
159 pent
= (struct sys_pwent
*) malloc(sizeof(struct sys_pwent
));
161 DEBUG (0, ("Out of memory in getpwent_list!\n"));
168 while (pwd
!= NULL
) {
169 memset(pent
, '\0', sizeof(struct sys_pwent
));
171 if ((pent
->pw_name
= strdup(pwd
->pw_name
)) == NULL
)
174 if (pwd
->pw_passwd
) {
175 if ((pent
->pw_passwd
= strdup(pwd
->pw_passwd
)) == NULL
)
178 pent
->pw_uid
= pwd
->pw_uid
;
179 pent
->pw_gid
= pwd
->pw_gid
;
181 if ((pent
->pw_name
= strdup(pwd
->pw_gecos
)) == NULL
)
185 if ((pent
->pw_name
= strdup(pwd
->pw_dir
)) == NULL
)
189 if ((pent
->pw_name
= strdup(pwd
->pw_shell
)) == NULL
)
195 pent
->next
= (struct sys_pwent
*) malloc(sizeof(struct sys_pwent
));
196 if (pent
->next
== NULL
)
208 DEBUG(0, ("Out of memory in getpwent_list!\n"));
213 /****************************************************************
214 Free the single linked list of passwd entries made by
216 ****************************************************************/
218 void pwent_free (struct sys_pwent
*plist
)
221 struct sys_pwent
*prev
;
223 SAFE_FREE(plist
->pw_name
);
224 SAFE_FREE(plist
->pw_passwd
);
225 SAFE_FREE(plist
->pw_gecos
);
226 SAFE_FREE(plist
->pw_dir
);
227 SAFE_FREE(plist
->pw_shell
);
235 /****************************************************************
236 Add the individual group users onto the list.
237 ****************************************************************/
239 static struct sys_userlist
*add_members_to_userlist(struct sys_userlist
*list_head
, const struct group
*grp
)
243 /* Count the number of users. */
244 for (num_users
= 0; grp
->gr_mem
[num_users
]; num_users
++)
247 for (i
= 0; i
< num_users
; i
++) {
248 struct sys_userlist
*entry
= (struct sys_userlist
*)malloc(sizeof(*entry
));
250 free_userlist(list_head
);
253 entry
->unix_name
= strdup(grp
->gr_mem
[i
]);
254 if (entry
->unix_name
== NULL
) {
256 free_userlist(list_head
);
259 DLIST_ADD(list_head
, entry
);
264 /****************************************************************
265 Get the list of UNIX users in a group.
266 We have to enumerate the /etc/group file as some UNIX getgrnam()
267 calls won't do that for us (notably Tru64 UNIX).
268 ****************************************************************/
270 struct sys_userlist
*get_users_in_group(const char *gname
)
272 struct sys_userlist
*list_head
= NULL
;
276 * If we're doing this via winbindd, don't do the
277 * entire group list enumeration as we know this is
278 * pointless (and slow).
281 if (strchr(gname
,*lp_winbind_separator())) {
282 if ((gptr
= (struct group
*)getgrnam(gname
)) == NULL
)
284 return add_members_to_userlist(list_head
, gptr
);
287 #if !defined(BROKEN_GETGRNAM)
288 if ((gptr
= (struct group
*)getgrnam(gname
)) == NULL
)
290 return add_members_to_userlist(list_head
, gptr
);
293 while((gptr
= getgrent()) != NULL
) {
294 if (strequal(gname
, gptr
->gr_name
)) {
295 list_head
= add_members_to_userlist(list_head
, gptr
);
296 if (list_head
== NULL
)
305 /****************************************************************
306 Free list allocated above.
307 ****************************************************************/
309 void free_userlist(struct sys_userlist
*list_head
)
312 struct sys_userlist
*old_head
= list_head
;
313 DLIST_REMOVE(list_head
, list_head
);
314 SAFE_FREE(old_head
->unix_name
);