home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Os2 / varios / APACHE / CHANGE-P.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-19  |  5.2 KB  |  200 lines

  1.  
  2. #include <sys/types.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/signal.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8.  
  9. #ifdef __EMX__
  10. #define USER_FILE "/os2httpd/conf/htpasswd"
  11. #else
  12. #define USER_FILE "/usr/local/etc/httpd/conf/.htpasswd"
  13. #endif
  14. #define WIZARD "surobm"
  15.  
  16. char *makeword(char *line, char stop);
  17. char *fmakeword(FILE *f, char stop, int *len);
  18. char x2c(char *what);
  19. void unescape_url(char *url);
  20. void plustospace(char *str);
  21.  
  22. char *crypt(char *pw, char *salt); /* why aren't these prototyped in include */
  23.  
  24.  
  25. char *tn;
  26.  
  27. /* From local_passwd.c (C) Regents of Univ. of California blah blah */
  28. static unsigned char itoa64[] =         /* 0 ... 63 => ascii - 64 */
  29.         "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  30.  
  31. to64(s, v, n)
  32.   register char *s;
  33.   register long v;
  34.   register int n;
  35. {
  36.     while (--n >= 0) {
  37.         *s++ = itoa64[v&0x3f];
  38.         v >>= 6;
  39.     }
  40. }
  41.  
  42. void change_password(char *user, char *pw, FILE *f) {
  43.     char *cpw, salt[3];
  44.  
  45.     (void)srand((int)time((time_t *)NULL));
  46.     to64(&salt[0],rand(),2);
  47.     cpw = crypt(pw,salt);
  48.     free(pw);
  49.     fprintf(f,"%s:%s\n",user,cpw);
  50. }
  51.  
  52. void putline(FILE *f,char *l) {
  53.     int x;
  54.  
  55.     for(x=0;l[x];x++) fputc(l[x],f);
  56.     fputc('\n',f);
  57. }
  58.  
  59. main(int argc, char *argv[]) {
  60.     register int x;
  61.     int cl,found,create;
  62.     char *u,*t1,*t2,*p1,*p2,*user, command[256], line[256], l[256], w[256];
  63.     FILE *tfp,*f;
  64.  
  65.     tn = NULL;
  66.  
  67.     printf("Content-type: text/html%c%c",10,10);
  68.  
  69.     printf("Part1 \n");
  70.  
  71.     u = getenv("REQUEST_METHOD");
  72.     if ((u == NULL) || strcmp(u,"POST")) {
  73.         printf("This script should be referenced with a METHOD of POST.\n");
  74.         printf("If you don't understand this, see this ");
  75.         printf("<A HREF=\"http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/fill-out-forms/overview.html\">forms overview</A>.%c",10);
  76.         exit(1);
  77.     }
  78.  
  79.     printf("Part2 \n");
  80.  
  81.     u = getenv("CONTENT_TYPE");
  82.     if((u == NULL) || strcmp(u,"application/x-www-form-urlencoded")) {
  83.         printf("This script can only be used to decode form results. \n");
  84.         exit(1);
  85.     }
  86.  
  87.     printf("Part3 \n");
  88.  
  89.     u = getenv("CONTENT_LENGTH");
  90.     if (u == NULL) {
  91.         printf("CONTENT_LENGTH not defined. \n");
  92.         exit(1);
  93.     } 
  94.     cl = atoi(u);
  95.  
  96.     printf("Part4 \n");
  97.  
  98.     user=NULL;
  99.     p1=NULL;
  100.     p2=NULL;
  101.     create=0;
  102.     for(x=0;cl && (!feof(stdin));x++) {
  103.         t1 = fmakeword(stdin,'&',&cl);
  104.         t2 = makeword(t1,'=');
  105.         unescape_url(t1);
  106.         unescape_url(t2);
  107.         if(!strcmp(t2,"user")) {
  108.             if(!user)
  109.                 user = t1;
  110.             else {
  111.                 printf("This script was accessed from the wrong form.\n");
  112.                 exit(1);
  113.             }
  114.         }
  115.         else if(!strcmp(t2,"newpasswd1")) {
  116.             if(!p1)
  117.                 p1 = t1;
  118.             else {
  119.                 printf("This script was accessed from the wrong form.\n");
  120.                 exit(1);
  121.             }
  122.         }
  123.         else if(!strcmp(t2,"newpasswd2")) {
  124.             if(!p2)
  125.                 p2 = t1;
  126.             else {
  127.                 printf("This script was accessed from the wrong form.\n");
  128.                 exit(1);
  129.             }
  130.         }
  131.         else {
  132.             printf("This script was accessed from the wrong form.\n");
  133.             printf("Unrecognized directive %s.\n",t2);
  134.             exit(1);
  135.         }
  136.         free(t2);
  137.     }
  138.     u=getenv("REMOTE_USER");
  139.     if((strcmp(u,WIZARD)) && (strcmp(user,u))) {
  140.             printf("<TITLE>User Mismatch</TITLE>");
  141.             printf("<H1>User Mismatch</H1>");
  142.             printf("The username you gave does not correspond with the ");
  143.             printf("user you authenticated as.\n");
  144.             exit(1);
  145.         }
  146.     if(strcmp(p1,p2)) {
  147.         printf("<TITLE>Password Mismatch</TITLE>");
  148.         printf("<H1>Password Mismatch</H1>");
  149.         printf("The two copies of your the password do not match. Please");
  150.         printf(" try again.");
  151.         exit(1);
  152.     }
  153.  
  154.     tn = tmpnam(NULL);
  155.     if(!(tfp = fopen(tn,"w"))) {
  156.         fprintf(stderr,"Could not open temp file.\n");
  157.         exit(1);
  158.     }
  159.  
  160.     if(!(f = fopen(USER_FILE,"r"))) {
  161.         fprintf(stderr,
  162.                 "Could not open passwd file for reading.\n",USER_FILE);
  163.         exit(1);
  164.     }
  165.  
  166.     found = 0;
  167.     while(!(getline(line,256,f))) {
  168.         if(found || (line[0] == '#') || (!line[0])) {
  169.             putline(tfp,line);
  170.             continue;
  171.         }
  172.         strcpy(l,line);
  173.         getword(w,l,':');
  174.         if(strcmp(user,w)) {
  175.             putline(tfp,line);
  176.             continue;
  177.         }
  178.         else {
  179.             change_password(user,p1,tfp);
  180.             found=1;
  181.         }
  182.     }
  183.     if((!found) && (create))
  184.         change_password(user,p1,tfp);
  185.     fclose(f);
  186.     fclose(tfp);
  187. #ifdef __EMX__
  188.     sprintf(command,"copy %s %s",tn,USER_FILE);
  189. #else
  190.     sprintf(command,"cp %s %s",tn,USER_FILE);
  191. #endif
  192.     system(command);
  193.     unlink(tn);
  194.     printf("<TITLE>Successful Change</TITLE>");
  195.     printf("<H1>Successful Change</H1>");
  196.     printf("Your password has been successfully changed.<P>");
  197.     exit(0);
  198. }
  199.  
  200.