]> andersk Git - openssh.git/blame - misc.c
- (tim) [configure.ac] Bug #1149. Disable /etc/default/login check for QNX.
[openssh.git] / misc.c
CommitLineData
bcbf86ec 1/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
a980cbd7 3 * Copyright (c) 2005 Damien Miller. All rights reserved.
bcbf86ec 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 ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
6d24277f 26#include "includes.h"
4116f5c0 27RCSID("$OpenBSD: misc.c,v 1.42 2006/01/31 10:19:02 djm Exp $");
1f1fbbd8 28
29#ifdef SSH_TUN_OPENBSD
30#include <net/if.h>
31#endif
6d24277f 32
1aa00dcb 33#include "misc.h"
42f11eb2 34#include "log.h"
4ab21f86 35#include "xmalloc.h"
6d24277f 36
255cabd9 37/* remove newline at end of string */
6d24277f 38char *
39chop(char *s)
40{
41 char *t = s;
42 while (*t) {
6aacefa7 43 if (*t == '\n' || *t == '\r') {
6d24277f 44 *t = '\0';
45 return s;
46 }
47 t++;
48 }
49 return s;
50
51}
52
255cabd9 53/* set/unset filedescriptor to non-blocking */
170694d7 54int
6d24277f 55set_nonblock(int fd)
56{
57 int val;
89aa792b 58
6d24277f 59 val = fcntl(fd, F_GETFL, 0);
60 if (val < 0) {
61 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
170694d7 62 return (-1);
6d24277f 63 }
a22aff1f 64 if (val & O_NONBLOCK) {
170694d7 65 debug3("fd %d is O_NONBLOCK", fd);
66 return (0);
a22aff1f 67 }
45c42d58 68 debug2("fd %d setting O_NONBLOCK", fd);
6d24277f 69 val |= O_NONBLOCK;
170694d7 70 if (fcntl(fd, F_SETFL, val) == -1) {
71 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd,
72 strerror(errno));
73 return (-1);
74 }
75 return (0);
6d24277f 76}
77
170694d7 78int
89aa792b 79unset_nonblock(int fd)
80{
81 int val;
82
83 val = fcntl(fd, F_GETFL, 0);
84 if (val < 0) {
85 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
170694d7 86 return (-1);
89aa792b 87 }
88 if (!(val & O_NONBLOCK)) {
170694d7 89 debug3("fd %d is not O_NONBLOCK", fd);
90 return (0);
89aa792b 91 }
e04e7a19 92 debug("fd %d clearing O_NONBLOCK", fd);
89aa792b 93 val &= ~O_NONBLOCK;
170694d7 94 if (fcntl(fd, F_SETFL, val) == -1) {
95 debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
c92ec40b 96 fd, strerror(errno));
170694d7 97 return (-1);
98 }
99 return (0);
89aa792b 100}
101
bcc0381e 102/* disable nagle on socket */
103void
104set_nodelay(int fd)
105{
3c05447a 106 int opt;
107 socklen_t optlen;
bcc0381e 108
811a6342 109 optlen = sizeof opt;
110 if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
f7fb35fe 111 debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
811a6342 112 return;
113 }
114 if (opt == 1) {
115 debug2("fd %d is TCP_NODELAY", fd);
116 return;
117 }
118 opt = 1;
6226a8f8 119 debug2("fd %d setting TCP_NODELAY", fd);
811a6342 120 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
bcc0381e 121 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
122}
123
6d24277f 124/* Characters considered whitespace in strsep calls. */
125#define WHITESPACE " \t\r\n"
126
255cabd9 127/* return next token in configuration line */
6d24277f 128char *
129strdelim(char **s)
130{
131 char *old;
132 int wspace = 0;
133
134 if (*s == NULL)
135 return NULL;
136
137 old = *s;
138
139 *s = strpbrk(*s, WHITESPACE "=");
140 if (*s == NULL)
141 return (old);
142
143 /* Allow only one '=' to be skipped */
144 if (*s[0] == '=')
145 wspace = 1;
146 *s[0] = '\0';
147
148 *s += strspn(*s + 1, WHITESPACE) + 1;
149 if (*s[0] == '=' && !wspace)
150 *s += strspn(*s + 1, WHITESPACE) + 1;
151
152 return (old);
153}
1aa00dcb 154
3b1a83df 155struct passwd *
156pwcopy(struct passwd *pw)
157{
158 struct passwd *copy = xmalloc(sizeof(*copy));
5649fbbe 159
3b1a83df 160 memset(copy, 0, sizeof(*copy));
161 copy->pw_name = xstrdup(pw->pw_name);
162 copy->pw_passwd = xstrdup(pw->pw_passwd);
5649fbbe 163 copy->pw_gecos = xstrdup(pw->pw_gecos);
3b1a83df 164 copy->pw_uid = pw->pw_uid;
165 copy->pw_gid = pw->pw_gid;
7751d4eb 166#ifdef HAVE_PW_EXPIRE_IN_PASSWD
c4d49b85 167 copy->pw_expire = pw->pw_expire;
7751d4eb 168#endif
169#ifdef HAVE_PW_CHANGE_IN_PASSWD
c4d49b85 170 copy->pw_change = pw->pw_change;
7751d4eb 171#endif
2605addd 172#ifdef HAVE_PW_CLASS_IN_PASSWD
3b1a83df 173 copy->pw_class = xstrdup(pw->pw_class);
2605addd 174#endif
3b1a83df 175 copy->pw_dir = xstrdup(pw->pw_dir);
176 copy->pw_shell = xstrdup(pw->pw_shell);
177 return copy;
178}
179
255cabd9 180/*
181 * Convert ASCII string to TCP/IP port number.
182 * Port must be >0 and <=65535.
183 * Return 0 if invalid.
184 */
185int
186a2port(const char *s)
2d2a2c65 187{
188 long port;
189 char *endp;
190
191 errno = 0;
192 port = strtol(s, &endp, 0);
193 if (s == endp || *endp != '\0' ||
194 (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
195 port <= 0 || port > 65535)
196 return 0;
197
198 return port;
199}
200
d20f3c9e 201int
202a2tun(const char *s, int *remote)
203{
204 const char *errstr = NULL;
205 char *sp, *ep;
206 int tun;
207
208 if (remote != NULL) {
a4f24bf8 209 *remote = SSH_TUNID_ANY;
d20f3c9e 210 sp = xstrdup(s);
211 if ((ep = strchr(sp, ':')) == NULL) {
212 xfree(sp);
213 return (a2tun(s, NULL));
214 }
215 ep[0] = '\0'; ep++;
216 *remote = a2tun(ep, NULL);
217 tun = a2tun(sp, NULL);
218 xfree(sp);
a4f24bf8 219 return (*remote == SSH_TUNID_ERR ? *remote : tun);
d20f3c9e 220 }
221
222 if (strcasecmp(s, "any") == 0)
a4f24bf8 223 return (SSH_TUNID_ANY);
d20f3c9e 224
a4f24bf8 225 tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr);
226 if (errstr != NULL)
227 return (SSH_TUNID_ERR);
d20f3c9e 228
229 return (tun);
230}
231
e2b1fb42 232#define SECONDS 1
233#define MINUTES (SECONDS * 60)
234#define HOURS (MINUTES * 60)
235#define DAYS (HOURS * 24)
236#define WEEKS (DAYS * 7)
237
255cabd9 238/*
239 * Convert a time string into seconds; format is
240 * a sequence of:
241 * time[qualifier]
242 *
243 * Valid time qualifiers are:
244 * <none> seconds
245 * s|S seconds
246 * m|M minutes
247 * h|H hours
248 * d|D days
249 * w|W weeks
250 *
251 * Examples:
252 * 90m 90 minutes
253 * 1h30m 90 minutes
254 * 2d 2 days
255 * 1w 1 week
256 *
257 * Return -1 if time string is invalid.
258 */
259long
260convtime(const char *s)
e2b1fb42 261{
262 long total, secs;
263 const char *p;
264 char *endp;
265
266 errno = 0;
267 total = 0;
268 p = s;
269
270 if (p == NULL || *p == '\0')
271 return -1;
272
273 while (*p) {
274 secs = strtol(p, &endp, 10);
275 if (p == endp ||
276 (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
277 secs < 0)
278 return -1;
279
280 switch (*endp++) {
281 case '\0':
282 endp--;
283 case 's':
284 case 'S':
285 break;
286 case 'm':
287 case 'M':
288 secs *= MINUTES;
289 break;
290 case 'h':
291 case 'H':
292 secs *= HOURS;
293 break;
294 case 'd':
295 case 'D':
296 secs *= DAYS;
297 break;
298 case 'w':
299 case 'W':
300 secs *= WEEKS;
301 break;
302 default:
303 return -1;
304 }
305 total += secs;
306 if (total < 0)
307 return -1;
308 p = endp;
309 }
310
311 return total;
312}
313
3867aa0a 314/*
315 * Search for next delimiter between hostnames/addresses and ports.
316 * Argument may be modified (for termination).
317 * Returns *cp if parsing succeeds.
318 * *cp is set to the start of the next delimiter, if one was found.
319 * If this is the last field, *cp is set to NULL.
320 */
321char *
322hpdelim(char **cp)
323{
324 char *s, *old;
325
326 if (cp == NULL || *cp == NULL)
327 return NULL;
328
329 old = s = *cp;
330 if (*s == '[') {
331 if ((s = strchr(s, ']')) == NULL)
332 return NULL;
333 else
334 s++;
335 } else if ((s = strpbrk(s, ":/")) == NULL)
336 s = *cp + strlen(*cp); /* skip to end (see first case below) */
337
338 switch (*s) {
339 case '\0':
340 *cp = NULL; /* no more fields*/
341 break;
f8cc7664 342
3867aa0a 343 case ':':
344 case '/':
345 *s = '\0'; /* terminate */
346 *cp = s + 1;
347 break;
f8cc7664 348
3867aa0a 349 default:
350 return NULL;
351 }
352
353 return old;
354}
355
1fcde3fe 356char *
357cleanhostname(char *host)
358{
359 if (*host == '[' && host[strlen(host) - 1] == ']') {
360 host[strlen(host) - 1] = '\0';
361 return (host + 1);
362 } else
363 return host;
364}
365
366char *
367colon(char *cp)
368{
369 int flag = 0;
370
371 if (*cp == ':') /* Leading colon is part of file name. */
372 return (0);
373 if (*cp == '[')
374 flag = 1;
375
376 for (; *cp; ++cp) {
377 if (*cp == '@' && *(cp+1) == '[')
378 flag = 1;
379 if (*cp == ']' && *(cp+1) == ':' && flag)
380 return (cp+1);
381 if (*cp == ':' && !flag)
382 return (cp);
383 if (*cp == '/')
384 return (0);
385 }
386 return (0);
387}
388
255cabd9 389/* function to assist building execv() arguments */
8a624ebf 390void
391addargs(arglist *args, char *fmt, ...)
392{
393 va_list ap;
4116f5c0 394 char *cp;
9a995072 395 u_int nalloc;
4116f5c0 396 int r;
8a624ebf 397
398 va_start(ap, fmt);
4116f5c0 399 r = vasprintf(&cp, fmt, ap);
8a624ebf 400 va_end(ap);
4116f5c0 401 if (r == -1)
402 fatal("addargs: argument too long");
8a624ebf 403
c46e584f 404 nalloc = args->nalloc;
8a624ebf 405 if (args->list == NULL) {
c46e584f 406 nalloc = 32;
8a624ebf 407 args->num = 0;
c46e584f 408 } else if (args->num+2 >= nalloc)
409 nalloc *= 2;
8a624ebf 410
c46e584f 411 args->list = xrealloc(args->list, nalloc * sizeof(char *));
412 args->nalloc = nalloc;
4116f5c0 413 args->list[args->num++] = cp;
8a624ebf 414 args->list[args->num] = NULL;
415}
ea067773 416
4116f5c0 417void
418replacearg(arglist *args, u_int which, char *fmt, ...)
419{
420 va_list ap;
421 char *cp;
422 int r;
423
424 va_start(ap, fmt);
425 r = vasprintf(&cp, fmt, ap);
426 va_end(ap);
427 if (r == -1)
428 fatal("replacearg: argument too long");
429
430 if (which >= args->num)
431 fatal("replacearg: tried to replace invalid arg %d >= %d",
432 which, args->num);
433 xfree(args->list[which]);
434 args->list[which] = cp;
435}
436
437void
438freeargs(arglist *args)
439{
440 u_int i;
441
442 if (args->list != NULL) {
443 for (i = 0; i < args->num; i++)
444 xfree(args->list[i]);
445 xfree(args->list);
446 args->nalloc = args->num = 0;
447 args->list = NULL;
448 }
449}
450
49e71137 451/*
452 * Expands tildes in the file name. Returns data allocated by xmalloc.
453 * Warning: this calls getpw*.
454 */
455char *
456tilde_expand_filename(const char *filename, uid_t uid)
457{
458 const char *path;
459 char user[128], ret[MAXPATHLEN];
460 struct passwd *pw;
2ceb8101 461 u_int len, slash;
49e71137 462
463 if (*filename != '~')
464 return (xstrdup(filename));
465 filename++;
466
467 path = strchr(filename, '/');
468 if (path != NULL && path > filename) { /* ~user/path */
2ceb8101 469 slash = path - filename;
470 if (slash > sizeof(user) - 1)
49e71137 471 fatal("tilde_expand_filename: ~username too long");
2ceb8101 472 memcpy(user, filename, slash);
473 user[slash] = '\0';
49e71137 474 if ((pw = getpwnam(user)) == NULL)
475 fatal("tilde_expand_filename: No such user %s", user);
476 } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */
477 fatal("tilde_expand_filename: No such uid %d", uid);
478
479 if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret))
480 fatal("tilde_expand_filename: Path too long");
481
482 /* Make sure directory has a trailing '/' */
483 len = strlen(pw->pw_dir);
484 if ((len == 0 || pw->pw_dir[len - 1] != '/') &&
485 strlcat(ret, "/", sizeof(ret)) >= sizeof(ret))
486 fatal("tilde_expand_filename: Path too long");
487
488 /* Skip leading '/' from specified path */
489 if (path != NULL)
490 filename = path + 1;
491 if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret))
492 fatal("tilde_expand_filename: Path too long");
493
494 return (xstrdup(ret));
495}
496
a980cbd7 497/*
498 * Expand a string with a set of %[char] escapes. A number of escapes may be
499 * specified as (char *escape_chars, char *replacement) pairs. The list must
6381acf0 500 * be terminated by a NULL escape_char. Returns replaced string in memory
a980cbd7 501 * allocated by xmalloc.
502 */
503char *
504percent_expand(const char *string, ...)
505{
506#define EXPAND_MAX_KEYS 16
507 struct {
508 const char *key;
509 const char *repl;
510 } keys[EXPAND_MAX_KEYS];
2ceb8101 511 u_int num_keys, i, j;
a980cbd7 512 char buf[4096];
513 va_list ap;
514
515 /* Gather keys */
516 va_start(ap, string);
517 for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) {
518 keys[num_keys].key = va_arg(ap, char *);
519 if (keys[num_keys].key == NULL)
520 break;
521 keys[num_keys].repl = va_arg(ap, char *);
522 if (keys[num_keys].repl == NULL)
523 fatal("percent_expand: NULL replacement");
524 }
525 va_end(ap);
526
527 if (num_keys >= EXPAND_MAX_KEYS)
528 fatal("percent_expand: too many keys");
529
530 /* Expand string */
531 *buf = '\0';
532 for (i = 0; *string != '\0'; string++) {
533 if (*string != '%') {
534 append:
535 buf[i++] = *string;
536 if (i >= sizeof(buf))
537 fatal("percent_expand: string too long");
538 buf[i] = '\0';
539 continue;
540 }
541 string++;
542 if (*string == '%')
543 goto append;
544 for (j = 0; j < num_keys; j++) {
545 if (strchr(keys[j].key, *string) != NULL) {
546 i = strlcat(buf, keys[j].repl, sizeof(buf));
547 if (i >= sizeof(buf))
548 fatal("percent_expand: string too long");
549 break;
550 }
551 }
552 if (j >= num_keys)
553 fatal("percent_expand: unknown key %%%c", *string);
554 }
555 return (xstrdup(buf));
556#undef EXPAND_MAX_KEYS
557}
558
ea067773 559/*
560 * Read an entire line from a public key file into a static buffer, discarding
561 * lines that exceed the buffer size. Returns 0 on success, -1 on failure.
562 */
563int
564read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
41feb690 565 u_long *lineno)
ea067773 566{
567 while (fgets(buf, bufsz, f) != NULL) {
568 (*lineno)++;
569 if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
570 return 0;
571 } else {
41feb690 572 debug("%s: %s line %lu exceeds size limit", __func__,
573 filename, *lineno);
ea067773 574 /* discard remainder of line */
f8cc7664 575 while (fgetc(f) != '\n' && !feof(f))
ea067773 576 ; /* nothing */
577 }
578 }
579 return -1;
580}
ef07103c 581
d20f3c9e 582int
a4f24bf8 583tun_open(int tun, int mode)
d20f3c9e 584{
6306853a 585#if defined(CUSTOM_SYS_TUN_OPEN)
586 return (sys_tun_open(tun, mode));
0f6cb079 587#elif defined(SSH_TUN_OPENBSD)
a4f24bf8 588 struct ifreq ifr;
d20f3c9e 589 char name[100];
a4f24bf8 590 int fd = -1, sock;
d20f3c9e 591
a4f24bf8 592 /* Open the tunnel device */
593 if (tun <= SSH_TUNID_MAX) {
d20f3c9e 594 snprintf(name, sizeof(name), "/dev/tun%d", tun);
a4f24bf8 595 fd = open(name, O_RDWR);
596 } else if (tun == SSH_TUNID_ANY) {
597 for (tun = 100; tun >= 0; tun--) {
598 snprintf(name, sizeof(name), "/dev/tun%d", tun);
599 if ((fd = open(name, O_RDWR)) >= 0)
600 break;
d20f3c9e 601 }
602 } else {
81c042a3 603 debug("%s: invalid tunnel %u", __func__, tun);
a4f24bf8 604 return (-1);
605 }
606
607 if (fd < 0) {
608 debug("%s: %s open failed: %s", __func__, name, strerror(errno));
609 return (-1);
610 }
611
612 debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
613
614 /* Set the tunnel device operation mode */
615 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun);
616 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
617 goto failed;
618
619 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
620 goto failed;
b1b65311 621
622 /* Set interface mode */
623 ifr.ifr_flags &= ~IFF_UP;
624 if (mode == SSH_TUNMODE_ETHERNET)
a4f24bf8 625 ifr.ifr_flags |= IFF_LINK0;
b1b65311 626 else
627 ifr.ifr_flags &= ~IFF_LINK0;
628 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
629 goto failed;
630
631 /* Bring interface up */
a4f24bf8 632 ifr.ifr_flags |= IFF_UP;
633 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
634 goto failed;
635
636 close(sock);
637 return (fd);
638
639 failed:
640 if (fd >= 0)
641 close(fd);
642 if (sock >= 0)
643 close(sock);
644 debug("%s: failed to set %s mode %d: %s", __func__, name,
645 mode, strerror(errno));
d20f3c9e 646 return (-1);
6306853a 647#else
648 error("Tunnel interfaces are not supported on this platform");
649 return (-1);
650#endif
d20f3c9e 651}
652
fd6168c1 653void
654sanitise_stdfd(void)
655{
db382686 656 int nullfd, dupfd;
fd6168c1 657
db382686 658 if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
fd6168c1 659 fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
660 exit(1);
661 }
db382686 662 while (++dupfd <= 2) {
663 /* Only clobber closed fds */
664 if (fcntl(dupfd, F_GETFL, 0) >= 0)
665 continue;
666 if (dup2(nullfd, dupfd) == -1) {
fd6168c1 667 fprintf(stderr, "dup2: %s", strerror(errno));
668 exit(1);
669 }
fd6168c1 670 }
671 if (nullfd > 2)
672 close(nullfd);
673}
674
ef07103c 675char *
676tohex(const u_char *d, u_int l)
677{
678 char b[3], *r;
679 u_int i, hl;
680
681 hl = l * 2 + 1;
682 r = xmalloc(hl);
683 *r = '\0';
684 for (i = 0; i < l; i++) {
685 snprintf(b, sizeof(b), "%02x", d[i]);
686 strlcat(r, b, hl);
687 }
688 return (r);
689}
690
This page took 0.325503 seconds and 5 git commands to generate.