home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / tel2305s.zip / TELPASS / TELPASS.C
C/C++ Source or Header  |  1991-06-26  |  4KB  |  200 lines

  1. /*
  2. *   NCSA Telpass - edit password files for NCSA Telnet.
  3. *   Tim Krauskopf 6/88
  4. *
  5. *   This program is rough-cut but functional.
  6. *   If you improve it substantially, let me know.
  7. */
  8. /************************************************************************/
  9. #define PASS
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #ifdef MSC
  15. #include <malloc.h>
  16. #endif
  17.  
  18. #include "externs.h"
  19. static void dochoice(int c);
  20. static void noecho(char *s);
  21. static void passwrite(char *s);
  22. static void passlist(char *s);
  23. static void Sencompass(char *ps);
  24.  
  25. void noecho();
  26. void passwrite();
  27. void Sencompass();
  28.  
  29. char space[256];
  30. char space2[256],*p;
  31. char *lines[200];
  32.  
  33. FILE *fout,*fin;
  34. int nnames;
  35.  
  36. void main(argc,argv)
  37. int argc;
  38. char *argv[];
  39. {
  40.     int i,choice;
  41.  
  42.     nnames=0;
  43.     n_clear();
  44.     n_cur(0,0);
  45.     n_puts("National Center for Supercomputing Applications");
  46.     n_puts("Password file editor for NCSA Telnet\n");
  47.     n_puts("Note:  This encryption of passwords is to hide clear text from casual viewing, ");
  48.     n_puts("       not to protect them from decryption.  You must assume that anyone with");
  49.     n_puts("       access to the password file can decrypt the passwords.\n");
  50.     if(argc<2) {
  51.         n_puts("A filename is required.  Usage: telpass filename");
  52.         exit(0);
  53.       }
  54.     passlist(argv[1]);
  55.     do {
  56.         n_puts("List of names:");
  57.         for(i=0; i<nnames; i++) {
  58.             sprintf(space,"%3d. %s",i+1,lines[i]);
  59.             n_puts(space);
  60.           }
  61.         n_puts("Enter the number of a user to change the password for,");
  62.         n_puts("'a' to add a user, 'd #' to delete a user, 'e' to exit:");
  63.         gets(space);
  64.         n_row();
  65.         choice=atoi(space);
  66.         if(choice)
  67.             dochoice(choice-1);
  68.         else 
  69.             if(*space=='a') {
  70.                 n_puts("Enter the username to add:");
  71.                 gets(space);
  72.                 n_row();
  73.                 lines[nnames] = malloc(strlen(space)+1);
  74.                 strcpy(lines[nnames++],space);
  75.                 dochoice(nnames-1);        /* get the password */
  76.               }
  77.             else 
  78.                 if(*space=='d') {
  79.                     choice=atoi(space+1);
  80.                     if(choice<=nnames&&choice>0) {
  81.                         for (i=choice-1; i<nnames; i++)
  82.                         lines[i]=lines[i+1];        /* shuffle down */
  83.                         nnames--;
  84.                         }
  85.                   }
  86.                 else 
  87.                     if(*space=='e')
  88.                     choice = -1;
  89.       }while(choice>=0);
  90.     passwrite(argv[1]);
  91.     exit(0);
  92. }
  93.  
  94. /****************************************************************************/
  95. /* dochoice
  96. *  prompt for a certain password
  97. */
  98. static void dochoice(int c)
  99. {
  100.     char *p;
  101.     char pwd[256],ver[256];
  102.  
  103.     strcpy(space,lines[c]);
  104.     do{
  105.         p=strchr(space,':');
  106.         if(!p)
  107.             strcat(space,":");
  108.       }while(!p);                        /* make sure we get a : */
  109.     *p='\0';
  110.     p++;
  111.     sprintf(space2,"Enter new password for user: %s",space);
  112.     n_puts(space2);
  113.     noecho(pwd);    
  114.     n_puts("Verify password by entering again:");
  115.     noecho(ver);
  116.     if(strcmp(pwd,ver)) {
  117.         n_puts("Password not verified");
  118.         return;
  119.       }
  120.     Sencompass(pwd);                    /* take password */
  121.     sprintf(ver,"%s:%s",space,space2);
  122.     lines[c]=malloc(strlen(ver)+1);
  123.     strcpy(lines[c],ver);
  124. }
  125.  
  126. static void noecho(char *s)
  127. {
  128.     int c;
  129.  
  130.     do {
  131.         c=n_getchar();
  132.         if(c>31&&c<128)
  133.             *s++= (char) c;
  134.     }while(c>31&&c<128);
  135.     *s='\0';
  136. }
  137.  
  138. /****************************************************************************/
  139. /* passwrite
  140. *  write them out
  141. */
  142. static void passwrite(char *s)
  143. {
  144.     int i;
  145.  
  146.     if(NULL==(fout=fopen(s,"w"))) {
  147.         n_puts("Cannot open file to write passwords. ");
  148.         exit(0);
  149.       }
  150.     for(i=0; i<nnames; i++) {
  151.         fputs(lines[i],fout);
  152.         fputs("\n",fout);
  153.       }
  154.     fclose(fout);
  155. }
  156.  
  157. /****************************************************************************/
  158. /*  passlist
  159. *   List the current file
  160. */
  161. static void passlist(char *s)
  162. {
  163.     if(NULL==(fin=fopen(s,"r"))) {
  164.         n_puts("Starting new file.");
  165.         return;
  166.       }
  167.     while(NULL!=fgets(space,250,fin)) {
  168.         space[strlen(space)-1]='\0';
  169.         lines[nnames]=malloc(strlen(space)+1);
  170.         strcpy(lines[nnames++],space);
  171.       }
  172.     fclose(fin);
  173. }
  174.  
  175. /****************************************************************************/
  176. /* Scompass
  177. *  compute and check the encrypted password
  178. */
  179. static void Sencompass(char *ps)
  180. {
  181.     int i,ck;
  182.     char *p,c,*en;
  183.  
  184.     en=space2;
  185.     ck=0;
  186.     p=ps;
  187.     while(*p)                /* checksum the string */
  188.         ck+=*p++;
  189.     c=(char) ck;
  190.     for(i=0; i<10; i++) {
  191.         *en=(char) (((*ps ^ c)|32)&127);     /* XOR with checksum */
  192.         if(*ps)
  193.             ps++;
  194.         else
  195.             c++;        /* to hide length */
  196.         en++;
  197.       }
  198.     *en=0;
  199. }
  200.