odds and sods
[tridge/junkcode.git] / pass.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <sys/types.h>
5 #include <sys/file.h>
6 #include <sys/stat.h>
7 #include <sys/param.h>
8 #include <sys/mount.h>
9 #include <pwd.h>
10 #include <unistd.h>
11 #include <pwd.h>
12
13 #ifdef SHADOW_PWD
14 #include <shadow.h>
15 #endif
16
17
18 void main()
19 {
20   char *password;
21 #ifdef SHADOW_PWD
22   struct spwd *spass = NULL;
23 #endif
24   struct passwd *pass = NULL;
25   char salt[100];
26   char user[100];
27
28   printf("Username: ");
29   if (scanf("%s",user) != 1)
30     exit(0);
31       
32   password = getpass("Password: ");
33   pass = getpwnam(user);
34   if (pass == NULL)
35     printf("couldn't find account %s\n",user); 
36   else
37     {
38       int pwok = 0;
39 #ifdef PWDAUTH
40       pwok = (pwdauth(user,password) == 0);
41 #else
42 #ifdef SHADOW_PWD
43       spass = getspnam(user);
44       if (spass && spass->sp_pwdp)
45         pass->pw_passwd = spass->sp_pwdp;
46 #endif
47       strncpy(salt,pass->pw_passwd,2);
48       salt[2] = 0;
49       pwok = (strcmp(crypt(password,salt),pass->pw_passwd) == 0);
50 #endif
51       if (!pwok)
52         printf("invalid password\n");
53       else
54         printf("password OK\n");
55     }
56 }