Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

nmlpwd.cc File Reference

#include "crypt2.hh"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>

Include dependency graph for nmlpwd.cc:

Include dependency graph

Go to the source code of this file.

Functions

int main (int argc, char **argv)


Function Documentation

int main int    argc,
char **    argv
 

Definition at line 28 of file nmlpwd.cc.

00029 {
00030 #endif
00031   char old_passwd_file[256];
00032   char new_passwd_file[256];
00033   char user_passwd[16];
00034   char user_logname[16];
00035   char temp_buffer[256];
00036   char *crypt_ret;
00037   char encrypted_passwd[16];
00038   int allow_read = 0;
00039   int allow_write = 0;
00040   int seed;
00041   int i;
00042   char new_line[256];
00043   char salt[3];
00044   int user_logname_length;
00045   int user_found = 0;
00046   char *end_line = NULL;
00047   char *getpass_ret = NULL;
00048 
00049   FILE *fp_old = NULL;
00050   FILE *fp_new = NULL;
00051 
00052   if (argc > 1)
00053     {
00054       strncpy (old_passwd_file, argv[1], 256);
00055     }
00056   else
00057     {
00058       printf ("Old Password File:(type none to create a file from scratch)");
00059       fgets (old_passwd_file, 256, stdin);
00060     }
00061   end_line = strpbrk (old_passwd_file, "\r\n");
00062   if (end_line != NULL)
00063     {
00064       *end_line = 0;
00065     }
00066   if (old_passwd_file[0] != 0)
00067     {
00068       if (strcmp (old_passwd_file, "none"))
00069         {
00070           fp_old = fopen (old_passwd_file, "r");
00071           if (fp_old == NULL)
00072             {
00073               fprintf (stderr, "Can not open %s. errno = %d -- %s\n",
00074                        old_passwd_file, errno, strerror (errno));
00075               exit (-1);
00076             }
00077         }
00078     }
00079 
00080   if (argc > 1)
00081     {
00082       strncpy (new_passwd_file, argv[1], 256);
00083     }
00084   else
00085     {
00086       printf ("New Password File:");
00087       fgets (new_passwd_file, 256, stdin);
00088     }
00089   end_line = strpbrk (new_passwd_file, "\r\n");
00090   if (end_line != NULL)
00091     {
00092       *end_line = 0;
00093     }
00094 
00095   if (!strcmp (new_passwd_file, old_passwd_file))
00096     {
00097       fprintf (stderr,
00098                "New passwd file must be different from old passwd file.\n");
00099       exit (-1);
00100     }
00101 
00102   fp_new = fopen (new_passwd_file, "w");
00103   if (NULL == fp_new)
00104     {
00105       fprintf (stderr, "Can not open/create %s. errno = %d -- %s\n",
00106                new_passwd_file, errno, strerror (errno));
00107       exit (-1);
00108     }
00109   if (argc > 3)
00110     {
00111       strncpy (user_logname, argv[3], 16);
00112     }
00113   else
00114     {
00115       printf ("Login name:");
00116       fgets (user_logname, 16, stdin);
00117     }
00118   end_line = strpbrk (user_logname, "\r\n");
00119   if (end_line != NULL)
00120     {
00121       *end_line = 0;
00122     }
00123   user_logname_length = strlen (user_logname);
00124   if (user_logname_length < 2)
00125     {
00126       fprintf (stderr, "Login name %s is too short.\n", user_logname);
00127       exit (-1);
00128     }
00129 
00130   if (argc > 4)
00131     {
00132       strncpy (user_passwd, argv[4], 16);
00133     }
00134   else
00135     {
00136       while (1)
00137         {
00138           getpass_ret = getpass ("Password:");
00139           if (NULL == getpass_ret)
00140             {
00141               fprintf (stderr, "getpass() failed. errno = %d -- %s\n",
00142                        errno, strerror (errno));
00143               exit (-1);
00144             }
00145           strncpy (user_passwd, getpass_ret, 16);
00146           getpass_ret = getpass ("Retype Password:");
00147           if (!strcmp (getpass_ret, user_passwd))
00148             {
00149               break;
00150             }
00151           printf ("Passwd did not match.\n");
00152         }
00153     }
00154   end_line = strpbrk (user_passwd, "\r\n");
00155   if (end_line != NULL)
00156     {
00157       *end_line = 0;
00158     }
00159 
00160   printf ("Should %s be allowed to read?(y/n)", user_logname);
00161   fgets (temp_buffer, 256, stdin);
00162   allow_read = (temp_buffer[0] == 'y' || temp_buffer[0] == 'Y');
00163 
00164 
00165   printf ("Should %s be allowed to write?(y/n)", user_logname);
00166   fgets (temp_buffer, 256, stdin);
00167   allow_write = (temp_buffer[0] == 'y' || temp_buffer[0] == 'Y');
00168 
00169   if (strlen (user_passwd) > 1)
00170     {
00171       seed = 1000;
00172 
00173       for (i = 0; i < 256 && new_passwd_file[i]; i++)
00174         {
00175           seed += new_passwd_file[i];
00176         }
00177 
00178       for (i = 0; i < 16 && user_logname[i]; i++)
00179         {
00180           seed += user_logname[i];
00181         }
00182 
00183       for (i = 0; i < 16 && user_passwd[i]; i++)
00184         {
00185           seed += user_passwd[i];
00186         }
00187       srand (seed);
00188       salt[0] = rand () % 128;
00189       while (!isgraph (salt[0])
00190              && salt[0] != ' '
00191              && salt[0] != '\t'
00192              && salt[0] != '\r'
00193              && salt[0] != '\n' && salt[0] != ':' && salt[0] != 0)
00194         {
00195           salt[0] = rand () % 128;
00196         }
00197       salt[1] = rand () % 128;
00198       while (!isgraph (salt[1])
00199              && salt[1] != ' '
00200              && salt[1] != '\t'
00201              && salt[1] != '\r'
00202              && salt[1] != '\n' && salt[1] != ':' && salt[1] != 1)
00203         {
00204           salt[1] = rand () % 128;
00205         }
00206       salt[2] = 0;
00207 
00208       crypt_ret = rcs_crypt (user_passwd, salt);
00209       if (NULL == crypt_ret)
00210         {
00211           fprintf (stderr, "crypt failed.\n");
00212           exit (-1);
00213         }
00214       strncpy (encrypted_passwd, crypt_ret, 16);
00215 
00216       sprintf (new_line, "%s:%s:", user_logname, encrypted_passwd);
00217     }
00218   else
00219     {
00220       sprintf (new_line, "%s::", user_logname);
00221     }
00222   if (allow_read)
00223     {
00224       strcat (new_line, " read=true,");
00225     }
00226   else
00227     {
00228       strcat (new_line, " read=false,");
00229     }
00230   if (allow_write)
00231     {
00232       strcat (new_line, " write=true");
00233     }
00234   else
00235     {
00236       strcat (new_line, " write=false");
00237     }
00238 
00239   printf ("New line will be:\n");
00240   puts (new_line);
00241 
00242   if (NULL != fp_old)
00243     {
00244       while (!feof (fp_old))
00245         {
00246           memset (temp_buffer, 0, 256);
00247           fgets (temp_buffer, 256, fp_old);
00248           if (!temp_buffer[0])
00249             {
00250               continue;
00251             }
00252           if (!strncmp (temp_buffer, user_logname, user_logname_length))
00253             {
00254               if (!user_found)
00255                 {
00256                   fputs (new_line, fp_new);
00257                   fputs ("\n", fp_new);
00258                 }
00259               user_found = 1;
00260             }
00261           else
00262             {
00263               fputs (temp_buffer, fp_new);
00264             }
00265         }
00266     }
00267   if (!user_found)
00268     {
00269       fputs (new_line, fp_new);
00270       fputs ("\n", fp_new);
00271     }
00272 }


Generated on Sun Dec 2 15:57:21 2001 for rcslib by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001