home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / httpd_source_1.3.tar.Z / httpd_source_1.3.tar / httpd_1.3 / support / htpasswd.c < prev    next >
C/C++ Source or Header  |  1994-05-07  |  4KB  |  181 lines

  1. /*
  2.  * htpasswd.c: simple program for manipulating password file for NCSA httpd
  3.  * 
  4.  * Rob McCool
  5.  */
  6.  
  7. #include <sys/types.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <sys/signal.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13.  
  14. #define LF 10
  15. #define CR 13
  16.  
  17. #define MAX_STRING_LEN 256
  18.  
  19. char *tn;
  20.  
  21. char *strd(char *s) {
  22.     char *d;
  23.  
  24.     d=(char *)malloc(strlen(s) + 1);
  25.     strcpy(d,s);
  26.     return(d);
  27. }
  28.  
  29. void getword(char *word, char *line, char stop) {
  30.     int x = 0,y;
  31.  
  32.     for(x=0;((line[x]) && (line[x] != stop));x++)
  33.         word[x] = line[x];
  34.  
  35.     word[x] = '\0';
  36.     if(line[x]) ++x;
  37.     y=0;
  38.  
  39.     while(line[y++] = line[x++]);
  40. }
  41.  
  42. int getline(char *s, int n, FILE *f) {
  43.     register int i=0;
  44.  
  45.     while(1) {
  46.         s[i] = (char)fgetc(f);
  47.  
  48.         if(s[i] == CR)
  49.             s[i] = fgetc(f);
  50.  
  51.         if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1))) {
  52.             s[i] = '\0';
  53.             return (feof(f) ? 1 : 0);
  54.         }
  55.         ++i;
  56.     }
  57. }
  58.  
  59. void putline(FILE *f,char *l) {
  60.     int x;
  61.  
  62.     for(x=0;l[x];x++) fputc(l[x],f);
  63.     fputc('\n',f);
  64. }
  65.  
  66.  
  67. /* From local_passwd.c (C) Regents of Univ. of California blah blah */
  68. static unsigned char itoa64[] =         /* 0 ... 63 => ascii - 64 */
  69.         "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  70.  
  71. to64(s, v, n)
  72.   register char *s;
  73.   register long v;
  74.   register int n;
  75. {
  76.     while (--n >= 0) {
  77.         *s++ = itoa64[v&0x3f];
  78.         v >>= 6;
  79.     }
  80. }
  81.  
  82. char *crypt(char *pw, char *salt); /* why aren't these prototyped in include */
  83.  
  84. void add_password(char *user, FILE *f) {
  85.     char *pw, *cpw, salt[3];
  86.  
  87.     pw = strd(getpass("New password:"));
  88.     if(strcmp(pw,getpass("Re-type new password:"))) {
  89.         fprintf(stderr,"They don't match, sorry.\n");
  90.         if(tn)
  91.             unlink(tn);
  92.         exit(1);
  93.     }
  94.     (void)srand((int)time((time_t *)NULL));
  95.     to64(&salt[0],rand(),2);
  96.     cpw = crypt(pw,salt);
  97.     free(pw);
  98.     fprintf(f,"%s:%s\n",user,cpw);
  99. }
  100.  
  101. void usage() {
  102.     fprintf(stderr,"Usage: htpasswd [-c] passwordfile username\n");
  103.     fprintf(stderr,"The -c flag creates a new file.\n");
  104.     exit(1);
  105. }
  106.  
  107. void interrupted() {
  108.     fprintf(stderr,"Interrupted.\n");
  109.     if(tn) unlink(tn);
  110.     exit(1);
  111. }
  112.  
  113. main(int argc, char *argv[]) {
  114.     FILE *tfp,*f;
  115.     char user[MAX_STRING_LEN];
  116.     char line[MAX_STRING_LEN];
  117.     char l[MAX_STRING_LEN];
  118.     char w[MAX_STRING_LEN];
  119.     char command[MAX_STRING_LEN];
  120.     int found;
  121.  
  122.     tn = NULL;
  123.     signal(SIGINT,(void (*)())interrupted);
  124.     if(argc == 4) {
  125.         if(strcmp(argv[1],"-c"))
  126.             usage();
  127.         if(!(tfp = fopen(argv[2],"w"))) {
  128.             fprintf(stderr,"Could not open passwd file %s for writing.\n",
  129.                     argv[2]);
  130.             perror("fopen");
  131.             exit(1);
  132.         }
  133.         printf("Adding password for %s.\n",argv[3]);
  134.         add_password(argv[3],tfp);
  135.         fclose(tfp);
  136.         exit(0);
  137.     } else if(argc != 3) usage();
  138.  
  139.     tn = tmpnam(NULL);
  140.     if(!(tfp = fopen(tn,"w"))) {
  141.         fprintf(stderr,"Could not open temp file.\n");
  142.         exit(1);
  143.     }
  144.  
  145.     if(!(f = fopen(argv[1],"r"))) {
  146.         fprintf(stderr,
  147.                 "Could not open passwd file %s for reading.\n",argv[1]);
  148.         fprintf(stderr,"Use -c option to create new one.\n");
  149.         exit(1);
  150.     }
  151.     strcpy(user,argv[2]);
  152.  
  153.     found = 0;
  154.     while(!(getline(line,MAX_STRING_LEN,f))) {
  155.         if(found || (line[0] == '#') || (!line[0])) {
  156.             putline(tfp,line);
  157.             continue;
  158.         }
  159.         strcpy(l,line);
  160.         getword(w,l,':');
  161.         if(strcmp(user,w)) {
  162.             putline(tfp,line);
  163.             continue;
  164.         }
  165.         else {
  166.             printf("Changing password for user %s\n",user);
  167.             add_password(user,tfp);
  168.             found = 1;
  169.         }
  170.     }
  171.     if(!found) {
  172.         printf("Adding user %s\n",user);
  173.         add_password(user,tfp);
  174.     }
  175.     fclose(f);
  176.     fclose(tfp);
  177.     sprintf(command,"cp %s %s",tn,argv[1]);
  178.     system(command);
  179.     unlink(tn);
  180. }
  181.