"Fossies" - the Fresh Open Source Software Archive

Member "pwgen-2.08/pwgen.c" (7 Aug 2017, 5279 Bytes) of package /linux/privat/pwgen-2.08.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "pwgen.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.07_vs_2.08.

    1 /*
    2  * pwgen.c --- generate secure passwords
    3  *
    4  * Copyright (C) 2001,2002 by Theodore Ts'o
    5  * 
    6  * This file may be distributed under the terms of the GNU Public
    7  * License.
    8  */
    9 
   10 #include <stdio.h>
   11 #include <unistd.h>
   12 #include <stdlib.h>
   13 #include <string.h>
   14 #include <ctype.h>
   15 #ifdef HAVE_GETOPT_H
   16 #include <getopt.h>
   17 #endif
   18 
   19 #include "pwgen.h"
   20 
   21 /* Globals variables */
   22 int (*pw_number)(int max_num);
   23 
   24 /* Program parameters set via getopt */
   25 
   26 int pw_length = 8;
   27 int num_pw = -1;
   28 int pwgen_flags = 0;
   29 int do_columns = 0;
   30 
   31 #ifdef HAVE_GETOPT_LONG
   32 struct option pwgen_options[] = {
   33     { "alt-phonics", no_argument, 0, 'a' },
   34     { "capitalize", no_argument, 0, 'c' },
   35     { "numerals", no_argument, 0, 'n'},
   36     { "symbols", no_argument, 0, 'y'},
   37     { "num-passwords", required_argument, 0, 'N'},
   38     { "remove-chars", required_argument, 0, 'r' },
   39     { "secure", no_argument, 0, 's' },
   40     { "help", no_argument, 0, 'h'},
   41     { "no-numerals", no_argument, 0, '0' },
   42     { "no-capitalize", no_argument, 0, 'A' },
   43     { "sha1", required_argument, 0, 'H' },
   44     { "ambiguous", no_argument, 0, 'B' },
   45     { "no-vowels", no_argument, 0, 'v' },
   46     { 0, 0, 0, 0}
   47 };
   48 #endif
   49 
   50 const char *pw_options = "01AaBCcnN:sr:hH:vy";
   51 
   52 static void usage(void)
   53 {
   54     fputs("Usage: pwgen [ OPTIONS ] [ pw_length ] [ num_pw ]\n\n", stderr);
   55     fputs("Options supported by pwgen:\n", stderr);
   56     fputs("  -c or --capitalize\n", stderr);
   57     fputs("\tInclude at least one capital letter in the password\n", 
   58           stderr);
   59     fputs("  -A or --no-capitalize\n", stderr);
   60     fputs("\tDon't include capital letters in the password\n", 
   61           stderr);
   62     fputs("  -n or --numerals\n", stderr);
   63     fputs("\tInclude at least one number in the password\n", stderr);
   64     fputs("  -0 or --no-numerals\n", stderr);
   65     fputs("\tDon't include numbers in the password\n", 
   66           stderr);
   67     fputs("  -y or --symbols\n", stderr);
   68     fputs("\tInclude at least one special symbol in the password\n", 
   69           stderr);
   70     fputs("  -r <chars> or --remove-chars=<chars>\n", stderr);
   71     fputs("\tRemove characters from the set of characters to "
   72           "generate passwords\n", stderr);
   73     fputs("  -s or --secure\n", stderr);
   74     fputs("\tGenerate completely random passwords\n", stderr);
   75     fputs("  -B or --ambiguous\n", stderr);
   76     fputs("\tDon't include ambiguous characters in the password\n", 
   77           stderr);
   78     fputs("  -h or --help\n", stderr);
   79     fputs("\tPrint a help message\n", stderr);
   80     fputs("  -H or --sha1=path/to/file[#seed]\n", stderr);
   81     fputs("\tUse sha1 hash of given file as a (not so) random generator\n",
   82           stderr);
   83     fputs("  -C\n\tPrint the generated passwords in columns\n", stderr);
   84     fputs("  -1\n\tDon't print the generated passwords in columns\n", 
   85           stderr);
   86     fputs("  -v or --no-vowels\n", stderr);
   87     fputs("\tDo not use any vowels so as to avoid accidental nasty words\n",
   88           stderr);
   89     exit(1);
   90 }
   91 
   92 
   93 int main(int argc, char **argv)
   94 {
   95     int term_width = 80;
   96     int c, i;
   97     int num_cols = -1;
   98     char    *buf, *tmp;
   99     char    *remove=NULL;
  100     void    (*pwgen)(char *inbuf, int size, int pw_flags, char *remove);
  101 
  102     pwgen = pw_phonemes;
  103     pw_number = pw_random_number;
  104     if (isatty(1))
  105         do_columns = 1;
  106     pwgen_flags |= PW_DIGITS | PW_UPPERS;
  107 
  108     while (1) {
  109 #ifdef HAVE_GETOPT_LONG
  110         c = getopt_long(argc, argv, pw_options, pwgen_options, 0);
  111 #else
  112         c = getopt(argc, argv, pw_options);
  113 #endif
  114         if (c == -1)
  115             break;
  116         switch (c) {
  117         case '0':
  118             pwgen_flags &= ~PW_DIGITS;
  119             break;
  120         case 'A':
  121             pwgen_flags &= ~PW_UPPERS;
  122             break;
  123         case 'a':
  124             break;
  125         case 'B':
  126             pwgen_flags |= PW_AMBIGUOUS;
  127             break;
  128         case 'c':
  129             pwgen_flags |= PW_UPPERS;
  130             break;
  131         case 'n':
  132             pwgen_flags |= PW_DIGITS;
  133             break;
  134         case 'N':
  135             num_pw = strtol(optarg, &tmp, 0);
  136             if (*tmp) {
  137                 fprintf(stderr,
  138                     "Invalid number of passwords: %s\n",
  139                     optarg);
  140                 exit(1);
  141             }
  142             break;
  143         case 's':
  144             pwgen = pw_rand;
  145             break;
  146         case 'C':
  147             do_columns = 1;
  148             break;
  149         case '1':
  150             do_columns = 0;
  151             break;
  152         case 'H': 
  153             pw_sha1_init(optarg);
  154             pw_number = pw_sha1_number;
  155             break;
  156         case 'y':
  157             pwgen_flags |= PW_SYMBOLS;
  158             break;
  159         case 'v':
  160             pwgen = pw_rand;
  161             pwgen_flags |= PW_NO_VOWELS;
  162             break;
  163         case 'r':
  164             remove = strdup(optarg);
  165             pwgen = pw_rand;
  166             break;
  167         case 'h':
  168         case '?':
  169             usage();
  170             break;
  171         }
  172     }
  173     if (optind < argc) {
  174         pw_length = strtol(argv[optind], &tmp, 0);
  175         if (pw_length < 5)
  176             pwgen = pw_rand;
  177         if (pwgen != pw_rand) {
  178             if (pw_length <= 2)
  179                 pwgen_flags &= ~PW_UPPERS;
  180             if (pw_length <= 1)
  181                 pwgen_flags &= ~PW_DIGITS;
  182         }
  183         if (*tmp) {
  184             fprintf(stderr, "Invalid password length: %s\n",
  185                 argv[optind]);
  186             exit(1);
  187         }
  188         optind++;
  189     }
  190 
  191     if (optind < argc) {
  192         num_pw = strtol(argv[optind], &tmp, 0);
  193         if (*tmp) {
  194             fprintf(stderr, "Invalid number of passwords: %s\n",
  195                 argv[optind]);
  196             exit(1);
  197         }
  198     }
  199     
  200     if (do_columns) {
  201         num_cols = term_width / (pw_length+1);
  202         if (num_cols == 0)
  203             num_cols = 1;
  204     }
  205     if (num_pw < 0)
  206         num_pw = do_columns ? num_cols * 20 : 1;
  207     
  208     buf = malloc(pw_length+1);
  209     if (!buf) {
  210         fprintf(stderr, "Couldn't malloc password buffer.\n");
  211         exit(1);
  212     }
  213     for (i=0; i < num_pw; i++) {
  214         pwgen(buf, pw_length, pwgen_flags, remove);
  215         if (!do_columns || ((i % num_cols) == (num_cols-1)) ||
  216             (i == (num_pw - 1)))
  217             printf("%s\n", buf);
  218         else
  219             printf("%s ", buf);
  220     }
  221     free(buf);
  222     return 0;
  223 }