File: confutil.c

package info (click to toggle)
ndtpd 3.1.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,524 kB
  • ctags: 690
  • sloc: ansic: 8,565; sh: 7,275; perl: 655; makefile: 175
file content (349 lines) | stat: -rw-r--r-- 7,460 bytes parent folder | download
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * Copyright (c) 1997, 98, 2000, 01  
 *    Motoyuki Kasahara
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

/*
 * This program requires the following Autoconf macros:
 *   AC_C_CONST
 *   AC_TYPE_SIZE_T
 *   AC_TYPE_UID_T
 *   AC_TYPE_GID_T
 *   AC_HEADER_STDC
 *   AC_CHECK_HEADERS(string.h, memory.h, stdlib.h, unistd.h)
 *   AC_CHECK_FUNCS(strcasecmp, strtol)
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pwd.h>
#include <grp.h>
#include <syslog.h>

#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
#include <string.h>
#if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
#include <memory.h>
#endif /* not STDC_HEADERS and HAVE_MEMORY_H */
#else /* not STDC_HEADERS and not HAVE_STRING_H */
#include <strings.h>
#endif /* not STDC_HEADERS and not HAVE_STRING_H */

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifndef HAVE_STRCASECMP
#ifdef __STDC__
int strcasecmp(const char *, const char *);
int strncasecmp(const char *, const char *, size_t);
#else /* not __STDC__ */
int strcasecmp()
int strncasecmp();
#endif /* not __STDC__ */
#endif /* not HAVE_STRCASECMP */

#ifndef HAVE_STRTOL
#ifdef __STDC__
long strtol(const char *, char **, int);
#else /* not __STDC__ */
long strtol();
#endif /* not __STDC__ */
#endif /* not HAVE_STRTOL */


/*
 * Convert a string to an integer.
 *
 * The argument `string' must consists of an optional sign ('-' or '+')
 * and at least one digit ('0'..'9').
 *
 * If `string' doesn't match this format, -1 is returned.
 * Otherwise 0 is returned, and the corresponding integer value is
 * put into `number'.
 */
int
parse_integer(string, number)
    const char *string;
    int *number;
{
    const char *p = string;

    if (*p == '-' || *p == '+')
	p++;
    if (*p == '\0')
	return -1;
    while ('0' <= *p && *p <= '9')
	p++;
    if (*p != '\0')
	return -1;

    *number = (int)strtol(string, NULL, 10);
    return 0;
}


/*
 * Convert a string to a port number. (TCP)
 *
 * The argument `string' must represents a service name on TCP (e.g. `smtp')
 * or a port number (e.g. `25').
 * 
 * If the string doesn't represent neither valid service name nor port
 * number, -1 is returned.  Otherwise, 0 is returned and the corresponding
 * port number is put into `port'.
 */
int
parse_tcp_port(string, port)
    const char *string;
    int *port;
{
    struct servent *serv;

    if (parse_integer(string, port) < 0) {
	serv = getservbyname(string, "tcp");
	if (serv == NULL)
	    return -1;
	*port = ntohs(serv->s_port);
    }
    return 0;
}


/*
 * Convert a string to a port number. (UDP)
 *
 * The argument `string' must represents a service name on UDP (e.g. `ntp')
 * or a port number (e.g. `123').
 * 
 * If the string doesn't represent neither valid service name nor port
 * number, -1 is returned.  Otherwise, 0 is returned and the corresponding
 * port number is put into `port'.
 */
int
parse_udp_port(string, port)
    const char *string;
    int *port;
{
    struct servent *entry;

    if (parse_integer(string, port) < 0) {
	entry = getservbyname(string, "udp");
	if (entry == NULL)
    return -1;
	*port = ntohs(entry->s_port);
    }
    return 0;
}


/*
 * Convert a string to an user ID.
 *
 * The argument `string' must represents an user name (e.g. `root') or
 * an user ID (e.g. `0').
 * 
 * If the string doesn't represent neither valid service name nor port
 * number, -1 is returned.  Otherwise, 0 is returned and the corresponding
 * user ID is put into `port'.
 */
int
parse_user(string, user)
    const char *string;
    uid_t *user;
{
    struct passwd *entry;
    int n;

    if (0 <= parse_integer(string, &n))
	*user = (uid_t) n;
    else {
	entry = getpwnam(string);
	if (entry == NULL)
	    return -1;
	*user = entry->pw_uid;
    }
    return 0;
}


/*
 * Convert a string to an group ID.
 *
 * The argument `string' must represents a group name (e.g. `sys') or an
 * group ID (e.g. `1').
 * 
 * If the string doesn't represent neither valid service name nor port
 * number, -1 is returned.  Otherwise, 0 is returned and the corresponding
 * group ID is put into `port'.
 */
int
parse_group(string, group)
    const char *string;
    gid_t *group;
{
    struct group *entry;
    int n;

    if (0 <= parse_integer(string, &n))
	*group = (gid_t) n;
    else {
	entry = getgrnam(string);
	if (entry == NULL)
	    return -1;
	*group = entry->gr_gid;
    }
    return 0;
}


/*
 * Convert a string (`ON' or `OFF') to an integer (0 or 1).
 *
 * The argument `string' must represents `ON' or `OFF'.  Cases in the
 * string are insensitive, so that `ON', `On' `oN', and `on' are accepted.
 *
 * If succeeded, the result is put into `result' and 0 is returned.
 * If `string' represents ON, `result' is set to 1.
 * If `string' represents OFF, `result' is set to 0.
 * If `string' represents neither ON nor OFF, -1 is returned.
 */
int
parse_on_off(string, result)
    const char *string;
    int *result;
{
    if (strcasecmp(string, "ON") == 0)
	*result = 1;
    else if (strcasecmp(string, "OFF") == 0)
	*result = 0;
    else
	return -1;

    return 0;
}


/*
 * Syslog facility and its name.
 */
typedef struct {
    int code;
    const char *name;
} Facility_Entry;


static const Facility_Entry facility_table[] = {
#ifdef LOG_AUTH
    {LOG_AUTH, "auth"},
#endif
#ifdef LOG_AUTHPRIV
    {LOG_AUTHPRIV, "authpriv"},
#endif
#ifdef LOG_CRON
    {LOG_CRON, "cron"},
#endif
#ifdef LOG_DAEMON
    {LOG_DAEMON, "daemon"},
#endif
#ifdef LOG_FTP
    {LOG_FTP, "ftp"},
#endif
#ifdef LOG_KERN
    {LOG_KERN, "kern"},
#endif
#ifdef LOG_LOCAL0
    {LOG_LOCAL0, "local0"},
#endif
#ifdef LOG_LOCAL1
    {LOG_LOCAL1, "local1"},
#endif
#ifdef LOG_LOCAL2
    {LOG_LOCAL2, "local2"},
#endif
#ifdef LOG_LOCAL3
    {LOG_LOCAL3, "local3"},
#endif
#ifdef LOG_LOCAL4
    {LOG_LOCAL4, "local4"},
#endif
#ifdef LOG_LOCAL5
    {LOG_LOCAL5, "local5"},
#endif
#ifdef LOG_LOCAL6
    {LOG_LOCAL6, "local6"},
#endif
#ifdef LOG_LOCAL7
    {LOG_LOCAL7, "local7"},
#endif
#ifdef LOG_LPR
    {LOG_LPR, "lpr"},
#endif
#ifdef LOG_MAIL
    {LOG_MAIL, "mail"},
#endif
#ifdef LOG_NEWS
    {LOG_NEWS, "news"},
#endif
#ifdef LOG_SYSLOG
    {LOG_SYSLOG, "syslog"},
#endif
#ifdef LOG_USER
    {LOG_USER, "user"},
#endif
#ifdef LOG_UUCP
    {LOG_UUCP, "uucp"},
#endif
    {-1, NULL}
};

/*
 * Convert a syslog facility name to a code.
 *
 * The argument `string' must represents a syslog facility name (e.g.
 * `auth').
 * 
 * If the string doesn't represent valid facility name, -1 is returned.
 * Otherwise, 0 is returned and the corresponding facility code (e.g.
 * LOG_AUTH) is put into `code'.
 */
int
parse_syslog_facility(name, code)
    const char *name;
    int *code;
{
#ifdef LOG_DAEMON
    const Facility_Entry *ent;

    for (ent = facility_table; ent->name != NULL; ent++) {
	if (strcasecmp(name, ent->name) == 0) {
	    *code = ent->code;
	    return 0;
	}
    }
#endif /* not LOG_DAEMON */

    return -1;
}