Add full PAM support for account management and sessions to su(1).
[dragonfly.git] / lib / libskey / skey_crypt.c
1 /* Author: Wietse Venema, Eindhoven University of Technology. */
2 /* $DragonFly: src/lib/libskey/skey_crypt.c,v 1.2 2008/09/30 16:57:05 swildner Exp $ */
3
4 #include <string.h>
5 #include <stdio.h>
6 #include <pwd.h>
7 #include <unistd.h>
8
9 #include "skey.h"
10
11 /* skey_crypt - return encrypted UNIX passwd if s/key or regular password ok */
12
13 const char *
14 skey_crypt(char *pp, char *salt, struct passwd *pwd, int pwok)
15 {
16     struct skey skey;
17     char   *p;
18
19     /* Try s/key authentication even when the UNIX password is permitted. */
20
21     if (pwd != 0 && skeyinfo(&skey, pwd->pw_name, (char *) 0) == 0
22         && skeyverify(&skey, pp) == 0) {
23         /* s/key authentication succeeded */
24         return (pwd->pw_passwd);
25     }
26
27     /* When s/key authentication does not work, always invoke crypt(). */
28
29     p = crypt(pp, salt);
30     if (pwok && pwd != 0 && strcmp(p, pwd->pw_passwd) == 0)
31         return (pwd->pw_passwd);
32
33     /* The user does not exist or entered bad input. */
34
35     return (":");
36 }