1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/* $OpenBSD: stringlist.c,v 1.2 2015/10/01 13:04:42 jsg Exp $ */
/*
* Copyright (c) 1994 Christos Zoulas
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Christos Zoulas.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <stdio.h>
#include <netgroup.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "stringlist.h"
static const char _ngstar[] = "*";
static int getstring(char **, int, char **);
static struct netgroup *getnetgroup(char **);
/*
* _ng_sl_init(): Initialize a string list
*/
struct stringlist *
_ng_sl_init(void)
{
struct stringlist *sl = malloc(sizeof(struct stringlist));
if (sl == NULL)
return NULL;
sl->sl_cur = 0;
sl->sl_max = 20;
sl->sl_str = calloc(sl->sl_max, sizeof(char *));
if (sl->sl_str == NULL) {
free(sl);
return NULL;
}
return sl;
}
/*
* _ng_sl_add(): Add an item to the string list
*/
int
_ng_sl_add(struct stringlist *sl, char *name)
{
if (sl->sl_cur == sl->sl_max - 1) {
char **slstr;
sl->sl_max += 20;
slstr = reallocarray(sl->sl_str, sl->sl_max, sizeof(char *));
if (slstr == NULL) {
free(sl->sl_str);
sl->sl_str = NULL;
return -1;
}
sl->sl_str = slstr;
}
sl->sl_str[sl->sl_cur++] = name;
return 0;
}
/*
* _ng_sl_free(): Free a stringlist
*/
void
_ng_sl_free(struct stringlist *sl, int all)
{
size_t i;
if (all)
for (i = 0; i < sl->sl_cur; i++)
free(sl->sl_str[i]);
free(sl->sl_str);
free(sl);
}
/*
* sl_find(): Find a name in the string list
*/
char *
_ng_sl_find(struct stringlist *sl, char *name)
{
size_t i;
for (i = 0; i < sl->sl_cur; i++)
if (strcmp(sl->sl_str[i], name) == 0)
return sl->sl_str[i];
return NULL;
}
/*
* _ng_parse(): Parse a line and return: _NG_ERROR: Syntax Error _NG_NONE:
* line was empty or a comment _NG_GROUP: line had a netgroup definition,
* returned in ng _NG_NAME: line had a netgroup name, returned in name
*
* Public since used by netgroup_mkdb
*/
int
_ng_parse(char **p, char **name, struct netgroup **ng)
{
while (**p) {
if (**p == '#')
/* comment */
return _NG_NONE;
while (**p && _NG_ISSPACE(**p))
/* skipblank */
(*p)++;
if (**p == '(') {
if ((*ng = getnetgroup(p)) == NULL)
return _NG_ERROR;
return _NG_GROUP;
} else {
char *np;
int i;
for (np = *p; **p && !_NG_ISSPACE(**p); (*p)++)
continue;
if (np != *p) {
i = (*p - np) + 1;
*name = malloc(i);
if (*name == NULL)
return _NG_ERROR;
memcpy(*name, np, i);
(*name)[i - 1] = '\0';
return _NG_NAME;
}
}
}
return _NG_NONE;
}
/*
* _ng_makekey(): Make a key from the two names given. The key is of the form
* <name1>.<name2> Names strings are replaced with * if they are empty;
*/
char *
_ng_makekey(const char *s1, const char *s2, size_t len)
{
char *buf = malloc(len);
int ret;
if (buf == NULL)
return NULL;
ret = snprintf(buf, len, "%s.%s", _NG_STAR(s1), _NG_STAR(s2));
if (ret < 0 || ret >= len) {
free(buf);
return NULL;
}
return buf;
}
void
_ng_print(char *buf, size_t len, const struct netgroup *ng)
{
(void) snprintf(buf, len, "(%s,%s,%s)", _NG_EMPTY(ng->ng_host),
_NG_EMPTY(ng->ng_user), _NG_EMPTY(ng->ng_domain));
}
/*
* getnetgroup(): Parse a netgroup, and advance the pointer
*/
static struct netgroup *
getnetgroup(char **pp)
{
struct netgroup *ng = malloc(sizeof(struct netgroup));
if (ng == NULL)
return NULL;
(*pp)++; /* skip '(' */
if (!getstring(pp, ',', &ng->ng_host))
goto badhost;
if (!getstring(pp, ',', &ng->ng_user))
goto baduser;
if (!getstring(pp, ')', &ng->ng_domain))
goto baddomain;
#ifdef DEBUG_NG
{
char buf[1024];
_ng_print(buf, sizeof(buf), ng);
fprintf(stderr, "netgroup %s\n", buf);
}
#endif
return ng;
baddomain:
if (ng->ng_user)
free(ng->ng_user);
baduser:
if (ng->ng_host)
free(ng->ng_host);
badhost:
free(ng);
return NULL;
}
/*
* getstring(): Get a string delimited by the character, skipping leading and
* trailing blanks and advancing the pointer
*/
static int
getstring(char **pp, int del, char **str)
{
char *sp, *ep, *dp;
/* skip leading blanks */
for (sp = *pp; *sp && _NG_ISSPACE(*sp); sp++)
continue;
/* accumulate till delimiter or space */
for (ep = sp; *ep && *ep != del && !_NG_ISSPACE(*ep); ep++)
continue;
/* hunt for the delimiter */
for (dp = ep; *dp && *dp != del && _NG_ISSPACE(*dp); dp++)
continue;
if (*dp != del) {
*str = NULL;
return 0;
}
*pp = ++dp;
del = (ep - sp) + 1;
if (del > 1) {
dp = malloc(del);
if (dp == NULL)
return 0;
memcpy(dp, sp, del);
dp[del - 1] = '\0';
} else
dp = NULL;
*str = dp;
return 1;
}
|