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)
53 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
54 rv = write(chkpwd_write_pipe[1], pass, 256);
55 rv = read(chkpwd_read_pipe[0], buf, 4);
57 if (!strncmp(buf, "PASS", 4)) {
67 * Start up the chkpwd daemon so validpw() has something to talk to
69 void start_chkpwd_daemon(void) {
71 struct stat filestats;
74 printf("Starting chkpwd daemon for host authentication mode\n");
76 if ((stat(file_chkpwd, &filestats)==-1) ||
77 (filestats.st_size==0)){
78 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
81 if (pipe(chkpwd_write_pipe) != 0) {
82 printf("Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
85 if (pipe(chkpwd_read_pipe) != 0) {
86 printf("Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
92 printf("Unable to fork chkpwd daemon: %s\n", strerror(errno));
95 if (chkpwd_pid == 0) {
96 dup2(chkpwd_write_pipe[0], 0);
97 dup2(chkpwd_read_pipe[1], 1);
98 for (i=2; i<256; ++i) close(i);
99 execl(file_chkpwd, file_chkpwd, NULL);
100 printf("Unable to exec chkpwd daemon: %s\n", strerror(errno));
108 int main(int argc, char **argv) {
112 char ctdldir[PATH_MAX]=CTDLDIR;
115 calc_dirs_n_files(0,0,"", ctdldir, 0);
117 printf("\n\n ** host auth mode test utility **\n\n");
118 start_chkpwd_daemon();
121 printf("\n\nERROR: you need to be root to run this!\n\n");
125 printf("\n\nUsername: ");
126 ptr = fgets(buf, sizeof buf, stdin);
127 buf[strlen(buf)-1] = 0;
130 printf("Not found\n");
134 printf(" uid: %d\n", uid);
135 printf("Password: ");
136 ptr = fgets(buf, sizeof buf, stdin);
137 buf[strlen(buf)-1] = 0;