#include "config.h"
#include <stdio.h>
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <sys/param.h>
#include <sys/types.h>
#include <pwd.h>
#include <time.h>
#include "sudo.h"
#include "sudo_auth.h"
#include "insults.h"
#ifndef lint
static const char rcsid[] = "$Sudo: sudo_auth.c,v 1.19 2000/03/06 19:42:21 millert Exp $";
#endif
sudo_auth auth_switch[] = {
#ifdef AUTH_STANDALONE
AUTH_STANDALONE
#else
# ifndef WITHOUT_PASSWD
AUTH_ENTRY(0, "passwd", NULL, NULL, passwd_verify, NULL)
# endif
# if defined(HAVE_GETPRPWNAM) && !defined(WITHOUT_PASSWD)
AUTH_ENTRY(0, "secureware", secureware_init, NULL, secureware_verify, NULL)
# endif
# ifdef HAVE_AFS
AUTH_ENTRY(0, "afs", NULL, NULL, afs_verify, NULL)
# endif
# ifdef HAVE_DCE
AUTH_ENTRY(0, "dce", NULL, NULL, dce_verify, NULL)
# endif
# ifdef HAVE_KERB4
AUTH_ENTRY(0, "kerb4", kerb4_init, NULL, kerb4_verify, NULL)
# endif
# ifdef HAVE_KERB5
AUTH_ENTRY(0, "kerb5", kerb5_init, NULL, kerb5_verify, kerb5_cleanup)
# endif
# ifdef HAVE_SKEY
AUTH_ENTRY(0, "S/Key", NULL, rfc1938_setup, rfc1938_verify, NULL)
# endif
# ifdef HAVE_OPIE
AUTH_ENTRY(0, "OPIE", NULL, rfc1938_setup, rfc1938_verify, NULL)
# endif
#endif
AUTH_ENTRY(0, NULL, NULL, NULL, NULL, NULL)
};
int nil_pw;
void
verify_user(pw, prompt)
struct passwd *pw;
char *prompt;
{
short counter = def_ival(I_PW_TRIES) + 1;
short success = AUTH_FAILURE;
short status;
char *p;
sudo_auth *auth;
if (auth_switch[0].name == NULL)
log_error(0, "%s %s %s",
"There are no authentication methods compiled into sudo!",
"If you want to turn off authentication, use the",
"--disable-authentication configure option.");
if (auth_switch[1].name == NULL)
auth_switch[0].flags |= FLAG_ONEANDONLY;
for (auth = auth_switch; auth->name; auth++) {
if (auth->init && IS_CONFIGURED(auth)) {
if (NEEDS_USER(auth))
set_perms(PERM_USER, 0);
status = (auth->init)(pw, &prompt, auth);
if (status == AUTH_FAILURE)
auth->flags &= ~FLAG_CONFIGURED;
else if (status == AUTH_FATAL)
exit(1);
if (NEEDS_USER(auth))
set_perms(PERM_ROOT, 0);
}
}
while (--counter) {
for (auth = auth_switch; auth->name; auth++) {
if (auth->setup && IS_CONFIGURED(auth)) {
if (NEEDS_USER(auth))
set_perms(PERM_USER, 0);
status = (auth->setup)(pw, &prompt, auth);
if (status == AUTH_FAILURE)
auth->flags &= ~FLAG_CONFIGURED;
else if (status == AUTH_FATAL)
exit(1);
if (NEEDS_USER(auth))
set_perms(PERM_ROOT, 0);
}
}
nil_pw = 0;
#ifdef AUTH_STANDALONE
p = prompt;
#else
p = (char *) tgetpass(prompt, def_ival(I_PW_TIMEOUT) * 60,
tgetpass_flags);
if (!p || *p == '\0')
nil_pw = 1;
#endif
for (auth = auth_switch; auth->name; auth++) {
if (!IS_CONFIGURED(auth))
continue;
if (NEEDS_USER(auth))
set_perms(PERM_USER, 0);
success = auth->status = (auth->verify)(pw, p, auth);
if (NEEDS_USER(auth))
set_perms(PERM_ROOT, 0);
if (auth->status != AUTH_FAILURE)
goto cleanup;
}
#ifndef AUTH_STANDALONE
(void) memset(p, 0, strlen(p));
#endif
if (nil_pw) {
if (counter == def_ival(I_PW_TRIES))
exit(1);
else
break;
}
pass_warn(stderr);
}
cleanup:
for (auth = auth_switch; auth->name; auth++) {
if (auth->cleanup && IS_CONFIGURED(auth)) {
if (NEEDS_USER(auth))
set_perms(PERM_USER, 0);
status = (auth->cleanup)(pw, auth);
if (status == AUTH_FATAL)
exit(1);
if (NEEDS_USER(auth))
set_perms(PERM_ROOT, 0);
}
}
switch (success) {
case AUTH_SUCCESS:
return;
case AUTH_FAILURE:
log_error(NO_MAIL, "%d incorrect password attempt%s",
def_ival(I_PW_TRIES) - counter,
(def_ival(I_PW_TRIES) - counter == 1) ? "" : "s");
case AUTH_FATAL:
exit(1);
}
}
void
pass_warn(fp)
FILE *fp;
{
#ifdef INSULT
if (def_flag(I_INSULTS))
(void) fprintf(fp, "%s\n", INSULT);
else
#endif
(void) fprintf(fp, "%s\n", def_str(I_BADPASS_MSG));
}
void
dump_auth_methods()
{
sudo_auth *auth;
(void) fputs("Authentication methods:", stdout);
for (auth = auth_switch; auth->name; auth++)
(void) printf(" '%s'", auth->name);
(void) putchar('\n');
}