2 // Copyright (c) 1987-2021 by the citadel.org team
4 // This program is open source software. Use, duplication, or disclosure
5 // is subject to the terms of the GNU General Public License, version 3.
6 // The program is distributed without any warranty, expressed or implied.
16 #include <sys/types.h>
27 #include "citadel_dirs.h"
28 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
29 int chkpwd_write_pipe[2];
30 int chkpwd_read_pipe[2];
33 * Validate a password on the host unix system by talking to the chkpwd daemon
35 static int validpw(uid_t uid, const char *pass)
40 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
42 printf( "Communicatino with chkpwd broken: %s\n", strerror(errno));
46 rv = write(chkpwd_write_pipe[1], pass, 256);
48 printf( "Communicatino with chkpwd broken: %s\n", strerror(errno));
51 rv = read(chkpwd_read_pipe[0], buf, 4);
53 printf( "Communicatino with chkpwd broken: %s\n", strerror(errno));
56 if (!strncmp(buf, "PASS", 4)) {
66 * Start up the chkpwd daemon so validpw() has something to talk to
68 void start_chkpwd_daemon(void) {
70 struct stat filestats;
73 printf("Starting chkpwd daemon for host authentication mode\n");
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));
80 if (pipe(chkpwd_write_pipe) != 0) {
81 printf("Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
84 if (pipe(chkpwd_read_pipe) != 0) {
85 printf("Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
91 printf("Unable to fork chkpwd daemon: %s\n", strerror(errno));
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));
107 int main(int argc, char **argv) {
111 char ctdldir[PATH_MAX]=CTDLDIR;
113 printf("\n\n ** host auth mode test utility **\n\n");
114 start_chkpwd_daemon();
117 printf("\n\nERROR: you need to be root to run this!\n\n");
121 printf("\n\nUsername: ");
122 fgets(buf, sizeof buf, stdin);
123 buf[strlen(buf)-1] = 0;
126 printf("Not found\n");
130 printf(" uid: %d\n", uid);
131 printf("Password: ");
132 fgets(buf, sizeof buf, stdin);
133 buf[strlen(buf)-1] = 0;