1 /*-
2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $OpenBSD: defs.c,v 1.24 2002/05/16 01:13:39 brian Exp $
27 */
28
29
30 #include <sys/param.h>
31 #include <netdb.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <sys/socket.h>
35
36 #include <ctype.h>
37 #include <errno.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
43 #include <sys/module.h>
44 #endif
45 #include <termios.h>
46 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
47 #include <time.h>
48 #endif
49 #include <unistd.h>
50
51 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
52 #include "id.h"
53 #include "log.h"
54 #endif
55 #include "defs.h"
56
57 #define issep(c) ((c) == '\t' || (c) == ' ')
58
59 ssize_t
fullread(int fd,void * v,size_t n)60 fullread(int fd, void *v, size_t n)
61 {
62 size_t got, total;
63
64 for (total = 0; total < n; total += got)
65 switch ((got = read(fd, (char *)v + total, n - total))) {
66 case 0:
67 return total;
68 case -1:
69 if (errno == EINTR)
70 got = 0;
71 else
72 return -1;
73 }
74 return total;
75 }
76
77 static struct {
78 int mode;
79 const char *name;
80 } modes[] = {
81 { PHYS_INTERACTIVE, "interactive" },
82 { PHYS_AUTO, "auto" },
83 { PHYS_DIRECT, "direct" },
84 { PHYS_DEDICATED, "dedicated" },
85 { PHYS_DDIAL, "ddial" },
86 { PHYS_BACKGROUND, "background" },
87 { PHYS_FOREGROUND, "foreground" },
88 { PHYS_ALL, "*" },
89 { 0, 0 }
90 };
91
92 const char *
mode2Nam(int mode)93 mode2Nam(int mode)
94 {
95 int m;
96
97 for (m = 0; modes[m].mode; m++)
98 if (modes[m].mode == mode)
99 return modes[m].name;
100
101 return "unknown";
102 }
103
104 int
Nam2mode(const char * name)105 Nam2mode(const char *name)
106 {
107 int m, got, len;
108
109 len = strlen(name);
110 got = -1;
111 for (m = 0; modes[m].mode; m++)
112 if (!strncasecmp(name, modes[m].name, len)) {
113 if (modes[m].name[len] == '\0')
114 return modes[m].mode;
115 if (got != -1)
116 return 0;
117 got = m;
118 }
119
120 return got == -1 ? 0 : modes[got].mode;
121 }
122
123 struct in_addr
GetIpAddr(const char * cp)124 GetIpAddr(const char *cp)
125 {
126 struct in_addr ipaddr;
127
128 if (!strcasecmp(cp, "default"))
129 ipaddr.s_addr = INADDR_ANY;
130 else if (inet_aton(cp, &ipaddr) == 0) {
131 const char *ptr;
132
133 /* Any illegal characters ? */
134 for (ptr = cp; *ptr != '\0'; ptr++)
135 if (!isalnum(*ptr) && strchr("-.", *ptr) == NULL)
136 break;
137
138 if (*ptr == '\0') {
139 struct hostent *hp;
140
141 hp = gethostbyname(cp);
142 if (hp && hp->h_addrtype == AF_INET)
143 memcpy(&ipaddr, hp->h_addr, hp->h_length);
144 else
145 ipaddr.s_addr = INADDR_NONE;
146 } else
147 ipaddr.s_addr = INADDR_NONE;
148 }
149
150 return ipaddr;
151 }
152
153 static const struct speeds {
154 int nspeed;
155 speed_t speed;
156 } speeds[] = {
157 #ifdef B50
158 { 50, B50, },
159 #endif
160 #ifdef B75
161 { 75, B75, },
162 #endif
163 #ifdef B110
164 { 110, B110, },
165 #endif
166 #ifdef B134
167 { 134, B134, },
168 #endif
169 #ifdef B150
170 { 150, B150, },
171 #endif
172 #ifdef B200
173 { 200, B200, },
174 #endif
175 #ifdef B300
176 { 300, B300, },
177 #endif
178 #ifdef B600
179 { 600, B600, },
180 #endif
181 #ifdef B1200
182 { 1200, B1200, },
183 #endif
184 #ifdef B1800
185 { 1800, B1800, },
186 #endif
187 #ifdef B2400
188 { 2400, B2400, },
189 #endif
190 #ifdef B4800
191 { 4800, B4800, },
192 #endif
193 #ifdef B9600
194 { 9600, B9600, },
195 #endif
196 #ifdef B19200
197 { 19200, B19200, },
198 #endif
199 #ifdef B38400
200 { 38400, B38400, },
201 #endif
202 #ifndef _POSIX_SOURCE
203 #ifdef B7200
204 { 7200, B7200, },
205 #endif
206 #ifdef B14400
207 { 14400, B14400, },
208 #endif
209 #ifdef B28800
210 { 28800, B28800, },
211 #endif
212 #ifdef B57600
213 { 57600, B57600, },
214 #endif
215 #ifdef B76800
216 { 76800, B76800, },
217 #endif
218 #ifdef B115200
219 { 115200, B115200, },
220 #endif
221 #ifdef B230400
222 { 230400, B230400, },
223 #endif
224 #ifdef B460800
225 { 460800, B460800, },
226 #endif
227 #ifdef B921600
228 { 921600, B921600, },
229 #endif
230 #ifdef EXTA
231 { 19200, EXTA, },
232 #endif
233 #ifdef EXTB
234 { 38400, EXTB, },
235 #endif
236 #endif /* _POSIX_SOURCE */
237 { 0, 0 }
238 };
239
240 int
SpeedToInt(speed_t speed)241 SpeedToInt(speed_t speed)
242 {
243 const struct speeds *sp;
244
245 for (sp = speeds; sp->nspeed; sp++) {
246 if (sp->speed == speed) {
247 return sp->nspeed;
248 }
249 }
250 return 0;
251 }
252
253 speed_t
IntToSpeed(int nspeed)254 IntToSpeed(int nspeed)
255 {
256 const struct speeds *sp;
257
258 for (sp = speeds; sp->nspeed; sp++) {
259 if (sp->nspeed == nspeed) {
260 return sp->speed;
261 }
262 }
263 return B0;
264 }
265
266 char *
findblank(char * p,int flags)267 findblank(char *p, int flags)
268 {
269 int instring;
270
271 instring = 0;
272 while (*p) {
273 if (*p == '\\') {
274 if (flags & PARSE_REDUCE) {
275 memmove(p, p + 1, strlen(p));
276 if (!*p)
277 break;
278 } else
279 p++;
280 } else if (*p == '"') {
281 memmove(p, p + 1, strlen(p));
282 instring = !instring;
283 continue;
284 } else if (!instring && (issep(*p) ||
285 (*p == '#' && !(flags & PARSE_NOHASH))))
286 return p;
287 p++;
288 }
289
290 return instring ? NULL : p;
291 }
292
293 int
MakeArgs(char * script,char ** pvect,int maxargs,int flags)294 MakeArgs(char *script, char **pvect, int maxargs, int flags)
295 {
296 int nargs;
297
298 nargs = 0;
299 while (*script) {
300 script += strspn(script, " \t");
301 if (*script == '#' && !(flags & PARSE_NOHASH)) {
302 *script = '\0';
303 break;
304 }
305 if (*script) {
306 if (nargs >= maxargs - 1)
307 break;
308 *pvect++ = script;
309 nargs++;
310 script = findblank(script, flags);
311 if (script == NULL)
312 return -1;
313 else if (!(flags & PARSE_NOHASH) && *script == '#')
314 *script = '\0';
315 else if (*script)
316 *script++ = '\0';
317 }
318 }
319 *pvect = NULL;
320 return nargs;
321 }
322
323 const char *
NumStr(long val,char * buf,size_t sz)324 NumStr(long val, char *buf, size_t sz)
325 {
326 static char result[23]; /* handles 64 bit numbers */
327
328 if (buf == NULL || sz == 0) {
329 buf = result;
330 sz = sizeof result;
331 }
332 snprintf(buf, sz, "<%ld>", val);
333 return buf;
334 }
335
336 const char *
HexStr(long val,char * buf,size_t sz)337 HexStr(long val, char *buf, size_t sz)
338 {
339 static char result[21]; /* handles 64 bit numbers */
340
341 if (buf == NULL || sz == 0) {
342 buf = result;
343 sz = sizeof result;
344 }
345 snprintf(buf, sz, "<0x%lx>", val);
346 return buf;
347 }
348
349 const char *
ex_desc(int ex)350 ex_desc(int ex)
351 {
352 static char num[12]; /* Used immediately if returned */
353 static const char * const desc[] = {
354 "normal", "start", "sock", "modem", "dial", "dead", "done",
355 "reboot", "errdead", "hangup", "term", "nodial", "nologin",
356 "redial", "reconnect"
357 };
358
359 if (ex >= 0 && ex < sizeof desc / sizeof *desc)
360 return desc[ex];
361 snprintf(num, sizeof num, "%d", ex);
362 return num;
363 }
364
365 void
SetTitle(const char * title)366 SetTitle(const char *title)
367 {
368 if (title == NULL)
369 setproctitle(NULL);
370 else if (title[0] == '-' && title[1] != '\0')
371 setproctitle("-%s", title + 1);
372 else
373 setproctitle("%s", title);
374 }
375
376 fd_set *
mkfdset()377 mkfdset()
378 {
379 return (fd_set *)malloc(howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
380 }
381
382 void
zerofdset(fd_set * s)383 zerofdset(fd_set *s)
384 {
385 memset(s, '\0', howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
386 }
387
388 void
Concatinate(char * buf,size_t sz,int argc,const char * const * argv)389 Concatinate(char *buf, size_t sz, int argc, const char *const *argv)
390 {
391 int i, n, pos;
392
393 *buf = '\0';
394 for (pos = i = 0; i < argc; i++) {
395 n = snprintf(buf + pos, sz - pos, "%s%s", i ? " " : "", argv[i]);
396 if (n < 0) {
397 buf[pos] = '\0';
398 break;
399 }
400 if ((pos += n) >= sz)
401 break;
402 }
403 }
404
405 int
loadmodules(int how,const char * module,...)406 loadmodules(int how, const char *module, ...)
407 {
408 int loaded = 0;
409 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
410 va_list ap;
411
412 va_start(ap, module);
413 while (module != NULL) {
414 if (modfind(module) == -1) {
415 if (ID0kldload(module) == -1) {
416 if (how == LOAD_VERBOSLY)
417 log_Printf(LogWARN, "%s: Cannot load module\n", module);
418 } else
419 loaded++;
420 }
421 module = va_arg(ap, const char *);
422 }
423 va_end(ap);
424 #endif
425 return loaded;
426 }
427