Annotation of src/usr.bin/ssh/misc.c, Revision 1.18
1.18 ! markus 1: /* $OpenBSD: misc.c,v 1.17 2002/02/26 20:03:51 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.18 ! markus 28: RCSID("$OpenBSD: misc.c,v 1.17 2002/02/26 20:03:51 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)
1.18 ! markus 68: debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
! 69: fd, strerror(errno));
1.8 markus 70: }
71:
72: void
73: unset_nonblock(int fd)
74: {
75: int val;
76:
77: val = fcntl(fd, F_GETFL, 0);
78: if (val < 0) {
79: error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
80: return;
81: }
82: if (!(val & O_NONBLOCK)) {
83: debug2("fd %d is not O_NONBLOCK", fd);
84: return;
85: }
1.10 markus 86: debug("fd %d clearing O_NONBLOCK", fd);
1.8 markus 87: val &= ~O_NONBLOCK;
1.1 markus 88: if (fcntl(fd, F_SETFL, val) == -1)
1.18 ! markus 89: debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
! 90: fd, strerror(errno));
1.15 stevesk 91: }
92:
93: /* disable nagle on socket */
94: void
95: set_nodelay(int fd)
96: {
1.17 stevesk 97: int opt;
98: socklen_t optlen;
1.15 stevesk 99:
1.16 stevesk 100: optlen = sizeof opt;
101: if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
102: error("getsockopt TCP_NODELAY: %.100s", strerror(errno));
103: return;
104: }
105: if (opt == 1) {
106: debug2("fd %d is TCP_NODELAY", fd);
107: return;
108: }
109: opt = 1;
1.15 stevesk 110: debug("fd %d setting TCP_NODELAY", fd);
1.16 stevesk 111: if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
1.15 stevesk 112: error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
1.1 markus 113: }
114:
115: /* Characters considered whitespace in strsep calls. */
116: #define WHITESPACE " \t\r\n"
117:
1.12 markus 118: /* return next token in configuration line */
1.1 markus 119: char *
120: strdelim(char **s)
121: {
122: char *old;
123: int wspace = 0;
124:
125: if (*s == NULL)
126: return NULL;
127:
128: old = *s;
129:
130: *s = strpbrk(*s, WHITESPACE "=");
131: if (*s == NULL)
132: return (old);
133:
134: /* Allow only one '=' to be skipped */
135: if (*s[0] == '=')
136: wspace = 1;
137: *s[0] = '\0';
138:
139: *s += strspn(*s + 1, WHITESPACE) + 1;
140: if (*s[0] == '=' && !wspace)
141: *s += strspn(*s + 1, WHITESPACE) + 1;
142:
143: return (old);
1.2 markus 144: }
145:
146: struct passwd *
147: pwcopy(struct passwd *pw)
148: {
149: struct passwd *copy = xmalloc(sizeof(*copy));
1.4 deraadt 150:
1.2 markus 151: memset(copy, 0, sizeof(*copy));
152: copy->pw_name = xstrdup(pw->pw_name);
153: copy->pw_passwd = xstrdup(pw->pw_passwd);
1.4 deraadt 154: copy->pw_gecos = xstrdup(pw->pw_gecos);
1.2 markus 155: copy->pw_uid = pw->pw_uid;
156: copy->pw_gid = pw->pw_gid;
1.11 markus 157: copy->pw_expire = pw->pw_expire;
158: copy->pw_change = pw->pw_change;
1.2 markus 159: copy->pw_class = xstrdup(pw->pw_class);
160: copy->pw_dir = xstrdup(pw->pw_dir);
161: copy->pw_shell = xstrdup(pw->pw_shell);
162: return copy;
1.5 stevesk 163: }
164:
1.12 markus 165: /*
166: * Convert ASCII string to TCP/IP port number.
167: * Port must be >0 and <=65535.
168: * Return 0 if invalid.
169: */
170: int
171: a2port(const char *s)
1.5 stevesk 172: {
173: long port;
174: char *endp;
175:
176: errno = 0;
177: port = strtol(s, &endp, 0);
178: if (s == endp || *endp != '\0' ||
179: (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
180: port <= 0 || port > 65535)
181: return 0;
182:
183: return port;
1.9 stevesk 184: }
185:
186: #define SECONDS 1
187: #define MINUTES (SECONDS * 60)
188: #define HOURS (MINUTES * 60)
189: #define DAYS (HOURS * 24)
190: #define WEEKS (DAYS * 7)
191:
1.12 markus 192: /*
193: * Convert a time string into seconds; format is
194: * a sequence of:
195: * time[qualifier]
196: *
197: * Valid time qualifiers are:
198: * <none> seconds
199: * s|S seconds
200: * m|M minutes
201: * h|H hours
202: * d|D days
203: * w|W weeks
204: *
205: * Examples:
206: * 90m 90 minutes
207: * 1h30m 90 minutes
208: * 2d 2 days
209: * 1w 1 week
210: *
211: * Return -1 if time string is invalid.
212: */
213: long
214: convtime(const char *s)
1.9 stevesk 215: {
216: long total, secs;
217: const char *p;
218: char *endp;
219:
220: errno = 0;
221: total = 0;
222: p = s;
223:
224: if (p == NULL || *p == '\0')
225: return -1;
226:
227: while (*p) {
228: secs = strtol(p, &endp, 10);
229: if (p == endp ||
230: (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
231: secs < 0)
232: return -1;
233:
234: switch (*endp++) {
235: case '\0':
236: endp--;
237: case 's':
238: case 'S':
239: break;
240: case 'm':
241: case 'M':
242: secs *= MINUTES;
243: break;
244: case 'h':
245: case 'H':
246: secs *= HOURS;
247: break;
248: case 'd':
249: case 'D':
250: secs *= DAYS;
251: break;
252: case 'w':
253: case 'W':
254: secs *= WEEKS;
255: break;
256: default:
257: return -1;
258: }
259: total += secs;
260: if (total < 0)
261: return -1;
262: p = endp;
263: }
264:
265: return total;
1.6 mouring 266: }
267:
268: char *
269: cleanhostname(char *host)
270: {
271: if (*host == '[' && host[strlen(host) - 1] == ']') {
272: host[strlen(host) - 1] = '\0';
273: return (host + 1);
274: } else
275: return host;
276: }
277:
278: char *
279: colon(char *cp)
280: {
281: int flag = 0;
282:
283: if (*cp == ':') /* Leading colon is part of file name. */
284: return (0);
285: if (*cp == '[')
286: flag = 1;
287:
288: for (; *cp; ++cp) {
289: if (*cp == '@' && *(cp+1) == '[')
290: flag = 1;
291: if (*cp == ']' && *(cp+1) == ':' && flag)
292: return (cp+1);
293: if (*cp == ':' && !flag)
294: return (cp);
295: if (*cp == '/')
296: return (0);
297: }
298: return (0);
1.7 mouring 299: }
300:
1.12 markus 301: /* function to assist building execv() arguments */
1.7 mouring 302: void
303: addargs(arglist *args, char *fmt, ...)
304: {
305: va_list ap;
306: char buf[1024];
307:
308: va_start(ap, fmt);
309: vsnprintf(buf, sizeof(buf), fmt, ap);
310: va_end(ap);
311:
312: if (args->list == NULL) {
313: args->nalloc = 32;
314: args->num = 0;
1.14 deraadt 315: } else if (args->num+2 >= args->nalloc)
1.7 mouring 316: args->nalloc *= 2;
317:
318: args->list = xrealloc(args->list, args->nalloc * sizeof(char *));
319: args->list[args->num++] = xstrdup(buf);
320: args->list[args->num] = NULL;
1.1 markus 321: }