Annotation of src/usr.bin/ssh/misc.c, Revision 1.16
1.16 ! stevesk 1: /* $OpenBSD: misc.c,v 1.15 2002/01/24 21:09:25 stevesk Exp $ */
1.1 markus 2:
3: /*
4: * Copyright (c) 2000 Markus Friedl. All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: *
15: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
27: #include "includes.h"
1.16 ! stevesk 28: RCSID("$OpenBSD: misc.c,v 1.15 2002/01/24 21:09:25 stevesk Exp $");
1.1 markus 29:
30: #include "misc.h"
31: #include "log.h"
1.3 deraadt 32: #include "xmalloc.h"
1.1 markus 33:
1.12 markus 34: /* remove newline at end of string */
1.1 markus 35: char *
36: chop(char *s)
37: {
38: char *t = s;
39: while (*t) {
1.13 deraadt 40: if (*t == '\n' || *t == '\r') {
1.1 markus 41: *t = '\0';
42: return s;
43: }
44: t++;
45: }
46: return s;
47:
48: }
49:
1.12 markus 50: /* set/unset filedescriptor to non-blocking */
1.1 markus 51: void
52: set_nonblock(int fd)
53: {
54: int val;
1.8 markus 55:
1.1 markus 56: val = fcntl(fd, F_GETFL, 0);
57: if (val < 0) {
58: error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
59: return;
60: }
61: if (val & O_NONBLOCK) {
1.8 markus 62: debug2("fd %d is O_NONBLOCK", fd);
1.1 markus 63: return;
64: }
65: debug("fd %d setting O_NONBLOCK", fd);
66: val |= O_NONBLOCK;
1.8 markus 67: if (fcntl(fd, F_SETFL, val) == -1)
68: if (errno != ENODEV)
69: error("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
70: fd, strerror(errno));
71: }
72:
73: void
74: unset_nonblock(int fd)
75: {
76: int val;
77:
78: val = fcntl(fd, F_GETFL, 0);
79: if (val < 0) {
80: error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
81: return;
82: }
83: if (!(val & O_NONBLOCK)) {
84: debug2("fd %d is not O_NONBLOCK", fd);
85: return;
86: }
1.10 markus 87: debug("fd %d clearing O_NONBLOCK", fd);
1.8 markus 88: val &= ~O_NONBLOCK;
1.1 markus 89: if (fcntl(fd, F_SETFL, val) == -1)
90: if (errno != ENODEV)
91: error("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
92: fd, strerror(errno));
1.15 stevesk 93: }
94:
95: /* disable nagle on socket */
96: void
97: set_nodelay(int fd)
98: {
1.16 ! stevesk 99: int opt, optlen;
1.15 stevesk 100:
1.16 ! stevesk 101: optlen = sizeof opt;
! 102: if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
! 103: error("getsockopt TCP_NODELAY: %.100s", strerror(errno));
! 104: return;
! 105: }
! 106: if (opt == 1) {
! 107: debug2("fd %d is TCP_NODELAY", fd);
! 108: return;
! 109: }
! 110: opt = 1;
1.15 stevesk 111: debug("fd %d setting TCP_NODELAY", fd);
1.16 ! stevesk 112: if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
1.15 stevesk 113: error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
1.1 markus 114: }
115:
116: /* Characters considered whitespace in strsep calls. */
117: #define WHITESPACE " \t\r\n"
118:
1.12 markus 119: /* return next token in configuration line */
1.1 markus 120: char *
121: strdelim(char **s)
122: {
123: char *old;
124: int wspace = 0;
125:
126: if (*s == NULL)
127: return NULL;
128:
129: old = *s;
130:
131: *s = strpbrk(*s, WHITESPACE "=");
132: if (*s == NULL)
133: return (old);
134:
135: /* Allow only one '=' to be skipped */
136: if (*s[0] == '=')
137: wspace = 1;
138: *s[0] = '\0';
139:
140: *s += strspn(*s + 1, WHITESPACE) + 1;
141: if (*s[0] == '=' && !wspace)
142: *s += strspn(*s + 1, WHITESPACE) + 1;
143:
144: return (old);
1.2 markus 145: }
146:
147: struct passwd *
148: pwcopy(struct passwd *pw)
149: {
150: struct passwd *copy = xmalloc(sizeof(*copy));
1.4 deraadt 151:
1.2 markus 152: memset(copy, 0, sizeof(*copy));
153: copy->pw_name = xstrdup(pw->pw_name);
154: copy->pw_passwd = xstrdup(pw->pw_passwd);
1.4 deraadt 155: copy->pw_gecos = xstrdup(pw->pw_gecos);
1.2 markus 156: copy->pw_uid = pw->pw_uid;
157: copy->pw_gid = pw->pw_gid;
1.11 markus 158: copy->pw_expire = pw->pw_expire;
159: copy->pw_change = pw->pw_change;
1.2 markus 160: copy->pw_class = xstrdup(pw->pw_class);
161: copy->pw_dir = xstrdup(pw->pw_dir);
162: copy->pw_shell = xstrdup(pw->pw_shell);
163: return copy;
1.5 stevesk 164: }
165:
1.12 markus 166: /*
167: * Convert ASCII string to TCP/IP port number.
168: * Port must be >0 and <=65535.
169: * Return 0 if invalid.
170: */
171: int
172: a2port(const char *s)
1.5 stevesk 173: {
174: long port;
175: char *endp;
176:
177: errno = 0;
178: port = strtol(s, &endp, 0);
179: if (s == endp || *endp != '\0' ||
180: (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
181: port <= 0 || port > 65535)
182: return 0;
183:
184: return port;
1.9 stevesk 185: }
186:
187: #define SECONDS 1
188: #define MINUTES (SECONDS * 60)
189: #define HOURS (MINUTES * 60)
190: #define DAYS (HOURS * 24)
191: #define WEEKS (DAYS * 7)
192:
1.12 markus 193: /*
194: * Convert a time string into seconds; format is
195: * a sequence of:
196: * time[qualifier]
197: *
198: * Valid time qualifiers are:
199: * <none> seconds
200: * s|S seconds
201: * m|M minutes
202: * h|H hours
203: * d|D days
204: * w|W weeks
205: *
206: * Examples:
207: * 90m 90 minutes
208: * 1h30m 90 minutes
209: * 2d 2 days
210: * 1w 1 week
211: *
212: * Return -1 if time string is invalid.
213: */
214: long
215: convtime(const char *s)
1.9 stevesk 216: {
217: long total, secs;
218: const char *p;
219: char *endp;
220:
221: errno = 0;
222: total = 0;
223: p = s;
224:
225: if (p == NULL || *p == '\0')
226: return -1;
227:
228: while (*p) {
229: secs = strtol(p, &endp, 10);
230: if (p == endp ||
231: (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
232: secs < 0)
233: return -1;
234:
235: switch (*endp++) {
236: case '\0':
237: endp--;
238: case 's':
239: case 'S':
240: break;
241: case 'm':
242: case 'M':
243: secs *= MINUTES;
244: break;
245: case 'h':
246: case 'H':
247: secs *= HOURS;
248: break;
249: case 'd':
250: case 'D':
251: secs *= DAYS;
252: break;
253: case 'w':
254: case 'W':
255: secs *= WEEKS;
256: break;
257: default:
258: return -1;
259: }
260: total += secs;
261: if (total < 0)
262: return -1;
263: p = endp;
264: }
265:
266: return total;
1.6 mouring 267: }
268:
269: char *
270: cleanhostname(char *host)
271: {
272: if (*host == '[' && host[strlen(host) - 1] == ']') {
273: host[strlen(host) - 1] = '\0';
274: return (host + 1);
275: } else
276: return host;
277: }
278:
279: char *
280: colon(char *cp)
281: {
282: int flag = 0;
283:
284: if (*cp == ':') /* Leading colon is part of file name. */
285: return (0);
286: if (*cp == '[')
287: flag = 1;
288:
289: for (; *cp; ++cp) {
290: if (*cp == '@' && *(cp+1) == '[')
291: flag = 1;
292: if (*cp == ']' && *(cp+1) == ':' && flag)
293: return (cp+1);
294: if (*cp == ':' && !flag)
295: return (cp);
296: if (*cp == '/')
297: return (0);
298: }
299: return (0);
1.7 mouring 300: }
301:
1.12 markus 302: /* function to assist building execv() arguments */
1.7 mouring 303: void
304: addargs(arglist *args, char *fmt, ...)
305: {
306: va_list ap;
307: char buf[1024];
308:
309: va_start(ap, fmt);
310: vsnprintf(buf, sizeof(buf), fmt, ap);
311: va_end(ap);
312:
313: if (args->list == NULL) {
314: args->nalloc = 32;
315: args->num = 0;
1.14 deraadt 316: } else if (args->num+2 >= args->nalloc)
1.7 mouring 317: args->nalloc *= 2;
318:
319: args->list = xrealloc(args->list, args->nalloc * sizeof(char *));
320: args->list[args->num++] = xstrdup(buf);
321: args->list[args->num] = NULL;
1.1 markus 322: }