I can't see the screen with my sunglasses on
[citadel.git] / citadel / utils / chkpw.c
1 // Copyright (c) 1987-2022 by the citadel.org team
2 //
3 // This program is open source software.  Use, duplication, or disclosure
4 // is subject to the terms of the GNU General Public License, version 3.
5
6 #include <errno.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <pwd.h>
13 #include <ctype.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <limits.h>
20 #include <dirent.h>
21 #include "../server/citadel.h"
22 #include "../server/sysdep.h"
23 #include "../server/citadel_dirs.h"
24
25 // These pipes are used to talk to the chkpwd daemon, which is forked during startup
26 int chkpwd_write_pipe[2];
27 int chkpwd_read_pipe[2];
28
29 // Validate a password on the host unix system by talking to the chkpwd daemon
30 static int validpw(uid_t uid, const char *pass) {
31         char buf[256];
32         int rv;
33
34         rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
35         if (rv == -1) {
36                 printf( "Communicatino with chkpwd broken: %s\n", strerror(errno));
37                 return 0;
38         }
39
40         rv = write(chkpwd_write_pipe[1], pass, 256);
41         if (rv == -1) {
42                 printf( "Communicatino with chkpwd broken: %s\n", strerror(errno));
43                 return 0;
44         }
45         rv = read(chkpwd_read_pipe[0], buf, 4);
46         if (rv == -1) {
47                 printf( "Communicatino with chkpwd broken: %s\n", strerror(errno));
48                 return 0;
49         }
50         if (!strncmp(buf, "PASS", 4)) {
51                 printf("pass\n");
52                 return(1);
53         }
54
55         printf("fail\n");
56         return 0;
57 }
58
59
60 // Start up the chkpwd daemon so validpw() has something to talk to
61 void start_chkpwd_daemon(void) {
62         pid_t chkpwd_pid;
63         struct stat filestats;
64         int i;
65
66         printf("Starting chkpwd daemon for host authentication mode\n");
67
68         if ((stat(file_chkpwd, &filestats)==-1) || (filestats.st_size==0)){
69                 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
70                 abort();
71         }
72         if (pipe(chkpwd_write_pipe) != 0) {
73                 printf("Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
74                 abort();
75         }
76         if (pipe(chkpwd_read_pipe) != 0) {
77                 printf("Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
78                 abort();
79         }
80
81         chkpwd_pid = fork();
82         if (chkpwd_pid < 0) {
83                 printf("Unable to fork chkpwd daemon: %s\n", strerror(errno));
84                 abort();
85         }
86         if (chkpwd_pid == 0) {
87                 dup2(chkpwd_write_pipe[0], 0);
88                 dup2(chkpwd_read_pipe[1], 1);
89                 for (i=2; i<256; ++i) close(i);
90                 execl(file_chkpwd, file_chkpwd, NULL);
91                 printf("Unable to exec chkpwd daemon: %s\n", strerror(errno));
92                 abort();
93                 exit(errno);
94         }
95 }
96
97
98 int main(int argc, char **argv) {
99         char buf[256];
100         struct passwd *p;
101         int uid;
102         
103         printf("\n\n ** host auth mode test utility **\n\n");
104         start_chkpwd_daemon();
105
106         if (getuid() != 0){
107                 printf("\n\nERROR: you need to be root to run this!\n\n");
108                 return(1);
109         }
110         while(1) {
111                 printf("\n\nUsername: ");
112                 fgets(buf, sizeof buf, stdin);
113                 buf[strlen(buf)-1] = 0;
114                 p = getpwnam(buf);
115                 if (p == NULL) {
116                         printf("Not found\n");
117                 }
118                 else {
119                         uid = p->pw_uid;
120                         printf("     uid: %d\n", uid);
121                         printf("Password: ");
122                         fgets(buf, sizeof buf, stdin);
123                         buf[strlen(buf)-1] = 0;
124                         validpw(uid, buf);
125                 }
126         }
127
128         return(0);
129 }