diff options
author | markm <markm@FreeBSD.org> | 2002-03-21 23:42:52 +0000 |
---|---|---|
committer | markm <markm@FreeBSD.org> | 2002-03-21 23:42:52 +0000 |
commit | b38a0011c18c49fcb516cc2b4f5d751565e53f44 (patch) | |
tree | 1ec99b3b189dcbff75d7f05f7c29bd03c4d795da /contrib/opie/libopie | |
parent | 084b4588f24346b3d369eaf49193461c380e8521 (diff) | |
download | FreeBSD-src-b38a0011c18c49fcb516cc2b4f5d751565e53f44.zip FreeBSD-src-b38a0011c18c49fcb516cc2b4f5d751565e53f44.tar.gz |
Resolve conflicts.
Diffstat (limited to 'contrib/opie/libopie')
-rw-r--r-- | contrib/opie/libopie/challenge.c | 8 | ||||
-rw-r--r-- | contrib/opie/libopie/generator.c | 364 | ||||
-rw-r--r-- | contrib/opie/libopie/hash.c | 10 | ||||
-rw-r--r-- | contrib/opie/libopie/hashlen.c | 8 | ||||
-rw-r--r-- | contrib/opie/libopie/insecure.c | 28 | ||||
-rw-r--r-- | contrib/opie/libopie/lock.c | 7 | ||||
-rw-r--r-- | contrib/opie/libopie/newseed.c | 109 |
7 files changed, 411 insertions, 123 deletions
diff --git a/contrib/opie/libopie/challenge.c b/contrib/opie/libopie/challenge.c index b931714..fc836aa 100644 --- a/contrib/opie/libopie/challenge.c +++ b/contrib/opie/libopie/challenge.c @@ -1,7 +1,7 @@ /* challenge.c: The opiechallenge() library function. %%% portions-copyright-cmetz-96 -Portions of this software are Copyright 1996-1998 by Craig Metz, All Rights +Portions of this software are Copyright 1996-1999 by Craig Metz, All Rights Reserved. The Inner Net License Version 2 applies to these portions of the software. You should have received a copy of the license with this software. If @@ -67,11 +67,11 @@ int opiechallenge FUNCTION((mp, name, ss), struct opie *mp AND char *name AND ch #endif /* DEBUG */ } - if (rval) { + if (rval || + (snprintf(ss, OPIE_CHALLENGE_MAX, "otp-%s %d %s ext", algids[MDX], mp->opie_n - 1, mp->opie_seed) >= OPIE_CHALLENGE_MAX)) { opierandomchallenge(ss); memset(mp, 0, sizeof(*mp)); - } else - sprintf(ss, "otp-%s %d %s ext", algids[MDX], mp->opie_n - 1, mp->opie_seed); + } return rval; } diff --git a/contrib/opie/libopie/generator.c b/contrib/opie/libopie/generator.c index eef7cac..d4888f9 100644 --- a/contrib/opie/libopie/generator.c +++ b/contrib/opie/libopie/generator.c @@ -1,7 +1,7 @@ /* generator.c: The opiegenerator() library function. %%% portions-copyright-cmetz-96 -Portions of this software are Copyright 1996-1998 by Craig Metz, All Rights +Portions of this software are Copyright 1996-1999 by Craig Metz, All Rights Reserved. The Inner Net License Version 2 applies to these portions of the software. You should have received a copy of the license with this software. If @@ -9,6 +9,9 @@ you didn't get a copy, you may request one from <license@inner.net>. History: + Modified by cmetz for OPIE 2.4. Added opieauto code based on + previously released test code. Renamed buffer to challenge. + Use struct opie_otpkey for keys. Modified by cmetz for OPIE 2.32. If secret=NULL, always return as if opieauto returned "get the secret". Renamed _opieparsechallenge() to __opieparsechallenge(). Check @@ -33,73 +36,362 @@ $FreeBSD$ #if HAVE_STRING_H #include <string.h> #endif /* HAVE_STRING_H */ +#if OPIEAUTO +#include <errno.h> +#if HAVE_STDLIB_H +#include <stdlib.h> +#endif /* HAVE_STDLIB_H */ +#include <sys/stat.h> + +#include <sys/socket.h> +#include <sys/un.h> +#endif /* OPIEAUTO */ +#if DEBUG +#include <syslog.h> +#endif /* DEBUG */ #include "opie.h" static char *algids[] = { NULL, NULL, NULL, "sha1", "md4", "md5" }; -int opiegenerator FUNCTION((buffer, secret, response), char *buffer AND char *secret AND char *response) +#if OPIEAUTO +#ifndef max +#define max(x, y) (((x) > (y)) ? (x) : (y)) +#endif /* max */ + +static int opieauto_connect FUNCTION_NOARGS +{ + int s; + struct sockaddr_un sun; + char buffer[1024]; + char *c, *c2 ="/.opieauto"; + uid_t myuid = getuid(), myeuid = geteuid(); + + if (!myuid || !myeuid || (myuid != myeuid)) { +#if DEBUG + syslog(LOG_DEBUG, "opieauto_connect: superuser and/or setuid not allowed"); +#endif /* DEBUG */ + return -1; + }; + + memset(&sun, 0, sizeof(struct sockaddr_un)); + sun.sun_family = AF_UNIX; + + if (!(c = getenv("HOME"))) { +#if DEBUG + syslog(LOG_DEBUG, "opieauto_connect: no HOME variable?"); +#endif /* DEBUG */ + return -1; + }; + + if (strlen(c) > (sizeof(sun.sun_path) - strlen(c2) - 1)) { +#if DEBUG + syslog(LOG_DEBUG, "opieauto_connect: HOME is too long: %s", c); +#endif /* DEBUG */ + return -1; + }; + + strcpy(sun.sun_path, c); + strcat(sun.sun_path, c2); + + if ((s = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { +#if DEBUG + syslog(LOG_DEBUG, "opieauto_connect: socket: %s(%d)", strerror(errno), errno); +#endif /* DEBUG */ + return -1; + }; + + { + struct stat st; + + if (stat(sun.sun_path, &st) < 0) { +#if DEBUG + syslog(LOG_DEBUG, "opieauto_connect: stat: %s(%d)\n", strerror(errno), errno); +#endif /* DEBUG */ + goto ret; + }; + + if (connect(s, (struct sockaddr *)&sun, sizeof(struct sockaddr_un))) { +#if DEBUG + syslog(LOG_DEBUG, "opieauto_connect: connect: %s(%d)\n", strerror(errno), errno); +#endif /* DEBUG */ + goto ret; + }; + + if ((st.st_uid != myuid) || (!S_ISSOCK(st.st_mode)) || ((st.st_mode & 07777) != 0600)) { +#if DEBUG + syslog(LOG_DEBUG, "opieauto_connect: something's fishy about the socket\n"); +#endif /* DEBUG */ + goto ret; + }; + }; + + return s; + +ret: + close(s); + return -1; +}; +#endif /* OPIEAUTO */ + +int opiegenerator FUNCTION((challenge, secret, response), char *challenge AND char *secret AND char *response) { int algorithm; int sequence; char *seed; - char key[8]; + struct opie_otpkey key; int i; int exts; +#if OPIEAUTO + int s; + int window; + char cmd[1+1+1+1+4+1+OPIE_SEED_MAX+1+4+1+4+1+4+1+4+1]; + char *c; +#endif /* OPIEAUTO */ - if (!(buffer = strstr(buffer, "otp-"))) + if (!(challenge = strstr(challenge, "otp-"))) return 1; - buffer += 4; + challenge += 4; - if (__opieparsechallenge(buffer, &algorithm, &sequence, &seed, &exts)) + if (__opieparsechallenge(challenge, &algorithm, &sequence, &seed, &exts)) return 1; if ((sequence < 2) || (sequence > 9999)) return 1; - if (!secret[0]) - return 2; + if (*secret) { + if (opiepasscheck(secret)) + return -2; - if (opiepasscheck(secret)) - return -2; + if (i = opiekeycrunch(algorithm, &key, seed, secret)) + return i; + + if (sequence <= OPIE_SEQUENCE_RESTRICT) { + if (!(exts & 1)) + return 1; + + { + char newseed[OPIE_SEED_MAX + 1]; + struct opie_otpkey newkey; + char *c; + char buf[OPIE_SEED_MAX + 48 + 1]; + + while (sequence-- != 0) + opiehash(&key, algorithm); + + if (opienewseed(strcpy(newseed, seed)) < 0) + return -1; + + if (opiekeycrunch(algorithm, &newkey, newseed, secret)) + return -1; - if (i = opiekeycrunch(algorithm, key, seed, secret)) - return i; + for (i = 0; i < 499; i++) + opiehash(&newkey, algorithm); - if (sequence < 10) { - if (!(exts & 1)) - return 1; + strcpy(response, "init-hex:"); + strcat(response, opiebtoh(buf, &key)); + if (snprintf(buf, sizeof(buf), ":%s 499 %s:", algids[algorithm], + newseed) >= sizeof(buf)) { +#ifdef DEBUG + syslog(LOG_DEBUG, "opiegenerator: snprintf truncation at init-hex"); +#endif /* DEBUG */ + return -1; + } + strcat(response, buf); + strcat(response, opiebtoh(buf, &newkey)); + }; + }; + }; + +#if OPIEAUTO + if ((s = opieauto_connect()) >= 0) { + if ((i = read(s, cmd, sizeof(cmd)-1)) < 0) { +#if DEBUG + syslog(LOG_DEBUG, "opiegenerator: read: %s(%d)\n", strerror(errno), errno); +#endif /* DEBUG */ + close(s); + s = -1; + goto l0; + }; + cmd[i] = 0; + if ((cmd[0] != 'C') || (cmd[1] != '+') || (cmd[2] != ' ')) { +#if DEBUG + syslog(LOG_DEBUG, "opiegenerator: got invalid/failing C+ response: %s\n", cmd); +#endif /* DEBUG */ + close(s); + s = -1; + goto l0; + }; + + window = strtoul(&cmd[3], &c, 10); + if (!window || (window >= (OPIE_SEQUENCE_MAX - OPIE_SEQUENCE_RESTRICT)) || !isspace(*c)) { +#if DEBUG + syslog(LOG_DEBUG, "opiegenerator: got bogus option response: %s\n", cmd); +#endif /* DEBUG */ + close(s); + s = -1; + goto l0; + }; + }; + +l0: + if (*secret) { + int j; + + if (s < 0) { + j = 0; + goto l1; + }; + + j = max(sequence - window + 1, OPIE_SEQUENCE_RESTRICT); + + for (i = j; i > 0; i--) + opiehash(&key, algorithm); { - char newseed[OPIE_SEED_MAX + 1]; - char newkey[8]; - char *c; - char buf[OPIE_SEED_MAX + 48 + 1]; + char buf[16+1]; - while (sequence-- != 0) - opiehash(key, algorithm); + opiebtoa8(buf, &key); - if (opienewseed(strcpy(newseed, seed)) < 0) - return -1; + if (snprintf(cmd, sizeof(cmd), "S= %d %d %s %s\n", algorithm, sequence, + seed, buf) >= sizeof(cmd)) { +#if DEBUG + syslog(LOG_DEBUG, "opiegenerator: snprintf truncation at S=\n"); +#endif /* DEBUG */ + goto l1; + } + } - if (opiekeycrunch(algorithm, newkey, newseed, secret)) - return -1; + if (write(s, cmd, i = strlen(cmd)) != i) { +#if DEBUG + syslog(LOG_DEBUG, "opiegenerator: write: %s(%d)\n", strerror(errno), errno); +#endif /* DEBUG */ + goto l1; + }; - for (i = 0; i < 499; i++) - opiehash(newkey, algorithm); + if ((i = read(s, cmd, sizeof(cmd))) < 0) { +#if DEBUG + syslog(LOG_DEBUG, "opiegenerator: read: %s(%d)\n", strerror(errno), errno); +#endif /* DEBUG */ + }; + close(s); - strcpy(response, "init-hex:"); - strcat(response, opiebtoh(buf, key)); - sprintf(buf, ":%s 499 %s:", algids[algorithm], newseed); - strcat(response, buf); - strcat(response, opiebtoh(buf, newkey)); + cmd[i] = 0; + i = strlen(seed); + if ((cmd[0] != 'S') || (cmd[1] != '+') || (cmd[2] != ' ') || (strtoul(&cmd[3], &c, 10) != algorithm) || (strtoul(c + 1, &c, 10) != sequence) || strncmp(++c, seed, i) || (*(c + i) != '\n')) { +#if DEBUG + syslog(LOG_DEBUG, "opiegenerator: got invalid/failing S+ response: %s\n", cmd); +#endif /* DEBUG */ }; + +l1: + for (i = sequence - j; i > 0; i--) + opiehash(&key, algorithm); + + opiebtoh(response, &key); } else { + if (s < 0) + goto l2; + + if ((snprintf(cmd, sizeof(cmd), "s= %d %d %s\n", algorithm, sequence, + seed) >= sizeof(cmd))) { +#if DEBUG + syslog(LOG_DEBUG, "opiegenerator: snprintf truncation at s=\n"); +#endif /* DEBUG */ + goto l2; + } + + if (write(s, cmd, i = strlen(cmd)) != i) { +#if DEBUG + syslog(LOG_DEBUG, "opiegenerator: write: %s(%d)\n", strerror(errno), errno); +#endif /* DEBUG */ + goto l2; + }; + + if ((i = read(s, cmd, sizeof(cmd))) < 0) { +#if DEBUG + syslog(LOG_DEBUG, "opiegenerator: read: %s(%d)\n", strerror(errno), errno); +#endif /* DEBUG */ + goto l2; + }; + close(s); + + i = strlen(seed); + + if ((cmd[0] != 's') || (cmd[2] != ' ') || (strtoul(&cmd[3], &c, 10) != algorithm) || (strtoul(c + 1, &c, 10) != sequence) || strncmp(++c, seed, i)) { +#if DEBUG + if (c) + *c = 0; + else + cmd[3] = 0; + + syslog(LOG_DEBUG, "opiegenerator: got bogus/invalid s response: %s\n", cmd); +#endif /* DEBUG */ + goto l2; + }; + + c += i; + + if (cmd[1] == '-') { +#if DEBUG + if (*c != '\n') { + *c = 0; + syslog(LOG_DEBUG, "opiegenerator: got invalid s- response: %s\n", cmd); + }; +#endif /* DEBUG */ + goto l2; + }; + + if (cmd[1] != '+') { +#if DEBUG + *c = 0; + syslog(LOG_DEBUG, "opiegenerator: got invalid s response: %s\n", cmd); +#endif /* DEBUG */ + goto l2; + }; + + { + char *c2; + + if (!(c2 = strchr(++c, '\n'))) { +#if DEBUG + *c = 0; + syslog(LOG_DEBUG, "opiegenerator: got invalid s+ response: %s\n", cmd); +#endif /* DEBUG */ + goto l2; + }; + + *c2++ = 0; + }; + + if (!opieatob8(&key, c)) + goto l2; + + opiebtoh(response, &key); + }; + + if (s >= 0) + close(s); +#else /* OPIEAUTO */ + if (*secret) { while (sequence-- != 0) - opiehash(key, algorithm); + opiehash(&key, algorithm); - opiebtoh(response, key); - } + opiebtoh(response, &key); + } else + return -2; +#endif /* OPIEAUTO */ return 0; -} + +#if OPIEAUTO +l2: +#if DEBUG + syslog(LOG_DEBUG, "opiegenerator: no opieauto response available.\n"); +#endif /* DEBUG */ + if (s >= 0) + close(s); + + return -2; +#endif /* OPIEAUTO */ +}; diff --git a/contrib/opie/libopie/hash.c b/contrib/opie/libopie/hash.c index 0edbdd1..babcbfa 100644 --- a/contrib/opie/libopie/hash.c +++ b/contrib/opie/libopie/hash.c @@ -1,14 +1,15 @@ /* hash.c: The opiehash() library function. %%% copyright-cmetz-96 -This software is Copyright 1996-1998 by Craig Metz, All Rights Reserved. -The Inner Net License Version 2 applies to this software. +This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved. +The Inner Net License Version 3 applies to this software. You should have received a copy of the license with this software. If you didn't get a copy, you may request one from <license@inner.net>. History: - Updated by cmetz for OPIE 2.31. Added SHA support (which may + Modified by cmetz for OPIE 2.4. Use struct opie_otpkey for binary arg. + Modified by cmetz for OPIE 2.31. Added SHA support (which may not be correct). Backed out previous optimizations as they killed thread-safety. Created by cmetz for OPIE 2.3 using the old hash.c as a guide. @@ -23,7 +24,8 @@ $FreeBSD$ #include <md4.h> #include <md5.h> -VOIDRET opiehash FUNCTION((x, algorithm), VOIDPTR x AND unsigned algorithm) +VOIDRET opiehash FUNCTION((x, algorithm), struct opie_otpkey *x AND +unsigned algorithm) { UINT4 *results = (UINT4 *)x; diff --git a/contrib/opie/libopie/hashlen.c b/contrib/opie/libopie/hashlen.c index 66f7d2c..29d855d 100644 --- a/contrib/opie/libopie/hashlen.c +++ b/contrib/opie/libopie/hashlen.c @@ -1,13 +1,14 @@ /* hashlen.c: The opiehashlen() library function. %%% copyright-cmetz-96 -This software is Copyright 1996-1998 by Craig Metz, All Rights Reserved. -The Inner Net License Version 2 applies to this software. +This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved. +The Inner Net License Version 3 applies to this software. You should have received a copy of the license with this software. If you didn't get a copy, you may request one from <license@inner.net>. History: + Modified by cmetz for OPIE 2.4. Use struct opie_otpkey, isolate variables. Created by cmetz for OPIE 2.3. $FreeBSD$ @@ -20,7 +21,8 @@ $FreeBSD$ #include <md4.h> #include <md5.h> -VOIDRET opiehashlen FUNCTION((algorithm, in, out, n), int algorithm AND VOIDPTR in AND VOIDPTR out AND int n) +VOIDRET opiehashlen FUNCTION((algorithm, in, out, n), int algorithm AND +VOIDPTR in AND struct opie_otpkey *out AND int n) { UINT4 *results = (UINT4 *)out; UINT4 mdx_tmp[4]; diff --git a/contrib/opie/libopie/insecure.c b/contrib/opie/libopie/insecure.c index e02183a..bc61c54 100644 --- a/contrib/opie/libopie/insecure.c +++ b/contrib/opie/libopie/insecure.c @@ -1,7 +1,7 @@ /* insecure.c: The opieinsecure() library function. %%% portions-copyright-cmetz-96 -Portions of this software are Copyright 1996-1998 by Craig Metz, All Rights +Portions of this software are Copyright 1996-1999 by Craig Metz, All Rights Reserved. The Inner Net License Version 2 applies to these portions of the software. You should have received a copy of the license with this software. If @@ -14,6 +14,8 @@ License Agreement applies to this software. History: + Modified by cmetz for OPIE 2.4. Do utmp checks on utmpx systems. + Handle unterminated ut_host. Modified by cmetz for OPIE 2.31. Fixed a logic bug. Call endut[x]ent(). Modified by cmetz for OPIE 2.3. Added result caching. Use __opiegetutmpentry(). Ifdef around ut_host check. Eliminate @@ -59,9 +61,9 @@ int opieinsecure FUNCTION_NOARGS char *s; char *term_name; int insecure = 0; -#if HAVE_UT_HOST +#if HAVE_UT_HOST || DOUTMPX struct utmp utmp; -#endif /* HAVE_UT_HOST */ +#endif /* HAVE_UT_HOST || DOUTMPX */ static int result = -1; if (result != -1) @@ -122,30 +124,34 @@ int opieinsecure FUNCTION_NOARGS return (result = 1); }; -#if HAVE_UT_HOST +#if HAVE_UT_HOST || DOUTMPX if (isatty(0)) { memset(&utmp, 0, sizeof(struct utmp)); { int i = __opiegetutmpentry(ttyname(0), &utmp); endutent(); if (!i && utmp.ut_host[0]) { + char host[sizeof(utmp.ut_host) + 1]; insecure = 1; - if (s = strchr(utmp.ut_host, ':')) { - int n = s - utmp.ut_host; + strncpy(host, utmp.ut_host, sizeof(utmp.ut_host)); + host[sizeof(utmp.ut_host)] = 0; + + if (s = strchr(host, ':')) { + int n = s - host; if (!n) insecure = 0; else if (display_name) { - if (!strncmp(utmp.ut_host, display_name, n)) + if (!strncmp(host, display_name, n)) insecure = 0; -#ifdef SOLARIS +#if 1 /* def SOLARIS */ else - if (s = strchr(utmp.ut_host, ' ')) { + if (s = strchr(host, ' ')) { *s = ':'; if (s = strchr(s + 1, ' ')) *s = '.'; - if (!strncmp(utmp.ut_host, display_name, n)) + if (!strncmp(host, display_name, n)) insecure = 0; } #endif /* SOLARIS */ @@ -154,7 +160,7 @@ int opieinsecure FUNCTION_NOARGS } }; }; -#endif /* HAVE_UT_HOST */ +#endif /* HAVE_UT_HOST || DOUTMPX */ if (insecure) return (result = 1); diff --git a/contrib/opie/libopie/lock.c b/contrib/opie/libopie/lock.c index 0f666a6..865d270 100644 --- a/contrib/opie/libopie/lock.c +++ b/contrib/opie/libopie/lock.c @@ -1,7 +1,7 @@ /* lock.c: The opielock() library function. %%% portions-copyright-cmetz-96 -Portions of this software are Copyright 1996-1998 by Craig Metz, All Rights +Portions of this software are Copyright 1996-1999 by Craig Metz, All Rights Reserved. The Inner Net License Version 2 applies to these portions of the software. You should have received a copy of the license with this software. If @@ -14,6 +14,7 @@ License Agreement applies to this software. History: + Modified by cmetz for OPIE 2.4. Use snprintf. Modified by cmetz for OPIE 2.31. Put locks in a separate dir. Bug fixes. Modified by cmetz for OPIE 2.3. Do refcounts whether or not we @@ -201,7 +202,9 @@ int opielock FUNCTION((principal), char *principal) if (!S_ISREG(statbuf[0].st_mode) || (statbuf[0].st_mode != statbuf[1].st_mode) || (statbuf[0].st_ino != statbuf[1].st_ino)) goto lockret; - sprintf(buffer, "%d\n%d\n", getpid(), time(0)); + if (snprintf(buffer, sizeof(buffer), "%d\n%d\n", getpid(), time(0)) >= sizeof(buffer)) + goto lockret; + i = strlen(buffer) + 1; if (lseek(fh, 0, SEEK_SET)) { close(fh); diff --git a/contrib/opie/libopie/newseed.c b/contrib/opie/libopie/newseed.c index 23cdce4..f32b075 100644 --- a/contrib/opie/libopie/newseed.c +++ b/contrib/opie/libopie/newseed.c @@ -1,13 +1,15 @@ /* newseed.c: The opienewseed() library function. %%% copyright-cmetz-96 -This software is Copyright 1996-1998 by Craig Metz, All Rights Reserved. -The Inner Net License Version 2 applies to this software. +This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved. +The Inner Net License Version 3 applies to this software. You should have received a copy of the license with this software. If you didn't get a copy, you may request one from <license@inner.net>. History: + Modified by cmetz for OPIE 2.4. Greatly simplified increment. Now does + not add digits. Reformatted the code. Modified by cmetz for OPIE 2.32. Added syslog.h if DEBUG. Modified by cmetz for OPIE 2.31. Added time.h. Created by cmetz for OPIE 2.22. @@ -37,73 +39,54 @@ $FreeBSD$ int opienewseed FUNCTION((seed), char *seed) { - if (!seed) - return -1; - - if (seed[0]) { - int i; - - if ((i = strlen(seed)) >= OPIE_SEED_MIN) { - long j; - char *c; - - if (i > OPIE_SEED_MAX) - i = OPIE_SEED_MAX; - - c = seed + i - 1; - - while(c != seed) { - if (!isdigit(*c)) - break; - c--; - } - - c++; - - if (j = strtol(c, (char **)0, 10)) { - char buf[OPIE_SEED_MAX]; - - *c = 0; - strcpy(buf, seed); - - if (errno == ERANGE) { - j = 1; - } else { - int k = 1, l = OPIE_SEED_MAX - strlen(buf); - while(l--) k *= 10; - - if (++j >= k) - j = 1; + if (!seed) + return -1; + + if (seed[0]) { + char *c, *end; + unsigned int i, max; + + if ((i = strlen(seed)) > OPIE_SEED_MAX) + i = OPIE_SEED_MAX; + + for (c = end = seed + i - 1, max = 1; + (c > seed) && isdigit(*c); c--) + max *= 10; + + if ((i = strtoul(++c, (char **)0, 10)) < max) { + if (++i >= max) + i = 1; + + snprintf(c, end - c, "%d", i); + seed[OPIE_SEED_MAX] = 0; + return 0; + } } - sprintf(seed, "%s%04ld", buf, j); - return 0; - } - } - } + { + time_t now; - { - { - time_t now; - time(&now); - srand(now); - } + time(&now); + srand(now); + } - { - struct utsname utsname; + { + struct utsname utsname; - if (uname(&utsname) < 0) { + if (uname(&utsname) < 0) { #if DEBUG - syslog(LOG_DEBUG, "uname: %s(%d)", strerror(errno), errno); + syslog(LOG_DEBUG, "uname: %s(%d)", strerror(errno), + errno); #endif /* DEBUG */ - utsname.nodename[0] = 'k'; - utsname.nodename[1] = 'e'; - } - utsname.nodename[2] = 0; - - sprintf(seed, "%s%04d", utsname.nodename, (rand() % 9999) + 1); - return 0; - } - } + utsname.nodename[0] = 'k'; + utsname.nodename[1] = 'e'; + } + utsname.nodename[2] = 0; + + if (snprintf(seed, OPIE_SEED_MAX+1, "%s%04d", utsname.nodename, + (rand() % 9999) + 1) >= OPIE_SEED_MAX+1) + return -1; + return 0; + } } |