Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 2 | * Copyright (c) 1999-2003 Damien Miller. All rights reserved. |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 24 | |
Damien Miller | 11fa2cc | 2000-08-16 10:35:58 +1000 | [diff] [blame] | 25 | #include "includes.h" |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 26 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 27 | |
Ben Lindstrom | 5ade9ab | 2003-08-25 01:16:21 +0000 | [diff] [blame^] | 28 | RCSID("$Id: bsd-misc.c,v 1.19 2003/08/25 01:16:21 mouring Exp $"); |
Damien Miller | e9cf357 | 2001-02-09 12:55:35 +1100 | [diff] [blame] | 29 | |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 30 | /* |
| 31 | * NB. duplicate __progname in case it is an alias for argv[0] |
| 32 | * Otherwise it may get clobbered by setproctitle() |
| 33 | */ |
Damien Miller | 59d3d5b | 2003-08-22 09:34:41 +1000 | [diff] [blame] | 34 | char *ssh_get_progname(char *argv0) |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 35 | { |
| 36 | #ifdef HAVE___PROGNAME |
| 37 | extern char *__progname; |
| 38 | |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 39 | return xstrdup(__progname); |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 40 | #else |
| 41 | char *p; |
| 42 | |
| 43 | if (argv0 == NULL) |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 44 | return ("unknown"); /* XXX */ |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 45 | p = strrchr(argv0, '/'); |
| 46 | if (p == NULL) |
| 47 | p = argv0; |
| 48 | else |
| 49 | p++; |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 50 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 51 | return (xstrdup(p)); |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 52 | #endif |
| 53 | } |
| 54 | |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 55 | #ifndef HAVE_SETLOGIN |
| 56 | int setlogin(const char *name) |
| 57 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 58 | return (0); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 59 | } |
| 60 | #endif /* !HAVE_SETLOGIN */ |
| 61 | |
| 62 | #ifndef HAVE_INNETGR |
| 63 | int innetgr(const char *netgroup, const char *host, |
| 64 | const char *user, const char *domain) |
| 65 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 66 | return (0); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 67 | } |
| 68 | #endif /* HAVE_INNETGR */ |
| 69 | |
| 70 | #if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) |
| 71 | int seteuid(uid_t euid) |
| 72 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 73 | return (setreuid(-1, euid)); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 74 | } |
| 75 | #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */ |
Damien Miller | ecbb26d | 2000-07-15 14:59:14 +1000 | [diff] [blame] | 76 | |
Kevin Steves | cb17e99 | 2001-04-09 14:50:52 +0000 | [diff] [blame] | 77 | #if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) |
| 78 | int setegid(uid_t egid) |
| 79 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 80 | return(setresgid(-1, egid, -1)); |
Kevin Steves | cb17e99 | 2001-04-09 14:50:52 +0000 | [diff] [blame] | 81 | } |
| 82 | #endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */ |
| 83 | |
Damien Miller | 11fa2cc | 2000-08-16 10:35:58 +1000 | [diff] [blame] | 84 | #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR) |
| 85 | const char *strerror(int e) |
Damien Miller | ecbb26d | 2000-07-15 14:59:14 +1000 | [diff] [blame] | 86 | { |
Damien Miller | 11fa2cc | 2000-08-16 10:35:58 +1000 | [diff] [blame] | 87 | extern int sys_nerr; |
| 88 | extern char *sys_errlist[]; |
| 89 | |
Ben Lindstrom | 46e55aa | 2001-03-13 23:38:20 +0000 | [diff] [blame] | 90 | if ((e >= 0) && (e < sys_nerr)) |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 91 | return (sys_errlist[e]); |
| 92 | |
| 93 | return ("unlisted error"); |
Damien Miller | ecbb26d | 2000-07-15 14:59:14 +1000 | [diff] [blame] | 94 | } |
Damien Miller | 11fa2cc | 2000-08-16 10:35:58 +1000 | [diff] [blame] | 95 | #endif |
Ben Lindstrom | 42202bc | 2001-01-15 02:34:37 +0000 | [diff] [blame] | 96 | |
| 97 | #ifndef HAVE_UTIMES |
| 98 | int utimes(char *filename, struct timeval *tvp) |
| 99 | { |
| 100 | struct utimbuf ub; |
| 101 | |
Ben Lindstrom | 100d586 | 2002-07-08 21:09:41 +0000 | [diff] [blame] | 102 | ub.actime = tvp[0].tv_sec; |
| 103 | ub.modtime = tvp[1].tv_sec; |
Ben Lindstrom | 42202bc | 2001-01-15 02:34:37 +0000 | [diff] [blame] | 104 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 105 | return (utime(filename, &ub)); |
Ben Lindstrom | 42202bc | 2001-01-15 02:34:37 +0000 | [diff] [blame] | 106 | } |
| 107 | #endif |
Tim Rice | 4bd2a19 | 2002-05-07 19:51:31 -0700 | [diff] [blame] | 108 | |
| 109 | #ifndef HAVE_TRUNCATE |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 110 | int truncate(const char *path, off_t length) |
Tim Rice | 4bd2a19 | 2002-05-07 19:51:31 -0700 | [diff] [blame] | 111 | { |
| 112 | int fd, ret, saverrno; |
| 113 | |
| 114 | fd = open(path, O_WRONLY); |
| 115 | if (fd < 0) |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 116 | return (-1); |
Tim Rice | 4bd2a19 | 2002-05-07 19:51:31 -0700 | [diff] [blame] | 117 | |
| 118 | ret = ftruncate(fd, length); |
| 119 | saverrno = errno; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 120 | close(fd); |
Tim Rice | 4bd2a19 | 2002-05-07 19:51:31 -0700 | [diff] [blame] | 121 | if (ret == -1) |
| 122 | errno = saverrno; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 123 | |
Tim Rice | 4bd2a19 | 2002-05-07 19:51:31 -0700 | [diff] [blame] | 124 | return(ret); |
| 125 | } |
| 126 | #endif /* HAVE_TRUNCATE */ |
| 127 | |
Ben Lindstrom | 837461b | 2002-06-12 16:57:14 +0000 | [diff] [blame] | 128 | #if !defined(HAVE_SETGROUPS) && defined(SETGROUPS_NOOP) |
| 129 | /* |
| 130 | * Cygwin setgroups should be a noop. |
| 131 | */ |
| 132 | int |
Ben Lindstrom | 0e23ebc | 2002-06-13 21:34:57 +0000 | [diff] [blame] | 133 | setgroups(size_t size, const gid_t *list) |
Ben Lindstrom | 837461b | 2002-06-12 16:57:14 +0000 | [diff] [blame] | 134 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 135 | return (0); |
Ben Lindstrom | 837461b | 2002-06-12 16:57:14 +0000 | [diff] [blame] | 136 | } |
| 137 | #endif |
| 138 | |
Tim Rice | 4e4dc56 | 2003-03-18 10:21:40 -0800 | [diff] [blame] | 139 | #if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP) |
| 140 | int nanosleep(const struct timespec *req, struct timespec *rem) |
| 141 | { |
| 142 | int rc, saverrno; |
| 143 | extern int errno; |
| 144 | struct timeval tstart, tstop, tremain, time2wait; |
| 145 | |
| 146 | TIMESPEC_TO_TIMEVAL(&time2wait, req) |
| 147 | (void) gettimeofday(&tstart, NULL); |
| 148 | rc = select(0, NULL, NULL, NULL, &time2wait); |
| 149 | if (rc == -1) { |
| 150 | saverrno = errno; |
| 151 | (void) gettimeofday (&tstop, NULL); |
| 152 | errno = saverrno; |
| 153 | tremain.tv_sec = time2wait.tv_sec - |
| 154 | (tstop.tv_sec - tstart.tv_sec); |
| 155 | tremain.tv_usec = time2wait.tv_usec - |
| 156 | (tstop.tv_usec - tstart.tv_usec); |
| 157 | tremain.tv_sec += tremain.tv_usec / 1000000L; |
| 158 | tremain.tv_usec %= 1000000L; |
| 159 | } else { |
| 160 | tremain.tv_sec = 0; |
| 161 | tremain.tv_usec = 0; |
| 162 | } |
| 163 | TIMEVAL_TO_TIMESPEC(&tremain, rem) |
| 164 | |
| 165 | return(rc); |
| 166 | } |
| 167 | |
| 168 | #endif |
| 169 | |
Darren Tucker | 2e9c9cf | 2003-08-02 23:31:42 +1000 | [diff] [blame] | 170 | #ifndef HAVE_TCGETPGRP |
| 171 | pid_t |
| 172 | tcgetpgrp(int fd) |
| 173 | { |
Darren Tucker | 048737c | 2003-08-02 23:33:48 +1000 | [diff] [blame] | 174 | int ctty_pgrp; |
Darren Tucker | 2e9c9cf | 2003-08-02 23:31:42 +1000 | [diff] [blame] | 175 | |
Darren Tucker | bdf571b | 2003-08-03 00:36:16 +1000 | [diff] [blame] | 176 | if (ioctl(fd, TIOCGPGRP, &ctty_pgrp) == -1) |
Darren Tucker | 2e9c9cf | 2003-08-02 23:31:42 +1000 | [diff] [blame] | 177 | return(-1); |
| 178 | else |
| 179 | return(ctty_pgrp); |
| 180 | } |
| 181 | #endif /* HAVE_TCGETPGRP */ |
| 182 | |
Darren Tucker | f38ea77 | 2003-08-13 20:48:07 +1000 | [diff] [blame] | 183 | #ifndef HAVE_TCSENDBREAK |
| 184 | int |
| 185 | tcsendbreak(int fd, int duration) |
| 186 | { |
| 187 | # if defined(TIOCSBRK) && defined(TIOCCBRK) |
| 188 | struct timeval sleepytime; |
| 189 | |
| 190 | sleepytime.tv_sec = 0; |
| 191 | sleepytime.tv_usec = 400000; |
| 192 | if (ioctl(fd, TIOCSBRK, 0) == -1) |
| 193 | return (-1); |
| 194 | (void)select(0, 0, 0, 0, &sleepytime); |
| 195 | if (ioctl(fd, TIOCCBRK, 0) == -1) |
| 196 | return (-1); |
| 197 | return (0); |
| 198 | # else |
| 199 | return -1; |
| 200 | # endif |
| 201 | } |
| 202 | #endif /* HAVE_TCSENDBREAK */ |
Ben Lindstrom | 5ade9ab | 2003-08-25 01:16:21 +0000 | [diff] [blame^] | 203 | |
| 204 | mysig_t |
| 205 | mysignal(int sig, mysig_t act) |
| 206 | { |
| 207 | #ifdef HAVE_SIGACTION |
| 208 | struct sigaction sa, osa; |
| 209 | |
| 210 | if (sigaction(sig, NULL, &osa) == -1) |
| 211 | return (mysig_t) -1; |
| 212 | if (osa.sa_handler != act) { |
| 213 | memset(&sa, 0, sizeof(sa)); |
| 214 | sigemptyset(&sa.sa_mask); |
| 215 | sa.sa_flags = 0; |
| 216 | #ifdef SA_INTERRUPT |
| 217 | if (sig == SIGALRM) |
| 218 | sa.sa_flags |= SA_INTERRUPT; |
| 219 | #endif |
| 220 | sa.sa_handler = act; |
| 221 | if (sigaction(sig, &sa, NULL) == -1) |
| 222 | return (mysig_t) -1; |
| 223 | } |
| 224 | return (osa.sa_handler); |
| 225 | #else |
| 226 | return (signal(sig, act)); |
| 227 | #endif |
| 228 | } |