1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/*++
/* NAME
/* sys_compat 3
/* SUMMARY
/* compatibility routines
/* SYNOPSIS
/* #include <sys_defs.h>
/*
/* const char *strerror(err)
/* int err;
/*
/* int setenv(name, value, clobber)
/* const char *name;
/* const char *value;
/* int clobber;
/*
/* int seteuid(euid)
/* uid_t euid;
/*
/* int setegid(egid)
/* gid_t euid;
/*
/* int mkfifo(path, mode)
/* char *path;
/* int mode;
/*
/* int waitpid(pid, statusp, options)
/* int pid;
/* WAIT_STATUS_T *statusp;
/* int options;
/*
/* int setsid()
/*
/* void dup2_pass_on_exec(int oldd, int newd)
/* DESCRIPTION
/* These routines are compiled for platforms that lack the functionality
/* or that have broken versions that we prefer to stay away from.
/* LICENSE
/* .ad
/* .fi
/* The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/* Wietse Venema
/* IBM T.J. Watson Research
/* P.O. Box 704
/* Yorktown Heights, NY 10598, USA
/*--*/
/* System library. */
#include "sys_defs.h"
/*
* ANSI strerror() emulation
*/
#ifdef MISSING_STRERROR
extern int errno;
extern char *sys_errlist[];
extern int sys_nerr;
#include <vstring.h>
/* strerror - print text corresponding to error */
const char *strerror(int err)
{
static VSTRING *buf;
if (err < 0 || err >= sys_nerr) {
if (buf == 0)
buf = vstring_alloc(10);
vstring_sprintf(buf, "Unknown error %d", err);
return (vstring_str(buf));
} else {
return (sys_errlist[errno]);
}
}
#endif
/*
* setenv() emulation on top of putenv().
*/
#ifdef MISSING_SETENV
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* setenv - update or insert environment (name,value) pair */
int setenv(const char *name, const char *value, int clobber)
{
char *cp;
if (clobber == 0 && getenv(name) != 0)
return (0);
if ((cp = malloc(strlen(name) + strlen(value) + 2)) == 0)
return (1);
sprintf(cp, "%s=%s", name, value);
return (putenv(cp));
}
#endif
/*
* seteuid() and setegid() emulation, the HP-UX way
*/
#ifdef MISSING_SETEUID
#ifdef HAVE_SETRESUID
#include <unistd.h>
int seteuid(uid_t euid)
{
return setresuid(-1, euid, -1);
}
#else
#error MISSING_SETEUID
#endif
#endif
#ifdef MISSING_SETEGID
#ifdef HAVE_SETRESGID
#include <unistd.h>
int setegid(gid_t egid)
{
return setresgid(-1, egid, -1);
}
#else
#error MISSING_SETEGID
#endif
#endif
/*
* mkfifo() emulation - requires superuser privileges
*/
#ifdef MISSING_MKFIFO
#include <sys/stat.h>
int mkfifo(char *path, int mode)
{
return mknod(path, (mode & ~_S_IFMT) | _S_IFIFO, 0);
}
#endif
/*
* waitpid() emulation on top of Berkeley UNIX wait4()
*/
#ifdef MISSING_WAITPID
#ifdef HAS_WAIT4
#include <sys/wait.h>
#include <errno.h>
int waitpid(int pid, WAIT_STATUS_T *status, int options)
{
if (pid == -1)
pid = 0;
return wait4(pid, status, options, (struct rusage *) 0);
}
#else
#error MISSING_WAITPID
#endif
#endif
/*
* setsid() emulation, the Berkeley UNIX way
*/
#ifdef MISSING_SETSID
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#ifdef TIOCNOTTY
#include <msg.h>
int setsid(void)
{
int p = getpid();
int fd;
if (setpgrp(p, p))
return -1;
fd = open("/dev/tty", O_RDONLY, 0);
if (fd >= 0 || errno != ENXIO) {
if (fd < 0) {
msg_warn("open /dev/tty: %m");
return -1;
}
if (ioctl(fd, TIOCNOTTY, 0)) {
msg_warn("ioctl TIOCNOTTY: %m");
return -1;
}
close(fd);
}
return 0;
}
#else
#error MISSING_SETSID
#endif
#endif
/*
* dup2_pass_on_exec() - dup2() and clear close-on-exec flag on the result
*/
#ifdef DUP2_DUPS_CLOSE_ON_EXEC
#include "iostuff.h"
int dup2_pass_on_exec(int oldd, int newd)
{
int res;
if ((res = dup2(oldd, newd)) >= 0)
close_on_exec(newd, PASS_ON_EXEC);
return res;
}
#endif
|