]> granicus.if.org Git - shadow/blob - lib/sgroupio.c
8437970cad4551b4d8b884bf4d3196c08992c8bd
[shadow] / 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$"
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                 return NULL;
58         }
59         sg->sg_passwd = strdup (sgent->sg_passwd);
60         if (NULL == sg->sg_passwd) {
61                 return NULL;
62         }
63
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) {
67                 return NULL;
68         }
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]) {
72                         return NULL;
73                 }
74         }
75         sg->sg_adm[i] = NULL;
76
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) {
80                 return NULL;
81         }
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]) {
85                         return NULL;
86                 }
87         }
88         sg->sg_mem[i] = NULL;
89
90         return sg;
91 }
92
93 static /*@null@*/ /*@only@*/void *gshadow_dup (const void *ent)
94 {
95         const struct sgrp *sg = ent;
96
97         return __sgr_dup (sg);
98 }
99
100 static void gshadow_free (/*@out@*/ /*@only@*/void *ent)
101 {
102         struct sgrp *sg = ent;
103
104         sgr_free (sg);
105 }
106
107 void sgr_free (/*@out@*/ /*@only@*/struct sgrp *sgent)
108 {
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));
114                 sgent->sg_adm++;
115         }
116         while (NULL != *(sgent->sg_mem)) {
117                 free (*(sgent->sg_mem));
118                 sgent->sg_mem++;
119         }
120         free (sgent);
121 }
122
123 static const char *gshadow_getname (const void *ent)
124 {
125         const struct sgrp *gr = ent;
126
127         return gr->sg_name;
128 }
129
130 static void *gshadow_parse (const char *line)
131 {
132         return (void *) sgetsgent (line);
133 }
134
135 static int gshadow_put (const void *ent, FILE * file)
136 {
137         const struct sgrp *sg = ent;
138
139         return (putsgent (sg, file) == -1) ? -1 : 0;
140 }
141
142 static struct commonio_ops gshadow_ops = {
143         gshadow_dup,
144         gshadow_free,
145         gshadow_getname,
146         gshadow_parse,
147         gshadow_put,
148         fgetsx,
149         fputsx,
150         NULL,                   /* open_hook */
151         NULL                    /* close_hook */
152 };
153
154 static struct commonio_db gshadow_db = {
155         SGROUP_FILE,            /* filename */
156         &gshadow_ops,           /* ops */
157         NULL,                   /* fp */
158 #ifdef WITH_SELINUX
159         NULL,                   /* scontext */
160 #endif
161         NULL,                   /* head */
162         NULL,                   /* tail */
163         NULL,                   /* cursor */
164         false,                  /* changed */
165         false,                  /* isopen */
166         false,                  /* locked */
167         false                   /* readonly */
168 };
169
170 int sgr_setdbname (const char *filename)
171 {
172         return commonio_setname (&gshadow_db, filename);
173 }
174
175 /*@observer@*/const char *sgr_dbname (void)
176 {
177         return gshadow_db.filename;
178 }
179
180 bool sgr_file_present (void)
181 {
182         return commonio_present (&gshadow_db);
183 }
184
185 int sgr_lock (void)
186 {
187         return commonio_lock (&gshadow_db);
188 }
189
190 int sgr_open (int mode)
191 {
192         return commonio_open (&gshadow_db, mode);
193 }
194
195 const struct sgrp *sgr_locate (const char *name)
196 {
197         return commonio_locate (&gshadow_db, name);
198 }
199
200 int sgr_update (const struct sgrp *sg)
201 {
202         return commonio_update (&gshadow_db, (const void *) sg);
203 }
204
205 int sgr_remove (const char *name)
206 {
207         return commonio_remove (&gshadow_db, name);
208 }
209
210 int sgr_rewind (void)
211 {
212         return commonio_rewind (&gshadow_db);
213 }
214
215 /*@null@*/const struct sgrp *sgr_next (void)
216 {
217         return commonio_next (&gshadow_db);
218 }
219
220 int sgr_close (void)
221 {
222         return commonio_close (&gshadow_db);
223 }
224
225 int sgr_unlock (void)
226 {
227         return commonio_unlock (&gshadow_db);
228 }
229
230 void __sgr_set_changed (void)
231 {
232         gshadow_db.changed = true;
233 }
234
235 /*@null@*/struct commonio_entry *__sgr_get_head (void)
236 {
237         return gshadow_db.head;
238 }
239
240 void __sgr_del_entry (const struct commonio_entry *ent)
241 {
242         commonio_del_entry (&gshadow_db, ent);
243 }
244
245 /* Sort with respect to group ordering. */
246 int sgr_sort ()
247 {
248         return commonio_sort_wrt (&gshadow_db, __gr_get_db ());
249 }
250 #else
251 extern int errno;               /* warning: ANSI C forbids an empty source file */
252 #endif