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
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
38 #ident "$Id: sgroupio.c 2797 2009-04-24 23:32:52Z nekral-guest $"
40 #include "prototypes.h"
45 /*@null@*/ /*@only@*/struct sgrp *__sgr_dup (const struct sgrp *sgent)
50 sg = (struct sgrp *) malloc (sizeof *sg);
55 sg->sg_name = strdup (sgent->sg_name);
56 if (NULL == sg->sg_name) {
60 sg->sg_passwd = strdup (sgent->sg_passwd);
61 if (NULL == sg->sg_passwd) {
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) {
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++) {
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++) {
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]);
109 for (i = 0; NULL != sg->sg_adm[i]; i++) {
110 free (sg->sg_adm[i]);
113 free (sg->sg_passwd);
119 sg->sg_mem[i] = NULL;
124 static /*@null@*/ /*@only@*/void *gshadow_dup (const void *ent)
126 const struct sgrp *sg = ent;
128 return __sgr_dup (sg);
131 static void gshadow_free (/*@out@*/ /*@only@*/void *ent)
133 struct sgrp *sg = ent;
138 void sgr_free (/*@out@*/ /*@only@*/struct sgrp *sgent)
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));
147 while (NULL != *(sgent->sg_mem)) {
148 free (*(sgent->sg_mem));
154 static const char *gshadow_getname (const void *ent)
156 const struct sgrp *gr = ent;
161 static void *gshadow_parse (const char *line)
163 return (void *) sgetsgent (line);
166 static int gshadow_put (const void *ent, FILE * file)
168 const struct sgrp *sg = ent;
170 return (putsgent (sg, file) == -1) ? -1 : 0;
173 static struct commonio_ops gshadow_ops = {
181 NULL, /* open_hook */
182 NULL /* close_hook */
185 static struct commonio_db gshadow_db = {
186 SGROUP_FILE, /* filename */
187 &gshadow_ops, /* ops */
201 int sgr_setdbname (const char *filename)
203 return commonio_setname (&gshadow_db, filename);
206 /*@observer@*/const char *sgr_dbname (void)
208 return gshadow_db.filename;
211 bool sgr_file_present (void)
213 return commonio_present (&gshadow_db);
218 return commonio_lock (&gshadow_db);
221 int sgr_open (int mode)
223 return commonio_open (&gshadow_db, mode);
226 /*@observer@*/ /*@null@*/const struct sgrp *sgr_locate (const char *name)
228 return commonio_locate (&gshadow_db, name);
231 int sgr_update (const struct sgrp *sg)
233 return commonio_update (&gshadow_db, (const void *) sg);
236 int sgr_remove (const char *name)
238 return commonio_remove (&gshadow_db, name);
241 int sgr_rewind (void)
243 return commonio_rewind (&gshadow_db);
246 /*@null@*/const struct sgrp *sgr_next (void)
248 return commonio_next (&gshadow_db);
253 return commonio_close (&gshadow_db);
256 int sgr_unlock (void)
258 return commonio_unlock (&gshadow_db);
261 void __sgr_set_changed (void)
263 gshadow_db.changed = true;
266 /*@dependent@*/ /*@null@*/struct commonio_entry *__sgr_get_head (void)
268 return gshadow_db.head;
271 void __sgr_del_entry (const struct commonio_entry *ent)
273 commonio_del_entry (&gshadow_db, ent);
276 /* Sort with respect to group ordering. */
279 return commonio_sort_wrt (&gshadow_db, __gr_get_db ());
282 extern int errno; /* warning: ANSI C forbids an empty source file */