Back to index

citadel  8.12
chkpw.c
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 1987-2012 by the citadel.org team
00003  *
00004  *  This program is open source software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License version 3.
00006  *
00007  *  This program is distributed in the hope that it will be useful,
00008  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  *  GNU General Public License for more details.
00011  */
00012 
00013 #include <errno.h>
00014 #include <stdlib.h>
00015 #include <unistd.h>
00016 #include <stdio.h>
00017 #include <fcntl.h>
00018 #include <signal.h>
00019 #include <pwd.h>
00020 #include <ctype.h>
00021 #include <sys/types.h>
00022 #include <sys/wait.h>
00023 #include <sys/stat.h>
00024 #include <unistd.h>
00025 #include <string.h>
00026 #include <limits.h>
00027 #include <dirent.h>
00028 
00029 
00030 #include "citadel.h"
00031 #include "sysdep.h"
00032 #include "citadel_dirs.h"
00033 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
00034 int chkpwd_write_pipe[2];
00035 int chkpwd_read_pipe[2];
00036 
00037 /*
00038  * Validate a password on the host unix system by talking to the chkpwd daemon
00039  */
00040 static int validpw(uid_t uid, const char *pass)
00041 {
00042        char buf[256];
00043        int rv;
00044 
00045        rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
00046        if (rv == -1) {
00047               printf( "Communicatino with chkpwd broken: %s\n", strerror(errno));
00048               return 0;
00049        }
00050 
00051        rv = write(chkpwd_write_pipe[1], pass, 256);
00052        if (rv == -1) {
00053               printf( "Communicatino with chkpwd broken: %s\n", strerror(errno));
00054               return 0;
00055        }
00056        rv = read(chkpwd_read_pipe[0], buf, 4);
00057        if (rv == -1) {
00058               printf( "Communicatino with chkpwd broken: %s\n", strerror(errno));
00059               return 0;
00060        }
00061        if (!strncmp(buf, "PASS", 4)) {
00062               printf("pass\n");
00063               return(1);
00064        }
00065 
00066        printf("fail\n");
00067        return 0;
00068 }
00069 
00070 /* 
00071  * Start up the chkpwd daemon so validpw() has something to talk to
00072  */
00073 void start_chkpwd_daemon(void) {
00074        pid_t chkpwd_pid;
00075        struct stat filestats;
00076        int i;
00077 
00078        printf("Starting chkpwd daemon for host authentication mode\n");
00079 
00080        if ((stat(file_chkpwd, &filestats)==-1) ||
00081            (filestats.st_size==0)){
00082               printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
00083               abort();
00084        }
00085        if (pipe(chkpwd_write_pipe) != 0) {
00086               printf("Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
00087               abort();
00088        }
00089        if (pipe(chkpwd_read_pipe) != 0) {
00090               printf("Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
00091               abort();
00092        }
00093 
00094        chkpwd_pid = fork();
00095        if (chkpwd_pid < 0) {
00096               printf("Unable to fork chkpwd daemon: %s\n", strerror(errno));
00097               abort();
00098        }
00099        if (chkpwd_pid == 0) {
00100               dup2(chkpwd_write_pipe[0], 0);
00101               dup2(chkpwd_read_pipe[1], 1);
00102               for (i=2; i<256; ++i) close(i);
00103               execl(file_chkpwd, file_chkpwd, NULL);
00104               printf("Unable to exec chkpwd daemon: %s\n", strerror(errno));
00105               abort();
00106               exit(errno);
00107        }
00108 }
00109 
00110 
00111 
00112 int main(int argc, char **argv) {
00113        char buf[256];
00114        struct passwd *p;
00115        int uid;
00116        char ctdldir[PATH_MAX]=CTDLDIR;
00117        
00118        calc_dirs_n_files(0,0,"", ctdldir, 0);
00119        
00120        printf("\n\n ** host auth mode test utility **\n\n");
00121        start_chkpwd_daemon();
00122 
00123        if (getuid() != 0){
00124               printf("\n\nERROR: you need to be root to run this!\n\n");
00125               return(1);
00126        }
00127        while(1) {
00128               printf("\n\nUsername: ");
00129               fgets(buf, sizeof buf, stdin);
00130               buf[strlen(buf)-1] = 0;
00131               p = getpwnam(buf);
00132               if (p == NULL) {
00133                      printf("Not found\n");
00134               }
00135               else {
00136                      uid = p->pw_uid;
00137                      printf("     uid: %d\n", uid);
00138                      printf("Password: ");
00139                      fgets(buf, sizeof buf, stdin);
00140                      buf[strlen(buf)-1] = 0;
00141                      validpw(uid, buf);
00142               }
00143        }
00144 
00145        return(0);
00146 }