2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 RCSID("$OpenBSD: misc.c,v 1.30 2005/04/09 04:32:54 djm Exp $");
32 /* remove newline at end of string */
38 if (*t == '\n' || *t == '\r') {
48 /* set/unset filedescriptor to non-blocking */
54 val = fcntl(fd, F_GETFL, 0);
56 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
59 if (val & O_NONBLOCK) {
60 debug3("fd %d is O_NONBLOCK", fd);
63 debug2("fd %d setting O_NONBLOCK", fd);
65 if (fcntl(fd, F_SETFL, val) == -1) {
66 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd,
74 unset_nonblock(int fd)
78 val = fcntl(fd, F_GETFL, 0);
80 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
83 if (!(val & O_NONBLOCK)) {
84 debug3("fd %d is not O_NONBLOCK", fd);
87 debug("fd %d clearing O_NONBLOCK", fd);
89 if (fcntl(fd, F_SETFL, val) == -1) {
90 debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
97 /* disable nagle on socket */
105 if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
106 debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
110 debug2("fd %d is TCP_NODELAY", fd);
114 debug2("fd %d setting TCP_NODELAY", fd);
115 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
116 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
119 /* Characters considered whitespace in strsep calls. */
120 #define WHITESPACE " \t\r\n"
122 /* return next token in configuration line */
134 *s = strpbrk(*s, WHITESPACE "=");
138 /* Allow only one '=' to be skipped */
143 *s += strspn(*s + 1, WHITESPACE) + 1;
144 if (*s[0] == '=' && !wspace)
145 *s += strspn(*s + 1, WHITESPACE) + 1;
151 pwcopy(struct passwd *pw)
153 struct passwd *copy = xmalloc(sizeof(*copy));
155 memset(copy, 0, sizeof(*copy));
156 copy->pw_name = xstrdup(pw->pw_name);
157 copy->pw_passwd = xstrdup(pw->pw_passwd);
158 copy->pw_gecos = xstrdup(pw->pw_gecos);
159 copy->pw_uid = pw->pw_uid;
160 copy->pw_gid = pw->pw_gid;
161 #ifdef HAVE_PW_EXPIRE_IN_PASSWD
162 copy->pw_expire = pw->pw_expire;
164 #ifdef HAVE_PW_CHANGE_IN_PASSWD
165 copy->pw_change = pw->pw_change;
167 #ifdef HAVE_PW_CLASS_IN_PASSWD
168 copy->pw_class = xstrdup(pw->pw_class);
170 copy->pw_dir = xstrdup(pw->pw_dir);
171 copy->pw_shell = xstrdup(pw->pw_shell);
176 * Convert ASCII string to TCP/IP port number.
177 * Port must be >0 and <=65535.
178 * Return 0 if invalid.
181 a2port(const char *s)
187 port = strtol(s, &endp, 0);
188 if (s == endp || *endp != '\0' ||
189 (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
190 port <= 0 || port > 65535)
197 #define MINUTES (SECONDS * 60)
198 #define HOURS (MINUTES * 60)
199 #define DAYS (HOURS * 24)
200 #define WEEKS (DAYS * 7)
203 * Convert a time string into seconds; format is
207 * Valid time qualifiers are:
221 * Return -1 if time string is invalid.
224 convtime(const char *s)
234 if (p == NULL || *p == '\0')
238 secs = strtol(p, &endp, 10);
240 (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
279 * Search for next delimiter between hostnames/addresses and ports.
280 * Argument may be modified (for termination).
281 * Returns *cp if parsing succeeds.
282 * *cp is set to the start of the next delimiter, if one was found.
283 * If this is the last field, *cp is set to NULL.
290 if (cp == NULL || *cp == NULL)
295 if ((s = strchr(s, ']')) == NULL)
299 } else if ((s = strpbrk(s, ":/")) == NULL)
300 s = *cp + strlen(*cp); /* skip to end (see first case below) */
304 *cp = NULL; /* no more fields*/
309 *s = '\0'; /* terminate */
321 cleanhostname(char *host)
323 if (*host == '[' && host[strlen(host) - 1] == ']') {
324 host[strlen(host) - 1] = '\0';
335 if (*cp == ':') /* Leading colon is part of file name. */
341 if (*cp == '@' && *(cp+1) == '[')
343 if (*cp == ']' && *(cp+1) == ':' && flag)
345 if (*cp == ':' && !flag)
353 /* function to assist building execv() arguments */
355 addargs(arglist *args, char *fmt, ...)
362 vsnprintf(buf, sizeof(buf), fmt, ap);
365 nalloc = args->nalloc;
366 if (args->list == NULL) {
369 } else if (args->num+2 >= nalloc)
372 args->list = xrealloc(args->list, nalloc * sizeof(char *));
373 args->nalloc = nalloc;
374 args->list[args->num++] = xstrdup(buf);
375 args->list[args->num] = NULL;
379 * Expands tildes in the file name. Returns data allocated by xmalloc.
380 * Warning: this calls getpw*.
383 tilde_expand_filename(const char *filename, uid_t uid)
386 char user[128], ret[MAXPATHLEN];
390 if (*filename != '~')
391 return (xstrdup(filename));
394 path = strchr(filename, '/');
395 if (path != NULL && path > filename) { /* ~user/path */
396 if (path - filename > sizeof(user) - 1)
397 fatal("tilde_expand_filename: ~username too long");
398 memcpy(user, filename, path - filename);
399 user[path - filename] = '\0';
400 if ((pw = getpwnam(user)) == NULL)
401 fatal("tilde_expand_filename: No such user %s", user);
402 } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */
403 fatal("tilde_expand_filename: No such uid %d", uid);
405 if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret))
406 fatal("tilde_expand_filename: Path too long");
408 /* Make sure directory has a trailing '/' */
409 len = strlen(pw->pw_dir);
410 if ((len == 0 || pw->pw_dir[len - 1] != '/') &&
411 strlcat(ret, "/", sizeof(ret)) >= sizeof(ret))
412 fatal("tilde_expand_filename: Path too long");
414 /* Skip leading '/' from specified path */
417 if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret))
418 fatal("tilde_expand_filename: Path too long");
420 return (xstrdup(ret));
424 * Read an entire line from a public key file into a static buffer, discarding
425 * lines that exceed the buffer size. Returns 0 on success, -1 on failure.
428 read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
431 while (fgets(buf, bufsz, f) != NULL) {
433 if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
436 debug("%s: %s line %lu exceeds size limit", __func__,
438 /* discard remainder of line */
439 while (fgetc(f) != '\n' && !feof(f))