4 * Copyright (c) 1987-2009 by the citadel.org team
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.
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.
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
29 #include <sys/types.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];
46 * Validate a password on the host unix system by talking to the chkpwd daemon
48 static int validpw(uid_t uid, const char *pass)
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);
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 calc_dirs_n_files(0,0,"", ctdldir, 0);
115 printf("\n\n ** host auth mode test utility **\n\n");
116 start_chkpwd_daemon();
119 printf("\n\nERROR: you need to be root to run this!\n\n");
123 printf("\n\nUsername: ");
124 fgets(buf, sizeof buf, stdin);
125 buf[strlen(buf)-1] = 0;
128 printf("Not found\n");
132 printf(" uid: %d\n", uid);
133 printf("Password: ");
134 fgets(buf, sizeof buf, stdin);
135 buf[strlen(buf)-1] = 0;