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 3 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, see <http://www.gnu.org/licenses/>.
24 /****************************************************************
25 Returns a single linked list of group entries.
26 Use grent_free() to free it after use.
27 ****************************************************************/
29 struct sys_grent
* getgrent_list(void)
31 struct sys_grent
*glist
;
32 struct sys_grent
*gent
;
35 gent
= malloc_p(struct sys_grent
);
37 DEBUG (0, ("Out of memory in getgrent_list!\n"));
40 memset(gent
, '\0', sizeof(struct sys_grent
));
55 if ((gent
->gr_name
= strdup(grp
->gr_name
)) == NULL
)
59 if ((gent
->gr_passwd
= strdup(grp
->gr_passwd
)) == NULL
)
62 gent
->gr_gid
= grp
->gr_gid
;
64 /* number of strings in gr_mem */
65 for (num
= 0; grp
->gr_mem
[num
]; num
++)
68 /* alloc space for gr_mem string pointers */
69 if ((gent
->gr_mem
= malloc_array_p(char *, num
+1)) == NULL
)
72 memset(gent
->gr_mem
, '\0', (num
+1) * sizeof(char *));
74 for (i
=0; i
< num
; i
++) {
75 if ((gent
->gr_mem
[i
] = strdup(grp
->gr_mem
[i
])) == NULL
)
78 gent
->gr_mem
[num
] = NULL
;
82 gent
->next
= malloc_p(struct sys_grent
);
83 if (gent
->next
== NULL
)
86 memset(gent
, '\0', sizeof(struct sys_grent
));
96 DEBUG(0, ("Out of memory in getgrent_list!\n"));
101 /****************************************************************
102 Free the single linked list of group entries made by
104 ****************************************************************/
106 void grent_free (struct sys_grent
*glist
)
109 struct sys_grent
*prev
;
111 SAFE_FREE(glist
->gr_name
);
112 SAFE_FREE(glist
->gr_passwd
);
115 for (i
= 0; glist
->gr_mem
[i
]; i
++)
116 SAFE_FREE(glist
->gr_mem
[i
]);
117 SAFE_FREE(glist
->gr_mem
);
125 /****************************************************************
126 Returns a single linked list of passwd entries.
127 Use pwent_free() to free it after use.
128 ****************************************************************/
130 struct sys_pwent
* getpwent_list(void)
132 struct sys_pwent
*plist
;
133 struct sys_pwent
*pent
;
136 pent
= malloc_p(struct sys_pwent
);
138 DEBUG (0, ("Out of memory in getpwent_list!\n"));
145 while (pwd
!= NULL
) {
146 memset(pent
, '\0', sizeof(struct sys_pwent
));
148 if ((pent
->pw_name
= strdup(pwd
->pw_name
)) == NULL
)
151 if (pwd
->pw_passwd
) {
152 if ((pent
->pw_passwd
= strdup(pwd
->pw_passwd
)) == NULL
)
155 pent
->pw_uid
= pwd
->pw_uid
;
156 pent
->pw_gid
= pwd
->pw_gid
;
158 if ((pent
->pw_name
= strdup(pwd
->pw_gecos
)) == NULL
)
162 if ((pent
->pw_name
= strdup(pwd
->pw_dir
)) == NULL
)
166 if ((pent
->pw_name
= strdup(pwd
->pw_shell
)) == NULL
)
172 pent
->next
= malloc_p(struct sys_pwent
);
173 if (pent
->next
== NULL
)
185 DEBUG(0, ("Out of memory in getpwent_list!\n"));
190 /****************************************************************
191 Free the single linked list of passwd entries made by
193 ****************************************************************/
195 void pwent_free (struct sys_pwent
*plist
)
198 struct sys_pwent
*prev
;
200 SAFE_FREE(plist
->pw_name
);
201 SAFE_FREE(plist
->pw_passwd
);
202 SAFE_FREE(plist
->pw_gecos
);
203 SAFE_FREE(plist
->pw_dir
);
204 SAFE_FREE(plist
->pw_shell
);
212 /****************************************************************
213 Add the individual group users onto the list.
214 ****************************************************************/
216 static struct sys_userlist
*add_members_to_userlist(struct sys_userlist
*list_head
, const struct group
*grp
)
220 /* Count the number of users. */
221 for (num_users
= 0; grp
->gr_mem
[num_users
]; num_users
++)
224 for (i
= 0; i
< num_users
; i
++) {
225 struct sys_userlist
*entry
= malloc_p(struct sys_userlist
);
227 free_userlist(list_head
);
230 entry
->unix_name
= (char *)strdup(grp
->gr_mem
[i
]);
231 if (entry
->unix_name
== NULL
) {
233 free_userlist(list_head
);
236 DLIST_ADD(list_head
, entry
);
241 /****************************************************************
242 Get the list of UNIX users in a group.
243 We have to enumerate the /etc/group file as some UNIX getgrnam()
244 calls won't do that for us (notably Tru64 UNIX).
245 ****************************************************************/
247 struct sys_userlist
*get_users_in_group(const char *gname
)
249 struct sys_userlist
*list_head
= NULL
;
252 #if !defined(BROKEN_GETGRNAM)
253 if ((gptr
= (struct group
*)getgrnam(gname
)) == NULL
)
255 return add_members_to_userlist(list_head
, gptr
);
257 /* BROKEN_GETGRNAM - True64 */
259 while((gptr
= getgrent()) != NULL
) {
260 if (strequal(gname
, gptr
->gr_name
)) {
261 list_head
= add_members_to_userlist(list_head
, gptr
);
262 if (list_head
== NULL
)
271 /****************************************************************
272 Free list allocated above.
273 ****************************************************************/
275 void free_userlist(struct sys_userlist
*list_head
)
278 struct sys_userlist
*old_head
= list_head
;
279 DLIST_REMOVE(list_head
, list_head
);
280 SAFE_FREE(old_head
->unix_name
);