2005-09-20 19:48:17 +04:00
|
|
|
/* $NetBSD: skeyinit.c,v 1.27 2005/09/20 15:48:17 he Exp $ */
|
1994-12-24 20:42:04 +03:00
|
|
|
|
1994-05-24 10:47:42 +04:00
|
|
|
/* S/KEY v1.1b (skeyinit.c)
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Neil M. Haller <nmh@thumper.bellcore.com>
|
|
|
|
* Philip R. Karn <karn@chicago.qualcomm.com>
|
|
|
|
* John S. Walden <jsw@thumper.bellcore.com>
|
|
|
|
* Scott Chasin <chasin@crimelab.com>
|
|
|
|
*
|
2000-07-07 04:18:29 +04:00
|
|
|
* Modifications:
|
|
|
|
* Todd C. Miller <Todd.Miller@courtesan.com>
|
|
|
|
*
|
1994-05-24 10:47:42 +04:00
|
|
|
* S/KEY initialization and seed update
|
|
|
|
*/
|
|
|
|
|
2003-06-23 17:05:47 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
#ifndef lint
|
2005-09-20 19:48:17 +04:00
|
|
|
__RCSID("$NetBSD: skeyinit.c,v 1.27 2005/09/20 15:48:17 he Exp $");
|
2003-06-23 17:05:47 +04:00
|
|
|
#endif
|
|
|
|
|
1994-05-30 07:54:37 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
|
2000-07-07 04:18:29 +04:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2002-04-23 10:10:42 +04:00
|
|
|
#include <paths.h>
|
2000-07-07 04:18:29 +04:00
|
|
|
#include <pwd.h>
|
1994-05-24 10:47:42 +04:00
|
|
|
#include <stdio.h>
|
1994-12-24 20:42:04 +03:00
|
|
|
#include <stdlib.h>
|
1994-05-24 10:47:42 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2000-07-07 04:18:29 +04:00
|
|
|
#include <unistd.h>
|
1994-05-30 07:54:37 +04:00
|
|
|
|
2000-07-07 04:18:29 +04:00
|
|
|
#include <skey.h>
|
1994-05-24 10:47:42 +04:00
|
|
|
|
2000-07-07 04:18:29 +04:00
|
|
|
#ifndef SKEY_NAMELEN
|
|
|
|
#define SKEY_NAMELEN 4
|
|
|
|
#endif
|
1994-05-24 10:47:42 +04:00
|
|
|
|
2000-07-07 04:18:29 +04:00
|
|
|
int main(int argc, char **argv)
|
1994-05-24 10:47:42 +04:00
|
|
|
{
|
2000-07-07 04:18:29 +04:00
|
|
|
int rval, nn, i, l;
|
|
|
|
int n = 0, defaultsetup = 1, zerokey = 0, hexmode = 0;
|
2005-09-19 01:50:20 +04:00
|
|
|
int argpass = 0, argkey = 0;
|
1994-05-24 10:47:42 +04:00
|
|
|
time_t now;
|
1998-07-06 10:45:41 +04:00
|
|
|
char hostname[MAXHOSTNAMELEN + 1];
|
2000-07-07 04:18:29 +04:00
|
|
|
char seed[SKEY_MAX_PW_LEN+2], key[SKEY_BINKEY_SIZE], defaultseed[SKEY_MAX_SEED_LEN+1];
|
|
|
|
char passwd[SKEY_MAX_PW_LEN+2], passwd2[SKEY_MAX_PW_LEN+2], tbuf[27], buf[80];
|
2002-08-02 02:43:34 +04:00
|
|
|
char lastc, me[LOGIN_NAME_MAX+1], *p, *pw, *ht = NULL;
|
1998-07-27 02:21:02 +04:00
|
|
|
const char *salt;
|
|
|
|
struct skey skey;
|
|
|
|
struct passwd *pp;
|
|
|
|
struct tm *tm;
|
2000-07-07 04:18:29 +04:00
|
|
|
int c;
|
1994-05-24 10:47:42 +04:00
|
|
|
|
2005-09-20 19:48:17 +04:00
|
|
|
pw = NULL; /* XXX gcc -Wuninitialized [sh3] */
|
|
|
|
|
2002-04-23 10:10:42 +04:00
|
|
|
/*
|
|
|
|
* Make sure using stdin/stdout/stderr is safe
|
|
|
|
* after opening any file.
|
|
|
|
*/
|
|
|
|
i = open(_PATH_DEVNULL, O_RDWR);
|
|
|
|
while (i >= 0 && i < 2)
|
|
|
|
i = dup(i);
|
|
|
|
if (i > 2)
|
|
|
|
close(i);
|
|
|
|
|
2000-07-07 04:18:29 +04:00
|
|
|
if (geteuid() != 0)
|
|
|
|
errx(1, "must be setuid root.");
|
1994-05-24 10:47:42 +04:00
|
|
|
|
1994-05-30 02:38:39 +04:00
|
|
|
if (gethostname(hostname, sizeof(hostname)) < 0)
|
|
|
|
err(1, "gethostname");
|
2000-07-28 23:19:23 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the hostname into the default seed, eliminating any
|
|
|
|
* non alpha-numeric characters.
|
|
|
|
*/
|
|
|
|
for (i = 0, l = 0; l < sizeof(defaultseed); i++) {
|
|
|
|
if (hostname[i] == '\0') {
|
|
|
|
defaultseed[l] = hostname[i];
|
|
|
|
break;
|
|
|
|
}
|
2004-11-03 23:10:08 +03:00
|
|
|
if (isalnum((unsigned char)hostname[i]))
|
2000-07-28 23:19:23 +04:00
|
|
|
defaultseed[l++] = hostname[i];
|
|
|
|
}
|
|
|
|
|
2000-07-07 04:18:29 +04:00
|
|
|
defaultseed[SKEY_NAMELEN] = '\0';
|
|
|
|
(void)time(&now);
|
2003-05-18 01:05:10 +04:00
|
|
|
(void)snprintf(tbuf, sizeof(tbuf), "%05ld", (long) (now % 100000));
|
|
|
|
(void)strlcat(defaultseed, tbuf, sizeof(defaultseed));
|
1994-05-24 10:47:42 +04:00
|
|
|
|
|
|
|
if ((pp = getpwuid(getuid())) == NULL)
|
1998-12-20 01:18:00 +03:00
|
|
|
err(1, "no user with uid %ld", (u_long)getuid());
|
2002-11-16 08:24:44 +03:00
|
|
|
(void)strlcpy(me, pp->pw_name, sizeof(me));
|
1994-05-24 10:47:42 +04:00
|
|
|
|
|
|
|
if ((pp = getpwnam(me)) == NULL)
|
|
|
|
err(1, "Who are you?");
|
2000-07-07 04:18:29 +04:00
|
|
|
salt = pp->pw_passwd;
|
1994-05-24 10:47:42 +04:00
|
|
|
|
2005-09-19 01:50:20 +04:00
|
|
|
while((c = getopt(argc, argv, "k:n:p:t:sxz")) != -1) {
|
2000-07-07 04:18:29 +04:00
|
|
|
switch(c) {
|
2005-09-19 01:50:20 +04:00
|
|
|
case 'k':
|
|
|
|
argkey = 1;
|
|
|
|
if (strlen(optarg) > SKEY_MAX_PW_LEN)
|
|
|
|
errx(1, "key too long");
|
|
|
|
strlcpy(passwd, optarg, sizeof(passwd));
|
|
|
|
strlcpy(passwd2, optarg, sizeof(passwd));
|
|
|
|
break;
|
2000-07-07 04:18:29 +04:00
|
|
|
case 'n':
|
|
|
|
n = atoi(optarg);
|
|
|
|
if(n < 1 || n > SKEY_MAX_SEQ)
|
|
|
|
errx(1, "count must be between 1 and %d", SKEY_MAX_SEQ);
|
|
|
|
break;
|
2005-09-19 01:50:20 +04:00
|
|
|
case 'p':
|
|
|
|
if (strlen(optarg) >= _PASSWORD_LEN)
|
|
|
|
errx(1, "password too long");
|
|
|
|
if ((pw = malloc(_PASSWORD_LEN + 1)) == NULL)
|
|
|
|
err(1, "no memory for password");
|
|
|
|
strlcpy(pw, optarg, _PASSWORD_LEN + 1);
|
|
|
|
break;
|
2000-07-07 04:18:29 +04:00
|
|
|
case 't':
|
|
|
|
if(skey_set_algorithm(optarg) == NULL)
|
|
|
|
errx(1, "Unknown hash algorithm %s", optarg);
|
|
|
|
ht = optarg;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
defaultsetup = 0;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
hexmode = 1;
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
zerokey = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2005-09-20 02:43:21 +04:00
|
|
|
errx(1, "usage: %s skeyinit [-sxz] [-k passphrase] "
|
|
|
|
"[-n count] [-p password] [-t hash] [user]",
|
|
|
|
argv[0]);
|
2000-07-07 04:18:29 +04:00
|
|
|
}
|
1994-05-24 10:47:42 +04:00
|
|
|
}
|
2000-07-07 04:18:29 +04:00
|
|
|
|
|
|
|
if(argc > optind) {
|
|
|
|
pp = getpwnam(argv[optind]);
|
|
|
|
if (pp == NULL)
|
|
|
|
errx(1, "User %s unknown", argv[optind]);
|
|
|
|
}
|
|
|
|
|
1994-05-24 10:47:42 +04:00
|
|
|
if (strcmp(pp->pw_name, me) != 0) {
|
|
|
|
if (getuid() != 0) {
|
|
|
|
/* Only root can change other's passwds */
|
2000-07-07 04:18:29 +04:00
|
|
|
errx(1, "Permission denied.");
|
1994-05-24 10:47:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getuid() != 0) {
|
2005-09-19 01:50:20 +04:00
|
|
|
if (!argpass)
|
|
|
|
pw = getpass("Password:");
|
1994-05-24 10:47:42 +04:00
|
|
|
p = crypt(pw, salt);
|
|
|
|
|
2000-07-07 04:18:29 +04:00
|
|
|
if (strcmp(p, pp->pw_passwd)) {
|
|
|
|
errx(1, "Password incorrect.");
|
1994-05-24 10:47:42 +04:00
|
|
|
}
|
|
|
|
}
|
2000-07-07 04:18:29 +04:00
|
|
|
|
1994-05-24 10:47:42 +04:00
|
|
|
rval = skeylookup(&skey, pp->pw_name);
|
|
|
|
switch (rval) {
|
|
|
|
case -1:
|
|
|
|
err(1, "cannot open database");
|
|
|
|
case 0:
|
2000-07-07 04:18:29 +04:00
|
|
|
/* comment out user if asked to */
|
|
|
|
if (zerokey)
|
|
|
|
exit(skeyzero(&skey, pp->pw_name));
|
|
|
|
|
1994-05-24 10:47:42 +04:00
|
|
|
printf("[Updating %s]\n", pp->pw_name);
|
2000-07-07 04:18:29 +04:00
|
|
|
printf("Old key: [%s] %s\n", skey_get_algorithm(), skey.seed);
|
1994-05-24 10:47:42 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* lets be nice if they have a skey.seed that
|
|
|
|
* ends in 0-8 just add one
|
|
|
|
*/
|
|
|
|
l = strlen(skey.seed);
|
|
|
|
if (l > 0) {
|
|
|
|
lastc = skey.seed[l - 1];
|
1998-12-20 01:18:00 +03:00
|
|
|
if (isdigit((unsigned char)lastc) && lastc != '9') {
|
2002-11-16 08:24:44 +03:00
|
|
|
(void)strlcpy(defaultseed, skey.seed,
|
|
|
|
sizeof(defaultseed));
|
1994-05-24 10:47:42 +04:00
|
|
|
defaultseed[l - 1] = lastc + 1;
|
|
|
|
}
|
1998-12-20 01:18:00 +03:00
|
|
|
if (isdigit((unsigned char)lastc) && lastc == '9' &&
|
|
|
|
l < 16) {
|
2003-05-18 01:05:10 +04:00
|
|
|
(void)strlcpy(defaultseed, skey.seed,
|
2002-11-16 08:24:44 +03:00
|
|
|
sizeof(defaultseed));
|
1994-05-24 10:47:42 +04:00
|
|
|
defaultseed[l - 1] = '0';
|
|
|
|
defaultseed[l] = '0';
|
|
|
|
defaultseed[l + 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
2000-07-07 04:18:29 +04:00
|
|
|
if (zerokey)
|
|
|
|
errx(1, "You have no entry to zero.");
|
1994-05-24 10:47:42 +04:00
|
|
|
printf("[Adding %s]\n", pp->pw_name);
|
|
|
|
break;
|
|
|
|
}
|
2000-07-07 04:18:29 +04:00
|
|
|
|
|
|
|
if(n==0)
|
|
|
|
n = 99;
|
|
|
|
|
|
|
|
/* Set hash type if asked to */
|
|
|
|
if (ht) {
|
|
|
|
/* Need to zero out old key when changing algorithm */
|
|
|
|
if (strcmp(ht, skey_get_algorithm()) && skey_set_algorithm(ht))
|
|
|
|
zerokey = 1;
|
|
|
|
}
|
1994-05-24 10:47:42 +04:00
|
|
|
|
|
|
|
if (!defaultsetup) {
|
2000-07-07 04:18:29 +04:00
|
|
|
printf("You need the 6 english words generated from the \"skey\" command.\n");
|
1994-05-24 10:47:42 +04:00
|
|
|
for (i = 0;; i++) {
|
|
|
|
if (i >= 2)
|
|
|
|
exit(1);
|
2000-07-07 04:18:29 +04:00
|
|
|
printf("Enter sequence count from 1 to %d: ", SKEY_MAX_SEQ);
|
|
|
|
fgets(buf, sizeof(buf), stdin);
|
|
|
|
n = atoi(buf);
|
|
|
|
if (n > 0 && n < SKEY_MAX_SEQ)
|
1994-05-24 10:47:42 +04:00
|
|
|
break; /* Valid range */
|
2000-07-07 04:18:29 +04:00
|
|
|
printf("\nError: Count must be between 0 and %d\n", SKEY_MAX_SEQ);
|
1994-05-24 10:47:42 +04:00
|
|
|
}
|
2000-07-07 04:18:29 +04:00
|
|
|
|
|
|
|
for (i = 0;; i++) {
|
|
|
|
if (i >= 2)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
printf("Enter new seed [default %s]: ", defaultseed);
|
|
|
|
fflush(stdout);
|
|
|
|
fgets(seed, sizeof(seed), stdin);
|
|
|
|
rip(seed);
|
|
|
|
for (p = seed; *p; p++) {
|
2004-11-03 23:10:08 +03:00
|
|
|
if (isalpha((unsigned char)*p)) {
|
|
|
|
*p = tolower((unsigned char)*p);
|
|
|
|
} else if (!isdigit((unsigned char)*p)) {
|
2000-07-07 04:18:29 +04:00
|
|
|
(void)puts("Error: seed may only contain alphanumeric characters");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*p == '\0')
|
|
|
|
break; /* Valid seed */
|
|
|
|
}
|
|
|
|
if (strlen(seed) > SKEY_MAX_SEED_LEN) {
|
|
|
|
printf("Notice: Seed truncated to %d characters.\n", SKEY_MAX_SEED_LEN);
|
|
|
|
seed[SKEY_MAX_SEED_LEN] = '\0';
|
1994-05-24 10:47:42 +04:00
|
|
|
}
|
|
|
|
if (seed[0] == '\0')
|
2002-11-16 08:24:44 +03:00
|
|
|
(void)strlcpy(seed, defaultseed, sizeof(seed));
|
1994-05-24 10:47:42 +04:00
|
|
|
|
|
|
|
for (i = 0;; i++) {
|
|
|
|
if (i >= 2)
|
|
|
|
exit(1);
|
|
|
|
|
2000-07-07 04:18:29 +04:00
|
|
|
printf("otp-%s %d %s\ns/key access password: ",
|
|
|
|
skey_get_algorithm(), n, seed);
|
|
|
|
fgets(buf, sizeof(buf), stdin);
|
|
|
|
rip(buf);
|
|
|
|
backspace(buf);
|
1994-05-24 10:47:42 +04:00
|
|
|
|
2000-07-07 04:18:29 +04:00
|
|
|
if (buf[0] == '?') {
|
|
|
|
puts("Enter 6 English words from secure S/Key calculation.");
|
1994-05-24 10:47:42 +04:00
|
|
|
continue;
|
2000-07-07 04:18:29 +04:00
|
|
|
} else if (buf[0] == '\0') {
|
1994-05-24 10:47:42 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
2000-07-07 04:18:29 +04:00
|
|
|
if (etob(key, buf) == 1 || atob8(key, buf) == 0)
|
1994-05-24 10:47:42 +04:00
|
|
|
break; /* Valid format */
|
2000-07-07 04:18:29 +04:00
|
|
|
(void)puts("Invalid format - try again with 6 English words.");
|
1994-05-24 10:47:42 +04:00
|
|
|
}
|
|
|
|
} else {
|
2000-07-07 04:18:29 +04:00
|
|
|
/* Get user's secret password */
|
|
|
|
puts("Reminder - Only use this method if you are directly connected\n"
|
|
|
|
" or have an encrypted channel. If you are using telnet\n"
|
|
|
|
" or rlogin, exit with no password and use skeyinit -s.\n");
|
|
|
|
|
|
|
|
for (i = 0;; i++) {
|
1994-05-24 10:47:42 +04:00
|
|
|
if (i >= 2)
|
|
|
|
exit(1);
|
|
|
|
|
2005-09-19 01:50:20 +04:00
|
|
|
if (!argkey) {
|
|
|
|
printf("Enter secret password: ");
|
|
|
|
readpass(passwd, sizeof(passwd));
|
|
|
|
if (passwd[0] == '\0')
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-05-24 10:47:42 +04:00
|
|
|
|
2000-07-07 04:18:29 +04:00
|
|
|
if (strlen(passwd) < SKEY_MIN_PW_LEN) {
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
"Your password must be at least %d characters long.\n", SKEY_MIN_PW_LEN);
|
|
|
|
continue;
|
|
|
|
} else if (strcmp(passwd, pp->pw_name) == 0) {
|
|
|
|
(void)fputs("Your password may not be the same as your user name.\n", stderr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
else if (strspn(passwd, "abcdefghijklmnopqrstuvwxyz") == strlen(passwd)) {
|
|
|
|
(void)fputs("Your password must contain more than just lower case letters.\n"
|
|
|
|
"Whitespace, numbers, and puctuation are suggested.\n", stderr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2005-09-19 01:50:20 +04:00
|
|
|
|
|
|
|
if (!argkey) {
|
|
|
|
printf("Again secret password: ");
|
|
|
|
readpass(passwd2, sizeof(passwd));
|
|
|
|
if (passwd2[0] == '\0')
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-05-24 10:47:42 +04:00
|
|
|
|
|
|
|
if (strcmp(passwd, passwd2) == 0)
|
|
|
|
break;
|
|
|
|
|
2000-07-07 04:18:29 +04:00
|
|
|
puts("Passwords do not match.");
|
1994-05-24 10:47:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Crunch seed and password into starting key */
|
2002-11-16 08:24:44 +03:00
|
|
|
(void)strlcpy(seed, defaultseed, sizeof(seed));
|
1994-05-24 10:47:42 +04:00
|
|
|
if (keycrunch(key, seed, passwd) != 0)
|
|
|
|
err(2, "key crunch failed");
|
|
|
|
nn = n;
|
|
|
|
while (nn-- != 0)
|
|
|
|
f(key);
|
|
|
|
}
|
2000-07-07 04:18:29 +04:00
|
|
|
(void)time(&now);
|
1994-05-24 10:47:42 +04:00
|
|
|
tm = localtime(&now);
|
2000-07-07 04:18:29 +04:00
|
|
|
(void)strftime(tbuf, sizeof(tbuf), " %b %d,%Y %T", tm);
|
|
|
|
|
|
|
|
if ((skey.val = (char *)malloc(16 + 1)) == NULL)
|
|
|
|
err(1, "Can't allocate memory");
|
1994-05-24 10:47:42 +04:00
|
|
|
|
2001-07-24 20:43:02 +04:00
|
|
|
/* Zero out old key if necessary (entry would change size) */
|
2000-07-07 04:18:29 +04:00
|
|
|
if (zerokey) {
|
|
|
|
(void)skeyzero(&skey, pp->pw_name);
|
|
|
|
/* Re-open keys file and seek to the end */
|
|
|
|
if (skeylookup(&skey, pp->pw_name) == -1)
|
|
|
|
err(1, "cannot open database");
|
|
|
|
}
|
1994-05-24 10:47:42 +04:00
|
|
|
|
|
|
|
btoa8(skey.val, key);
|
|
|
|
|
2000-07-07 04:18:29 +04:00
|
|
|
/*
|
|
|
|
* Obtain an exclusive lock on the key file so we don't
|
|
|
|
* clobber someone authenticating themselves at the same time.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 300; i++) {
|
|
|
|
if ((rval = flock(fileno(skey.keyfile), LOCK_EX|LOCK_NB)) == 0
|
|
|
|
|| errno != EWOULDBLOCK)
|
|
|
|
break;
|
|
|
|
usleep(100000); /* Sleep for 0.1 seconds */
|
|
|
|
}
|
|
|
|
if (rval == -1) { /* Can't get exclusive lock */
|
|
|
|
errno = EAGAIN;
|
|
|
|
err(1, "cannot open database");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't save algorithm type for md4 (keep record length same) */
|
|
|
|
if (strcmp(skey_get_algorithm(), "md4") == 0)
|
|
|
|
(void)fprintf(skey.keyfile, "%s %04d %-16s %s %-21s\n",
|
|
|
|
pp->pw_name, n, seed, skey.val, tbuf);
|
|
|
|
else
|
|
|
|
(void)fprintf(skey.keyfile, "%s %s %04d %-16s %s %-21s\n",
|
|
|
|
pp->pw_name, skey_get_algorithm(), n, seed, skey.val, tbuf);
|
|
|
|
|
|
|
|
(void)fclose(skey.keyfile);
|
|
|
|
|
|
|
|
(void)printf("\nID %s skey is otp-%s %d %s\n", pp->pw_name,
|
|
|
|
skey_get_algorithm(), n, seed);
|
|
|
|
(void)printf("Next login password: %s\n\n",
|
|
|
|
hexmode ? put8(buf, key) : btoe(buf, key));
|
1994-05-24 10:47:42 +04:00
|
|
|
|
2000-07-07 04:18:29 +04:00
|
|
|
return(0);
|
1994-05-24 10:47:42 +04:00
|
|
|
}
|