diff options
author | Damien Miller <djm@mindrot.org> | 1999-11-26 12:21:24 +1100 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 1999-11-26 12:21:24 +1100 |
commit | 42b81ffe7d1e98b362696076edbc56a66368053e (patch) | |
tree | 0c11756e8b9def63760aa59e6d2a195fcc380196 | |
parent | bf3f6ef19b8d43b09598807cc30de7e66be37563 (diff) |
- Add definition for __P()
- Added [v]snprintf() replacement for systems that lack it
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | Makefile.in | 4 | ||||
-rw-r--r-- | acconfig.h | 4 | ||||
-rw-r--r-- | bsd-snprintf.c | 162 | ||||
-rw-r--r-- | bsd-snprintf.h | 17 | ||||
-rw-r--r-- | configure.in | 2 | ||||
-rw-r--r-- | includes.h | 1 |
7 files changed, 191 insertions, 3 deletions
@@ -1,3 +1,7 @@ +19991126 + - Add definition for __P() + - Added [v]snprintf() replacement for systems that lack it + 19991125 - More reformatting merged from OpenBSD CVS - Merged OpenBSD CVS changes: diff --git a/Makefile.in b/Makefile.in index e09fcc2f..abf81fa5 100644 --- a/Makefile.in +++ b/Makefile.in @@ -30,11 +30,11 @@ OBJS= authfd.o authfile.o auth-passwd.o auth-rhosts.o auth-rh-rsa.o \ packet.o pty.o readconf.o readpass.o rsa.o servconf.o serverloop.o \ sshconnect.o tildexpand.o ttymodes.o uidswap.o xmalloc.o \ helper.o bsd-mktemp.o bsd-strlcpy.o bsd-strlcat.o bsd-daemon.o \ - bsd-login.o rc4.o md5crypt.o + bsd-login.o bsd-snprintf.o rc4.o md5crypt.o all: $(OBJS) $(TARGETS) -libssh.a: authfd.o authfile.o bufaux.o buffer.o canohost.o channels.o cipher.o compat.o compress.o crc32.o deattack.o hostfile.o match.o mpaux.o nchan.o packet.o readpass.o rsa.o tildexpand.o ttymodes.o uidswap.o xmalloc.o helper.o rc4.o bsd-mktemp.o bsd-strlcpy.o bsd-strlcat.o log.o fingerprint.o +libssh.a: authfd.o authfile.o bufaux.o buffer.o canohost.o channels.o cipher.o compat.o compress.o crc32.o deattack.o hostfile.o match.o mpaux.o nchan.o packet.o readpass.o rsa.o tildexpand.o ttymodes.o uidswap.o xmalloc.o helper.o rc4.o bsd-mktemp.o bsd-strlcpy.o bsd-strlcat.o bsd-snprintf.o log.o fingerprint.o $(AR) rv $@ $^ $(RANLIB) $@ @@ -221,3 +221,7 @@ enum #else # define PAM_STRERROR(a,b) pam_strerror((a),(b)) #endif + +#ifndef __P +# define __P(x) x +#endif diff --git a/bsd-snprintf.c b/bsd-snprintf.c new file mode 100644 index 00000000..11c4ff39 --- /dev/null +++ b/bsd-snprintf.c @@ -0,0 +1,162 @@ +/* + * Revision 12: http://theos.com/~deraadt/snprintf.c + * + * Copyright (c) 1997 Theo de Raadt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) + +#include <sys/param.h> +#include <sys/types.h> +#include <sys/mman.h> +#include <signal.h> +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#if __STDC__ +#include <stdarg.h> +#include <stdlib.h> +#else +#include <varargs.h> +#endif +#include <setjmp.h> + +#ifndef roundup +#define roundup (x, y) ((((x)+((y)-1))/(y))*(y)) +#endif + +static int pgsize; +static char *curobj; +static int caught; +static sigjmp_buf bail; + +#define EXTRABYTES 2 /* XXX: why 2? you don't want to know */ + +static char * +msetup(str, n) + char *str; + size_t n; +{ + char *e; + + if (n == 0) + return NULL; + if (pgsize == 0) + pgsize = getpagesize(); + curobj = (char *)malloc(n + EXTRABYTES + pgsize * 2); + if (curobj == NULL) + return NULL; + e = curobj + n + EXTRABYTES; + e = (char *)roundup((unsigned long)e, pgsize); + if (mprotect(e, pgsize, PROT_NONE) == -1) { + free(curobj); + curobj = NULL; + return NULL; + } + e = e - n - EXTRABYTES; + *e = '\0'; + return (e); +} + +static void +mcatch() +{ + siglongjmp(bail, 1); +} + +static void +mcleanup(str, n, p) + char *str; + size_t n; + char *p; +{ + strncpy(str, p, n-1); + str[n-1] = '\0'; + if (mprotect((caddr_t)(p + n + EXTRABYTES), pgsize, + PROT_READ|PROT_WRITE|PROT_EXEC) == -1) + mprotect((caddr_t)(p + n + EXTRABYTES), pgsize, + PROT_READ|PROT_WRITE); + free(curobj); +} + +#if !defined(HAVE_SNPRINTF) +int +#if __STDC__ +snprintf(char *str, size_t n, char const *fmt, ...) +#else +snprintf(str, n, fmt, va_alist) + char *str; + size_t n; + char *fmt; + va_dcl +#endif +{ + va_list ap; +#if __STDC__ + va_start(ap, fmt); +#else + va_start(ap); +#endif + + return (vsnprintf(str, n, fmt, ap)); + va_end(ap); +} +#endif /* !defined(HAVE_SNPRINTF) */ + +#if !defined(HAVE_VSNPRINTF) +int +vsnprintf(str, n, fmt, ap) + char *str; + size_t n; + char *fmt; + char *ap; +{ + struct sigaction osa, nsa; + char *p; + int ret = n + 1; /* if we bail, indicated we overflowed */ + + memset(&nsa, 0, sizeof nsa); + nsa.sa_handler = mcatch; + sigemptyset(&nsa.sa_mask); + + p = msetup(str, n); + if (p == NULL) { + *str = '\0'; + return 0; + } + if (sigsetjmp(bail, 1) == 0) { + if (sigaction(SIGSEGV, &nsa, &osa) == -1) { + mcleanup(str, n, p); + return (0); + } + ret = vsprintf(p, fmt, ap); + } + mcleanup(str, n, p); + (void) sigaction(SIGSEGV, &osa, NULL); + return (ret); +} +#endif /* !defined(HAVE_VSNPRINTF) */ + +#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */ diff --git a/bsd-snprintf.h b/bsd-snprintf.h new file mode 100644 index 00000000..d4f83aac --- /dev/null +++ b/bsd-snprintf.h @@ -0,0 +1,17 @@ +#ifndef _BSD_SNPRINTF_H +#define _BSD_SNPRINTF_H + +#include "config.h" + +#include <sys/types.h> /* For size_t */ + +#ifndef HAVE_SNPRINTF +int snprintf(char *str, size_t n, char const *fmt, ...); +#endif /* !HAVE_SNPRINTF */ + +#ifndef HAVE_VSNPRINTF +int vsnprintf(char *str, size_t n, char *fmt, char *ap); +#endif /* !HAVE_SNPRINTF */ + + +#endif /* _BSD_SNPRINTF_H */ diff --git a/configure.in b/configure.in index 037478a6..86f2dde7 100644 --- a/configure.in +++ b/configure.in @@ -59,7 +59,7 @@ dnl Checks for header files. AC_CHECK_HEADERS(endian.h lastlog.h login.h maillock.h netgroup.h paths.h pty.h shadow.h util.h utmp.h sys/select.h sys/time.h) dnl Checks for library functions. -AC_CHECK_FUNCS(arc4random mkdtemp openpty setenv setlogin setproctitle strlcat strlcpy) +AC_CHECK_FUNCS(arc4random mkdtemp openpty setenv setlogin setproctitle snprintf strlcat strlcpy vsnprintf) AC_CHECK_FUNC(login, [AC_DEFINE(HAVE_LOGIN)], @@ -76,6 +76,7 @@ static /**/const char *const rcsid[] = { (char *)rcsid, "\100(#)" msg } #include "bsd-strlcpy.h" #include "bsd-strlcat.h" #include "bsd-mktemp.h" +#include "bsd-snprintf.h" /* Define this to be the path of the xauth program. */ #ifndef XAUTH_PATH |