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));
55 printf( "Communicatino with chkpwd broken: %s\n", strerror(errno));
59 rv = write(chkpwd_write_pipe[1], pass, 256);
61 printf( "Communicatino with chkpwd broken: %s\n", strerror(errno));
64 rv = read(chkpwd_read_pipe[0], buf, 4);
66 printf( "Communicatino with chkpwd broken: %s\n", strerror(errno));
69 if (!strncmp(buf, "PASS", 4)) {
79 * Start up the chkpwd daemon so validpw() has something to talk to
81 void start_chkpwd_daemon(void) {
83 struct stat filestats;
86 printf("Starting chkpwd daemon for host authentication mode\n");
88 if ((stat(file_chkpwd, &filestats)==-1) ||
89 (filestats.st_size==0)){
90 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
93 if (pipe(chkpwd_write_pipe) != 0) {
94 printf("Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
97 if (pipe(chkpwd_read_pipe) != 0) {
98 printf("Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
103 if (chkpwd_pid < 0) {
104 printf("Unable to fork chkpwd daemon: %s\n", strerror(errno));
107 if (chkpwd_pid == 0) {
108 dup2(chkpwd_write_pipe[0], 0);
109 dup2(chkpwd_read_pipe[1], 1);
110 for (i=2; i<256; ++i) close(i);
111 execl(file_chkpwd, file_chkpwd, NULL);
112 printf("Unable to exec chkpwd daemon: %s\n", strerror(errno));
120 int main(int argc, char **argv) {
124 char ctdldir[PATH_MAX]=CTDLDIR;
127 calc_dirs_n_files(0,0,"", ctdldir, 0);
129 printf("\n\n ** host auth mode test utility **\n\n");
130 start_chkpwd_daemon();
133 printf("\n\nERROR: you need to be root to run this!\n\n");
137 printf("\n\nUsername: ");
138 ptr = fgets(buf, sizeof buf, stdin);
139 buf[strlen(buf)-1] = 0;
142 printf("Not found\n");
146 printf(" uid: %d\n", uid);
147 printf("Password: ");
148 ptr = fgets(buf, sizeof buf, stdin);
149 buf[strlen(buf)-1] = 0;