1 
2 /*
3  * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include "includes.h"
19 
20 #include <sys/types.h>
21 #ifdef HAVE_SYS_SELECT_H
22 # include <sys/select.h>
23 #endif
24 #ifdef HAVE_SYS_TIME_H
25 # include <sys/time.h>
26 #endif
27 
28 #include <fcntl.h>
29 #include <string.h>
30 #include <signal.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <time.h>
34 #include <unistd.h>
35 
36 #ifndef HAVE___PROGNAME
37 char *__progname;
38 #endif
39 
40 /*
41  * NB. duplicate __progname in case it is an alias for argv[0]
42  * Otherwise it may get clobbered by setproctitle()
43  */
44 char *ssh_get_progname(char *argv0)
45 {
46 	char *p, *q;
47 #ifdef HAVE___PROGNAME
48 	extern char *__progname;
49 
50 	p = __progname;
51 #else
52 	if (argv0 == NULL)
53 		return ("unknown");	/* XXX */
54 	p = strrchr(argv0, '/');
55 	if (p == NULL)
56 		p = argv0;
57 	else
58 		p++;
59 #endif
60 	if ((q = strdup(p)) == NULL) {
61 		perror("strdup");
62 		exit(1);
63 	}
64 	return q;
65 }
66 
67 #ifndef HAVE_SETLOGIN
68 int setlogin(const char *name)
69 {
70 	return (0);
71 }
72 #endif /* !HAVE_SETLOGIN */
73 
74 #ifndef HAVE_INNETGR
75 int innetgr(const char *netgroup, const char *host,
76 	    const char *user, const char *domain)
77 {
78 	return (0);
79 }
80 #endif /* HAVE_INNETGR */
81 
82 #if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
83 int seteuid(uid_t euid)
84 {
85 	return (setreuid(-1, euid));
86 }
87 #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */
88 
89 #if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID)
90 int setegid(uid_t egid)
91 {
92 	return(setresgid(-1, egid, -1));
93 }
94 #endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */
95 
96 #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR)
97 const char *strerror(int e)
98 {
99 	extern int sys_nerr;
100 	extern char *sys_errlist[];
101 
102 	if ((e >= 0) && (e < sys_nerr))
103 		return (sys_errlist[e]);
104 
105 	return ("unlisted error");
106 }
107 #endif
108 
109 #ifndef HAVE_UTIMES
110 int utimes(char *filename, struct timeval *tvp)
111 {
112 	struct utimbuf ub;
113 
114 	ub.actime = tvp[0].tv_sec;
115 	ub.modtime = tvp[1].tv_sec;
116 
117 	return (utime(filename, &ub));
118 }
119 #endif
120 
121 #ifndef HAVE_UTIMENSAT
122 /*
123  * A limited implementation of utimensat() that only implements the
124  * functionality used by OpenSSH, currently only AT_FDCWD and
125  * AT_SYMLINK_NOFOLLOW.
126  */
127 int
128 utimensat(int fd, const char *path, const struct timespec times[2],
129     int flag)
130 {
131 	struct timeval tv[2];
132 	int ret, oflags = O_WRONLY;
133 
134 	tv[0].tv_sec = times[0].tv_sec;
135 	tv[0].tv_usec = times[0].tv_nsec / 1000;
136 	tv[1].tv_sec = times[1].tv_sec;
137 	tv[1].tv_usec = times[1].tv_nsec / 1000;
138 
139 	if (fd != AT_FDCWD) {
140 		errno = ENOSYS;
141 		return -1;
142 	}
143 # ifndef HAVE_FUTIMES
144 	return utimes(path, tv);
145 # else
146 	if (flag & AT_SYMLINK_NOFOLLOW)
147 		oflags |= O_NOFOLLOW;
148 	if ((fd = open(path, oflags)) == -1)
149 		return -1;
150 	ret = futimes(fd, tv);
151 	close(fd);
152 	return ret;
153 # endif
154 }
155 #endif
156 
157 #ifndef HAVE_TRUNCATE
158 int truncate(const char *path, off_t length)
159 {
160 	int fd, ret, saverrno;
161 
162 	fd = open(path, O_WRONLY);
163 	if (fd < 0)
164 		return (-1);
165 
166 	ret = ftruncate(fd, length);
167 	saverrno = errno;
168 	close(fd);
169 	if (ret == -1)
170 		errno = saverrno;
171 
172 	return(ret);
173 }
174 #endif /* HAVE_TRUNCATE */
175 
176 #if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
177 int nanosleep(const struct timespec *req, struct timespec *rem)
178 {
179 	int rc, saverrno;
180 	extern int errno;
181 	struct timeval tstart, tstop, tremain, time2wait;
182 
183 	TIMESPEC_TO_TIMEVAL(&time2wait, req)
184 	(void) gettimeofday(&tstart, NULL);
185 	rc = select(0, NULL, NULL, NULL, &time2wait);
186 	if (rc == -1) {
187 		saverrno = errno;
188 		(void) gettimeofday (&tstop, NULL);
189 		errno = saverrno;
190 		tremain.tv_sec = time2wait.tv_sec -
191 			(tstop.tv_sec - tstart.tv_sec);
192 		tremain.tv_usec = time2wait.tv_usec -
193 			(tstop.tv_usec - tstart.tv_usec);
194 		tremain.tv_sec += tremain.tv_usec / 1000000L;
195 		tremain.tv_usec %= 1000000L;
196 	} else {
197 		tremain.tv_sec = 0;
198 		tremain.tv_usec = 0;
199 	}
200 	if (rem != NULL)
201 		TIMEVAL_TO_TIMESPEC(&tremain, rem)
202 
203 	return(rc);
204 }
205 #endif
206 
207 #if !defined(HAVE_USLEEP)
208 int usleep(unsigned int useconds)
209 {
210 	struct timespec ts;
211 
212 	ts.tv_sec = useconds / 1000000;
213 	ts.tv_nsec = (useconds % 1000000) * 1000;
214 	return nanosleep(&ts, NULL);
215 }
216 #endif
217 
218 #ifndef HAVE_TCGETPGRP
219 pid_t
220 tcgetpgrp(int fd)
221 {
222 	int ctty_pgrp;
223 
224 	if (ioctl(fd, TIOCGPGRP, &ctty_pgrp) == -1)
225 		return(-1);
226 	else
227 		return(ctty_pgrp);
228 }
229 #endif /* HAVE_TCGETPGRP */
230 
231 #ifndef HAVE_TCSENDBREAK
232 int
233 tcsendbreak(int fd, int duration)
234 {
235 # if defined(TIOCSBRK) && defined(TIOCCBRK)
236 	struct timeval sleepytime;
237 
238 	sleepytime.tv_sec = 0;
239 	sleepytime.tv_usec = 400000;
240 	if (ioctl(fd, TIOCSBRK, 0) == -1)
241 		return (-1);
242 	(void)select(0, 0, 0, 0, &sleepytime);
243 	if (ioctl(fd, TIOCCBRK, 0) == -1)
244 		return (-1);
245 	return (0);
246 # else
247 	return -1;
248 # endif
249 }
250 #endif /* HAVE_TCSENDBREAK */
251 
252 #ifndef HAVE_STRDUP
253 char *
254 strdup(const char *str)
255 {
256 	size_t len;
257 	char *cp;
258 
259 	len = strlen(str) + 1;
260 	cp = malloc(len);
261 	if (cp != NULL)
262 		return(memcpy(cp, str, len));
263 	return NULL;
264 }
265 #endif
266 
267 #ifndef HAVE_ISBLANK
268 int
269 isblank(int c)
270 {
271 	return (c == ' ' || c == '\t');
272 }
273 #endif
274 
275 #ifndef HAVE_GETPGID
276 pid_t
277 getpgid(pid_t pid)
278 {
279 #if defined(HAVE_GETPGRP) && !defined(GETPGRP_VOID) && GETPGRP_VOID == 0
280 	return getpgrp(pid);
281 #elif defined(HAVE_GETPGRP)
282 	if (pid == 0)
283 		return getpgrp();
284 #endif
285 
286 	errno = ESRCH;
287 	return -1;
288 }
289 #endif
290 
291 #ifndef HAVE_PLEDGE
292 int
293 pledge(const char *promises, const char *paths[])
294 {
295 	return 0;
296 }
297 #endif
298 
299 #ifndef HAVE_MBTOWC
300 /* a mbtowc that only supports ASCII */
301 int
302 mbtowc(wchar_t *pwc, const char *s, size_t n)
303 {
304 	if (s == NULL || *s == '\0')
305 		return 0;	/* ASCII is not state-dependent */
306 	if (*s < 0 || *s > 0x7f || n < 1) {
307 		errno = EOPNOTSUPP;
308 		return -1;
309 	}
310 	if (pwc != NULL)
311 		*pwc = *s;
312 	return 1;
313 }
314 #endif
315 
316 #ifndef HAVE_LLABS
317 long long
318 llabs(long long j)
319 {
320 	return (j < 0 ? -j : j);
321 }
322 #endif
323 
324 #ifndef HAVE_BZERO
325 void
326 bzero(void *b, size_t n)
327 {
328 	(void)memset(b, 0, n);
329 }
330 #endif
331 
332 #ifndef HAVE_RAISE
333 int
334 raise(int sig)
335 {
336 	kill(getpid(), sig);
337 }
338 #endif
339 
340 #ifndef HAVE_GETSID
341 pid_t
342 getsid(pid_t pid)
343 {
344 	errno = ENOSYS;
345 	return -1;
346 }
347 #endif
348 
349 #ifdef FFLUSH_NULL_BUG
350 #undef fflush
351 int _ssh_compat_fflush(FILE *f)
352 {
353 	int r1, r2;
354 
355 	if (f == NULL) {
356 		r1 = fflush(stdout);
357 		r2 = fflush(stderr);
358 		if (r1 == -1 || r2 == -1)
359 			return -1;
360 		return 0;
361 	}
362 	return fflush(f);
363 }
364 #endif
365