Eliminated the unpacking of uid in the chkpwd
[citadel.git] / citadel / chkpw.c
1 /* 
2  *
3  */
4
5 #include <errno.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <signal.h>
11 #include <pwd.h>
12 #include <ctype.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <string.h>
16 #include <limits.h>
17
18 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
19 int chkpwd_write_pipe[2];
20 int chkpwd_read_pipe[2];
21
22 /*
23  * Validate a password on the host unix system by talking to the chkpwd daemon
24  */
25 static int validpw(uid_t uid, const char *pass)
26 {
27         char buf[256];
28
29         write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
30         write(chkpwd_write_pipe[1], pass, 256);
31         read(chkpwd_read_pipe[0], buf, 4);
32
33         if (!strncmp(buf, "PASS", 4)) {
34                 printf("pass\n");
35                 return(1);
36         }
37
38         printf("fail\n");
39         return 0;
40 }
41
42 /* 
43  * Start up the chkpwd daemon so validpw() has something to talk to
44  */
45 void start_chkpwd_daemon(void) {
46         pid_t chkpwd_pid;
47         int i;
48
49         printf("Starting chkpwd daemon for host authentication mode\n");
50
51         if (pipe(chkpwd_write_pipe) != 0) {
52                 printf("Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
53                 abort();
54         }
55         if (pipe(chkpwd_read_pipe) != 0) {
56                 printf("Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
57                 abort();
58         }
59
60         chkpwd_pid = fork();
61         if (chkpwd_pid < 0) {
62                 printf("Unable to fork chkpwd daemon: %s\n", strerror(errno));
63                 abort();
64         }
65         if (chkpwd_pid == 0) {
66                 dup2(chkpwd_write_pipe[0], 0);
67                 dup2(chkpwd_read_pipe[1], 1);
68                 for (i=2; i<256; ++i) close(i);
69                 execl("./chkpwd", "chkpwd", NULL);
70                 printf("Unable to exec chkpwd daemon: %s\n", strerror(errno));
71                 abort();
72                 exit(errno);
73         }
74 }
75
76
77
78 int main(int argc, char **argv) {
79         char buf[256];
80         struct passwd *p;
81         int uid;
82         
83         printf("\n\n ** host auth mode test utility **\n\n");
84         start_chkpwd_daemon();
85
86         while(1) {
87                 printf("\n\nUsername: ");
88                 gets(buf);
89                 p = getpwnam(buf);
90                 if (p == NULL) {
91                         printf("Not found\n");
92                 }
93                 else {
94                         uid = p->pw_uid;
95                         printf("     uid: %d\n", uid);
96                         printf("Password: ");
97                         gets(buf);
98                         validpw(uid, buf);
99                 }
100         }
101
102         return(0);
103 }