newseed.c revision 302408
154359Sroberto/* newseed.c: The opienewseed() library function. 2182007Sroberto 354359Sroberto%%% copyright-cmetz-96 4182007SrobertoThis software is Copyright 1996-2001 by Craig Metz, All Rights Reserved. 5182007SrobertoThe Inner Net License Version 3 applies to this software. 654359SrobertoYou should have received a copy of the license with this software. If 754359Srobertoyou didn't get a copy, you may request one from <license@inner.net>. 8182007Sroberto 9182007Sroberto History: 10182007Sroberto 11182007Sroberto Modified by cmetz for OPIE 2.4. Greatly simplified increment. Now does 12182007Sroberto not add digits. Reformatted the code. 13182007Sroberto Modified by cmetz for OPIE 2.32. Added syslog.h if DEBUG. 14182007Sroberto Modified by cmetz for OPIE 2.31. Added time.h. 15182007Sroberto Created by cmetz for OPIE 2.22. 16182007Sroberto 17182007Sroberto$FreeBSD: stable/11/contrib/opie/libopie/newseed.c 257264 2013-10-28 18:24:31Z sbruno $ 18182007Sroberto*/ 19182007Sroberto 20182007Sroberto#include "opie_cfg.h" 21182007Sroberto#ifndef HAVE_TIME_H 22182007Sroberto#define HAVE_TIME_H 1 23182007Sroberto#endif 24182007Sroberto#if HAVE_TIME_H 25182007Sroberto#include <time.h> 26182007Sroberto#endif /* HAVE_TIME_H */ 27182007Sroberto#if HAVE_STRING_H 28182007Sroberto#include <string.h> 29182007Sroberto#endif /* HAVE_STRING_H */ 30182007Sroberto#include <ctype.h> 31182007Sroberto#if HAVE_UNISTD_H 32182007Sroberto#include <unistd.h> 33182007Sroberto#endif /* HAVE_UNISTD_H */ 3454359Sroberto#if HAVE_SYS_UTSNAME_H 3554359Sroberto#include <sys/utsname.h> 3654359Sroberto#endif /* HAVE_SYS_UTSNAME_H */ 3754359Sroberto#include <errno.h> 3854359Sroberto#if DEBUG 3954359Sroberto#include <syslog.h> 4054359Sroberto#endif /* DEBUG */ 4154359Sroberto#include <stdio.h> 4254359Sroberto#include <stdlib.h> 4354359Sroberto#include "opie.h" 4454359Sroberto 4554359Srobertoint opienewseed FUNCTION((seed), char *seed) 4654359Sroberto{ 4754359Sroberto if (!seed) 4854359Sroberto return -1; 4954359Sroberto 5054359Sroberto if (seed[0]) { 5154359Sroberto char *c; 5254359Sroberto unsigned int i, max; 5354359Sroberto 5454359Sroberto if ((i = strlen(seed)) > OPIE_SEED_MAX) 5554359Sroberto i = OPIE_SEED_MAX; 5654359Sroberto 5754359Sroberto for (c = seed + i - 1, max = 1; 5854359Sroberto (c >= seed) && isdigit(*c); c--) 5954359Sroberto max *= 10; 60182007Sroberto 61182007Sroberto if ((i = strtoul(++c, (char **)0, 10)) < max) { 6254359Sroberto if (++i >= max) 63182007Sroberto i = 1; 64182007Sroberto 65182007Sroberto sprintf(c, "%d", i); 66182007Sroberto return 0; 67182007Sroberto } 68182007Sroberto } 6954359Sroberto 7054359Sroberto { 7154359Sroberto time_t now; 7254359Sroberto 7354359Sroberto time(&now); 7454359Sroberto srand(now); 7554359Sroberto } 7654359Sroberto 7754359Sroberto { 7854359Sroberto struct utsname utsname; 79 80 if (uname(&utsname) < 0) { 81#if DEBUG 82 syslog(LOG_DEBUG, "uname: %s(%d)", strerror(errno), 83 errno); 84#endif /* DEBUG */ 85 utsname.nodename[0] = 'k'; 86 utsname.nodename[1] = 'e'; 87 } 88 utsname.nodename[2] = 0; 89 90 if (snprintf(seed, OPIE_SEED_MAX+1, "%s%04d", utsname.nodename, 91 (rand() % 9999) + 1) >= OPIE_SEED_MAX+1) 92 return -1; 93 return 0; 94 } 95} 96 97