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