[BACK]Return to encrypt.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / encrypt

Diff for /src/usr.bin/encrypt/encrypt.c between version 1.4 and 1.5

version 1.4, 1997/03/27 23:43:36 version 1.5, 1997/03/30 19:22:46
Line 31 
Line 31 
 #include <errno.h>  #include <errno.h>
 #include <string.h>  #include <string.h>
 #include <unistd.h>  #include <unistd.h>
   #include <pwd.h>
   
 /*  /*
  * Very simple little program, for encrypting passwords from the command   * Very simple little program, for encrypting passwords from the command
  * line.  Useful for scripts and such.   * line.  Useful for scripts and such.
  */   */
   
   #define DO_MAKEKEY 0
   #define DO_DES     1
   #define DO_MD5     2
   #define DO_BLF     3
   
 extern char *optarg;  extern char *optarg;
 extern int optind;  extern int optind;
   
 char *progname;  char *progname;
   char buffer[_PASSWORD_LEN];
   
 void usage()  void usage()
 {  {
     errx(1, "usage: %s [-k] [-m] [-s salt] [string]", progname);      errx(1, "usage: %s [-k] [-b rounds] [-m] [-s salt] [string]", progname);
 }  }
   
 char *trim(line)  char *trim(line)
Line 63 
Line 70 
     return(ptr);      return(ptr);
 }  }
   
   void print_passwd(char *string, int operation, void *extra)
   {
        char msalt[3], *salt;
        struct passwd pwd;
        extern char *bcrypt_gensalt __P((int));
        extern pwd_gensalt __P((char *, int, struct passwd *, char));
   
        switch(operation) {
        case DO_MAKEKEY:
             /*
              * makekey mode: parse string into seperate DES key and salt.
              */
             if (strlen(string) != 10) {
                  /* To be compatible... */
                  fprintf (stderr, "%s: %s\n", progname, strerror(EFTYPE));
                  exit (1);
             }
             strcpy(msalt, &string[8]);
             salt = msalt;
             break;
        case DO_MD5:
             strcpy(buffer, "$1$");
             to64(&buffer[3], arc4random(), 4);
             to64(&buffer[7], arc4random(), 4);
             strcpy(buffer+11, "$");
             salt = buffer;
             break;
        case DO_BLF:
             strncpy(buffer, bcrypt_gensalt(*(int *)extra), _PASSWORD_LEN - 1);
             buffer[_PASSWORD_LEN-1] = 0;
             salt = buffer;
             break;
        case DO_DES:
             salt = extra;
             break;
        default:
             pwd.pw_name = "default";
             if (!pwd_gensalt(buffer, _PASSWORD_LEN, &pwd, 'l')) {
                  fprintf (stderr, "%s: Can't generate salt\n", progname);
                  exit (1);
             }
             salt = buffer;
             break;
        }
   
        fputs(crypt(string, salt), stdout);
   }
   
 int main(argc, argv)  int main(argc, argv)
     int argc;      int argc;
     char *argv[];      char *argv[];
 {  {
     int opt;      int opt;
     int do_md5 = 0;      int operation = -1;
     int do_makekey = 0;      int rounds;
     char *salt = (char *)NULL;      void *extra;                       /* Store salt or number of rounds */
   
     if ((progname = strrchr(argv[0], '/')))      if ((progname = strrchr(argv[0], '/')))
         progname++;          progname++;
Line 78 
Line 133 
         progname = argv[0];          progname = argv[0];
   
     if (strcmp(progname, "makekey") == 0)      if (strcmp(progname, "makekey") == 0)
         do_makekey = 1;           operation = DO_MAKEKEY;
   
     while ((opt = getopt(argc, argv, "kms:")) != -1) {      while ((opt = getopt(argc, argv, "kms:b:")) != -1) {
         switch (opt) {          switch (opt) {
         case 'k':          case 'k':                       /* Stdin/Stdout Unix crypt */
             do_makekey = 1;              if (operation != -1)
                    usage();
               operation = DO_MAKEKEY;
             break;              break;
         case 'm':          case 'm':                       /* MD5 password hash */
             do_md5 = 1;              if (operation != -1)
                    usage();
               operation = DO_MD5;
             break;              break;
         case 's':          case 's':                       /* Unix crypt (DES) */
             salt = optarg;              if (operation != -1)
             if (salt[0] == '$')         /* -s is only for DES. */                   usage();
               operation = DO_DES;
               if (optarg[0] == '$')       /* -s is only for DES. */
                 usage();                  usage();
               extra = optarg;
             break;              break;
           case 'b':                       /* Blowfish password hash */
               if (operation != -1)
                    usage();
                operation = DO_BLF;
                rounds = atoi(optarg);
                extra = &rounds;
                break;
         default:          default:
             usage();              usage();
         }          }
     }      }
   
     if (do_md5 && !do_makekey && (salt != (char *)NULL))      if (((argc - optind) < 1) || operation == DO_MAKEKEY) {
         usage();          char line[BUFSIZ], *string;
   
     if (!do_md5 && !do_makekey && (salt == (char *)NULL))  
         usage();  
   
     if (do_makekey && (do_md5 || (salt != (char *)NULL)))  
         usage();  
   
     if (((argc - optind) < 1) || do_makekey) {  
         char line[BUFSIZ], *string, msalt[3];  
   
         /* Encrypt stdin to stdout. */          /* Encrypt stdin to stdout. */
         while (!feof(stdin) && (fgets(line, sizeof(line), stdin) != NULL)) {          while (!feof(stdin) && (fgets(line, sizeof(line), stdin) != NULL)) {
             /* Kill the whitesapce. */              /* Kill the whitesapce. */
             string = trim(line);              string = trim(line);
             if (*string == '\0')              if (*string == '\0')
                 continue;                  continue;
             if (do_makekey) {  
                 /*              print_passwd(string, operation, extra);
                  * makekey mode: parse string into seperate DES key and salt.  
                  */  
                 if (strlen(string) != 10) {  
                     /* To be compatible... */  
                     fprintf (stderr, "%s: %s\n", progname, strerror(EFTYPE));  
                     exit (1);  
                 }  
                 strcpy(msalt, &string[8]);  
                 salt = msalt;  
             }  
   
             fputs(crypt(string, (do_md5 ? "$1$" : salt)), stdout);              if (operation == DO_MAKEKEY) {
             if (do_makekey) {  
                 fflush(stdout);                  fflush(stdout);
                 break;                  break;
             }              }
Line 146 
Line 195 
         /* Wipe the argument. */          /* Wipe the argument. */
         bzero(argv[optind], strlen(argv[optind]));          bzero(argv[optind], strlen(argv[optind]));
   
         fputs(crypt(string, (do_md5 ? "$1$" : salt)), stdout);          print_passwd(string, operation, extra);
   
         fputc('\n', stdout);          fputc('\n', stdout);
   
         /* Wipe our copy, before we free it. */          /* Wipe our copy, before we free it. */

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5