Initial commit for Tizen
[profile/extras/shadow-utils.git] / lib / sgroupio.c
1 /*
2  * Copyright (c) 1990 - 1994, Julianne Frances Haugh
3  * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4  * Copyright (c) 2001       , Michał Moskal
5  * Copyright (c) 2005       , Tomasz Kłoczko
6  * Copyright (c) 2007 - 2008, Nicolas François
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the copyright holders or contributors may not be used to
18  *    endorse or promote products derived from this software without
19  *    specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
25  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <config.h>
35
36 #ifdef SHADOWGRP
37
38 #ident "$Id: sgroupio.c 2797 2009-04-24 23:32:52Z nekral-guest $"
39
40 #include "prototypes.h"
41 #include "defines.h"
42 #include "commonio.h"
43 #include "sgroupio.h"
44
45 /*@null@*/ /*@only@*/struct sgrp *__sgr_dup (const struct sgrp *sgent)
46 {
47         struct sgrp *sg;
48         int i;
49
50         sg = (struct sgrp *) malloc (sizeof *sg);
51         if (NULL == sg) {
52                 return NULL;
53         }
54         *sg = *sgent;
55         sg->sg_name = strdup (sgent->sg_name);
56         if (NULL == sg->sg_name) {
57                 free (sg);
58                 return NULL;
59         }
60         sg->sg_passwd = strdup (sgent->sg_passwd);
61         if (NULL == sg->sg_passwd) {
62                 free (sg->sg_name);
63                 free (sg);
64                 return NULL;
65         }
66
67         for (i = 0; NULL != sgent->sg_adm[i]; i++);
68         sg->sg_adm = (char **) malloc ((i + 1) * sizeof (char *));
69         if (NULL == sg->sg_adm) {
70                 free (sg->sg_passwd);
71                 free (sg->sg_name);
72                 free (sg);
73                 return NULL;
74         }
75         for (i = 0; NULL != sgent->sg_adm[i]; i++) {
76                 sg->sg_adm[i] = strdup (sgent->sg_adm[i]);
77                 if (NULL == sg->sg_adm[i]) {
78                         for (i = 0; NULL != sg->sg_adm[i]; i++) {
79                                 free (sg->sg_adm[i]);
80                         }
81                         free (sg->sg_adm);
82                         free (sg->sg_passwd);
83                         free (sg->sg_name);
84                         free (sg);
85                         return NULL;
86                 }
87         }
88         sg->sg_adm[i] = NULL;
89
90         for (i = 0; NULL != sgent->sg_mem[i]; i++);
91         sg->sg_mem = (char **) malloc ((i + 1) * sizeof (char *));
92         if (NULL == sg->sg_mem) {
93                 for (i = 0; NULL != sg->sg_adm[i]; i++) {
94                         free (sg->sg_adm[i]);
95                 }
96                 free (sg->sg_adm);
97                 free (sg->sg_passwd);
98                 free (sg->sg_name);
99                 free (sg);
100                 return NULL;
101         }
102         for (i = 0; NULL != sgent->sg_mem[i]; i++) {
103                 sg->sg_mem[i] = strdup (sgent->sg_mem[i]);
104                 if (NULL == sg->sg_mem[i]) {
105                         for (i = 0; NULL != sg->sg_mem[i]; i++) {
106                                 free (sg->sg_mem[i]);
107                         }
108                         free (sg->sg_mem);
109                         for (i = 0; NULL != sg->sg_adm[i]; i++) {
110                                 free (sg->sg_adm[i]);
111                         }
112                         free (sg->sg_adm);
113                         free (sg->sg_passwd);
114                         free (sg->sg_name);
115                         free (sg);
116                         return NULL;
117                 }
118         }
119         sg->sg_mem[i] = NULL;
120
121         return sg;
122 }
123
124 static /*@null@*/ /*@only@*/void *gshadow_dup (const void *ent)
125 {
126         const struct sgrp *sg = ent;
127
128         return __sgr_dup (sg);
129 }
130
131 static void gshadow_free (/*@out@*/ /*@only@*/void *ent)
132 {
133         struct sgrp *sg = ent;
134
135         sgr_free (sg);
136 }
137
138 void sgr_free (/*@out@*/ /*@only@*/struct sgrp *sgent)
139 {
140         free (sgent->sg_name);
141         memzero (sgent->sg_passwd, strlen (sgent->sg_passwd));
142         free (sgent->sg_passwd);
143         while (NULL != *(sgent->sg_adm)) {
144                 free (*(sgent->sg_adm));
145                 sgent->sg_adm++;
146         }
147         while (NULL != *(sgent->sg_mem)) {
148                 free (*(sgent->sg_mem));
149                 sgent->sg_mem++;
150         }
151         free (sgent);
152 }
153
154 static const char *gshadow_getname (const void *ent)
155 {
156         const struct sgrp *gr = ent;
157
158         return gr->sg_name;
159 }
160
161 static void *gshadow_parse (const char *line)
162 {
163         return (void *) sgetsgent (line);
164 }
165
166 static int gshadow_put (const void *ent, FILE * file)
167 {
168         const struct sgrp *sg = ent;
169
170         return (putsgent (sg, file) == -1) ? -1 : 0;
171 }
172
173 static struct commonio_ops gshadow_ops = {
174         gshadow_dup,
175         gshadow_free,
176         gshadow_getname,
177         gshadow_parse,
178         gshadow_put,
179         fgetsx,
180         fputsx,
181         NULL,                   /* open_hook */
182         NULL                    /* close_hook */
183 };
184
185 static struct commonio_db gshadow_db = {
186         SGROUP_FILE,            /* filename */
187         &gshadow_ops,           /* ops */
188         NULL,                   /* fp */
189 #ifdef WITH_SELINUX
190         NULL,                   /* scontext */
191 #endif
192         NULL,                   /* head */
193         NULL,                   /* tail */
194         NULL,                   /* cursor */
195         false,                  /* changed */
196         false,                  /* isopen */
197         false,                  /* locked */
198         false                   /* readonly */
199 };
200
201 int sgr_setdbname (const char *filename)
202 {
203         return commonio_setname (&gshadow_db, filename);
204 }
205
206 /*@observer@*/const char *sgr_dbname (void)
207 {
208         return gshadow_db.filename;
209 }
210
211 bool sgr_file_present (void)
212 {
213         return commonio_present (&gshadow_db);
214 }
215
216 int sgr_lock (void)
217 {
218         return commonio_lock (&gshadow_db);
219 }
220
221 int sgr_open (int mode)
222 {
223         return commonio_open (&gshadow_db, mode);
224 }
225
226 /*@observer@*/ /*@null@*/const struct sgrp *sgr_locate (const char *name)
227 {
228         return commonio_locate (&gshadow_db, name);
229 }
230
231 int sgr_update (const struct sgrp *sg)
232 {
233         return commonio_update (&gshadow_db, (const void *) sg);
234 }
235
236 int sgr_remove (const char *name)
237 {
238         return commonio_remove (&gshadow_db, name);
239 }
240
241 int sgr_rewind (void)
242 {
243         return commonio_rewind (&gshadow_db);
244 }
245
246 /*@null@*/const struct sgrp *sgr_next (void)
247 {
248         return commonio_next (&gshadow_db);
249 }
250
251 int sgr_close (void)
252 {
253         return commonio_close (&gshadow_db);
254 }
255
256 int sgr_unlock (void)
257 {
258         return commonio_unlock (&gshadow_db);
259 }
260
261 void __sgr_set_changed (void)
262 {
263         gshadow_db.changed = true;
264 }
265
266 /*@dependent@*/ /*@null@*/struct commonio_entry *__sgr_get_head (void)
267 {
268         return gshadow_db.head;
269 }
270
271 void __sgr_del_entry (const struct commonio_entry *ent)
272 {
273         commonio_del_entry (&gshadow_db, ent);
274 }
275
276 /* Sort with respect to group ordering. */
277 int sgr_sort ()
278 {
279         return commonio_sort_wrt (&gshadow_db, __gr_get_db ());
280 }
281 #else
282 extern int errno;               /* warning: ANSI C forbids an empty source file */
283 #endif