Rename bzero -> memset.
[tprouty/samba.git] / source / lib / util_getent.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    Samba utility functions
5    Copyright (C) Simo Sorce 2001
6
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 /****************************************************************
25  Returns a single linked list of group entries.
26  Use grent_free() to free it after use.
27 ****************************************************************/
28
29 struct sys_grent * getgrent_list(void)
30 {
31         struct sys_grent *glist;
32         struct sys_grent *gent;
33         struct group *grp;
34         
35         gent = (struct sys_grent *) malloc(sizeof(struct sys_grent));
36         if (gent == NULL) {
37                 DEBUG (0, ("Out of memory in getgrent_list!\n"));
38                 return NULL;
39         }
40         glist = gent;
41         
42         setgrent();
43         grp = getgrent();
44         while (grp != NULL) {
45                 int i,num;
46                 
47                 memset(gent, '\0', sizeof(struct sys_grent));
48                 if (grp->gr_name) {
49                         if ((gent->gr_name = strdup(grp->gr_name)) == NULL)
50                                 goto err;
51                 }
52                 if (grp->gr_passwd) {
53                         if ((gent->gr_passwd = strdup(grp->gr_passwd)) == NULL)
54                                 goto err;
55                 }
56                 gent->gr_gid = grp->gr_gid;
57                 
58                 /* number of strings in gr_mem */
59                 for (num = 0; grp->gr_mem[num]; num++)
60                         ;
61                 
62                 /* alloc space for gr_mem string pointers */
63                 if ((gent->gr_mem = (char **) malloc(num+1 * sizeof(char *))) == NULL)
64                         goto err;
65
66                 for (i=0; i < num; i++) {
67                         if ((gent->gr_mem[i] = strdup(grp->gr_mem[i])) == NULL)
68                                 goto err;
69                 }
70                 gent->gr_mem[num] = NULL;
71                 
72                 grp = getgrent();
73                 if (grp) {
74                         gent->next = (struct sys_grent *) malloc(sizeof(struct sys_grent));
75                         if (gent->next == NULL)
76                                 goto err;
77                         gent = gent->next;
78                 }
79         }
80         
81         endgrent();
82         return glist;
83
84   err:
85
86         endgrent();
87         DEBUG(0, ("Out of memory in getgrent_list!\n"));
88         grent_free(glist);
89         return NULL;
90 }
91
92 /****************************************************************
93  Free the single linked list of group entries made by
94  getgrent_list()
95 ****************************************************************/
96
97 void grent_free (struct sys_grent *glist)
98 {
99         while (glist) {
100                 char **ary;
101                 struct sys_grent *temp;
102                 
103                 if (glist->gr_name)
104                         free(glist->gr_name);
105                 if (glist->gr_passwd)
106                         free(glist->gr_passwd);
107                 if (glist->gr_mem) {
108                         ary = glist->gr_mem;
109                         while (*ary) {
110                                 free(*ary);
111                                 ary++;
112                         }
113                         free(glist->gr_mem);
114                 }
115                 temp = glist->next;
116                 free(glist);
117                 glist = temp;
118         }
119 }
120
121 /****************************************************************
122  Returns a single linked list of passwd entries.
123  Use pwent_free() to free it after use.
124 ****************************************************************/
125
126 struct sys_pwent * getpwent_list(void)
127 {
128         struct sys_pwent *plist;
129         struct sys_pwent *pent;
130         struct passwd *pwd;
131         
132         pent = (struct sys_pwent *) malloc(sizeof(struct sys_pwent));
133         if (pent == NULL) {
134                 DEBUG (0, ("Out of memory in getpwent_list!\n"));
135                 return NULL;
136         }
137         plist = pent;
138         
139         setpwent();
140         pwd = getpwent();
141         while (pwd != NULL) {
142                 memset(pent, '\0', sizeof(struct sys_pwent));
143                 if (pwd->pw_name) {
144                         if ((pent->pw_name = strdup(pwd->pw_name)) == NULL)
145                                 goto err;
146                 }
147                 if (pwd->pw_passwd) {
148                         if ((pent->pw_passwd = strdup(pwd->pw_passwd)) == NULL)
149                                 goto err;
150                 }
151                 pent->pw_uid = pwd->pw_uid;
152                 pent->pw_gid = pwd->pw_gid;
153                 if (pwd->pw_gecos) {
154                         if ((pent->pw_name = strdup(pwd->pw_gecos)) == NULL)
155                                 goto err;
156                 }
157                 if (pwd->pw_dir) {
158                         if ((pent->pw_name = strdup(pwd->pw_dir)) == NULL)
159                                 goto err;
160                 }
161                 if (pwd->pw_shell) {
162                         if ((pent->pw_name = strdup(pwd->pw_shell)) == NULL)
163                                 goto err;
164                 }
165
166                 pwd = getpwent();
167                 if (pwd) {
168                         pent->next = (struct sys_pwent *) malloc(sizeof(struct sys_pwent));
169                         if (pent->next == NULL)
170                                 goto err;
171                         pent = pent->next;
172                 }
173         }
174         
175         endpwent();
176         return plist;
177
178   err:
179
180         endpwent();
181         DEBUG(0, ("Out of memory in getpwent_list!\n"));
182         pwent_free(plist);
183         return NULL;
184 }
185
186 /****************************************************************
187  Free the single linked list of passwd entries made by
188  getpwent_list()
189 ****************************************************************/
190
191 void pwent_free (struct sys_pwent *plist)
192 {
193         while (plist) {
194                 struct sys_pwent *temp;
195                 
196                 if (plist->pw_name)
197                         free(plist->pw_name);
198                 if (plist->pw_passwd)
199                         free(plist->pw_passwd);
200                 if (plist->pw_gecos)
201                         free(plist->pw_gecos);
202                 if (plist->pw_dir)
203                         free(plist->pw_dir);
204                 if (plist->pw_shell)
205                         free(plist->pw_shell);
206
207                 temp = plist->next;
208                 free(plist);
209                 plist = temp;
210         }
211 }