]> andersk Git - openssh.git/blame - misc.c
- markus@cvs.openbsd.org 2002/02/11 16:19:39
[openssh.git] / misc.c
CommitLineData
bcc0381e 1/* $OpenBSD: misc.c,v 1.15 2002/01/24 21:09:25 stevesk Exp $ */
bcbf86ec 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
6d24277f 27#include "includes.h"
bcc0381e 28RCSID("$OpenBSD: misc.c,v 1.15 2002/01/24 21:09:25 stevesk Exp $");
6d24277f 29
1aa00dcb 30#include "misc.h"
42f11eb2 31#include "log.h"
4ab21f86 32#include "xmalloc.h"
6d24277f 33
255cabd9 34/* remove newline at end of string */
6d24277f 35char *
36chop(char *s)
37{
38 char *t = s;
39 while (*t) {
6aacefa7 40 if (*t == '\n' || *t == '\r') {
6d24277f 41 *t = '\0';
42 return s;
43 }
44 t++;
45 }
46 return s;
47
48}
49
255cabd9 50/* set/unset filedescriptor to non-blocking */
6d24277f 51void
52set_nonblock(int fd)
53{
54 int val;
89aa792b 55
6d24277f 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 }
a22aff1f 61 if (val & O_NONBLOCK) {
89aa792b 62 debug2("fd %d is O_NONBLOCK", fd);
6d24277f 63 return;
a22aff1f 64 }
6d24277f 65 debug("fd %d setting O_NONBLOCK", fd);
66 val |= O_NONBLOCK;
67 if (fcntl(fd, F_SETFL, val) == -1)
14a9a859 68 if (errno != ENODEV)
69 error("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
70 fd, strerror(errno));
6d24277f 71}
72
89aa792b 73void
74unset_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 }
e04e7a19 87 debug("fd %d clearing O_NONBLOCK", fd);
89aa792b 88 val &= ~O_NONBLOCK;
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));
93}
94
bcc0381e 95/* disable nagle on socket */
96void
97set_nodelay(int fd)
98{
99 int on = 1;
100
101 debug("fd %d setting TCP_NODELAY", fd);
102 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on) == -1)
103 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
104}
105
6d24277f 106/* Characters considered whitespace in strsep calls. */
107#define WHITESPACE " \t\r\n"
108
255cabd9 109/* return next token in configuration line */
6d24277f 110char *
111strdelim(char **s)
112{
113 char *old;
114 int wspace = 0;
115
116 if (*s == NULL)
117 return NULL;
118
119 old = *s;
120
121 *s = strpbrk(*s, WHITESPACE "=");
122 if (*s == NULL)
123 return (old);
124
125 /* Allow only one '=' to be skipped */
126 if (*s[0] == '=')
127 wspace = 1;
128 *s[0] = '\0';
129
130 *s += strspn(*s + 1, WHITESPACE) + 1;
131 if (*s[0] == '=' && !wspace)
132 *s += strspn(*s + 1, WHITESPACE) + 1;
133
134 return (old);
135}
1aa00dcb 136
3b1a83df 137struct passwd *
138pwcopy(struct passwd *pw)
139{
140 struct passwd *copy = xmalloc(sizeof(*copy));
5649fbbe 141
3b1a83df 142 memset(copy, 0, sizeof(*copy));
143 copy->pw_name = xstrdup(pw->pw_name);
144 copy->pw_passwd = xstrdup(pw->pw_passwd);
5649fbbe 145 copy->pw_gecos = xstrdup(pw->pw_gecos);
3b1a83df 146 copy->pw_uid = pw->pw_uid;
147 copy->pw_gid = pw->pw_gid;
7751d4eb 148#ifdef HAVE_PW_EXPIRE_IN_PASSWD
c4d49b85 149 copy->pw_expire = pw->pw_expire;
7751d4eb 150#endif
151#ifdef HAVE_PW_CHANGE_IN_PASSWD
c4d49b85 152 copy->pw_change = pw->pw_change;
7751d4eb 153#endif
2605addd 154#ifdef HAVE_PW_CLASS_IN_PASSWD
3b1a83df 155 copy->pw_class = xstrdup(pw->pw_class);
2605addd 156#endif
3b1a83df 157 copy->pw_dir = xstrdup(pw->pw_dir);
158 copy->pw_shell = xstrdup(pw->pw_shell);
159 return copy;
160}
161
255cabd9 162/*
163 * Convert ASCII string to TCP/IP port number.
164 * Port must be >0 and <=65535.
165 * Return 0 if invalid.
166 */
167int
168a2port(const char *s)
2d2a2c65 169{
170 long port;
171 char *endp;
172
173 errno = 0;
174 port = strtol(s, &endp, 0);
175 if (s == endp || *endp != '\0' ||
176 (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
177 port <= 0 || port > 65535)
178 return 0;
179
180 return port;
181}
182
e2b1fb42 183#define SECONDS 1
184#define MINUTES (SECONDS * 60)
185#define HOURS (MINUTES * 60)
186#define DAYS (HOURS * 24)
187#define WEEKS (DAYS * 7)
188
255cabd9 189/*
190 * Convert a time string into seconds; format is
191 * a sequence of:
192 * time[qualifier]
193 *
194 * Valid time qualifiers are:
195 * <none> seconds
196 * s|S seconds
197 * m|M minutes
198 * h|H hours
199 * d|D days
200 * w|W weeks
201 *
202 * Examples:
203 * 90m 90 minutes
204 * 1h30m 90 minutes
205 * 2d 2 days
206 * 1w 1 week
207 *
208 * Return -1 if time string is invalid.
209 */
210long
211convtime(const char *s)
e2b1fb42 212{
213 long total, secs;
214 const char *p;
215 char *endp;
216
217 errno = 0;
218 total = 0;
219 p = s;
220
221 if (p == NULL || *p == '\0')
222 return -1;
223
224 while (*p) {
225 secs = strtol(p, &endp, 10);
226 if (p == endp ||
227 (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
228 secs < 0)
229 return -1;
230
231 switch (*endp++) {
232 case '\0':
233 endp--;
234 case 's':
235 case 'S':
236 break;
237 case 'm':
238 case 'M':
239 secs *= MINUTES;
240 break;
241 case 'h':
242 case 'H':
243 secs *= HOURS;
244 break;
245 case 'd':
246 case 'D':
247 secs *= DAYS;
248 break;
249 case 'w':
250 case 'W':
251 secs *= WEEKS;
252 break;
253 default:
254 return -1;
255 }
256 total += secs;
257 if (total < 0)
258 return -1;
259 p = endp;
260 }
261
262 return total;
263}
264
1fcde3fe 265char *
266cleanhostname(char *host)
267{
268 if (*host == '[' && host[strlen(host) - 1] == ']') {
269 host[strlen(host) - 1] = '\0';
270 return (host + 1);
271 } else
272 return host;
273}
274
275char *
276colon(char *cp)
277{
278 int flag = 0;
279
280 if (*cp == ':') /* Leading colon is part of file name. */
281 return (0);
282 if (*cp == '[')
283 flag = 1;
284
285 for (; *cp; ++cp) {
286 if (*cp == '@' && *(cp+1) == '[')
287 flag = 1;
288 if (*cp == ']' && *(cp+1) == ':' && flag)
289 return (cp+1);
290 if (*cp == ':' && !flag)
291 return (cp);
292 if (*cp == '/')
293 return (0);
294 }
295 return (0);
296}
297
255cabd9 298/* function to assist building execv() arguments */
8a624ebf 299void
300addargs(arglist *args, char *fmt, ...)
301{
302 va_list ap;
303 char buf[1024];
304
305 va_start(ap, fmt);
306 vsnprintf(buf, sizeof(buf), fmt, ap);
307 va_end(ap);
308
309 if (args->list == NULL) {
310 args->nalloc = 32;
311 args->num = 0;
184eed6a 312 } else if (args->num+2 >= args->nalloc)
8a624ebf 313 args->nalloc *= 2;
314
315 args->list = xrealloc(args->list, args->nalloc * sizeof(char *));
316 args->list[args->num++] = xstrdup(buf);
317 args->list[args->num] = NULL;
318}
319
1aa00dcb 320mysig_t
321mysignal(int sig, mysig_t act)
322{
323#ifdef HAVE_SIGACTION
324 struct sigaction sa, osa;
325
120bf679 326 if (sigaction(sig, NULL, &osa) == -1)
1aa00dcb 327 return (mysig_t) -1;
328 if (osa.sa_handler != act) {
120bf679 329 memset(&sa, 0, sizeof(sa));
1aa00dcb 330 sigemptyset(&sa.sa_mask);
331 sa.sa_flags = 0;
d54d99a3 332#if defined(SA_INTERRUPT)
333 if (sig == SIGALRM)
df538d55 334 sa.sa_flags |= SA_INTERRUPT;
25cd3375 335#endif
1aa00dcb 336 sa.sa_handler = act;
120bf679 337 if (sigaction(sig, &sa, NULL) == -1)
1aa00dcb 338 return (mysig_t) -1;
339 }
340 return (osa.sa_handler);
341#else
342 return (signal(sig, act));
343#endif
344}
This page took 0.169434 seconds and 5 git commands to generate.