home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / htdigest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-12  |  4.0 KB  |  183 lines

  1. /*
  2.  * htdigest.c: simple program for manipulating digest passwd file for Apache
  3.  *
  4.  * by Alexei Kosut, based on htpasswd.c, by 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. /* This is probably the easiest way to do it */
  15. #include "../src/md5c.c"
  16.  
  17. #define LF 10
  18. #define CR 13
  19.  
  20. #define MAX_STRING_LEN 256
  21.  
  22. char *tn;
  23.  
  24. char *strd(char *s) {
  25.     char *d;
  26.  
  27.     d=(char *)malloc(strlen(s) + 1);
  28.     strcpy(d,s);
  29.     return(d);
  30. }
  31.  
  32. void getword(char *word, char *line, char stop) {
  33.     int x = 0,y;
  34.  
  35.     for(x=0;((line[x]) && (line[x] != stop));x++)
  36.         word[x] = line[x];
  37.  
  38.     word[x] = '\0';
  39.     if(line[x]) ++x;
  40.     y=0;
  41.  
  42.     while((line[y++] = line[x++]));
  43. }
  44.  
  45. int getline(char *s, int n, FILE *f) {
  46.     register int i=0;
  47.  
  48.     while(1) {
  49.         s[i] = (char)fgetc(f);
  50.  
  51.         if(s[i] == CR)
  52.             s[i] = fgetc(f);
  53.  
  54.         if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1))) {
  55.             s[i] = '\0';
  56.             return (feof(f) ? 1 : 0);
  57.         }
  58.         ++i;
  59.     }
  60. }
  61.  
  62. void putline(FILE *f,char *l) {
  63.     int x;
  64.  
  65.     for(x=0;l[x];x++) fputc(l[x],f);
  66.     fputc('\n',f);
  67. }
  68.  
  69.  
  70. void add_password(char *user, char *realm, FILE *f) {
  71.     char *pw, *cpw;
  72.     MD5_CTX context;
  73.     unsigned char digest[16];
  74.     char string[MAX_STRING_LEN];
  75.     unsigned int i;
  76.  
  77.     pw = strd((char *) getpass("New password:"));
  78.     if(strcmp(pw,(char *) getpass("Re-type new password:"))) {
  79.         fprintf(stderr,"They don't match, sorry.\n");
  80.         if(tn)
  81.             unlink(tn);
  82.         exit(1);
  83.     }
  84.     fprintf(f,"%s:%s:",user,realm);
  85.  
  86.     /* Do MD5 stuff */
  87.     sprintf(string, "%s:%s:%s", user, realm, pw);
  88.  
  89.     MD5Init (&context);
  90.     MD5Update (&context, string, strlen(string));
  91.     MD5Final (digest, &context);
  92.  
  93.     for (i = 0; i < 16; i++)
  94.         fprintf(f, "%02x", digest[i]);
  95.  
  96.     fprintf(f, "\n");
  97. }
  98.  
  99. void usage() {
  100.     fprintf(stderr,"Usage: htdigest [-c] passwordfile realm username\n");
  101.     fprintf(stderr,"The -c flag creates a new file.\n");
  102.     exit(1);
  103. }
  104.  
  105. void interrupted() {
  106.     fprintf(stderr,"Interrupted.\n");
  107.     if(tn) unlink(tn);
  108.     exit(1);
  109. }
  110.  
  111. void main(int argc, char *argv[]) {
  112.     FILE *tfp,*f;
  113.     char user[MAX_STRING_LEN];
  114.     char realm[MAX_STRING_LEN];
  115.     char line[MAX_STRING_LEN];
  116.     char l[MAX_STRING_LEN];
  117.     char w[MAX_STRING_LEN];
  118.     char x[MAX_STRING_LEN];
  119.     char command[MAX_STRING_LEN];
  120.     int found;
  121.  
  122.     tn = NULL;
  123.     signal(SIGINT,(void (*)())interrupted);
  124.     if(argc == 5) {
  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 in realm %s.\n",argv[4], argv[3]);
  134.         add_password(argv[4],argv[3],tfp);
  135.         fclose(tfp);
  136.         exit(0);
  137.     } else if(argc != 4) 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[3]);
  152.     strcpy(realm,argv[2]);
  153.  
  154.     found = 0;
  155.     while(!(getline(line,MAX_STRING_LEN,f))) {
  156.         if(found || (line[0] == '#') || (!line[0])) {
  157.             putline(tfp,line);
  158.             continue;
  159.         }
  160.         strcpy(l,line);
  161.         getword(w,l,':');
  162.     getword(x,l,':');
  163.         if(strcmp(user,w) || strcmp(realm,x)) {
  164.             putline(tfp,line);
  165.             continue;
  166.         }
  167.         else {
  168.             printf("Changing password for user %s in realm %s\n",user,realm);
  169.             add_password(user,realm,tfp);
  170.             found = 1;
  171.         }
  172.     }
  173.     if(!found) {
  174.         printf("Adding user %s in realm %s\n",user,realm);
  175.         add_password(user,realm,tfp);
  176.     }
  177.     fclose(f);
  178.     fclose(tfp);
  179.     sprintf(command,"cp %s %s",tn,argv[1]);
  180.     system(command);
  181.     unlink(tn);
  182. }
  183.