Damien Miller | 36f4965 | 2004-08-15 18:40:59 +1000 | [diff] [blame] | 1 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 98225c2 | 2004-02-17 16:49:41 +1100 | [diff] [blame] | 3 | * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org> |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 4 | * |
Damien Miller | 98225c2 | 2004-02-17 16:49:41 +1100 | [diff] [blame] | 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. |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 8 | * |
Damien Miller | 98225c2 | 2004-02-17 16:49:41 +1100 | [diff] [blame] | 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. |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 16 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 17 | |
Damien Miller | 11fa2cc | 2000-08-16 10:35:58 +1000 | [diff] [blame] | 18 | #include "includes.h" |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 19 | |
Darren Tucker | 781e7a2 | 2007-04-29 12:06:55 +1000 | [diff] [blame] | 20 | #include <sys/types.h> |
Darren Tucker | 450d2af | 2006-08-24 19:45:33 +1000 | [diff] [blame] | 21 | #ifdef HAVE_SYS_SELECT_H |
| 22 | # include <sys/select.h> |
| 23 | #endif |
Darren Tucker | e086955 | 2006-08-24 19:43:16 +1000 | [diff] [blame] | 24 | #ifdef HAVE_SYS_TIME_H |
| 25 | # include <sys/time.h> |
| 26 | #endif |
| 27 | |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 28 | #include <string.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 29 | #include <signal.h> |
Darren Tucker | e086955 | 2006-08-24 19:43:16 +1000 | [diff] [blame] | 30 | #include <stdlib.h> |
Darren Tucker | d527704 | 2013-11-03 16:30:46 +1100 | [diff] [blame] | 31 | #include <time.h> |
Darren Tucker | 781e7a2 | 2007-04-29 12:06:55 +1000 | [diff] [blame] | 32 | #include <unistd.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 33 | |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 34 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 35 | |
Darren Tucker | 03669a3 | 2004-08-13 18:37:21 +1000 | [diff] [blame] | 36 | #ifndef HAVE___PROGNAME |
Darren Tucker | ba6de95 | 2004-07-17 14:07:42 +1000 | [diff] [blame] | 37 | char *__progname; |
| 38 | #endif |
Damien Miller | e9cf357 | 2001-02-09 12:55:35 +1100 | [diff] [blame] | 39 | |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 40 | /* |
| 41 | * NB. duplicate __progname in case it is an alias for argv[0] |
| 42 | * Otherwise it may get clobbered by setproctitle() |
| 43 | */ |
Damien Miller | 59d3d5b | 2003-08-22 09:34:41 +1000 | [diff] [blame] | 44 | char *ssh_get_progname(char *argv0) |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 45 | { |
| 46 | #ifdef HAVE___PROGNAME |
| 47 | extern char *__progname; |
| 48 | |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 49 | return xstrdup(__progname); |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 50 | #else |
| 51 | char *p; |
| 52 | |
| 53 | if (argv0 == NULL) |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 54 | return ("unknown"); /* XXX */ |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 55 | p = strrchr(argv0, '/'); |
| 56 | if (p == NULL) |
| 57 | p = argv0; |
| 58 | else |
| 59 | p++; |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 60 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 61 | return (xstrdup(p)); |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 62 | #endif |
| 63 | } |
| 64 | |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 65 | #ifndef HAVE_SETLOGIN |
| 66 | int setlogin(const char *name) |
| 67 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 68 | return (0); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 69 | } |
| 70 | #endif /* !HAVE_SETLOGIN */ |
| 71 | |
| 72 | #ifndef HAVE_INNETGR |
| 73 | int innetgr(const char *netgroup, const char *host, |
| 74 | const char *user, const char *domain) |
| 75 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 76 | return (0); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 77 | } |
| 78 | #endif /* HAVE_INNETGR */ |
| 79 | |
| 80 | #if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) |
| 81 | int seteuid(uid_t euid) |
| 82 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 83 | return (setreuid(-1, euid)); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 84 | } |
| 85 | #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */ |
Damien Miller | ecbb26d | 2000-07-15 14:59:14 +1000 | [diff] [blame] | 86 | |
Kevin Steves | cb17e99 | 2001-04-09 14:50:52 +0000 | [diff] [blame] | 87 | #if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) |
| 88 | int setegid(uid_t egid) |
| 89 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 90 | return(setresgid(-1, egid, -1)); |
Kevin Steves | cb17e99 | 2001-04-09 14:50:52 +0000 | [diff] [blame] | 91 | } |
| 92 | #endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */ |
| 93 | |
Damien Miller | 11fa2cc | 2000-08-16 10:35:58 +1000 | [diff] [blame] | 94 | #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR) |
| 95 | const char *strerror(int e) |
Damien Miller | ecbb26d | 2000-07-15 14:59:14 +1000 | [diff] [blame] | 96 | { |
Damien Miller | 11fa2cc | 2000-08-16 10:35:58 +1000 | [diff] [blame] | 97 | extern int sys_nerr; |
| 98 | extern char *sys_errlist[]; |
| 99 | |
Ben Lindstrom | 46e55aa | 2001-03-13 23:38:20 +0000 | [diff] [blame] | 100 | if ((e >= 0) && (e < sys_nerr)) |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 101 | return (sys_errlist[e]); |
| 102 | |
| 103 | return ("unlisted error"); |
Damien Miller | ecbb26d | 2000-07-15 14:59:14 +1000 | [diff] [blame] | 104 | } |
Damien Miller | 11fa2cc | 2000-08-16 10:35:58 +1000 | [diff] [blame] | 105 | #endif |
Ben Lindstrom | 42202bc | 2001-01-15 02:34:37 +0000 | [diff] [blame] | 106 | |
| 107 | #ifndef HAVE_UTIMES |
| 108 | int utimes(char *filename, struct timeval *tvp) |
| 109 | { |
| 110 | struct utimbuf ub; |
| 111 | |
Ben Lindstrom | 100d586 | 2002-07-08 21:09:41 +0000 | [diff] [blame] | 112 | ub.actime = tvp[0].tv_sec; |
| 113 | ub.modtime = tvp[1].tv_sec; |
Ben Lindstrom | 42202bc | 2001-01-15 02:34:37 +0000 | [diff] [blame] | 114 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 115 | return (utime(filename, &ub)); |
Ben Lindstrom | 42202bc | 2001-01-15 02:34:37 +0000 | [diff] [blame] | 116 | } |
| 117 | #endif |
Tim Rice | 4bd2a19 | 2002-05-07 19:51:31 -0700 | [diff] [blame] | 118 | |
| 119 | #ifndef HAVE_TRUNCATE |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 120 | int truncate(const char *path, off_t length) |
Tim Rice | 4bd2a19 | 2002-05-07 19:51:31 -0700 | [diff] [blame] | 121 | { |
| 122 | int fd, ret, saverrno; |
| 123 | |
| 124 | fd = open(path, O_WRONLY); |
| 125 | if (fd < 0) |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 126 | return (-1); |
Tim Rice | 4bd2a19 | 2002-05-07 19:51:31 -0700 | [diff] [blame] | 127 | |
| 128 | ret = ftruncate(fd, length); |
| 129 | saverrno = errno; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 130 | close(fd); |
Tim Rice | 4bd2a19 | 2002-05-07 19:51:31 -0700 | [diff] [blame] | 131 | if (ret == -1) |
| 132 | errno = saverrno; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 133 | |
Tim Rice | 4bd2a19 | 2002-05-07 19:51:31 -0700 | [diff] [blame] | 134 | return(ret); |
| 135 | } |
| 136 | #endif /* HAVE_TRUNCATE */ |
| 137 | |
Tim Rice | 4e4dc56 | 2003-03-18 10:21:40 -0800 | [diff] [blame] | 138 | #if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP) |
| 139 | int nanosleep(const struct timespec *req, struct timespec *rem) |
| 140 | { |
| 141 | int rc, saverrno; |
| 142 | extern int errno; |
| 143 | struct timeval tstart, tstop, tremain, time2wait; |
| 144 | |
| 145 | TIMESPEC_TO_TIMEVAL(&time2wait, req) |
| 146 | (void) gettimeofday(&tstart, NULL); |
| 147 | rc = select(0, NULL, NULL, NULL, &time2wait); |
| 148 | if (rc == -1) { |
| 149 | saverrno = errno; |
| 150 | (void) gettimeofday (&tstop, NULL); |
| 151 | errno = saverrno; |
| 152 | tremain.tv_sec = time2wait.tv_sec - |
| 153 | (tstop.tv_sec - tstart.tv_sec); |
| 154 | tremain.tv_usec = time2wait.tv_usec - |
| 155 | (tstop.tv_usec - tstart.tv_usec); |
| 156 | tremain.tv_sec += tremain.tv_usec / 1000000L; |
| 157 | tremain.tv_usec %= 1000000L; |
| 158 | } else { |
| 159 | tremain.tv_sec = 0; |
| 160 | tremain.tv_usec = 0; |
| 161 | } |
Darren Tucker | 1534fa4 | 2007-06-11 14:34:53 +1000 | [diff] [blame] | 162 | if (rem != NULL) |
| 163 | TIMEVAL_TO_TIMESPEC(&tremain, rem) |
Tim Rice | 4e4dc56 | 2003-03-18 10:21:40 -0800 | [diff] [blame] | 164 | |
| 165 | return(rc); |
| 166 | } |
Tim Rice | 4e4dc56 | 2003-03-18 10:21:40 -0800 | [diff] [blame] | 167 | #endif |
| 168 | |
Damien Miller | f4db77d | 2013-03-15 10:34:25 +1100 | [diff] [blame] | 169 | #if !defined(HAVE_USLEEP) |
| 170 | int usleep(unsigned int useconds) |
| 171 | { |
| 172 | struct timespec ts; |
| 173 | |
| 174 | ts.tv_sec = useconds / 1000000; |
| 175 | ts.tv_nsec = (useconds % 1000000) * 1000; |
| 176 | return nanosleep(&ts, NULL); |
| 177 | } |
| 178 | #endif |
| 179 | |
Darren Tucker | 2e9c9cf | 2003-08-02 23:31:42 +1000 | [diff] [blame] | 180 | #ifndef HAVE_TCGETPGRP |
| 181 | pid_t |
| 182 | tcgetpgrp(int fd) |
| 183 | { |
Darren Tucker | 048737c | 2003-08-02 23:33:48 +1000 | [diff] [blame] | 184 | int ctty_pgrp; |
Darren Tucker | 2e9c9cf | 2003-08-02 23:31:42 +1000 | [diff] [blame] | 185 | |
Darren Tucker | bdf571b | 2003-08-03 00:36:16 +1000 | [diff] [blame] | 186 | if (ioctl(fd, TIOCGPGRP, &ctty_pgrp) == -1) |
Darren Tucker | 2e9c9cf | 2003-08-02 23:31:42 +1000 | [diff] [blame] | 187 | return(-1); |
| 188 | else |
| 189 | return(ctty_pgrp); |
| 190 | } |
| 191 | #endif /* HAVE_TCGETPGRP */ |
| 192 | |
Darren Tucker | f38ea77 | 2003-08-13 20:48:07 +1000 | [diff] [blame] | 193 | #ifndef HAVE_TCSENDBREAK |
| 194 | int |
| 195 | tcsendbreak(int fd, int duration) |
| 196 | { |
| 197 | # if defined(TIOCSBRK) && defined(TIOCCBRK) |
| 198 | struct timeval sleepytime; |
| 199 | |
| 200 | sleepytime.tv_sec = 0; |
| 201 | sleepytime.tv_usec = 400000; |
| 202 | if (ioctl(fd, TIOCSBRK, 0) == -1) |
| 203 | return (-1); |
| 204 | (void)select(0, 0, 0, 0, &sleepytime); |
| 205 | if (ioctl(fd, TIOCCBRK, 0) == -1) |
| 206 | return (-1); |
| 207 | return (0); |
| 208 | # else |
| 209 | return -1; |
| 210 | # endif |
| 211 | } |
| 212 | #endif /* HAVE_TCSENDBREAK */ |
Ben Lindstrom | 5ade9ab | 2003-08-25 01:16:21 +0000 | [diff] [blame] | 213 | |
| 214 | mysig_t |
| 215 | mysignal(int sig, mysig_t act) |
| 216 | { |
| 217 | #ifdef HAVE_SIGACTION |
| 218 | struct sigaction sa, osa; |
| 219 | |
| 220 | if (sigaction(sig, NULL, &osa) == -1) |
| 221 | return (mysig_t) -1; |
| 222 | if (osa.sa_handler != act) { |
| 223 | memset(&sa, 0, sizeof(sa)); |
| 224 | sigemptyset(&sa.sa_mask); |
| 225 | sa.sa_flags = 0; |
| 226 | #ifdef SA_INTERRUPT |
| 227 | if (sig == SIGALRM) |
| 228 | sa.sa_flags |= SA_INTERRUPT; |
| 229 | #endif |
| 230 | sa.sa_handler = act; |
| 231 | if (sigaction(sig, &sa, NULL) == -1) |
| 232 | return (mysig_t) -1; |
| 233 | } |
| 234 | return (osa.sa_handler); |
| 235 | #else |
Ben Lindstrom | 563eb99 | 2003-12-18 00:34:06 +0000 | [diff] [blame] | 236 | #undef signal |
Ben Lindstrom | 5ade9ab | 2003-08-25 01:16:21 +0000 | [diff] [blame] | 237 | return (signal(sig, act)); |
| 238 | #endif |
| 239 | } |
Darren Tucker | 2be1cbb | 2005-05-27 21:13:40 +1000 | [diff] [blame] | 240 | |
| 241 | #ifndef HAVE_STRDUP |
| 242 | char * |
| 243 | strdup(const char *str) |
| 244 | { |
| 245 | size_t len; |
| 246 | char *cp; |
| 247 | |
| 248 | len = strlen(str) + 1; |
| 249 | cp = malloc(len); |
| 250 | if (cp != NULL) |
Darren Tucker | d32e293 | 2005-11-02 09:07:31 +1100 | [diff] [blame] | 251 | return(memcpy(cp, str, len)); |
| 252 | return NULL; |
Darren Tucker | 2be1cbb | 2005-05-27 21:13:40 +1000 | [diff] [blame] | 253 | } |
| 254 | #endif |
Tim Rice | c7a8af0 | 2010-11-08 14:26:23 -0800 | [diff] [blame] | 255 | |
| 256 | #ifndef HAVE_ISBLANK |
Darren Tucker | 62e4edc | 2013-02-15 11:50:03 +1100 | [diff] [blame] | 257 | int |
| 258 | isblank(int c) |
Tim Rice | c7a8af0 | 2010-11-08 14:26:23 -0800 | [diff] [blame] | 259 | { |
| 260 | return (c == ' ' || c == '\t'); |
| 261 | } |
| 262 | #endif |
Darren Tucker | 3c4a24c | 2013-02-15 11:41:35 +1100 | [diff] [blame] | 263 | |
| 264 | #ifndef HAVE_GETPGID |
| 265 | pid_t |
| 266 | getpgid(pid_t pid) |
| 267 | { |
Darren Tucker | 2991d28 | 2013-02-15 14:55:38 +1100 | [diff] [blame] | 268 | #if defined(HAVE_GETPGRP) && !defined(GETPGRP_VOID) |
| 269 | return getpgrp(pid); |
| 270 | #elif defined(HAVE_GETPGRP) |
Darren Tucker | 3c4a24c | 2013-02-15 11:41:35 +1100 | [diff] [blame] | 271 | if (pid == 0) |
| 272 | return getpgrp(); |
| 273 | #endif |
Darren Tucker | 2991d28 | 2013-02-15 14:55:38 +1100 | [diff] [blame] | 274 | |
Darren Tucker | 3c4a24c | 2013-02-15 11:41:35 +1100 | [diff] [blame] | 275 | errno = ESRCH; |
| 276 | return -1; |
| 277 | } |
| 278 | #endif |