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.
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) {
59 sg->sg_passwd = strdup (sgent->sg_passwd);
60 if (NULL == sg->sg_passwd) {
64 for (i = 0; NULL != sgent->sg_adm[i]; i++);
65 sg->sg_adm = (char **) malloc ((i + 1) * sizeof (char *));
66 if (NULL == sg->sg_adm) {
69 for (i = 0; NULL != sgent->sg_adm[i]; i++) {
70 sg->sg_adm[i] = strdup (sgent->sg_adm[i]);
71 if (NULL == sg->sg_adm[i]) {
77 for (i = 0; NULL != sgent->sg_mem[i]; i++);
78 sg->sg_mem = (char **) malloc ((i + 1) * sizeof (char *));
79 if (NULL == sg->sg_mem) {
82 for (i = 0; NULL != sgent->sg_mem[i]; i++) {
83 sg->sg_mem[i] = strdup (sgent->sg_mem[i]);
84 if (NULL == sg->sg_mem[i]) {
93 static /*@null@*/ /*@only@*/void *gshadow_dup (const void *ent)
95 const struct sgrp *sg = ent;
97 return __sgr_dup (sg);
100 static void gshadow_free (/*@out@*/ /*@only@*/void *ent)
102 struct sgrp *sg = ent;
107 void sgr_free (/*@out@*/ /*@only@*/struct sgrp *sgent)
109 free (sgent->sg_name);
110 memzero (sgent->sg_passwd, strlen (sgent->sg_passwd));
111 free (sgent->sg_passwd);
112 while (NULL != *(sgent->sg_adm)) {
113 free (*(sgent->sg_adm));
116 while (NULL != *(sgent->sg_mem)) {
117 free (*(sgent->sg_mem));
123 static const char *gshadow_getname (const void *ent)
125 const struct sgrp *gr = ent;
130 static void *gshadow_parse (const char *line)
132 return (void *) sgetsgent (line);
135 static int gshadow_put (const void *ent, FILE * file)
137 const struct sgrp *sg = ent;
139 return (putsgent (sg, file) == -1) ? -1 : 0;
142 static struct commonio_ops gshadow_ops = {
150 NULL, /* open_hook */
151 NULL /* close_hook */
154 static struct commonio_db gshadow_db = {
155 SGROUP_FILE, /* filename */
156 &gshadow_ops, /* ops */
170 int sgr_setdbname (const char *filename)
172 return commonio_setname (&gshadow_db, filename);
175 /*@observer@*/const char *sgr_dbname (void)
177 return gshadow_db.filename;
180 bool sgr_file_present (void)
182 return commonio_present (&gshadow_db);
187 return commonio_lock (&gshadow_db);
190 int sgr_open (int mode)
192 return commonio_open (&gshadow_db, mode);
195 const struct sgrp *sgr_locate (const char *name)
197 return commonio_locate (&gshadow_db, name);
200 int sgr_update (const struct sgrp *sg)
202 return commonio_update (&gshadow_db, (const void *) sg);
205 int sgr_remove (const char *name)
207 return commonio_remove (&gshadow_db, name);
210 int sgr_rewind (void)
212 return commonio_rewind (&gshadow_db);
215 /*@null@*/const struct sgrp *sgr_next (void)
217 return commonio_next (&gshadow_db);
222 return commonio_close (&gshadow_db);
225 int sgr_unlock (void)
227 return commonio_unlock (&gshadow_db);
230 void __sgr_set_changed (void)
232 gshadow_db.changed = true;
235 /*@null@*/struct commonio_entry *__sgr_get_head (void)
237 return gshadow_db.head;
240 void __sgr_del_entry (const struct commonio_entry *ent)
242 commonio_del_entry (&gshadow_db, ent);
245 /* Sort with respect to group ordering. */
248 return commonio_sort_wrt (&gshadow_db, __gr_get_db ());
251 extern int errno; /* warning: ANSI C forbids an empty source file */