wsh/
wsh/binsrc/
wsh/docs/help/
wsh/docs/old/
wsh/etc/
wsh/src/util/
/* See the file wizshell.c, wizshell.h or the files in docs/ for information */

/* Adapted from the mkpasswd.c file by Bryan Kilian */

/* This utility is only useful if you are using the etc/passwd file
 * if you link 'chfn' to this program ('passwd') in bin/ then chfn will
 * be the same as calling 'passwd -f'
 */

#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

main(argc,argv)
  int argc;
  char **argv;
{
  int getfinger = 0;
  char c,buf[200],buf2[200];
  char passwd[200], user[50];
  char ppasswd[200],pacc[200];
  char *r, *s;
  char line[132], tmp[132];
  char file[10000];
  int rnd = 0;
  FILE *fp;
  time_t  tme;
  
    
  *file='\0';
  
  if(!strcmp(argv[0]+strlen(argv[0])-4,"chfn")) {
    getfinger=1;
  } else {
    if(argc>2) {
      fprintf(stderr,
              "Usage:  passwd [-f] \n   -f : Change Finger info (Full name)\n");
      return;
    }
    if (argc == 2) {
      if (strcmp(argv[1],"-f")) {
        fprintf(stderr,
             "Usage:  passwd [-f] \n   -f : Change Finger info (Full name)\n");
        return;
      } else getfinger = 1;
    }    
  }
  
  strcpy(user, getenv("WSHLOGIN"));
  if (!(*user)) {
    fprintf(stderr,"Must be inside wsh (or set WSHLOGIN) first.\n");
    return;
  }  

  fp = fopen(PASSWD_FILE,"r");
  while (fgets(line,132,fp) != NULL) { 
    if ((*line == '#') || (strlen(line) < 5) || (strchr(line,':') == NULL))
      strcat(file, line);
    else
    {
      r = line;
      s = strchr(line,':');
      *s++ = '\0';
      if (strcmp(user, r) == 0)
      {
        r = strchr(s,':'); 
        if (!(r)) return;
        *r++ = '\0';
        if (*s == '\0') {
          if (getfinger) {
            fprintf(stderr,"You cannot change your finger info.\n");
          } else {
            fprintf(stderr,"You cannot change your password.\n");
          }
          return;
        }
        if(!getfinger) {
          strcpy(ppasswd, s);
          signal(SIGINT, SIG_IGN);
          system("stty -echo");
          fprintf(stderr,"Current Password: ");
          gets(buf);
          if (strcmp(crypt(buf,ppasswd),ppasswd)) {
            fprintf(stderr,"\nWrong Password.\n");
            system("stty echo");
            return;
          }  
        }
        if(getfinger) {
          system("stty echo");
          strcpy(passwd, s);
          s = r;
          r = strchr(s,':'); 
          if (!(r)) return;
          *r++ = '\0';
          strcpy(pacc, s);
          s = r;
          r = strchr(s,':');
          if (!(r)) return;
          *r++ = '\0';
          fprintf(stderr,"\nNew Finger Info [%s]: ",s);
          gets(buf);
          signal(SIGINT, SIG_DFL);
          if (*buf == 0) {
            fprintf(stderr,"Finger info not changed.\n");
            return;
          }  
          if(strchr(buf,':')) {
            fprintf(stderr,"Illegal password entry.\n");
            exit(-1);
          }
          sprintf(tmp, "%s:%s:%s:%s:%s", line, passwd, pacc, buf, r);
        } else {
          fprintf(stderr,"\nNew Password: ");
          gets(buf);
          fprintf(stderr,"\nAgain: ");
          gets(buf2);
          system("stty echo");
          fprintf(stderr,"\n");
          signal(SIGINT, SIG_DFL);
          if(strcmp(buf,buf2)) {
            fprintf(stderr,"You changed.\n");
            return;
          }
          tme = time(NULL);
          rnd = (int) (tme & 0xffff);
          srand(rnd);
        
          rnd = (rand() & 0xff0000) >> 16;
          rnd = rnd/2;
          if (rnd < 33) rnd += 33;
          buf2[0] = rnd;
          rnd = (rand() & 0xff0000) >> 16;
          rnd = rnd/2;
          if (rnd < 33) rnd += 33;
          buf2[1] = rnd;
          buf2[2] = 0;
        /*  fprintf(stderr,"salt:  (any two characters) ");
          gets(buf2);
        */
         
          strcpy(passwd,crypt(buf,buf2));
          sprintf(tmp, "%s:%s:%s",line,passwd,r);
          }
        strcat(file,tmp);        
      } else {
        sprintf(tmp,"%s:%s",r,s);
        strcat(file,tmp);
      }       
    }
  }
  fclose(fp);
  fp = fopen(PASSWD_FILE,"w");
  fprintf(fp,"%s",file);
  fclose(fp);
  if (getfinger)
    fprintf(stderr, "Finger info changed.\n");
  else  
    fprintf(stderr, "Password changed.\n");
  return;
}