2002-06-22 15:52:40 +04:00
|
|
|
/* $NetBSD: skeylogin.c,v 1.17 2002/06/22 11:59:12 itojun Exp $ */
|
1996-09-19 23:39:36 +04:00
|
|
|
|
1994-05-21 09:45:58 +04:00
|
|
|
/* S/KEY v1.1b (skeylogin.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 02:30:18 +04:00
|
|
|
* Modifications:
|
|
|
|
* Todd C. Miller <Todd.Miller@courtesan.com>
|
|
|
|
* Angelos D. Keromytis <adk@adk.gr>
|
|
|
|
*
|
1994-05-21 09:45:58 +04:00
|
|
|
* S/KEY verification check, lookups, and authentication.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2000-07-07 02:30:18 +04:00
|
|
|
#include <sys/types.h>
|
1994-05-21 09:45:58 +04:00
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <paths.h>
|
1994-05-21 09:45:58 +04:00
|
|
|
#include <stdio.h>
|
1995-06-05 23:48:33 +04:00
|
|
|
#include <stdlib.h>
|
1994-05-21 09:45:58 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2000-07-07 02:30:18 +04:00
|
|
|
#include <unistd.h>
|
1994-05-21 09:45:58 +04:00
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
#include "skey.h"
|
1994-05-21 09:45:58 +04:00
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
#define OTP_FMT "otp-%.*s %d %.*s"
|
1994-05-21 09:45:58 +04:00
|
|
|
|
|
|
|
/* Issue a skey challenge for user 'name'. If successful,
|
|
|
|
* fill in the caller's skey structure and return 0. If unsuccessful
|
|
|
|
* (e.g., if name is unknown) return -1.
|
|
|
|
*
|
|
|
|
* The file read/write pointer is left at the start of the
|
|
|
|
* record.
|
|
|
|
*/
|
2000-07-07 02:30:18 +04:00
|
|
|
int getskeyprompt(struct skey *mp, char *name, char *prompt)
|
1994-05-21 09:45:58 +04:00
|
|
|
{
|
|
|
|
int rval;
|
|
|
|
|
|
|
|
sevenbit(name);
|
2000-07-07 02:30:18 +04:00
|
|
|
rval = skeylookup(mp, name);
|
|
|
|
|
|
|
|
*prompt = '\0';
|
1995-06-05 23:48:33 +04:00
|
|
|
switch (rval) {
|
1994-05-21 09:45:58 +04:00
|
|
|
case -1: /* File error */
|
|
|
|
return -1;
|
|
|
|
case 0: /* Lookup succeeded, return challenge */
|
2000-07-07 02:30:18 +04:00
|
|
|
sprintf(prompt, OTP_FMT "\n",
|
|
|
|
SKEY_MAX_HASHNAME_LEN, skey_get_algorithm(),
|
|
|
|
mp->n - 1, SKEY_MAX_SEED_LEN, mp->seed);
|
1994-05-21 09:45:58 +04:00
|
|
|
return 0;
|
|
|
|
case 1: /* User not found */
|
|
|
|
fclose(mp->keyfile);
|
2000-07-07 02:30:18 +04:00
|
|
|
mp->keyfile = NULL;
|
1994-05-21 09:45:58 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return -1; /* Can't happen */
|
1995-06-05 23:48:33 +04:00
|
|
|
}
|
|
|
|
|
1994-05-21 09:45:58 +04:00
|
|
|
/* Return a skey challenge string for user 'name'. If successful,
|
|
|
|
* fill in the caller's skey structure and return 0. If unsuccessful
|
|
|
|
* (e.g., if name is unknown) return -1.
|
|
|
|
*
|
|
|
|
* The file read/write pointer is left at the start of the
|
|
|
|
* record.
|
|
|
|
*/
|
2000-07-07 02:30:18 +04:00
|
|
|
int skeychallenge(struct skey *mp, const char *name, char *ss, size_t sslen)
|
1994-05-21 09:45:58 +04:00
|
|
|
{
|
|
|
|
int rval;
|
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
rval = skeylookup(mp, name);
|
|
|
|
|
|
|
|
*ss = '\0';
|
1994-05-21 09:45:58 +04:00
|
|
|
switch(rval){
|
|
|
|
case -1: /* File error */
|
|
|
|
return -1;
|
|
|
|
case 0: /* Lookup succeeded, issue challenge */
|
2000-07-07 02:30:18 +04:00
|
|
|
snprintf(ss, sslen, OTP_FMT, SKEY_MAX_HASHNAME_LEN,
|
|
|
|
skey_get_algorithm(), mp->n - 1,
|
|
|
|
SKEY_MAX_SEED_LEN, mp->seed);
|
1994-05-21 09:45:58 +04:00
|
|
|
return 0;
|
|
|
|
case 1: /* User not found */
|
|
|
|
fclose(mp->keyfile);
|
2000-07-07 02:30:18 +04:00
|
|
|
mp->keyfile = NULL;
|
1994-05-21 09:45:58 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return -1; /* Can't happen */
|
1999-07-02 19:37:33 +04:00
|
|
|
}
|
1994-05-21 09:45:58 +04:00
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
static FILE *openSkey(void)
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
FILE *keyfile = NULL;
|
|
|
|
|
|
|
|
/* Open _PATH_SKEYKEYS if it exists, else return an error */
|
|
|
|
if (stat(_PATH_SKEYKEYS, &statbuf) == 0 &&
|
|
|
|
(keyfile = fopen(_PATH_SKEYKEYS, "r+"))) {
|
|
|
|
if ((statbuf.st_mode & 0007777) != 0600)
|
|
|
|
fchmod(fileno(keyfile), 0600);
|
|
|
|
} else {
|
|
|
|
keyfile = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyfile;
|
|
|
|
}
|
|
|
|
|
1994-05-21 09:45:58 +04:00
|
|
|
/* Find an entry in the One-time Password database.
|
|
|
|
* Return codes:
|
|
|
|
* -1: error in opening database
|
|
|
|
* 0: entry found, file R/W pointer positioned at beginning of record
|
|
|
|
* 1: entry not found, file R/W pointer positioned at EOF
|
|
|
|
*/
|
2000-07-07 02:30:18 +04:00
|
|
|
int skeylookup(struct skey *mp, const char *name)
|
1994-05-21 09:45:58 +04:00
|
|
|
{
|
2000-07-07 02:30:18 +04:00
|
|
|
int found = 0;
|
1997-06-18 23:18:27 +04:00
|
|
|
long recstart = 0;
|
2000-07-07 02:30:18 +04:00
|
|
|
const char *ht = NULL;
|
2002-06-22 15:52:40 +04:00
|
|
|
char *last;
|
1994-05-31 12:50:31 +04:00
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
if(!(mp->keyfile = openSkey()))
|
|
|
|
return(-1);
|
1994-05-21 09:45:58 +04:00
|
|
|
|
|
|
|
/* Look up user name in database */
|
1995-06-05 23:48:33 +04:00
|
|
|
while (!feof(mp->keyfile)) {
|
2000-07-07 02:30:18 +04:00
|
|
|
char *cp;
|
|
|
|
|
1994-05-21 09:45:58 +04:00
|
|
|
recstart = ftell(mp->keyfile);
|
|
|
|
mp->recstart = recstart;
|
2000-07-07 02:30:18 +04:00
|
|
|
if (fgets(mp->buf, sizeof(mp->buf), mp->keyfile) != mp->buf)
|
1994-05-21 09:45:58 +04:00
|
|
|
break;
|
2000-07-07 02:30:18 +04:00
|
|
|
|
1994-05-21 09:45:58 +04:00
|
|
|
rip(mp->buf);
|
1995-06-05 23:48:33 +04:00
|
|
|
if (mp->buf[0] == '#')
|
1994-05-21 09:45:58 +04:00
|
|
|
continue; /* Comment */
|
2002-06-22 15:52:40 +04:00
|
|
|
if ((mp->logname = strtok_r(mp->buf, " \t", &last)) == NULL)
|
1994-05-21 09:45:58 +04:00
|
|
|
continue;
|
2002-06-22 15:52:40 +04:00
|
|
|
if ((cp = strtok_r(NULL, " \t", &last)) == NULL)
|
1994-05-21 09:45:58 +04:00
|
|
|
continue;
|
2000-07-07 02:30:18 +04:00
|
|
|
/* Save hash type if specified, else use md4 */
|
2000-07-11 10:07:25 +04:00
|
|
|
if (isalpha((u_char) *cp)) {
|
2000-07-07 02:30:18 +04:00
|
|
|
ht = cp;
|
2002-06-22 15:52:40 +04:00
|
|
|
if ((cp = strtok_r(NULL, " \t", &last)) == NULL)
|
2000-07-07 02:30:18 +04:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
ht = "md4";
|
|
|
|
}
|
1994-05-21 09:45:58 +04:00
|
|
|
mp->n = atoi(cp);
|
2002-06-22 15:52:40 +04:00
|
|
|
if ((mp->seed = strtok_r(NULL, " \t", &last)) == NULL)
|
1994-05-21 09:45:58 +04:00
|
|
|
continue;
|
2002-06-22 15:52:40 +04:00
|
|
|
if ((mp->val = strtok_r(NULL, " \t", &last)) == NULL)
|
1994-05-21 09:45:58 +04:00
|
|
|
continue;
|
2000-07-07 02:30:18 +04:00
|
|
|
if (strcmp(mp->logname, name) == 0) {
|
1994-05-21 09:45:58 +04:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1995-06-05 23:48:33 +04:00
|
|
|
if (found) {
|
2000-07-07 02:30:18 +04:00
|
|
|
fseek(mp->keyfile, recstart, SEEK_SET);
|
|
|
|
/* Set hash type */
|
|
|
|
if (ht && skey_set_algorithm(ht) == NULL) {
|
|
|
|
warnx("Unknown hash algorithm %s, using %s", ht,
|
|
|
|
skey_get_algorithm());
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
} else {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the next entry in the One-time Password database.
|
|
|
|
* Return codes:
|
|
|
|
* -1: error in opening database
|
|
|
|
* 0: next entry found and stored in mp
|
|
|
|
* 1: no more entries, file R/W pointer positioned at EOF
|
|
|
|
*/
|
|
|
|
int skeygetnext(struct skey *mp)
|
|
|
|
{
|
|
|
|
long recstart = 0;
|
2002-06-22 15:52:40 +04:00
|
|
|
char *last;
|
2000-07-07 02:30:18 +04:00
|
|
|
|
|
|
|
/* Open _PATH_SKEYKEYS if it exists, else return an error */
|
|
|
|
if (mp->keyfile == NULL) {
|
|
|
|
if(!(mp->keyfile = openSkey()))
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look up next user in database */
|
|
|
|
while (!feof(mp->keyfile)) {
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
recstart = ftell(mp->keyfile);
|
|
|
|
mp->recstart = recstart;
|
|
|
|
if (fgets(mp->buf, sizeof(mp->buf), mp->keyfile) != mp->buf)
|
|
|
|
break;
|
|
|
|
rip(mp->buf);
|
|
|
|
if (mp->buf[0] == '#')
|
|
|
|
continue; /* Comment */
|
2002-06-22 15:52:40 +04:00
|
|
|
if ((mp->logname = strtok_r(mp->buf, " \t", &last)) == NULL)
|
2000-07-07 02:30:18 +04:00
|
|
|
continue;
|
2002-06-22 15:52:40 +04:00
|
|
|
if ((cp = strtok_r(NULL, " \t", &last)) == NULL)
|
2000-07-07 02:30:18 +04:00
|
|
|
continue;
|
|
|
|
/* Save hash type if specified, else use md4 */
|
2000-07-11 10:07:25 +04:00
|
|
|
if (isalpha((u_char) *cp)) {
|
2002-06-22 15:52:40 +04:00
|
|
|
if ((cp = strtok_r(NULL, " \t", &last)) == NULL)
|
2000-07-07 02:30:18 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mp->n = atoi(cp);
|
2002-06-22 15:52:40 +04:00
|
|
|
if ((mp->seed = strtok_r(NULL, " \t", &last)) == NULL)
|
2000-07-07 02:30:18 +04:00
|
|
|
continue;
|
2002-06-22 15:52:40 +04:00
|
|
|
if ((mp->val = strtok_r(NULL, " \t", &last)) == NULL)
|
2000-07-07 02:30:18 +04:00
|
|
|
continue;
|
|
|
|
/* Got a real entry */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return(feof(mp->keyfile));
|
1994-05-21 09:45:58 +04:00
|
|
|
}
|
2000-07-07 02:30:18 +04:00
|
|
|
|
1994-05-21 09:45:58 +04:00
|
|
|
/* Verify response to a s/key challenge.
|
|
|
|
*
|
|
|
|
* Return codes:
|
|
|
|
* -1: Error of some sort; database unchanged
|
|
|
|
* 0: Verify successful, database updated
|
|
|
|
* 1: Verify failed, database unchanged
|
|
|
|
*
|
|
|
|
* The database file is always closed by this call.
|
|
|
|
*/
|
2000-07-07 02:30:18 +04:00
|
|
|
|
|
|
|
int skeyverify(struct skey *mp, char *response)
|
1994-05-21 09:45:58 +04:00
|
|
|
{
|
2000-07-07 02:30:18 +04:00
|
|
|
char key[SKEY_BINKEY_SIZE];
|
|
|
|
char fkey[SKEY_BINKEY_SIZE];
|
|
|
|
char filekey[SKEY_BINKEY_SIZE];
|
1994-05-21 09:45:58 +04:00
|
|
|
time_t now;
|
|
|
|
struct tm *tm;
|
1995-06-05 23:48:33 +04:00
|
|
|
char tbuf[27];
|
2002-06-22 15:52:40 +04:00
|
|
|
char *cp, *last;
|
2000-07-07 02:30:18 +04:00
|
|
|
int i, rval;
|
1994-05-21 09:45:58 +04:00
|
|
|
|
|
|
|
time(&now);
|
|
|
|
tm = localtime(&now);
|
|
|
|
strftime(tbuf, sizeof(tbuf), " %b %d,%Y %T", tm);
|
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
if (response == NULL) {
|
1994-05-21 09:45:58 +04:00
|
|
|
fclose(mp->keyfile);
|
2000-07-07 02:30:18 +04:00
|
|
|
mp->keyfile = NULL;
|
1994-05-21 09:45:58 +04:00
|
|
|
return -1;
|
|
|
|
}
|
1995-06-05 23:48:33 +04:00
|
|
|
rip(response);
|
1994-05-21 09:45:58 +04:00
|
|
|
|
|
|
|
/* Convert response to binary */
|
1995-06-05 23:48:33 +04:00
|
|
|
if (etob(key, response) != 1 && atob8(key, response) != 0) {
|
1994-05-21 09:45:58 +04:00
|
|
|
/* Neither english words or ascii hex */
|
|
|
|
fclose(mp->keyfile);
|
2000-07-07 02:30:18 +04:00
|
|
|
mp->keyfile = NULL;
|
1994-05-21 09:45:58 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute fkey = f(key) */
|
2000-07-07 02:30:18 +04:00
|
|
|
memcpy(fkey, key, sizeof(key));
|
|
|
|
fflush(stdout);
|
1994-05-21 09:45:58 +04:00
|
|
|
|
|
|
|
f(fkey);
|
2000-07-07 02:30:18 +04:00
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
/*
|
2000-07-07 02:30:18 +04:00
|
|
|
* Obtain an exclusive lock on the key file so the same password
|
|
|
|
* cannot be used twice to get in to the system.
|
1995-06-05 23:48:33 +04:00
|
|
|
*/
|
2000-07-07 02:30:18 +04:00
|
|
|
for (i = 0; i < 300; i++) {
|
|
|
|
if ((rval = flock(fileno(mp->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;
|
|
|
|
return(-1);
|
2000-04-13 17:25:56 +04:00
|
|
|
}
|
1994-05-21 09:45:58 +04:00
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
/* Reread the file record NOW */
|
1994-05-21 09:45:58 +04:00
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
fseek(mp->keyfile, mp->recstart, SEEK_SET);
|
|
|
|
if (fgets(mp->buf, sizeof(mp->buf), mp->keyfile) != mp->buf) {
|
1994-05-21 09:45:58 +04:00
|
|
|
fclose(mp->keyfile);
|
2000-07-07 02:30:18 +04:00
|
|
|
mp->keyfile = NULL;
|
1994-05-21 09:45:58 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
rip(mp->buf);
|
2002-06-22 15:52:40 +04:00
|
|
|
if ((mp->logname = strtok_r(mp->buf, " \t", &last)) == NULL)
|
|
|
|
goto verify_failure;
|
|
|
|
if ((cp = strtok_r(NULL, " \t", &last)) == NULL)
|
|
|
|
goto verify_failure;
|
2000-07-11 10:07:25 +04:00
|
|
|
if (isalpha((u_char) *cp))
|
2002-06-22 15:52:40 +04:00
|
|
|
if ((cp = strtok_r(NULL, " \t", &last)) == NULL)
|
|
|
|
goto verify_failure;
|
|
|
|
if ((mp->seed = strtok_r(NULL, " \t", &last)) == NULL)
|
|
|
|
goto verify_failure;
|
|
|
|
if ((mp->val = strtok_r(NULL, " \t", &last)) == NULL)
|
|
|
|
goto verify_failure;
|
1994-05-21 09:45:58 +04:00
|
|
|
/* And convert file value to hex for comparison */
|
2000-07-07 02:30:18 +04:00
|
|
|
atob8(filekey, mp->val);
|
1994-05-21 09:45:58 +04:00
|
|
|
|
|
|
|
/* Do actual comparison */
|
2000-07-07 02:30:18 +04:00
|
|
|
if (memcmp(filekey, fkey, SKEY_BINKEY_SIZE) != 0) {
|
1994-05-21 09:45:58 +04:00
|
|
|
/* Wrong response */
|
|
|
|
fclose(mp->keyfile);
|
2000-07-07 02:30:18 +04:00
|
|
|
mp->keyfile = NULL;
|
1994-05-21 09:45:58 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
/*
|
|
|
|
* Update key in database by overwriting entire record. Note
|
1994-05-21 09:45:58 +04:00
|
|
|
* that we must write exactly the same number of bytes as in
|
|
|
|
* the original record (note fixed width field for N)
|
|
|
|
*/
|
2000-07-07 02:30:18 +04:00
|
|
|
btoa8(mp->val, key);
|
1994-05-21 09:45:58 +04:00
|
|
|
mp->n--;
|
2000-07-07 02:30:18 +04:00
|
|
|
fseek(mp->keyfile, mp->recstart, SEEK_SET);
|
|
|
|
/* Don't save algorithm type for md4 (keep record length same) */
|
|
|
|
if (strcmp(skey_get_algorithm(), "md4") == 0)
|
|
|
|
(void)fprintf(mp->keyfile, "%s %04d %-16s %s %-21s\n",
|
|
|
|
mp->logname, mp->n, mp->seed, mp->val, tbuf);
|
|
|
|
else
|
|
|
|
(void)fprintf(mp->keyfile, "%s %s %04d %-16s %s %-21s\n",
|
|
|
|
mp->logname, skey_get_algorithm(), mp->n,
|
|
|
|
mp->seed, mp->val, tbuf);
|
1994-05-21 09:45:58 +04:00
|
|
|
|
|
|
|
fclose(mp->keyfile);
|
2000-07-07 02:30:18 +04:00
|
|
|
mp->keyfile = NULL;
|
1994-05-21 09:45:58 +04:00
|
|
|
return 0;
|
2002-06-22 15:52:40 +04:00
|
|
|
|
|
|
|
verify_failure:
|
|
|
|
fclose(mp->keyfile);
|
|
|
|
mp->keyfile = NULL;
|
|
|
|
return -1;
|
1994-05-21 09:45:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
/* Returns: 1 user doesnt exist, -1 file error, 0 user exists. */
|
1999-07-02 19:37:33 +04:00
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
int skey_haskey(const char *username)
|
1994-05-21 09:45:58 +04:00
|
|
|
{
|
1995-06-05 23:48:33 +04:00
|
|
|
struct skey skey;
|
2000-07-07 02:30:18 +04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
i = skeylookup(&skey, username);
|
1999-07-02 19:37:33 +04:00
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
if (skey.keyfile != NULL) {
|
|
|
|
fclose(skey.keyfile);
|
|
|
|
skey.keyfile = NULL;
|
|
|
|
}
|
|
|
|
return(i);
|
1994-05-21 09:45:58 +04:00
|
|
|
}
|
1999-07-02 19:37:33 +04:00
|
|
|
|
1994-05-21 09:45:58 +04:00
|
|
|
/*
|
|
|
|
* Returns the current sequence number and
|
|
|
|
* seed for the passed user.
|
|
|
|
*/
|
2000-07-28 20:35:11 +04:00
|
|
|
const char *skey_keyinfo(const char *username)
|
1994-05-21 09:45:58 +04:00
|
|
|
{
|
1995-06-05 23:48:33 +04:00
|
|
|
int i;
|
2000-07-07 02:30:18 +04:00
|
|
|
static char str[SKEY_MAX_CHALLENGE];
|
1995-06-05 23:48:33 +04:00
|
|
|
struct skey skey;
|
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
i = skeychallenge(&skey, username, str, sizeof str);
|
1995-06-05 23:48:33 +04:00
|
|
|
if (i == -1)
|
|
|
|
return 0;
|
1994-05-21 09:45:58 +04:00
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
if (skey.keyfile != NULL) {
|
|
|
|
fclose(skey.keyfile);
|
|
|
|
skey.keyfile = NULL;
|
|
|
|
}
|
1995-06-05 23:48:33 +04:00
|
|
|
return str;
|
1994-05-21 09:45:58 +04:00
|
|
|
}
|
1999-07-02 19:37:33 +04:00
|
|
|
|
1994-05-21 09:45:58 +04:00
|
|
|
/*
|
|
|
|
* Check to see if answer is the correct one to the current
|
|
|
|
* challenge.
|
|
|
|
*
|
|
|
|
* Returns: 0 success, -1 failure
|
|
|
|
*/
|
1999-07-02 19:37:33 +04:00
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
int skey_passcheck (const char *username, char *passwd)
|
1994-05-21 09:45:58 +04:00
|
|
|
{
|
1995-06-05 23:48:33 +04:00
|
|
|
int i;
|
|
|
|
struct skey skey;
|
|
|
|
|
|
|
|
i = skeylookup (&skey, username);
|
|
|
|
if (i == -1 || i == 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (skeyverify (&skey, passwd) == 0)
|
|
|
|
return skey.n;
|
|
|
|
|
|
|
|
return -1;
|
1994-05-21 09:45:58 +04:00
|
|
|
}
|
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
#if DO_FAKE_CHALLENGE
|
|
|
|
#define ROUND(x) (((x)[0] << 24) + (((x)[1]) << 16) + (((x)[2]) << 8) + \
|
|
|
|
((x)[3]))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hash_collapse()
|
|
|
|
*/
|
|
|
|
static u_int32_t hash_collapse(u_char *s)
|
|
|
|
{
|
|
|
|
int len, target;
|
|
|
|
u_int32_t i;
|
|
|
|
int slen;
|
|
|
|
|
|
|
|
slen = strlen((char *)s);
|
|
|
|
if ((slen % sizeof(u_int32_t)) == 0)
|
|
|
|
target = slen; /* Multiple of 4 */
|
|
|
|
else
|
|
|
|
target = slen - slen % sizeof(u_int32_t);
|
|
|
|
|
|
|
|
for (i = 0, len = 0; len < target; len += 4)
|
|
|
|
i ^= ROUND(s + len);
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1994-05-21 09:45:58 +04:00
|
|
|
/*
|
|
|
|
* Used when calling program will allow input of the user's
|
|
|
|
* response to the challenge.
|
|
|
|
*
|
|
|
|
* Returns: 0 success, -1 failure
|
|
|
|
*/
|
1999-07-02 19:37:33 +04:00
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
int skey_authenticate(const char *username)
|
1994-05-21 09:45:58 +04:00
|
|
|
{
|
1995-06-05 23:48:33 +04:00
|
|
|
int i;
|
2000-07-07 02:30:18 +04:00
|
|
|
char pbuf[SKEY_MAX_PW_LEN+1], skeyprompt[SKEY_MAX_CHALLENGE+1];
|
1995-06-05 23:48:33 +04:00
|
|
|
struct skey skey;
|
2000-07-07 02:30:18 +04:00
|
|
|
#if DO_FAKE_CHALLENGE
|
|
|
|
u_int ptr;
|
|
|
|
u_char hseed[SKEY_MAX_SEED_LEN], flg = 1, *up;
|
|
|
|
size_t secretlen;
|
|
|
|
struct skey skey;
|
|
|
|
SHA1_CTX ctx;
|
|
|
|
#endif
|
1995-06-05 23:48:33 +04:00
|
|
|
/* Attempt a S/Key challenge */
|
2000-07-07 02:30:18 +04:00
|
|
|
i = skeychallenge(&skey, username, skeyprompt, sizeof skeyprompt);
|
|
|
|
|
|
|
|
#if DO_FAKE_CHALLENGE
|
|
|
|
/* Cons up a fake prompt if no entry in keys file */
|
|
|
|
if (i != 0) {
|
|
|
|
char *p, *u;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Base first 4 chars of seed on hostname.
|
|
|
|
* Add some filler for short hostnames if necessary.
|
|
|
|
*/
|
|
|
|
if (gethostname(pbuf, sizeof(pbuf)) == -1)
|
|
|
|
*(p = pbuf) = '.';
|
|
|
|
else
|
2000-07-11 10:07:25 +04:00
|
|
|
for (p = pbuf; *p && isalnum((u_char) *p); p++)
|
|
|
|
if (isalpha((u_char)*p) && isupper((u_char)*p))
|
|
|
|
*p = tolower((u_char)*p);
|
2000-07-07 02:30:18 +04:00
|
|
|
if (*p && pbuf - p < 4)
|
|
|
|
(void)strncpy(p, "asjd", 4 - (pbuf - p));
|
|
|
|
pbuf[4] = '\0';
|
|
|
|
|
|
|
|
/* Hash the username if possible */
|
|
|
|
if ((up = SHA1Data(username, strlen(username), NULL)) != NULL) {
|
|
|
|
struct stat sb;
|
|
|
|
time_t t;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* Collapse the hash */
|
|
|
|
ptr = hash_collapse(up);
|
|
|
|
memset(up, 0, strlen(up));
|
|
|
|
|
|
|
|
/* See if the random file's there, else use ctime */
|
|
|
|
if ((fd = open(_SKEY_RAND_FILE_PATH_, O_RDONLY)) != -1
|
|
|
|
&& fstat(fd, &sb) == 0 &&
|
|
|
|
sb.st_size > (off_t)SKEY_MAX_SEED_LEN &&
|
|
|
|
lseek(fd, ptr % (sb.st_size - SKEY_MAX_SEED_LEN),
|
|
|
|
SEEK_SET) != -1 && read(fd, hseed,
|
|
|
|
SKEY_MAX_SEED_LEN) == SKEY_MAX_SEED_LEN) {
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
secret = hseed;
|
|
|
|
secretlen = SKEY_MAX_SEED_LEN;
|
|
|
|
flg = 0;
|
|
|
|
} else if (!stat(_PATH_MEM, &sb) || !stat("/", &sb)) {
|
|
|
|
t = sb.st_ctime;
|
|
|
|
secret = ctime(&t);
|
|
|
|
secretlen = strlen(secret);
|
|
|
|
flg = 0;
|
|
|
|
}
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
}
|
1994-05-21 09:45:58 +04:00
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
/* Put that in your pipe and smoke it */
|
|
|
|
if (flg == 0) {
|
|
|
|
/* Hash secret value with username */
|
|
|
|
SHA1Init(&ctx);
|
|
|
|
SHA1Update(&ctx, secret, secretlen);
|
|
|
|
SHA1Update(&ctx, username, strlen(username));
|
|
|
|
SHA1End(&ctx, up);
|
|
|
|
|
|
|
|
/* Zero out */
|
|
|
|
memset(secret, 0, secretlen);
|
|
|
|
|
|
|
|
/* Now hash the hash */
|
|
|
|
SHA1Init(&ctx);
|
|
|
|
SHA1Update(&ctx, up, strlen(up));
|
|
|
|
SHA1End(&ctx, up);
|
|
|
|
|
|
|
|
ptr = hash_collapse(up + 4);
|
|
|
|
|
|
|
|
for (i = 4; i < 9; i++) {
|
|
|
|
pbuf[i] = (ptr % 10) + '0';
|
|
|
|
ptr /= 10;
|
|
|
|
}
|
|
|
|
pbuf[i] = '\0';
|
|
|
|
|
|
|
|
/* Sequence number */
|
|
|
|
ptr = ((up[2] + up[3]) % 99) + 1;
|
|
|
|
|
|
|
|
memset(up, 0, 20); /* SHA1 specific */
|
|
|
|
free(up);
|
|
|
|
|
|
|
|
(void)sprintf(skeyprompt,
|
|
|
|
"otp-%.*s %d %.*s",
|
|
|
|
SKEY_MAX_HASHNAME_LEN,
|
|
|
|
skey_get_algorithm(),
|
|
|
|
ptr, SKEY_MAX_SEED_LEN,
|
|
|
|
pbuf);
|
|
|
|
} else {
|
|
|
|
/* Base last 8 chars of seed on username */
|
|
|
|
u = username;
|
|
|
|
i = 8;
|
|
|
|
p = &pbuf[4];
|
|
|
|
do {
|
|
|
|
if (*u == 0) {
|
|
|
|
/* Pad remainder with zeros */
|
|
|
|
while (--i >= 0)
|
|
|
|
*p++ = '0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p++ = (*u++ % 10) + '0';
|
|
|
|
} while (--i != 0);
|
|
|
|
pbuf[12] = '\0';
|
|
|
|
|
|
|
|
(void)sprintf(skeyprompt, "otp-%.*s %d %.*s",
|
|
|
|
SKEY_MAX_HASHNAME_LEN,
|
|
|
|
skey_get_algorithm(),
|
|
|
|
99, SKEY_MAX_SEED_LEN, pbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1995-06-05 23:48:33 +04:00
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
fprintf(stderr, "[%s]\n", skeyprompt);
|
|
|
|
fflush(stderr);
|
1995-06-05 23:48:33 +04:00
|
|
|
|
2000-07-07 02:30:18 +04:00
|
|
|
fputs("Response: ", stderr);
|
|
|
|
readskey(pbuf, sizeof(pbuf));
|
1995-06-05 23:48:33 +04:00
|
|
|
|
|
|
|
/* Is it a valid response? */
|
2000-07-07 02:30:18 +04:00
|
|
|
if (i == 0 && skeyverify(&skey, pbuf) == 0) {
|
1995-06-05 23:48:33 +04:00
|
|
|
if (skey.n < 5) {
|
2000-07-07 02:30:18 +04:00
|
|
|
fprintf(stderr,
|
|
|
|
"\nWarning! Key initialization needed soon. (%d logins left)\n",
|
|
|
|
skey.n);
|
1995-06-05 23:48:33 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2000-07-07 02:30:18 +04:00
|
|
|
|
|
|
|
/* Comment out user's entry in the s/key database
|
|
|
|
*
|
|
|
|
* Return codes:
|
|
|
|
* -1: Write error; database unchanged
|
|
|
|
* 0: Database updated
|
|
|
|
*
|
|
|
|
* The database file is always closed by this call.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
int skeyzero(struct skey *mp, char *response)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Seek to the right place and write comment character
|
|
|
|
* which effectively zero's out the entry.
|
|
|
|
*/
|
|
|
|
fseek(mp->keyfile, mp->recstart, SEEK_SET);
|
|
|
|
if (fputc('#', mp->keyfile) == EOF) {
|
|
|
|
fclose(mp->keyfile);
|
|
|
|
mp->keyfile = NULL;
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(mp->keyfile);
|
|
|
|
mp->keyfile = NULL;
|
|
|
|
return(0);
|
|
|
|
}
|