blob: 55f100ac04328624fee68e4763df1b864e2ea4e7 [file] [log] [blame]
Damien Miller36f49652004-08-15 18:40:59 +10001
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller98225c22004-02-17 16:49:41 +11003 * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org>
Damien Miller040f3832000-04-03 14:50:43 +10004 *
Damien Miller98225c22004-02-17 16:49:41 +11005 * 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 Miller040f3832000-04-03 14:50:43 +10008 *
Damien Miller98225c22004-02-17 16:49:41 +11009 * 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 Miller040f3832000-04-03 14:50:43 +100016 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100017
Damien Miller11fa2cc2000-08-16 10:35:58 +100018#include "includes.h"
Damien Miller6645e7a2006-03-15 14:42:54 +110019
Darren Tucker781e7a22007-04-29 12:06:55 +100020#include <sys/types.h>
Darren Tucker450d2af2006-08-24 19:45:33 +100021#ifdef HAVE_SYS_SELECT_H
22# include <sys/select.h>
23#endif
Darren Tuckere0869552006-08-24 19:43:16 +100024#ifdef HAVE_SYS_TIME_H
25# include <sys/time.h>
26#endif
27
Damien Millerb8fe89c2006-07-24 14:51:00 +100028#include <string.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110029#include <signal.h>
Darren Tuckere0869552006-08-24 19:43:16 +100030#include <stdlib.h>
Darren Tucker781e7a22007-04-29 12:06:55 +100031#include <unistd.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110032
Damien Millera8ed44b2003-01-10 09:53:12 +110033#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034
Darren Tucker03669a32004-08-13 18:37:21 +100035#ifndef HAVE___PROGNAME
Darren Tuckerba6de952004-07-17 14:07:42 +100036char *__progname;
37#endif
Damien Millere9cf3572001-02-09 12:55:35 +110038
Damien Millera8ed44b2003-01-10 09:53:12 +110039/*
40 * NB. duplicate __progname in case it is an alias for argv[0]
41 * Otherwise it may get clobbered by setproctitle()
42 */
Damien Miller59d3d5b2003-08-22 09:34:41 +100043char *ssh_get_progname(char *argv0)
Ben Lindstrom49a79c02000-11-17 03:47:20 +000044{
45#ifdef HAVE___PROGNAME
46 extern char *__progname;
47
Damien Millera8ed44b2003-01-10 09:53:12 +110048 return xstrdup(__progname);
Ben Lindstrom49a79c02000-11-17 03:47:20 +000049#else
50 char *p;
51
52 if (argv0 == NULL)
Damien Miller31741252003-05-19 00:13:38 +100053 return ("unknown"); /* XXX */
Ben Lindstrom49a79c02000-11-17 03:47:20 +000054 p = strrchr(argv0, '/');
55 if (p == NULL)
56 p = argv0;
57 else
58 p++;
Damien Millera8ed44b2003-01-10 09:53:12 +110059
Damien Miller31741252003-05-19 00:13:38 +100060 return (xstrdup(p));
Ben Lindstrom49a79c02000-11-17 03:47:20 +000061#endif
62}
63
Damien Millere72b7af1999-12-30 15:08:44 +110064#ifndef HAVE_SETLOGIN
65int setlogin(const char *name)
66{
Damien Miller31741252003-05-19 00:13:38 +100067 return (0);
Damien Millere72b7af1999-12-30 15:08:44 +110068}
69#endif /* !HAVE_SETLOGIN */
70
71#ifndef HAVE_INNETGR
72int innetgr(const char *netgroup, const char *host,
73 const char *user, const char *domain)
74{
Damien Miller31741252003-05-19 00:13:38 +100075 return (0);
Damien Millere72b7af1999-12-30 15:08:44 +110076}
77#endif /* HAVE_INNETGR */
78
79#if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
80int seteuid(uid_t euid)
81{
Damien Miller31741252003-05-19 00:13:38 +100082 return (setreuid(-1, euid));
Damien Millere72b7af1999-12-30 15:08:44 +110083}
84#endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */
Damien Millerecbb26d2000-07-15 14:59:14 +100085
Kevin Stevescb17e992001-04-09 14:50:52 +000086#if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID)
87int setegid(uid_t egid)
88{
Damien Miller31741252003-05-19 00:13:38 +100089 return(setresgid(-1, egid, -1));
Kevin Stevescb17e992001-04-09 14:50:52 +000090}
91#endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */
92
Damien Miller11fa2cc2000-08-16 10:35:58 +100093#if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR)
94const char *strerror(int e)
Damien Millerecbb26d2000-07-15 14:59:14 +100095{
Damien Miller11fa2cc2000-08-16 10:35:58 +100096 extern int sys_nerr;
97 extern char *sys_errlist[];
98
Ben Lindstrom46e55aa2001-03-13 23:38:20 +000099 if ((e >= 0) && (e < sys_nerr))
Damien Miller31741252003-05-19 00:13:38 +1000100 return (sys_errlist[e]);
101
102 return ("unlisted error");
Damien Millerecbb26d2000-07-15 14:59:14 +1000103}
Damien Miller11fa2cc2000-08-16 10:35:58 +1000104#endif
Ben Lindstrom42202bc2001-01-15 02:34:37 +0000105
106#ifndef HAVE_UTIMES
107int utimes(char *filename, struct timeval *tvp)
108{
109 struct utimbuf ub;
110
Ben Lindstrom100d5862002-07-08 21:09:41 +0000111 ub.actime = tvp[0].tv_sec;
112 ub.modtime = tvp[1].tv_sec;
Ben Lindstrom42202bc2001-01-15 02:34:37 +0000113
Damien Miller31741252003-05-19 00:13:38 +1000114 return (utime(filename, &ub));
Ben Lindstrom42202bc2001-01-15 02:34:37 +0000115}
116#endif
Tim Rice4bd2a192002-05-07 19:51:31 -0700117
118#ifndef HAVE_TRUNCATE
Damien Miller31741252003-05-19 00:13:38 +1000119int truncate(const char *path, off_t length)
Tim Rice4bd2a192002-05-07 19:51:31 -0700120{
121 int fd, ret, saverrno;
122
123 fd = open(path, O_WRONLY);
124 if (fd < 0)
Damien Miller31741252003-05-19 00:13:38 +1000125 return (-1);
Tim Rice4bd2a192002-05-07 19:51:31 -0700126
127 ret = ftruncate(fd, length);
128 saverrno = errno;
Damien Miller31741252003-05-19 00:13:38 +1000129 close(fd);
Tim Rice4bd2a192002-05-07 19:51:31 -0700130 if (ret == -1)
131 errno = saverrno;
Damien Miller31741252003-05-19 00:13:38 +1000132
Tim Rice4bd2a192002-05-07 19:51:31 -0700133 return(ret);
134}
135#endif /* HAVE_TRUNCATE */
136
Tim Rice4e4dc562003-03-18 10:21:40 -0800137#if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
138int nanosleep(const struct timespec *req, struct timespec *rem)
139{
140 int rc, saverrno;
141 extern int errno;
142 struct timeval tstart, tstop, tremain, time2wait;
143
144 TIMESPEC_TO_TIMEVAL(&time2wait, req)
145 (void) gettimeofday(&tstart, NULL);
146 rc = select(0, NULL, NULL, NULL, &time2wait);
147 if (rc == -1) {
148 saverrno = errno;
149 (void) gettimeofday (&tstop, NULL);
150 errno = saverrno;
151 tremain.tv_sec = time2wait.tv_sec -
152 (tstop.tv_sec - tstart.tv_sec);
153 tremain.tv_usec = time2wait.tv_usec -
154 (tstop.tv_usec - tstart.tv_usec);
155 tremain.tv_sec += tremain.tv_usec / 1000000L;
156 tremain.tv_usec %= 1000000L;
157 } else {
158 tremain.tv_sec = 0;
159 tremain.tv_usec = 0;
160 }
Darren Tucker1534fa42007-06-11 14:34:53 +1000161 if (rem != NULL)
162 TIMEVAL_TO_TIMESPEC(&tremain, rem)
Tim Rice4e4dc562003-03-18 10:21:40 -0800163
164 return(rc);
165}
Tim Rice4e4dc562003-03-18 10:21:40 -0800166#endif
167
Darren Tucker2e9c9cf2003-08-02 23:31:42 +1000168#ifndef HAVE_TCGETPGRP
169pid_t
170tcgetpgrp(int fd)
171{
Darren Tucker048737c2003-08-02 23:33:48 +1000172 int ctty_pgrp;
Darren Tucker2e9c9cf2003-08-02 23:31:42 +1000173
Darren Tuckerbdf571b2003-08-03 00:36:16 +1000174 if (ioctl(fd, TIOCGPGRP, &ctty_pgrp) == -1)
Darren Tucker2e9c9cf2003-08-02 23:31:42 +1000175 return(-1);
176 else
177 return(ctty_pgrp);
178}
179#endif /* HAVE_TCGETPGRP */
180
Darren Tuckerf38ea772003-08-13 20:48:07 +1000181#ifndef HAVE_TCSENDBREAK
182int
183tcsendbreak(int fd, int duration)
184{
185# if defined(TIOCSBRK) && defined(TIOCCBRK)
186 struct timeval sleepytime;
187
188 sleepytime.tv_sec = 0;
189 sleepytime.tv_usec = 400000;
190 if (ioctl(fd, TIOCSBRK, 0) == -1)
191 return (-1);
192 (void)select(0, 0, 0, 0, &sleepytime);
193 if (ioctl(fd, TIOCCBRK, 0) == -1)
194 return (-1);
195 return (0);
196# else
197 return -1;
198# endif
199}
200#endif /* HAVE_TCSENDBREAK */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000201
202mysig_t
203mysignal(int sig, mysig_t act)
204{
205#ifdef HAVE_SIGACTION
206 struct sigaction sa, osa;
207
208 if (sigaction(sig, NULL, &osa) == -1)
209 return (mysig_t) -1;
210 if (osa.sa_handler != act) {
211 memset(&sa, 0, sizeof(sa));
212 sigemptyset(&sa.sa_mask);
213 sa.sa_flags = 0;
214#ifdef SA_INTERRUPT
215 if (sig == SIGALRM)
216 sa.sa_flags |= SA_INTERRUPT;
217#endif
218 sa.sa_handler = act;
219 if (sigaction(sig, &sa, NULL) == -1)
220 return (mysig_t) -1;
221 }
222 return (osa.sa_handler);
223#else
Ben Lindstrom563eb992003-12-18 00:34:06 +0000224 #undef signal
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000225 return (signal(sig, act));
226#endif
227}
Darren Tucker2be1cbb2005-05-27 21:13:40 +1000228
229#ifndef HAVE_STRDUP
230char *
231strdup(const char *str)
232{
233 size_t len;
234 char *cp;
235
236 len = strlen(str) + 1;
237 cp = malloc(len);
238 if (cp != NULL)
Darren Tuckerd32e2932005-11-02 09:07:31 +1100239 return(memcpy(cp, str, len));
240 return NULL;
Darren Tucker2be1cbb2005-05-27 21:13:40 +1000241}
242#endif