* Began (but did not finish) applying GPL3+ declarations to each source file. This...
[citadel.git] / citadel / chkpw.c
1 /* 
2  *
3  *
4  * Copyright (c) 1987-2009 by the citadel.org team
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <pwd.h>
28 #include <ctype.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <dirent.h>
36
37
38 #include "citadel.h"
39 #include "sysdep.h"
40 #include "citadel_dirs.h"
41 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
42 int chkpwd_write_pipe[2];
43 int chkpwd_read_pipe[2];
44
45 /*
46  * Validate a password on the host unix system by talking to the chkpwd daemon
47  */
48 static int validpw(uid_t uid, const char *pass)
49 {
50         char buf[256];
51
52         write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
53         write(chkpwd_write_pipe[1], pass, 256);
54         read(chkpwd_read_pipe[0], buf, 4);
55
56         if (!strncmp(buf, "PASS", 4)) {
57                 printf("pass\n");
58                 return(1);
59         }
60
61         printf("fail\n");
62         return 0;
63 }
64
65 /* 
66  * Start up the chkpwd daemon so validpw() has something to talk to
67  */
68 void start_chkpwd_daemon(void) {
69         pid_t chkpwd_pid;
70         struct stat filestats;
71         int i;
72
73         printf("Starting chkpwd daemon for host authentication mode\n");
74
75         if ((stat(file_chkpwd, &filestats)==-1) ||
76             (filestats.st_size==0)){
77                 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
78                 abort();
79         }
80         if (pipe(chkpwd_write_pipe) != 0) {
81                 printf("Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
82                 abort();
83         }
84         if (pipe(chkpwd_read_pipe) != 0) {
85                 printf("Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
86                 abort();
87         }
88
89         chkpwd_pid = fork();
90         if (chkpwd_pid < 0) {
91                 printf("Unable to fork chkpwd daemon: %s\n", strerror(errno));
92                 abort();
93         }
94         if (chkpwd_pid == 0) {
95                 dup2(chkpwd_write_pipe[0], 0);
96                 dup2(chkpwd_read_pipe[1], 1);
97                 for (i=2; i<256; ++i) close(i);
98                 execl(file_chkpwd, file_chkpwd, NULL);
99                 printf("Unable to exec chkpwd daemon: %s\n", strerror(errno));
100                 abort();
101                 exit(errno);
102         }
103 }
104
105
106
107 int main(int argc, char **argv) {
108         char buf[256];
109         struct passwd *p;
110         int uid;
111         char ctdldir[PATH_MAX]=CTDLDIR;
112         
113         calc_dirs_n_files(0,0,"", ctdldir, 0);
114         
115         printf("\n\n ** host auth mode test utility **\n\n");
116         start_chkpwd_daemon();
117
118         if (getuid() != 0){
119                 printf("\n\nERROR: you need to be root to run this!\n\n");
120                 return(1);
121         }
122         while(1) {
123                 printf("\n\nUsername: ");
124                 fgets(buf, sizeof buf, stdin);
125                 buf[strlen(buf)-1] = 0;
126                 p = getpwnam(buf);
127                 if (p == NULL) {
128                         printf("Not found\n");
129                 }
130                 else {
131                         uid = p->pw_uid;
132                         printf("     uid: %d\n", uid);
133                         printf("Password: ");
134                         fgets(buf, sizeof buf, stdin);
135                         buf[strlen(buf)-1] = 0;
136                         validpw(uid, buf);
137                 }
138         }
139
140         return(0);
141 }