summaryrefslogtreecommitdiff
path: root/openbsd-compat
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat')
-rw-r--r--openbsd-compat/Makefile.in81
-rw-r--r--openbsd-compat/bsd-cray.c816
-rw-r--r--openbsd-compat/bsd-cray.h59
-rw-r--r--openbsd-compat/bsd-flock.c81
-rw-r--r--openbsd-compat/bsd-getpagesize.c2
-rw-r--r--openbsd-compat/bsd-malloc.c2
-rw-r--r--openbsd-compat/bsd-misc.c81
-rw-r--r--openbsd-compat/bsd-misc.h34
-rw-r--r--openbsd-compat/bsd-openpty.c25
-rw-r--r--openbsd-compat/bsd-signal.c62
-rw-r--r--openbsd-compat/bsd-signal.h39
-rw-r--r--openbsd-compat/bsd-statvfs.c8
-rw-r--r--openbsd-compat/bsd-statvfs.h3
-rw-r--r--openbsd-compat/freezero.c4
-rw-r--r--openbsd-compat/openbsd-compat.h14
-rw-r--r--openbsd-compat/port-aix.c4
-rw-r--r--openbsd-compat/port-linux.c9
-rw-r--r--openbsd-compat/port-net.c (renamed from openbsd-compat/port-tun.c)101
-rw-r--r--openbsd-compat/port-net.h (renamed from openbsd-compat/port-tun.h)16
-rw-r--r--openbsd-compat/port-uw.c4
-rw-r--r--openbsd-compat/readpassphrase.c8
-rw-r--r--openbsd-compat/regress/Makefile.in2
-rw-r--r--openbsd-compat/strndup.c43
-rw-r--r--openbsd-compat/strnlen.c2
24 files changed, 524 insertions, 976 deletions
diff --git a/openbsd-compat/Makefile.in b/openbsd-compat/Makefile.in
index ac8ae4305..8e3b42991 100644
--- a/openbsd-compat/Makefile.in
+++ b/openbsd-compat/Makefile.in
@@ -1,5 +1,3 @@
1# $Id: Makefile.in,v 1.56 2014/09/30 23:43:08 djm Exp $
2
3sysconfdir=@sysconfdir@ 1sysconfdir=@sysconfdir@
4piddir=@piddir@ 2piddir=@piddir@
5srcdir=@srcdir@ 3srcdir=@srcdir@
@@ -16,11 +14,84 @@ RANLIB=@RANLIB@
16INSTALL=@INSTALL@ 14INSTALL=@INSTALL@
17LDFLAGS=-L. @LDFLAGS@ 15LDFLAGS=-L. @LDFLAGS@
18 16
19OPENBSD=base64.o basename.o bcrypt_pbkdf.o bindresvport.o blowfish.o daemon.o dirname.o fmt_scaled.o getcwd.o getgrouplist.o getopt_long.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o pwcache.o readpassphrase.o reallocarray.o realpath.o recallocarray.o rresvport.o setenv.o setproctitle.o sha1.o sha2.o rmd160.o md5.o sigact.o strcasestr.o strlcat.o strlcpy.o strmode.o strnlen.o strptime.o strsep.o strtonum.o strtoll.o strtoul.o strtoull.o timingsafe_bcmp.o vis.o blowfish.o bcrypt_pbkdf.o explicit_bzero.o freezero.o 17OPENBSD=base64.o \
18 basename.o \
19 bcrypt_pbkdf.o \
20 bcrypt_pbkdf.o \
21 bindresvport.o \
22 blowfish.o \
23 daemon.o \
24 dirname.o \
25 explicit_bzero.o \
26 fmt_scaled.o \
27 freezero.o \
28 getcwd.o \
29 getgrouplist.o \
30 getopt_long.o \
31 getrrsetbyname.o \
32 glob.o \
33 inet_aton.o \
34 inet_ntoa.o \
35 inet_ntop.o \
36 md5.o \
37 mktemp.o \
38 pwcache.o \
39 readpassphrase.o \
40 reallocarray.o \
41 realpath.o \
42 recallocarray.o \
43 rmd160.o \
44 rresvport.o \
45 setenv.o \
46 setproctitle.o \
47 sha1.o \
48 sha2.o \
49 sigact.o \
50 strcasestr.o \
51 strlcat.o \
52 strlcpy.o \
53 strmode.o \
54 strndup.o \
55 strnlen.o \
56 strptime.o \
57 strsep.o \
58 strtoll.o \
59 strtonum.o \
60 strtoull.o \
61 strtoul.o \
62 timingsafe_bcmp.o \
63 vis.o
20 64
21COMPAT=arc4random.o bsd-asprintf.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o getrrsetbyname-ldns.o bsd-err.o bsd-getpagesize.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-poll.o bsd-malloc.o bsd-setres_id.o bsd-snprintf.o bsd-statvfs.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xcrypt.o kludge-fd_set.o 65COMPAT= arc4random.o \
66 bsd-asprintf.o \
67 bsd-closefrom.o \
68 bsd-cygwin_util.o \
69 bsd-err.o \
70 bsd-flock.o \
71 bsd-getpagesize.o \
72 bsd-getpeereid.o \
73 bsd-malloc.o \
74 bsd-misc.o \
75 bsd-nextstep.o \
76 bsd-openpty.o \
77 bsd-poll.o \
78 bsd-setres_id.o \
79 bsd-signal.o \
80 bsd-snprintf.o \
81 bsd-statvfs.o \
82 bsd-waitpid.o \
83 fake-rfc2553.o \
84 getrrsetbyname-ldns.o \
85 kludge-fd_set.o \
86 openssl-compat.o \
87 xcrypt.o
22 88
23PORTS=port-aix.o port-irix.o port-linux.o port-solaris.o port-tun.o port-uw.o 89PORTS= port-aix.o \
90 port-irix.o \
91 port-linux.o \
92 port-solaris.o \
93 port-net.o \
94 port-uw.o
24 95
25.c.o: 96.c.o:
26 $(CC) $(CFLAGS) $(CPPFLAGS) -c $< 97 $(CC) $(CFLAGS) $(CPPFLAGS) -c $<
diff --git a/openbsd-compat/bsd-cray.c b/openbsd-compat/bsd-cray.c
deleted file mode 100644
index c02e63261..000000000
--- a/openbsd-compat/bsd-cray.c
+++ /dev/null
@@ -1,816 +0,0 @@
1/*
2 *
3 * bsd-cray.c
4 *
5 * Copyright (c) 2002, Cray Inc. (Wendy Palm <wendyp@cray.com>)
6 * Significant portions provided by
7 * Wayne Schroeder, SDSC <schroeder@sdsc.edu>
8 * William Jones, UTexas <jones@tacc.utexas.edu>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Created: Apr 22 16.34:00 2002 wp
31 *
32 * This file contains functions required for proper execution
33 * on UNICOS systems.
34 *
35 */
36#ifdef _UNICOS
37
38#include <udb.h>
39#include <tmpdir.h>
40#include <unistd.h>
41#include <sys/category.h>
42#include <utmp.h>
43#include <sys/jtab.h>
44#include <signal.h>
45#include <sys/priv.h>
46#include <sys/secparm.h>
47#include <sys/tfm.h>
48#include <sys/usrv.h>
49#include <sys/sysv.h>
50#include <sys/sectab.h>
51#include <sys/secstat.h>
52#include <sys/stat.h>
53#include <sys/session.h>
54#include <stdarg.h>
55#include <stdlib.h>
56#include <string.h>
57#include <unistd.h>
58#include <pwd.h>
59#include <fcntl.h>
60#include <errno.h>
61#include <ia.h>
62#include <urm.h>
63#include "ssh.h"
64
65#include "includes.h"
66#include "sys/types.h"
67
68#ifndef HAVE_STRUCT_SOCKADDR_STORAGE
69# define _SS_MAXSIZE 128 /* Implementation specific max size */
70# define _SS_PADSIZE (_SS_MAXSIZE - sizeof (struct sockaddr))
71
72# define ss_family ss_sa.sa_family
73#endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */
74
75#ifndef IN6_IS_ADDR_LOOPBACK
76# define IN6_IS_ADDR_LOOPBACK(a) \
77 (((u_int32_t *) (a))[0] == 0 && ((u_int32_t *) (a))[1] == 0 && \
78 ((u_int32_t *) (a))[2] == 0 && ((u_int32_t *) (a))[3] == htonl (1))
79#endif /* !IN6_IS_ADDR_LOOPBACK */
80
81#ifndef AF_INET6
82/* Define it to something that should never appear */
83#define AF_INET6 AF_MAX
84#endif
85
86#include "log.h"
87#include "servconf.h"
88#include "bsd-cray.h"
89
90#define MAXACID 80
91
92extern ServerOptions options;
93
94char cray_tmpdir[TPATHSIZ + 1]; /* job TMPDIR path */
95
96struct sysv sysv; /* system security structure */
97struct usrv usrv; /* user security structure */
98
99/*
100 * Functions.
101 */
102void cray_retain_utmp(struct utmp *, int);
103void cray_delete_tmpdir(char *, int, uid_t);
104void cray_init_job(struct passwd *);
105void cray_set_tmpdir(struct utmp *);
106void cray_login_failure(char *, int);
107int cray_setup(uid_t, char *, const char *);
108int cray_access_denied(char *);
109
110void
111cray_login_failure(char *username, int errcode)
112{
113 struct udb *ueptr; /* UDB pointer for username */
114 ia_failure_t fsent; /* ia_failure structure */
115 ia_failure_ret_t fret; /* ia_failure return stuff */
116 struct jtab jtab; /* job table structure */
117 int jid = 0; /* job id */
118
119 if ((jid = getjtab(&jtab)) < 0)
120 debug("cray_login_failure(): getjtab error");
121
122 getsysudb();
123 if ((ueptr = getudbnam(username)) == UDB_NULL)
124 debug("cray_login_failure(): getudbname() returned NULL");
125 endudb();
126
127 memset(&fsent, '\0', sizeof(fsent));
128 fsent.revision = 0;
129 fsent.uname = username;
130 fsent.host = (char *)get_canonical_hostname(options.use_dns);
131 fsent.ttyn = "sshd";
132 fsent.caller = IA_SSHD;
133 fsent.flags = IA_INTERACTIVE;
134 fsent.ueptr = ueptr;
135 fsent.jid = jid;
136 fsent.errcode = errcode;
137 fsent.pwdp = NULL;
138 fsent.exitcode = 0; /* dont exit in ia_failure() */
139
140 fret.revision = 0;
141 fret.normal = 0;
142
143 /*
144 * Call ia_failure because of an login failure.
145 */
146 ia_failure(&fsent, &fret);
147}
148
149/*
150 * Cray access denied
151 */
152int
153cray_access_denied(char *username)
154{
155 struct udb *ueptr; /* UDB pointer for username */
156 int errcode; /* IA errorcode */
157
158 errcode = 0;
159 getsysudb();
160 if ((ueptr = getudbnam(username)) == UDB_NULL)
161 debug("cray_login_failure(): getudbname() returned NULL");
162 endudb();
163
164 if (ueptr != NULL && ueptr->ue_disabled)
165 errcode = IA_DISABLED;
166 if (errcode)
167 cray_login_failure(username, errcode);
168
169 return (errcode);
170}
171
172/*
173 * record_failed_login: generic "login failed" interface function
174 */
175void
176record_failed_login(const char *user, const char *hostname, const char *ttyname)
177{
178 cray_login_failure((char *)user, IA_UDBERR);
179}
180
181int
182cray_setup (uid_t uid, char *username, const char *command)
183{
184 extern struct udb *getudb();
185 extern char *setlimits();
186
187 int err; /* error return */
188 time_t system_time; /* current system clock */
189 time_t expiration_time; /* password expiration time */
190 int maxattempts; /* maximum no. of failed login attempts */
191 int SecureSys; /* unicos security flag */
192 int minslevel = 0; /* system minimum security level */
193 int i, j;
194 int valid_acct = -1; /* flag for reading valid acct */
195 char acct_name[MAXACID] = { "" }; /* used to read acct name */
196 struct jtab jtab; /* Job table struct */
197 struct udb ue; /* udb entry for logging-in user */
198 struct udb *up; /* pointer to UDB entry */
199 struct secstat secinfo; /* file security attributes */
200 struct servprov init_info; /* used for sesscntl() call */
201 int jid; /* job ID */
202 int pid; /* process ID */
203 char *sr; /* status return from setlimits() */
204 char *ttyn = NULL; /* ttyname or command name*/
205 char hostname[MAXHOSTNAMELEN];
206 /* passwd stuff for ia_user */
207 passwd_t pwdacm, pwddialup, pwdudb, pwdwal, pwddce;
208 ia_user_ret_t uret; /* stuff returned from ia_user */
209 ia_user_t usent; /* ia_user main structure */
210 int ia_rcode; /* ia_user return code */
211 ia_failure_t fsent; /* ia_failure structure */
212 ia_failure_ret_t fret; /* ia_failure return stuff */
213 ia_success_t ssent; /* ia_success structure */
214 ia_success_ret_t sret; /* ia_success return stuff */
215 int ia_mlsrcode; /* ia_mlsuser return code */
216 int secstatrc; /* [f]secstat return code */
217
218 if (SecureSys = (int)sysconf(_SC_CRAY_SECURE_SYS)) {
219 getsysv(&sysv, sizeof(struct sysv));
220 minslevel = sysv.sy_minlvl;
221 if (getusrv(&usrv) < 0)
222 fatal("getusrv() failed, errno = %d", errno);
223 }
224 hostname[0] = '\0';
225 strlcpy(hostname,
226 (char *)get_canonical_hostname(options.use_dns),
227 MAXHOSTNAMELEN);
228 /*
229 * Fetch user's UDB entry.
230 */
231 getsysudb();
232 if ((up = getudbnam(username)) == UDB_NULL)
233 fatal("cannot fetch user's UDB entry");
234
235 /*
236 * Prevent any possible fudging so perform a data
237 * safety check and compare the supplied uid against
238 * the udb's uid.
239 */
240 if (up->ue_uid != uid)
241 fatal("IA uid missmatch");
242 endudb();
243
244 if ((jid = getjtab(&jtab)) < 0) {
245 debug("getjtab");
246 return(-1);
247 }
248 pid = getpid();
249 ttyn = ttyname(0);
250 if (SecureSys) {
251 if (ttyn != NULL)
252 secstatrc = secstat(ttyn, &secinfo);
253 else
254 secstatrc = fsecstat(1, &secinfo);
255
256 if (secstatrc == 0)
257 debug("[f]secstat() successful");
258 else
259 fatal("[f]secstat() error, rc = %d", secstatrc);
260 }
261 if ((ttyn == NULL) && ((char *)command != NULL))
262 ttyn = (char *)command;
263 /*
264 * Initialize all structures to call ia_user
265 */
266 usent.revision = 0;
267 usent.uname = username;
268 usent.host = hostname;
269 usent.ttyn = ttyn;
270 usent.caller = IA_SSHD;
271 usent.pswdlist = &pwdacm;
272 usent.ueptr = &ue;
273 usent.flags = IA_INTERACTIVE | IA_FFLAG;
274 pwdacm.atype = IA_SECURID;
275 pwdacm.pwdp = NULL;
276 pwdacm.next = &pwdudb;
277
278 pwdudb.atype = IA_UDB;
279 pwdudb.pwdp = NULL;
280 pwdudb.next = &pwddce;
281
282 pwddce.atype = IA_DCE;
283 pwddce.pwdp = NULL;
284 pwddce.next = &pwddialup;
285
286 pwddialup.atype = IA_DIALUP;
287 pwddialup.pwdp = NULL;
288 /* pwddialup.next = &pwdwal; */
289 pwddialup.next = NULL;
290
291 pwdwal.atype = IA_WAL;
292 pwdwal.pwdp = NULL;
293 pwdwal.next = NULL;
294
295 uret.revision = 0;
296 uret.pswd = NULL;
297 uret.normal = 0;
298
299 ia_rcode = ia_user(&usent, &uret);
300 switch (ia_rcode) {
301 /*
302 * These are acceptable return codes from ia_user()
303 */
304 case IA_UDBWEEK: /* Password Expires in 1 week */
305 expiration_time = ue.ue_pwage.time + ue.ue_pwage.maxage;
306 printf ("WARNING - your current password will expire %s\n",
307 ctime((const time_t *)&expiration_time));
308 break;
309 case IA_UDBEXPIRED:
310 if (ttyname(0) != NULL) {
311 /* Force a password change */
312 printf("Your password has expired; Choose a new one.\n");
313 execl("/bin/passwd", "passwd", username, 0);
314 exit(9);
315 }
316 break;
317 case IA_NORMAL: /* Normal Return Code */
318 break;
319 case IA_BACKDOOR:
320 /* XXX: can we memset it to zero here so save some of this */
321 strlcpy(ue.ue_name, "root", sizeof(ue.ue_name));
322 strlcpy(ue.ue_dir, "/", sizeof(ue.ue_dir));
323 strlcpy(ue.ue_shell, "/bin/sh", sizeof(ue.ue_shell));
324
325 ue.ue_passwd[0] = '\0';
326 ue.ue_age[0] = '\0';
327 ue.ue_comment[0] = '\0';
328 ue.ue_loghost[0] = '\0';
329 ue.ue_logline[0] = '\0';
330
331 ue.ue_uid = -1;
332 ue.ue_nice[UDBRC_INTER] = 0;
333
334 for (i = 0; i < MAXVIDS; i++)
335 ue.ue_gids[i] = 0;
336
337 ue.ue_logfails = 0;
338 ue.ue_minlvl = ue.ue_maxlvl = ue.ue_deflvl = minslevel;
339 ue.ue_defcomps = 0;
340 ue.ue_comparts = 0;
341 ue.ue_permits = 0;
342 ue.ue_trap = 0;
343 ue.ue_disabled = 0;
344 ue.ue_logtime = 0;
345 break;
346 case IA_CONSOLE: /* Superuser not from Console */
347 case IA_TRUSTED: /* Trusted user */
348 if (options.permit_root_login > PERMIT_NO)
349 break; /* Accept root login */
350 default:
351 /*
352 * These are failed return codes from ia_user()
353 */
354 switch (ia_rcode)
355 {
356 case IA_BADAUTH:
357 printf("Bad authorization, access denied.\n");
358 break;
359 case IA_DISABLED:
360 printf("Your login has been disabled. Contact the system ");
361 printf("administrator for assistance.\n");
362 break;
363 case IA_GETSYSV:
364 printf("getsysv() failed - errno = %d\n", errno);
365 break;
366 case IA_MAXLOGS:
367 printf("Maximum number of failed login attempts exceeded.\n");
368 printf("Access denied.\n");
369 break;
370 case IA_UDBPWDNULL:
371 if (SecureSys)
372 printf("NULL Password not allowed on MLS systems.\n");
373 break;
374 default:
375 break;
376 }
377
378 /*
379 * Authentication failed.
380 */
381 printf("sshd: Login incorrect, (0%o)\n",
382 ia_rcode-IA_ERRORCODE);
383
384 /*
385 * Initialize structure for ia_failure
386 * which will exit.
387 */
388 fsent.revision = 0;
389 fsent.uname = username;
390 fsent.host = hostname;
391 fsent.ttyn = ttyn;
392 fsent.caller = IA_SSHD;
393 fsent.flags = IA_INTERACTIVE;
394 fsent.ueptr = &ue;
395 fsent.jid = jid;
396 fsent.errcode = ia_rcode;
397 fsent.pwdp = uret.pswd;
398 fsent.exitcode = 1;
399
400 fret.revision = 0;
401 fret.normal = 0;
402
403 /*
404 * Call ia_failure because of an IA failure.
405 * There is no return because ia_failure exits.
406 */
407 ia_failure(&fsent, &fret);
408
409 exit(1);
410 }
411
412 ia_mlsrcode = IA_NORMAL;
413 if (SecureSys) {
414 debug("calling ia_mlsuser()");
415 ia_mlsrcode = ia_mlsuser(&ue, &secinfo, &usrv, NULL, 0);
416 }
417 if (ia_mlsrcode != IA_NORMAL) {
418 printf("sshd: Login incorrect, (0%o)\n",
419 ia_mlsrcode-IA_ERRORCODE);
420 /*
421 * Initialize structure for ia_failure
422 * which will exit.
423 */
424 fsent.revision = 0;
425 fsent.uname = username;
426 fsent.host = hostname;
427 fsent.ttyn = ttyn;
428 fsent.caller = IA_SSHD;
429 fsent.flags = IA_INTERACTIVE;
430 fsent.ueptr = &ue;
431 fsent.jid = jid;
432 fsent.errcode = ia_mlsrcode;
433 fsent.pwdp = uret.pswd;
434 fsent.exitcode = 1;
435 fret.revision = 0;
436 fret.normal = 0;
437
438 /*
439 * Call ia_failure because of an IA failure.
440 * There is no return because ia_failure exits.
441 */
442 ia_failure(&fsent,&fret);
443 exit(1);
444 }
445
446 /* Provide login status information */
447 if (options.print_lastlog && ue.ue_logtime != 0) {
448 printf("Last successful login was : %.*s ", 19,
449 (char *)ctime(&ue.ue_logtime));
450
451 if (*ue.ue_loghost != '\0') {
452 printf("from %.*s\n", sizeof(ue.ue_loghost),
453 ue.ue_loghost);
454 } else {
455 printf("on %.*s\n", sizeof(ue.ue_logline),
456 ue.ue_logline);
457 }
458
459 if (SecureSys && (ue.ue_logfails != 0)) {
460 printf(" followed by %d failed attempts\n",
461 ue.ue_logfails);
462 }
463 }
464
465 /*
466 * Call ia_success to process successful I/A.
467 */
468 ssent.revision = 0;
469 ssent.uname = username;
470 ssent.host = hostname;
471 ssent.ttyn = ttyn;
472 ssent.caller = IA_SSHD;
473 ssent.flags = IA_INTERACTIVE;
474 ssent.ueptr = &ue;
475 ssent.jid = jid;
476 ssent.errcode = ia_rcode;
477 ssent.us = NULL;
478 ssent.time = 1; /* Set ue_logtime */
479
480 sret.revision = 0;
481 sret.normal = 0;
482
483 ia_success(&ssent, &sret);
484
485 /*
486 * Query for account, iff > 1 valid acid & askacid permbit
487 */
488 if (((ue.ue_permbits & PERMBITS_ACCTID) ||
489 (ue.ue_acids[0] >= 0) && (ue.ue_acids[1] >= 0)) &&
490 ue.ue_permbits & PERMBITS_ASKACID) {
491 if (ttyname(0) != NULL) {
492 debug("cray_setup: ttyname true case, %.100s", ttyname);
493 while (valid_acct == -1) {
494 printf("Account (? for available accounts)"
495 " [%s]: ", acid2nam(ue.ue_acids[0]));
496 fgets(acct_name, MAXACID, stdin);
497 switch (acct_name[0]) {
498 case EOF:
499 exit(0);
500 break;
501 case '\0':
502 valid_acct = ue.ue_acids[0];
503 strlcpy(acct_name, acid2nam(valid_acct), MAXACID);
504 break;
505 case '?':
506 /* Print the list 3 wide */
507 for (i = 0, j = 0; i < MAXVIDS; i++) {
508 if (ue.ue_acids[i] == -1) {
509 printf("\n");
510 break;
511 }
512 if (++j == 4) {
513 j = 1;
514 printf("\n");
515 }
516 printf(" %s",
517 acid2nam(ue.ue_acids[i]));
518 }
519 if (ue.ue_permbits & PERMBITS_ACCTID) {
520 printf("\"acctid\" permbit also allows"
521 " you to select any valid "
522 "account name.\n");
523 }
524 printf("\n");
525 break;
526 default:
527 valid_acct = nam2acid(acct_name);
528 if (valid_acct == -1)
529 printf(
530 "Account id not found for"
531 " account name \"%s\"\n\n",
532 acct_name);
533 break;
534 }
535 /*
536 * If an account was given, search the user's
537 * acids array to verify they can use this account.
538 */
539 if ((valid_acct != -1) &&
540 !(ue.ue_permbits & PERMBITS_ACCTID)) {
541 for (i = 0; i < MAXVIDS; i++) {
542 if (ue.ue_acids[i] == -1)
543 break;
544 if (valid_acct == ue.ue_acids[i])
545 break;
546 }
547 if (i == MAXVIDS ||
548 ue.ue_acids[i] == -1) {
549 fprintf(stderr, "Cannot set"
550 " account name to "
551 "\"%s\", permission "
552 "denied\n\n", acct_name);
553 valid_acct = -1;
554 }
555 }
556 }
557 } else {
558 /*
559 * The client isn't connected to a terminal and can't
560 * respond to an acid prompt. Use default acid.
561 */
562 debug("cray_setup: ttyname false case, %.100s",
563 ttyname);
564 valid_acct = ue.ue_acids[0];
565 }
566 } else {
567 /*
568 * The user doesn't have the askacid permbit set or
569 * only has one valid account to use.
570 */
571 valid_acct = ue.ue_acids[0];
572 }
573 if (acctid(0, valid_acct) < 0) {
574 printf ("Bad account id: %d\n", valid_acct);
575 exit(1);
576 }
577
578 /*
579 * Now set shares, quotas, limits, including CPU time for the
580 * (interactive) job and process, and set up permissions
581 * (for chown etc), etc.
582 */
583 if (setshares(ue.ue_uid, valid_acct, printf, 0, 0)) {
584 printf("Unable to give %d shares to <%s>(%d/%d)\n",
585 ue.ue_shares, ue.ue_name, ue.ue_uid, valid_acct);
586 exit(1);
587 }
588
589 sr = setlimits(username, C_PROC, pid, UDBRC_INTER);
590 if (sr != NULL) {
591 debug("%.200s", sr);
592 exit(1);
593 }
594 sr = setlimits(username, C_JOB, jid, UDBRC_INTER);
595 if (sr != NULL) {
596 debug("%.200s", sr);
597 exit(1);
598 }
599 /*
600 * Place the service provider information into
601 * the session table (Unicos) or job table (Unicos/mk).
602 * There exist double defines for the job/session table in
603 * unicos/mk (jtab.h) so no need for a compile time switch.
604 */
605 memset(&init_info, '\0', sizeof(init_info));
606 init_info.s_sessinit.si_id = URM_SPT_LOGIN;
607 init_info.s_sessinit.si_pid = getpid();
608 init_info.s_sessinit.si_sid = jid;
609 sesscntl(0, S_SETSERVPO, (int)&init_info);
610
611 /*
612 * Set user and controlling tty security attributes.
613 */
614 if (SecureSys) {
615 if (setusrv(&usrv) == -1) {
616 debug("setusrv() failed, errno = %d",errno);
617 exit(1);
618 }
619 }
620
621 return (0);
622}
623
624/*
625 * The rc.* and /etc/sdaemon methods of starting a program on unicos/unicosmk
626 * can have pal privileges that sshd can inherit which
627 * could allow a user to su to root with out a password.
628 * This subroutine clears all privileges.
629 */
630void
631drop_cray_privs()
632{
633#if defined(_SC_CRAY_PRIV_SU)
634 priv_proc_t *privstate;
635 int result;
636 extern int priv_set_proc();
637 extern priv_proc_t *priv_init_proc();
638
639 /*
640 * If ether of theses two flags are not set
641 * then don't allow this version of ssh to run.
642 */
643 if (!sysconf(_SC_CRAY_PRIV_SU))
644 fatal("Not PRIV_SU system.");
645 if (!sysconf(_SC_CRAY_POSIX_PRIV))
646 fatal("Not POSIX_PRIV.");
647
648 debug("Setting MLS labels.");;
649
650 if (sysconf(_SC_CRAY_SECURE_MAC)) {
651 usrv.sv_minlvl = SYSLOW;
652 usrv.sv_actlvl = SYSHIGH;
653 usrv.sv_maxlvl = SYSHIGH;
654 } else {
655 usrv.sv_minlvl = sysv.sy_minlvl;
656 usrv.sv_actlvl = sysv.sy_minlvl;
657 usrv.sv_maxlvl = sysv.sy_maxlvl;
658 }
659 usrv.sv_actcmp = 0;
660 usrv.sv_valcmp = sysv.sy_valcmp;
661
662 usrv.sv_intcat = TFM_SYSTEM;
663 usrv.sv_valcat |= (TFM_SYSTEM | TFM_SYSFILE);
664
665 if (setusrv(&usrv) < 0) {
666 fatal("%s(%d): setusrv(): %s", __FILE__, __LINE__,
667 strerror(errno));
668 }
669
670 if ((privstate = priv_init_proc()) != NULL) {
671 result = priv_set_proc(privstate);
672 if (result != 0 ) {
673 fatal("%s(%d): priv_set_proc(): %s",
674 __FILE__, __LINE__, strerror(errno));
675 }
676 priv_free_proc(privstate);
677 }
678 debug ("Privileges should be cleared...");
679#else
680 /* XXX: do this differently */
681# error Cray systems must be run with _SC_CRAY_PRIV_SU on!
682#endif
683}
684
685
686/*
687 * Retain utmp/wtmp information - used by cray accounting.
688 */
689void
690cray_retain_utmp(struct utmp *ut, int pid)
691{
692 int fd;
693 struct utmp utmp;
694
695 if ((fd = open(UTMP_FILE, O_RDONLY)) != -1) {
696 /* XXX use atomicio */
697 while (read(fd, (char *)&utmp, sizeof(utmp)) == sizeof(utmp)) {
698 if (pid == utmp.ut_pid) {
699 ut->ut_jid = utmp.ut_jid;
700 strncpy(ut->ut_tpath, utmp.ut_tpath, sizeof(utmp.ut_tpath));
701 strncpy(ut->ut_host, utmp.ut_host, sizeof(utmp.ut_host));
702 strncpy(ut->ut_name, utmp.ut_name, sizeof(utmp.ut_name));
703 break;
704 }
705 }
706 close(fd);
707 } else
708 fatal("Unable to open utmp file");
709}
710
711/*
712 * tmpdir support.
713 */
714
715/*
716 * find and delete jobs tmpdir.
717 */
718void
719cray_delete_tmpdir(char *login, int jid, uid_t uid)
720{
721 static char jtmp[TPATHSIZ];
722 struct stat statbuf;
723 int child, c, wstat;
724
725 for (c = 'a'; c <= 'z'; c++) {
726 snprintf(jtmp, TPATHSIZ, "%s/jtmp.%06d%c", JTMPDIR, jid, c);
727 if (stat(jtmp, &statbuf) == 0 && statbuf.st_uid == uid)
728 break;
729 }
730
731 if (c > 'z')
732 return;
733
734 if ((child = fork()) == 0) {
735 execl(CLEANTMPCMD, CLEANTMPCMD, login, jtmp, (char *)NULL);
736 fatal("cray_delete_tmpdir: execl of CLEANTMPCMD failed");
737 }
738
739 while (waitpid(child, &wstat, 0) == -1 && errno == EINTR)
740 ;
741}
742
743/*
744 * Remove tmpdir on job termination.
745 */
746void
747cray_job_termination_handler(int sig)
748{
749 int jid;
750 char *login = NULL;
751 struct jtab jtab;
752
753 if ((jid = waitjob(&jtab)) == -1 ||
754 (login = uid2nam(jtab.j_uid)) == NULL)
755 return;
756
757 cray_delete_tmpdir(login, jid, jtab.j_uid);
758}
759
760/*
761 * Set job id and create tmpdir directory.
762 */
763void
764cray_init_job(struct passwd *pw)
765{
766 int jid;
767 int c;
768
769 jid = setjob(pw->pw_uid, WJSIGNAL);
770 if (jid < 0)
771 fatal("System call setjob failure");
772
773 for (c = 'a'; c <= 'z'; c++) {
774 snprintf(cray_tmpdir, TPATHSIZ, "%s/jtmp.%06d%c", JTMPDIR, jid, c);
775 if (mkdir(cray_tmpdir, JTMPMODE) != 0)
776 continue;
777 if (chown(cray_tmpdir, pw->pw_uid, pw->pw_gid) != 0) {
778 rmdir(cray_tmpdir);
779 continue;
780 }
781 break;
782 }
783
784 if (c > 'z')
785 cray_tmpdir[0] = '\0';
786}
787
788void
789cray_set_tmpdir(struct utmp *ut)
790{
791 int jid;
792 struct jtab jbuf;
793
794 if ((jid = getjtab(&jbuf)) < 0)
795 return;
796
797 /*
798 * Set jid and tmpdir in utmp record.
799 */
800 ut->ut_jid = jid;
801 strncpy(ut->ut_tpath, cray_tmpdir, TPATHSIZ);
802}
803#endif /* UNICOS */
804
805#ifdef _UNICOSMP
806#include <pwd.h>
807/*
808 * Set job id and create tmpdir directory.
809 */
810void
811cray_init_job(struct passwd *pw)
812{
813 initrm_silent(pw->pw_uid);
814 return;
815}
816#endif /* _UNICOSMP */
diff --git a/openbsd-compat/bsd-cray.h b/openbsd-compat/bsd-cray.h
deleted file mode 100644
index ca626a021..000000000
--- a/openbsd-compat/bsd-cray.h
+++ /dev/null
@@ -1,59 +0,0 @@
1/*
2 * Copyright (c) 2002, Cray Inc. (Wendy Palm <wendyp@cray.com>)
3 * Significant portions provided by
4 * Wayne Schroeder, SDSC <schroeder@sdsc.edu>
5 * William Jones, UTexas <jones@tacc.utexas.edu>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Created: Apr 22 16.34:00 2002 wp
28 *
29 * This file contains functions required for proper execution
30 * on UNICOS systems.
31 *
32 */
33
34#ifndef _BSD_CRAY_H
35#define _BSD_CRAY_H
36
37#ifdef _UNICOS
38
39void cray_init_job(struct passwd *);
40void cray_job_termination_handler(int);
41void cray_login_failure(char *, int );
42int cray_access_denied(char *);
43extern char cray_tmpdir[];
44
45#define CUSTOM_FAILED_LOGIN 1
46
47#ifndef IA_SSHD
48# define IA_SSHD IA_LOGIN
49#endif
50#ifndef MAXHOSTNAMELEN
51# define MAXHOSTNAMELEN 64
52#endif
53#ifndef _CRAYT3E
54# define TIOCGPGRP (tIOC|20)
55#endif
56
57#endif /* UNICOS */
58
59#endif /* _BSD_CRAY_H */
diff --git a/openbsd-compat/bsd-flock.c b/openbsd-compat/bsd-flock.c
new file mode 100644
index 000000000..9b15d1eaf
--- /dev/null
+++ b/openbsd-compat/bsd-flock.c
@@ -0,0 +1,81 @@
1/* $NetBSD: flock.c,v 1.6 2008/04/28 20:24:12 martin Exp $ */
2
3/*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Todd Vierling.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Emulate flock() with fcntl(), where available.
34 * Otherwise, don't do locking; just pretend success.
35 */
36
37#include "includes.h"
38
39#ifndef HAVE_FLOCK
40#include <errno.h>
41#include <fcntl.h>
42
43int
44flock(int fd, int op)
45{
46 int rc = 0;
47
48#if defined(F_SETLK) && defined(F_SETLKW)
49 struct flock fl = {0};
50
51 switch (op & (LOCK_EX|LOCK_SH|LOCK_UN)) {
52 case LOCK_EX:
53 fl.l_type = F_WRLCK;
54 break;
55
56 case LOCK_SH:
57 fl.l_type = F_RDLCK;
58 break;
59
60 case LOCK_UN:
61 fl.l_type = F_UNLCK;
62 break;
63
64 default:
65 errno = EINVAL;
66 return -1;
67 }
68
69 fl.l_whence = SEEK_SET;
70 rc = fcntl(fd, op & LOCK_NB ? F_SETLK : F_SETLKW, &fl);
71
72 if (rc && (errno == EAGAIN))
73 errno = EWOULDBLOCK;
74#else
75 rc = -1;
76 errno = ENOSYS;
77#endif
78
79 return rc;
80}
81#endif
diff --git a/openbsd-compat/bsd-getpagesize.c b/openbsd-compat/bsd-getpagesize.c
index 9daddfbd3..416a8d4cb 100644
--- a/openbsd-compat/bsd-getpagesize.c
+++ b/openbsd-compat/bsd-getpagesize.c
@@ -1,5 +1,7 @@
1/* Placed in the public domain */ 1/* Placed in the public domain */
2 2
3#include "includes.h"
4
3#ifndef HAVE_GETPAGESIZE 5#ifndef HAVE_GETPAGESIZE
4 6
5#include <unistd.h> 7#include <unistd.h>
diff --git a/openbsd-compat/bsd-malloc.c b/openbsd-compat/bsd-malloc.c
index 6402ab588..482facdc9 100644
--- a/openbsd-compat/bsd-malloc.c
+++ b/openbsd-compat/bsd-malloc.c
@@ -50,6 +50,8 @@ rpl_realloc(void *ptr, size_t size)
50{ 50{
51 if (size == 0) 51 if (size == 0)
52 size = 1; 52 size = 1;
53 if (ptr == 0)
54 return malloc(size);
53 return realloc(ptr, size); 55 return realloc(ptr, size);
54} 56}
55#endif 57#endif
diff --git a/openbsd-compat/bsd-misc.c b/openbsd-compat/bsd-misc.c
index 29f6ad38c..3daf61071 100644
--- a/openbsd-compat/bsd-misc.c
+++ b/openbsd-compat/bsd-misc.c
@@ -104,16 +104,6 @@ const char *strerror(int e)
104} 104}
105#endif 105#endif
106 106
107#if !defined(HAVE_STRSIGNAL)
108char *strsignal(int sig)
109{
110 static char buf[16];
111
112 (void)snprintf(buf, sizeof(buf), "%d", sig);
113 return buf;
114}
115#endif
116
117#ifndef HAVE_UTIMES 107#ifndef HAVE_UTIMES
118int utimes(char *filename, struct timeval *tvp) 108int utimes(char *filename, struct timeval *tvp)
119{ 109{
@@ -221,33 +211,6 @@ tcsendbreak(int fd, int duration)
221} 211}
222#endif /* HAVE_TCSENDBREAK */ 212#endif /* HAVE_TCSENDBREAK */
223 213
224mysig_t
225mysignal(int sig, mysig_t act)
226{
227#ifdef HAVE_SIGACTION
228 struct sigaction sa, osa;
229
230 if (sigaction(sig, NULL, &osa) == -1)
231 return (mysig_t) -1;
232 if (osa.sa_handler != act) {
233 memset(&sa, 0, sizeof(sa));
234 sigemptyset(&sa.sa_mask);
235 sa.sa_flags = 0;
236#ifdef SA_INTERRUPT
237 if (sig == SIGALRM)
238 sa.sa_flags |= SA_INTERRUPT;
239#endif
240 sa.sa_handler = act;
241 if (sigaction(sig, &sa, NULL) == -1)
242 return (mysig_t) -1;
243 }
244 return (osa.sa_handler);
245#else
246 #undef signal
247 return (signal(sig, act));
248#endif
249}
250
251#ifndef HAVE_STRDUP 214#ifndef HAVE_STRDUP
252char * 215char *
253strdup(const char *str) 216strdup(const char *str)
@@ -275,7 +238,7 @@ isblank(int c)
275pid_t 238pid_t
276getpgid(pid_t pid) 239getpgid(pid_t pid)
277{ 240{
278#if defined(HAVE_GETPGRP) && !defined(GETPGRP_VOID) 241#if defined(HAVE_GETPGRP) && !defined(GETPGRP_VOID) && GETPGRP_VOID == 0
279 return getpgrp(pid); 242 return getpgrp(pid);
280#elif defined(HAVE_GETPGRP) 243#elif defined(HAVE_GETPGRP)
281 if (pid == 0) 244 if (pid == 0)
@@ -319,3 +282,45 @@ llabs(long long j)
319 return (j < 0 ? -j : j); 282 return (j < 0 ? -j : j);
320} 283}
321#endif 284#endif
285
286#ifndef HAVE_BZERO
287void
288bzero(void *b, size_t n)
289{
290 (void)memset(b, 0, n);
291}
292#endif
293
294#ifndef HAVE_RAISE
295int
296raise(int sig)
297{
298 kill(getpid(), sig);
299}
300#endif
301
302#ifndef HAVE_GETSID
303pid_t
304getsid(pid_t pid)
305{
306 errno = ENOSYS;
307 return -1;
308}
309#endif
310
311#ifdef FFLUSH_NULL_BUG
312#undef fflush
313int _ssh_compat_fflush(FILE *f)
314{
315 int r1, r2, r3;
316
317 if (f == NULL) {
318 r2 = fflush(stdout);
319 r3 = fflush(stderr);
320 if (r1 == -1 || r2 == -1 || r3 == -1)
321 return -1;
322 return 0;
323 }
324 return fflush(f);
325}
326#endif
diff --git a/openbsd-compat/bsd-misc.h b/openbsd-compat/bsd-misc.h
index 0b1a3504f..52ec52853 100644
--- a/openbsd-compat/bsd-misc.h
+++ b/openbsd-compat/bsd-misc.h
@@ -49,10 +49,6 @@ int setegid(uid_t);
49const char *strerror(int); 49const char *strerror(int);
50#endif 50#endif
51 51
52#if !defined(HAVE_STRSIGNAL)
53char *strsignal(int);
54#endif
55
56#if !defined(HAVE_SETLINEBUF) 52#if !defined(HAVE_SETLINEBUF)
57#define setlinebuf(a) (setvbuf((a), NULL, _IOLBF, 0)) 53#define setlinebuf(a) (setvbuf((a), NULL, _IOLBF, 0))
58#endif 54#endif
@@ -98,12 +94,6 @@ int tcsendbreak(int, int);
98int unsetenv(const char *); 94int unsetenv(const char *);
99#endif 95#endif
100 96
101/* wrapper for signal interface */
102typedef void (*mysig_t)(int);
103mysig_t mysignal(int sig, mysig_t act);
104
105#define signal(a,b) mysignal(a,b)
106
107#ifndef HAVE_ISBLANK 97#ifndef HAVE_ISBLANK
108int isblank(int); 98int isblank(int);
109#endif 99#endif
@@ -143,4 +133,28 @@ void warn(const char *, ...) __attribute__((format(printf, 1, 2)));
143long long llabs(long long); 133long long llabs(long long);
144#endif 134#endif
145 135
136#if defined(HAVE_DECL_BZERO) && HAVE_DECL_BZERO == 0
137void bzero(void *, size_t);
138#endif
139
140#ifndef HAVE_RAISE
141int raise(int);
142#endif
143
144#ifndef HAVE_GETSID
145pid_t getsid(pid_t);
146#endif
147
148#ifndef HAVE_FLOCK
149# define LOCK_SH 0x01
150# define LOCK_EX 0x02
151# define LOCK_NB 0x04
152# define LOCK_UN 0x08
153int flock(int, int);
154#endif
155
156#ifdef FFLUSH_NULL_BUG
157# define fflush(x) (_ssh_compat_fflush(x))
158#endif
159
146#endif /* _BSD_MISC_H */ 160#endif /* _BSD_MISC_H */
diff --git a/openbsd-compat/bsd-openpty.c b/openbsd-compat/bsd-openpty.c
index b28235860..e8ad542f8 100644
--- a/openbsd-compat/bsd-openpty.c
+++ b/openbsd-compat/bsd-openpty.c
@@ -147,31 +147,6 @@ openpty(int *amaster, int *aslave, char *name, struct termios *termp,
147 } 147 }
148 return (0); 148 return (0);
149 149
150#elif defined(_UNICOS)
151 char ptbuf[64], ttbuf[64];
152 int i;
153 int highpty;
154
155 highpty = 128;
156#ifdef _SC_CRAY_NPTY
157 if ((highpty = sysconf(_SC_CRAY_NPTY)) == -1)
158 highpty = 128;
159#endif /* _SC_CRAY_NPTY */
160
161 for (i = 0; i < highpty; i++) {
162 snprintf(ptbuf, sizeof(ptbuf), "/dev/pty/%03d", i);
163 snprintf(ttbuf, sizeof(ttbuf), "/dev/ttyp%03d", i);
164 if ((*amaster = open(ptbuf, O_RDWR|O_NOCTTY)) == -1)
165 continue;
166 /* Open the slave side. */
167 if ((*aslave = open(ttbuf, O_RDWR|O_NOCTTY)) == -1) {
168 close(*amaster);
169 return (-1);
170 }
171 return (0);
172 }
173 return (-1);
174
175#else 150#else
176 /* BSD-style pty code. */ 151 /* BSD-style pty code. */
177 char ptbuf[64], ttbuf[64]; 152 char ptbuf[64], ttbuf[64];
diff --git a/openbsd-compat/bsd-signal.c b/openbsd-compat/bsd-signal.c
new file mode 100644
index 000000000..979010e84
--- /dev/null
+++ b/openbsd-compat/bsd-signal.c
@@ -0,0 +1,62 @@
1/*
2 * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include "includes.h"
18
19#include <signal.h>
20#include <string.h>
21#include <unistd.h>
22
23#include "openbsd-compat/bsd-signal.h"
24
25#undef signal
26
27mysig_t
28mysignal(int sig, mysig_t act)
29{
30#ifdef HAVE_SIGACTION
31 struct sigaction sa, osa;
32
33 if (sigaction(sig, NULL, &osa) == -1)
34 return (mysig_t) -1;
35 if (osa.sa_handler != act) {
36 memset(&sa, 0, sizeof(sa));
37 sigemptyset(&sa.sa_mask);
38 sa.sa_flags = 0;
39#ifdef SA_INTERRUPT
40 if (sig == SIGALRM)
41 sa.sa_flags |= SA_INTERRUPT;
42#endif
43 sa.sa_handler = act;
44 if (sigaction(sig, &sa, NULL) == -1)
45 return (mysig_t) -1;
46 }
47 return (osa.sa_handler);
48#else
49 return (signal(sig, act));
50#endif
51}
52
53#if !defined(HAVE_STRSIGNAL)
54char *strsignal(int sig)
55{
56 static char buf[16];
57
58 (void)snprintf(buf, sizeof(buf), "%d", sig);
59 return buf;
60}
61#endif
62
diff --git a/openbsd-compat/bsd-signal.h b/openbsd-compat/bsd-signal.h
new file mode 100644
index 000000000..4cb8cb7a0
--- /dev/null
+++ b/openbsd-compat/bsd-signal.h
@@ -0,0 +1,39 @@
1/*
2 * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#ifndef _BSD_SIGNAL_H
18#define _BSD_SIGNAL_H
19
20#include "includes.h"
21
22#ifndef _NSIG
23# ifdef NSIG
24# define _NSIG NSIG
25# else
26# define _NSIG 128
27# endif
28#endif
29
30/* wrapper for signal interface */
31typedef void (*mysig_t)(int);
32mysig_t mysignal(int sig, mysig_t act);
33#define signal(a,b) mysignal(a,b)
34
35#if !defined(HAVE_STRSIGNAL)
36char *strsignal(int);
37#endif
38
39#endif /* _BSD_SIGNAL_H */
diff --git a/openbsd-compat/bsd-statvfs.c b/openbsd-compat/bsd-statvfs.c
index 458dbe89c..e3bd87d98 100644
--- a/openbsd-compat/bsd-statvfs.c
+++ b/openbsd-compat/bsd-statvfs.c
@@ -25,6 +25,10 @@
25 25
26#include <errno.h> 26#include <errno.h>
27 27
28#ifndef MNAMELEN
29# define MNAMELEN 32
30#endif
31
28static void 32static void
29copy_statfs_to_statvfs(struct statvfs *to, struct statfs *from) 33copy_statfs_to_statvfs(struct statvfs *to, struct statfs *from)
30{ 34{
@@ -37,7 +41,11 @@ copy_statfs_to_statvfs(struct statvfs *to, struct statfs *from)
37 to->f_ffree = from->f_ffree; 41 to->f_ffree = from->f_ffree;
38 to->f_favail = from->f_ffree; /* no exact equivalent */ 42 to->f_favail = from->f_ffree; /* no exact equivalent */
39 to->f_fsid = 0; /* XXX fix me */ 43 to->f_fsid = 0; /* XXX fix me */
44#ifdef HAVE_STRUCT_STATFS_F_FLAGS
40 to->f_flag = from->f_flags; 45 to->f_flag = from->f_flags;
46#else
47 to->f_flag = 0;
48#endif
41 to->f_namemax = MNAMELEN; 49 to->f_namemax = MNAMELEN;
42} 50}
43 51
diff --git a/openbsd-compat/bsd-statvfs.h b/openbsd-compat/bsd-statvfs.h
index 815ec03b2..e2a4c15f7 100644
--- a/openbsd-compat/bsd-statvfs.h
+++ b/openbsd-compat/bsd-statvfs.h
@@ -26,6 +26,9 @@
26#ifdef HAVE_SYS_STATFS_H 26#ifdef HAVE_SYS_STATFS_H
27#include <sys/statfs.h> 27#include <sys/statfs.h>
28#endif 28#endif
29#ifdef HAVE_SYS_VFS_H
30#include <sys/vfs.h>
31#endif
29 32
30#ifndef HAVE_FSBLKCNT_T 33#ifndef HAVE_FSBLKCNT_T
31typedef unsigned long fsblkcnt_t; 34typedef unsigned long fsblkcnt_t;
diff --git a/openbsd-compat/freezero.c b/openbsd-compat/freezero.c
index 3af8f4a73..90b9d3813 100644
--- a/openbsd-compat/freezero.c
+++ b/openbsd-compat/freezero.c
@@ -16,11 +16,15 @@
16 16
17#include "includes.h" 17#include "includes.h"
18 18
19#include <string.h>
20
19#ifndef HAVE_FREEZERO 21#ifndef HAVE_FREEZERO
20 22
21void 23void
22freezero(void *ptr, size_t sz) 24freezero(void *ptr, size_t sz)
23{ 25{
26 if (ptr == NULL)
27 return;
24 explicit_bzero(ptr, sz); 28 explicit_bzero(ptr, sz);
25 free(ptr); 29 free(ptr);
26} 30}
diff --git a/openbsd-compat/openbsd-compat.h b/openbsd-compat/openbsd-compat.h
index cac799e84..b48fb9342 100644
--- a/openbsd-compat/openbsd-compat.h
+++ b/openbsd-compat/openbsd-compat.h
@@ -170,15 +170,24 @@ int BSDgetopt(int argc, char * const *argv, const char *opts);
170#include "openbsd-compat/getopt.h" 170#include "openbsd-compat/getopt.h"
171#endif 171#endif
172 172
173#if defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0 173#if ((defined(HAVE_DECL_READV) && HAVE_DECL_READV == 0) || \
174 (defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0))
174# include <sys/types.h> 175# include <sys/types.h>
175# include <sys/uio.h> 176# include <sys/uio.h>
177
178# if defined(HAVE_DECL_READV) && HAVE_DECL_READV == 0
179int readv(int, struct iovec *, int);
180# endif
181
182# if defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0
176int writev(int, struct iovec *, int); 183int writev(int, struct iovec *, int);
184# endif
177#endif 185#endif
178 186
179/* Home grown routines */ 187/* Home grown routines */
180#include "bsd-misc.h" 188#include "bsd-misc.h"
181#include "bsd-setres_id.h" 189#include "bsd-setres_id.h"
190#include "bsd-signal.h"
182#include "bsd-statvfs.h" 191#include "bsd-statvfs.h"
183#include "bsd-waitpid.h" 192#include "bsd-waitpid.h"
184#include "bsd-poll.h" 193#include "bsd-poll.h"
@@ -315,14 +324,13 @@ char *shadow_pw(struct passwd *pw);
315#include "fake-rfc2553.h" 324#include "fake-rfc2553.h"
316 325
317/* Routines for a single OS platform */ 326/* Routines for a single OS platform */
318#include "bsd-cray.h"
319#include "bsd-cygwin_util.h" 327#include "bsd-cygwin_util.h"
320 328
321#include "port-aix.h" 329#include "port-aix.h"
322#include "port-irix.h" 330#include "port-irix.h"
323#include "port-linux.h" 331#include "port-linux.h"
324#include "port-solaris.h" 332#include "port-solaris.h"
325#include "port-tun.h" 333#include "port-net.h"
326#include "port-uw.h" 334#include "port-uw.h"
327 335
328/* _FORTIFY_SOURCE breaks FD_ISSET(n)/FD_SET(n) for n > FD_SETSIZE. Avoid. */ 336/* _FORTIFY_SOURCE breaks FD_ISSET(n)/FD_SET(n) for n > FD_SETSIZE. Avoid. */
diff --git a/openbsd-compat/port-aix.c b/openbsd-compat/port-aix.c
index c2970c4db..79c868966 100644
--- a/openbsd-compat/port-aix.c
+++ b/openbsd-compat/port-aix.c
@@ -32,6 +32,7 @@
32#include "hostfile.h" 32#include "hostfile.h"
33#include "auth.h" 33#include "auth.h"
34#include "ssh.h" 34#include "ssh.h"
35#include "ssh_api.h"
35#include "log.h" 36#include "log.h"
36 37
37#ifdef _AIX 38#ifdef _AIX
@@ -171,8 +172,9 @@ aix_valid_authentications(const char *user)
171 * returns 0. 172 * returns 0.
172 */ 173 */
173int 174int
174sys_auth_passwd(Authctxt *ctxt, const char *password) 175sys_auth_passwd(struct ssh *ssh, const char *password)
175{ 176{
177 Authctxt *ctxt = ssh->authctxt;
176 char *authmsg = NULL, *msg = NULL, *name = ctxt->pw->pw_name; 178 char *authmsg = NULL, *msg = NULL, *name = ctxt->pw->pw_name;
177 int authsuccess = 0, expired, reenter, result; 179 int authsuccess = 0, expired, reenter, result;
178 180
diff --git a/openbsd-compat/port-linux.c b/openbsd-compat/port-linux.c
index e26faf08c..8a3e5c68d 100644
--- a/openbsd-compat/port-linux.c
+++ b/openbsd-compat/port-linux.c
@@ -39,7 +39,6 @@
39 39
40#ifdef WITH_SELINUX 40#ifdef WITH_SELINUX
41#include <selinux/selinux.h> 41#include <selinux/selinux.h>
42#include <selinux/flask.h>
43#include <selinux/get_context_list.h> 42#include <selinux/get_context_list.h>
44 43
45#ifndef SSH_SELINUX_UNCONFINED_TYPE 44#ifndef SSH_SELINUX_UNCONFINED_TYPE
@@ -152,6 +151,7 @@ ssh_selinux_setup_pty(char *pwname, const char *tty, const char *role)
152 security_context_t new_tty_ctx = NULL; 151 security_context_t new_tty_ctx = NULL;
153 security_context_t user_ctx = NULL; 152 security_context_t user_ctx = NULL;
154 security_context_t old_tty_ctx = NULL; 153 security_context_t old_tty_ctx = NULL;
154 security_class_t chrclass;
155 155
156 if (!ssh_selinux_enabled()) 156 if (!ssh_selinux_enabled())
157 return; 157 return;
@@ -166,9 +166,12 @@ ssh_selinux_setup_pty(char *pwname, const char *tty, const char *role)
166 error("%s: getfilecon: %s", __func__, strerror(errno)); 166 error("%s: getfilecon: %s", __func__, strerror(errno));
167 goto out; 167 goto out;
168 } 168 }
169 169 if ((chrclass = string_to_security_class("chr_file")) == 0) {
170 error("%s: couldn't get security class for chr_file", __func__);
171 goto out;
172 }
170 if (security_compute_relabel(user_ctx, old_tty_ctx, 173 if (security_compute_relabel(user_ctx, old_tty_ctx,
171 SECCLASS_CHR_FILE, &new_tty_ctx) != 0) { 174 chrclass, &new_tty_ctx) != 0) {
172 error("%s: security_compute_relabel: %s", 175 error("%s: security_compute_relabel: %s",
173 __func__, strerror(errno)); 176 __func__, strerror(errno));
174 goto out; 177 goto out;
diff --git a/openbsd-compat/port-tun.c b/openbsd-compat/port-net.c
index 7579c6084..7050629c3 100644
--- a/openbsd-compat/port-tun.c
+++ b/openbsd-compat/port-net.c
@@ -37,6 +37,90 @@
37#include "ssherr.h" 37#include "ssherr.h"
38 38
39/* 39/*
40 * This file contains various portability code for network support,
41 * including tun/tap forwarding and routing domains.
42 */
43
44#if defined(SYS_RDOMAIN_LINUX) || defined(SSH_TUN_LINUX)
45#include <linux/if.h>
46#endif
47
48#if defined(SYS_RDOMAIN_LINUX)
49char *
50sys_get_rdomain(int fd)
51{
52 char dev[IFNAMSIZ + 1];
53 socklen_t len = sizeof(dev) - 1;
54
55 if (getsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, dev, &len) == -1) {
56 error("%s: cannot determine VRF for fd=%d : %s",
57 __func__, fd, strerror(errno));
58 return NULL;
59 }
60 dev[len] = '\0';
61 return strdup(dev);
62}
63
64int
65sys_set_rdomain(int fd, const char *name)
66{
67 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
68 name, strlen(name)) == -1) {
69 error("%s: setsockopt(%d, SO_BINDTODEVICE, %s): %s",
70 __func__, fd, name, strerror(errno));
71 return -1;
72 }
73 return 0;
74}
75
76int
77sys_valid_rdomain(const char *name)
78{
79 int fd;
80
81 /*
82 * This is a pretty crappy way to test. It would be better to
83 * check whether "name" represents a VRF device, but apparently
84 * that requires an rtnetlink transaction.
85 */
86 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
87 return 0;
88 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
89 name, strlen(name)) == -1) {
90 close(fd);
91 return 0;
92 }
93 close(fd);
94 return 1;
95}
96#elif defined(SYS_RDOMAIN_XXX)
97/* XXX examples */
98char *
99sys_get_rdomain(int fd)
100{
101 return NULL;
102}
103
104int
105sys_set_rdomain(int fd, const char *name)
106{
107 return -1;
108}
109
110int
111valid_rdomain(const char *name)
112{
113 return 0;
114}
115
116void
117sys_set_process_rdomain(const char *name)
118{
119 fatal("%s: not supported", __func__);
120}
121#endif /* defined(SYS_RDOMAIN_XXX) */
122
123/*
40 * This is the portable version of the SSH tunnel forwarding, it 124 * This is the portable version of the SSH tunnel forwarding, it
41 * uses some preprocessor definitions for various platform-specific 125 * uses some preprocessor definitions for various platform-specific
42 * settings. 126 * settings.
@@ -52,16 +136,18 @@
52 */ 136 */
53 137
54#if defined(SSH_TUN_LINUX) 138#if defined(SSH_TUN_LINUX)
55#include <linux/if.h>
56#include <linux/if_tun.h> 139#include <linux/if_tun.h>
57 140
58int 141int
59sys_tun_open(int tun, int mode) 142sys_tun_open(int tun, int mode, char **ifname)
60{ 143{
61 struct ifreq ifr; 144 struct ifreq ifr;
62 int fd = -1; 145 int fd = -1;
63 const char *name = NULL; 146 const char *name = NULL;
64 147
148 if (ifname != NULL)
149 *ifname = NULL;
150
65 if ((fd = open("/dev/net/tun", O_RDWR)) == -1) { 151 if ((fd = open("/dev/net/tun", O_RDWR)) == -1) {
66 debug("%s: failed to open tunnel control interface: %s", 152 debug("%s: failed to open tunnel control interface: %s",
67 __func__, strerror(errno)); 153 __func__, strerror(errno));
@@ -99,6 +185,9 @@ sys_tun_open(int tun, int mode)
99 else 185 else
100 debug("%s: %s mode %d fd %d", __func__, ifr.ifr_name, mode, fd); 186 debug("%s: %s mode %d fd %d", __func__, ifr.ifr_name, mode, fd);
101 187
188 if (ifname != NULL && (*ifname = strdup(ifr.ifr_name)))
189 goto failed;
190
102 return (fd); 191 return (fd);
103 192
104 failed: 193 failed:
@@ -116,13 +205,16 @@ sys_tun_open(int tun, int mode)
116#endif 205#endif
117 206
118int 207int
119sys_tun_open(int tun, int mode) 208sys_tun_open(int tun, int mode, char **ifname)
120{ 209{
121 struct ifreq ifr; 210 struct ifreq ifr;
122 char name[100]; 211 char name[100];
123 int fd = -1, sock, flag; 212 int fd = -1, sock, flag;
124 const char *tunbase = "tun"; 213 const char *tunbase = "tun";
125 214
215 if (ifname != NULL)
216 *ifname = NULL;
217
126 if (mode == SSH_TUNMODE_ETHERNET) { 218 if (mode == SSH_TUNMODE_ETHERNET) {
127#ifdef SSH_TUN_NO_L2 219#ifdef SSH_TUN_NO_L2
128 debug("%s: no layer 2 tunnelling support", __func__); 220 debug("%s: no layer 2 tunnelling support", __func__);
@@ -180,6 +272,9 @@ sys_tun_open(int tun, int mode)
180 goto failed; 272 goto failed;
181 } 273 }
182 274
275 if (ifname != NULL && (*ifname = strdup(ifr.ifr_name)))
276 goto failed;
277
183 close(sock); 278 close(sock);
184 return (fd); 279 return (fd);
185 280
diff --git a/openbsd-compat/port-tun.h b/openbsd-compat/port-net.h
index 103514370..3a0d1104b 100644
--- a/openbsd-compat/port-tun.h
+++ b/openbsd-compat/port-net.h
@@ -22,7 +22,7 @@ struct ssh;
22 22
23#if defined(SSH_TUN_LINUX) || defined(SSH_TUN_FREEBSD) 23#if defined(SSH_TUN_LINUX) || defined(SSH_TUN_FREEBSD)
24# define CUSTOM_SYS_TUN_OPEN 24# define CUSTOM_SYS_TUN_OPEN
25int sys_tun_open(int, int); 25int sys_tun_open(int, int, char **);
26#endif 26#endif
27 27
28#if defined(SSH_TUN_COMPAT_AF) || defined(SSH_TUN_PREPEND_AF) 28#if defined(SSH_TUN_COMPAT_AF) || defined(SSH_TUN_PREPEND_AF)
@@ -31,4 +31,18 @@ int sys_tun_infilter(struct ssh *, struct Channel *, char *, int);
31u_char *sys_tun_outfilter(struct ssh *, struct Channel *, u_char **, size_t *); 31u_char *sys_tun_outfilter(struct ssh *, struct Channel *, u_char **, size_t *);
32#endif 32#endif
33 33
34#if defined(SYS_RDOMAIN_LINUX)
35# define HAVE_SYS_GET_RDOMAIN
36# define HAVE_SYS_SET_RDOMAIN
37# define HAVE_SYS_VALID_RDOMAIN
38char *sys_get_rdomain(int fd);
39int sys_set_rdomain(int fd, const char *name);
40int sys_valid_rdomain(const char *name);
41#endif
42
43#if defined(SYS_RDOMAIN_XXX)
44# define HAVE_SYS_SET_PROCESS_RDOMAIN
45void sys_set_process_rdomain(const char *name);
46#endif
47
34#endif 48#endif
diff --git a/openbsd-compat/port-uw.c b/openbsd-compat/port-uw.c
index db24dbb94..014cac264 100644
--- a/openbsd-compat/port-uw.c
+++ b/openbsd-compat/port-uw.c
@@ -47,12 +47,14 @@
47#include "hostfile.h" 47#include "hostfile.h"
48#include "auth.h" 48#include "auth.h"
49#include "ssh.h" 49#include "ssh.h"
50#include "ssh_api.h"
50 51
51int nischeck(char *); 52int nischeck(char *);
52 53
53int 54int
54sys_auth_passwd(Authctxt *authctxt, const char *password) 55sys_auth_passwd(struct ssh *ssh, const char *password)
55{ 56{
57 Authctxt *authctxt = ssh->authctxt;
56 struct passwd *pw = authctxt->pw; 58 struct passwd *pw = authctxt->pw;
57 char *salt; 59 char *salt;
58 int result; 60 int result;
diff --git a/openbsd-compat/readpassphrase.c b/openbsd-compat/readpassphrase.c
index 24aed6e46..ff8ff3dec 100644
--- a/openbsd-compat/readpassphrase.c
+++ b/openbsd-compat/readpassphrase.c
@@ -46,14 +46,6 @@
46# define _POSIX_VDISABLE VDISABLE 46# define _POSIX_VDISABLE VDISABLE
47#endif 47#endif
48 48
49#ifndef _NSIG
50# ifdef NSIG
51# define _NSIG NSIG
52# else
53# define _NSIG 128
54# endif
55#endif
56
57static volatile sig_atomic_t signo[_NSIG]; 49static volatile sig_atomic_t signo[_NSIG];
58 50
59static void handler(int); 51static void handler(int);
diff --git a/openbsd-compat/regress/Makefile.in b/openbsd-compat/regress/Makefile.in
index dabdb0912..529331be5 100644
--- a/openbsd-compat/regress/Makefile.in
+++ b/openbsd-compat/regress/Makefile.in
@@ -1,5 +1,3 @@
1# $Id: Makefile.in,v 1.5 2014/06/17 13:06:08 dtucker Exp $
2
3sysconfdir=@sysconfdir@ 1sysconfdir=@sysconfdir@
4piddir=@piddir@ 2piddir=@piddir@
5srcdir=@srcdir@ 3srcdir=@srcdir@
diff --git a/openbsd-compat/strndup.c b/openbsd-compat/strndup.c
new file mode 100644
index 000000000..ebb4eccfb
--- /dev/null
+++ b/openbsd-compat/strndup.c
@@ -0,0 +1,43 @@
1/* $OpenBSD: strndup.c,v 1.2 2015/08/31 02:53:57 guenther Exp $ */
2
3/*
4 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include "config.h"
20#if !defined(HAVE_STRNDUP) || defined(BROKEN_STRNDUP)
21#include <sys/types.h>
22
23#include <stddef.h>
24#include <stdlib.h>
25#include <string.h>
26
27char *
28strndup(const char *str, size_t maxlen)
29{
30 char *copy;
31 size_t len;
32
33 len = strnlen(str, maxlen);
34 copy = malloc(len + 1);
35 if (copy != NULL) {
36 (void)memcpy(copy, str, len);
37 copy[len] = '\0';
38 }
39
40 return copy;
41}
42DEF_WEAK(strndup);
43#endif /* HAVE_STRNDUP */
diff --git a/openbsd-compat/strnlen.c b/openbsd-compat/strnlen.c
index 93d515595..8cc6b96b5 100644
--- a/openbsd-compat/strnlen.c
+++ b/openbsd-compat/strnlen.c
@@ -19,7 +19,7 @@
19/* OPENBSD ORIGINAL: lib/libc/string/strnlen.c */ 19/* OPENBSD ORIGINAL: lib/libc/string/strnlen.c */
20 20
21#include "config.h" 21#include "config.h"
22#ifndef HAVE_STRNLEN 22#if !defined(HAVE_STRNLEN) || defined(BROKEN_STRNLEN)
23#include <sys/types.h> 23#include <sys/types.h>
24 24
25#include <string.h> 25#include <string.h>