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