File: apg.c

package info (click to toggle)
apg 2.2.3.dfsg.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 696 kB
  • ctags: 731
  • sloc: ansic: 4,867; php: 744; sh: 186; makefile: 140; perl: 9
file content (760 lines) | stat: -rw-r--r-- 23,581 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/*
** Copyright (c) 1999, 2000, 2001, 2002, 2003
** Adel I. Mirzazhanov. All rights reserved
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 
**     1.Redistributions of source code must retain the above copyright notice,
**       this list of conditions and the following disclaimer. 
**     2.Redistributions in binary form must reproduce the above copyright
**       notice, this list of conditions and the following disclaimer in the
**       documentation and/or other materials provided with the distribution. 
**     3.The name of the author may not be used to endorse or promote products
**       derived from this software without specific prior written permission. 
** 		  
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND ANY EXPRESS
** OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO, THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED.  IN  NO  EVENT  SHALL THE AUTHOR BE LIABLE FOR ANY
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE
** GOODS OR SERVICES;  LOSS OF USE,  DATA,  OR  PROFITS;  OR BUSINESS
** INTERRUPTION)  HOWEVER  CAUSED  AND  ON  ANY  THEORY OF LIABILITY,
** WHETHER  IN  CONTRACT,   STRICT   LIABILITY,  OR  TORT  (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
** Main Module of apg programm
*/
#include <stdio.h>
#include <stdlib.h>
#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32__)
#include <strings.h>
#endif
#include <string.h>
#include <time.h>

#ifndef APG_USE_SHA
#define APG_VERSION "2.2.3 (PRNG: X9.17/CAST)"
#else /* APG_USE_SHA */
#define APG_VERSION "2.2.3 (PRNG: X9.17/SHA-1)"
#endif /* APG_USE_SHA */

#ifdef __NetBSD__
#include <unistd.h>
#endif

#if defined(__sun) || defined(sun) || defined(linux) || defined(__linux) || defined(__linux__)
#include <crypt.h>
#endif

#define MAX_MODE_LENGTH   4
#define DEFAULT_MIN_PASS_LEN 8
#define DEFAULT_MAX_PASS_LEN 10
#define DEFAULT_NUM_OF_PASS 6

#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#endif

#ifndef __NetBSD__
#include <unistd.h>
#endif

#ifdef __CYGWIN__
#undef APG_USE_CRYPT
#endif /* __CYGWIN__ */

#ifdef CLISERV
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <syslog.h>
#define MAXSOCKADDDR 128
#endif /* CLISERV */

#include "owntypes.h"
#include "pronpass.h"
#include "randpass.h"
#include "restrict.h"
#include "bloom.h"
#include "rnd.h"
#include "errs.h"
#include "getopt.h"
#include "convert.h"

 struct pass_m {
        unsigned int pass;	         /* password generation mode        */
        unsigned int filter;	         /* password generation mode        */
	};
#ifndef CLISERV
UINT32 get_user_seq (void);
UINT32 com_line_user_seq (char * seq);
char *crypt_passstring (const char *p);
void print_help (void);
#endif /* CLISERV */

int main (int argc, char *argv[]);
void checkopt(char *opt);
int construct_mode(char *str_mode, struct pass_m * mde);

/*
** main()
*/
int
main (int argc, char *argv[])
{
 int i = 0;
 int restrict_res = 0;
 
 char *pass_string;
 char *hyph_pass_string;
 time_t tme;
 
 
 int option = 0;                         /* programm option                 */

 int algorithm = 0;                      /* algorithm for generation        */
 int restrictions_present = FALSE;       /* restrictions flag               */
 int plain_restrictions_present = FALSE; /* dictionary restrictions_flag    */
 int bloom_restrict_present = FALSE;     /* bloom filter restrictions flag  */
 int paranoid_bloom_restrict_present = FALSE;     /* paranoid bloom filter restrictions flag  */
 int filter_restrict_present = FALSE;    /* filter restrictions flag        */
 int exclude_list_present = FALSE;       /* exclude list present            */
 int quiet_present = FALSE;              /* quiet mode flag                 */
 int hyph_req_present = FALSE;           /* Request to print hyphenated password              */
 char *restrictions_file;                /* dictionary file name            */
 char *plain_restrictions_file;          /* dictionary file name            */
 struct pass_m mode;
 unsigned int pass_mode_present = FALSE; /* password generation mode flag   */
 USHORT min_pass_length = DEFAULT_MIN_PASS_LEN;             /* min password length             */
 USHORT max_pass_length = DEFAULT_MAX_PASS_LEN;             /* max password length             */
 USHORT min_substr_len = 0;              /* min substring length to check if
                                         ** paranoid check is used          */
 int number_of_pass = DEFAULT_NUM_OF_PASS;                 /* number of passwords to generate */
 UINT32 user_defined_seed = 0L;          /* user defined random seed        */
 int user_defined_seed_present = FALSE;  /* user defined random seed flag   */
 char *str_mode;                         /* string mode pointer             */
#ifndef CLISERV
 char *com_line_seq;
 char *spell_pass_string;
 int spell_present = FALSE;              /* spell password mode flag        */
 unsigned int delimiter_flag_present = FALSE;
#ifdef APG_USE_CRYPT
 char *crypt_string;
 unsigned int show_crypt_text = FALSE;   /* display crypt(3)'d text flag    */
#endif /* APG_USE_CRYPT */
#endif /* CLISERV */
#ifdef CLISERV
#if defined(sgi) || defined(__APPLE__) || defined(__QNX__) /* Thanks to Andrew J. Caird */
 typedef unsigned int socklen_t;
#endif
 socklen_t len;
 struct sockaddr_in *cliaddr;
 char delim[2]={0x0d,0x0a};
 char *out_pass;
 char *peer_ip_unknown = "UNKNOWN";
 char *peer_ip;

 openlog(argv[0], LOG_PID, LOG_DAEMON);
 cliaddr = (struct sockaddr_in *)calloc(1,MAXSOCKADDDR);
 len = MAXSOCKADDDR;
 if( getpeername(0, (struct sockaddr *)cliaddr, &len) != 0)
  {
   err_sys("getpeername");
   peer_ip = peer_ip_unknown;
  }
 else
  {
   peer_ip = inet_ntoa(cliaddr->sin_addr);
  }
 syslog (LOG_INFO, "password generation request from %s.%d\n", peer_ip, htons(cliaddr->sin_port));
#endif /* CLISERV */

 /*
 ** Analize options
 */
#ifndef CLISERV
#ifdef APG_USE_CRYPT
 while ((option = apg_getopt (argc, argv, "M:E:a:r:b:p:sdc:n:m:x:htvylq")) != -1)
#else /* APG_USE_CRYPT */
 while ((option = apg_getopt (argc, argv, "M:E:a:r:b:p:sdc:n:m:x:htvlq")) != -1)
#endif /* APG_USE_CRYPT */
#else /* CLISERV */
 while ((option = apg_getopt (argc, argv, "M:E:a:r:b:p:n:m:x:vt")) != -1)
#endif /* CLISERV */
  {
   switch (option)
    {
     case 'M': /* mode parameter */
      str_mode = apg_optarg;
      if( (construct_mode(str_mode,&mode)) == -1)
         err_app_fatal("construct_mode","wrong parameter");
      pass_mode_present = TRUE;
      if(mode.filter != 0)
        {
         filter_restrict_present = TRUE;
	 restrictions_present = TRUE;
	}
      break;
     case 'E': /* exclude char */
      if(set_exclude_list(apg_optarg)==-1)
         err_app_fatal("set_exclude_list","string is too long (max. 93 characters)");
      exclude_list_present = TRUE;
      break;
     case 'a': /* algorithm specification */
      checkopt(apg_optarg);
      algorithm = atoi (apg_optarg);
      break;
     case 'r': /* restrictions */
      restrictions_present = TRUE;
      plain_restrictions_present = TRUE;
      plain_restrictions_file = apg_optarg;
      break;
     case 'b': /* bloom restrictions */
      restrictions_present = TRUE;
      bloom_restrict_present = TRUE;
      restrictions_file = apg_optarg;
      break;
     case 'p': /* paranoid bloom restrictions */
      checkopt(apg_optarg);
      min_substr_len = atoi (apg_optarg);
      paranoid_bloom_restrict_present = TRUE;
      break;
#ifndef CLISERV
     case 'l':
      spell_present = TRUE;
      break;
#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32__)
     case 's': /* user random seed required */
      user_defined_seed = get_user_seq ();
      user_defined_seed_present = TRUE;
      break;
#endif /* WIN32 */
     case 'c': /* user random seed given in command line */
      com_line_seq = apg_optarg;
      user_defined_seed = com_line_user_seq (com_line_seq);
      user_defined_seed_present = TRUE;
      break;
     case 'd': /* no delimiters option */
      delimiter_flag_present = TRUE;
      break;
     case 'q': /* quiet mode */
      quiet_present = TRUE;
      break;
#ifdef APG_USE_CRYPT
     case 'y': /* display crypt(3)'d text next to passwords */
      show_crypt_text = TRUE;
      break;                                                                  
#endif /* APG_USE_CRYPT */
#endif /* CLISERV */
     case 'n': /* number of password specification */
      checkopt(apg_optarg);
      number_of_pass = atoi (apg_optarg);
      break;
     case 'm': /* min password length */
      checkopt(apg_optarg);
      min_pass_length = (USHORT) atoi (apg_optarg);
      break;
     case 'x': /* max password length */
      checkopt(apg_optarg);
      max_pass_length = (USHORT) atoi (apg_optarg);
      break;
     case 't': /* request to print hyphenated password */
      hyph_req_present = TRUE;
      break;
#ifndef CLISERV
     case 'h': /* print help */
      print_help ();
      return (0);
#endif /* CLISERV */
     case 'v': /* print version */
      printf ("APG (Automated Password Generator)");
      printf ("\nversion %s", APG_VERSION);
      printf ("\nCopyright (c) 1999, 2000, 2001, 2002, 2003 Adel I. Mirzazhanov\n");
      return (0);
     default: /* print help end exit */
#ifndef CLISERV
      print_help ();
#endif /* CLISERV */
      exit (-1);
    }
  }
 if (pass_mode_present != TRUE)
    mode.pass = S_SS | S_NB | S_CL | S_SL;
 if (exclude_list_present == TRUE)
    mode.pass = mode.pass | S_RS;
 if( (tme = time(NULL)) == ( (time_t)-1))
    err_sys("time");
 if (user_defined_seed_present != TRUE)
    x917_setseed ( (UINT32)tme, quiet_present);
 else
    x917_setseed (user_defined_seed ^ (UINT32)tme, quiet_present);
 if (min_pass_length > max_pass_length)
    max_pass_length = min_pass_length;
 /* main code section */
 
 /*
 ** reserv space for password and hyphenated password and report of errors
 ** 18 because the maximum length of element for hyphenated password is 17
 */
 if ( (pass_string = (char *)calloc (1, (size_t)(max_pass_length + 1)))==NULL ||
      (hyph_pass_string = (char *)calloc (1, (size_t)(max_pass_length*18)))==NULL)
      err_sys_fatal("calloc");
#ifndef CLISERV
#ifdef APG_USE_CRYPT
 if (show_crypt_text == TRUE)
   if ((crypt_string = (char *)calloc (1, 255))==NULL)      
      err_sys_fatal("calloc");
#endif /* APG_USE_CRYPT */
#endif /* CLISERV */
#ifdef CLISERV
 if ( (out_pass = (char *)calloc(1, (size_t)(max_pass_length*19 + 4))) == NULL)
      err_sys_fatal("calloc");
#endif /* CLISERV */
 /*
 ** generate required amount of passwords using specified algorithm
 ** and check for restrictions if specified with command line parameters
 */
 while (i < number_of_pass)
  {
   if (algorithm == 0)
    {
     if (gen_pron_pass(pass_string, hyph_pass_string,
                       min_pass_length, max_pass_length, mode.pass) == -1)
        err_app_fatal("apg","wrong password length parameter");
#ifndef CLISERV
#ifdef APG_USE_CRYPT
     if (show_crypt_text == TRUE)
         (void) memcpy ((void *)crypt_string,
	                (void *)crypt_passstring (pass_string), 255);
#endif /* APG_USE_CRYPT */
#endif /* CLISERV */
     /***************************************
     ** ALGORITHM = 0 RESTRICTIONS = PRESENT
     ****************************************/
     if (restrictions_present == TRUE)
       {
        /* Filter check */
        if (filter_restrict_present == TRUE)
	  restrict_res = filter_check_pass(pass_string, mode.filter);
	/* Bloom-filter check */
	if (restrict_res == 0)
	 {
	  if (bloom_restrict_present == TRUE)
	   {
	    if(paranoid_bloom_restrict_present != TRUE)
              restrict_res = bloom_check_pass(pass_string, restrictions_file);
	    else
	      restrict_res = paranoid_bloom_check_pass(pass_string, restrictions_file, min_substr_len);
	   }
	 }
	 /* Dictionary check */
	 if (restrict_res == 0)
	  if (plain_restrictions_present == TRUE)
            restrict_res = check_pass(pass_string, plain_restrictions_file);


        switch (restrict_res)
	  {
	  case 0:
#ifndef CLISERV
            fprintf (stdout, "%s", pass_string);
            if (hyph_req_present == TRUE)
	      fprintf (stdout, " (%s)", hyph_pass_string);
#ifdef APG_USE_CRYPT
            if (show_crypt_text == TRUE)
	      fprintf (stdout, " %s", crypt_string);
#endif /* APG_USE_CRYPT */
	    if (spell_present == TRUE)
	     {
	      spell_pass_string = spell_word(pass_string, spell_pass_string);
	      fprintf (stdout, (" %s"), spell_pass_string);
	      free((void*)spell_pass_string);
	     }
	    if ( delimiter_flag_present == FALSE )
	       fprintf (stdout, "\n");
	    fflush (stdout);
#else /* CLISERV */
            if (hyph_req_present == TRUE)
             snprintf(out_pass, max_pass_length*19 + 4, "%s (%s)", pass_string, hyph_pass_string);
	    else
             snprintf(out_pass, max_pass_length*19 + 4, "%s", pass_string);	    
	    write (0, (void*) out_pass, strlen(out_pass));
	    write (0, (void*)&delim[0],2);
#endif /* CLISERV */
	    i++;
	    break;
	  case 1:
	    break;
	  case -1:
	    err_sys_fatal ("check_pass");
	  default:
	    break;
	  } /* switch */
       }
     /******************************************
     ** ALGORITHM = 0 RESTRICTIONS = NOT_PRESENT
     *******************************************/
     else
       {
#ifndef CLISERV
        fprintf (stdout, "%s", pass_string);
        if (hyph_req_present == TRUE)
	  fprintf (stdout, " (%s)", hyph_pass_string);
#ifdef APG_USE_CRYPT
        if (show_crypt_text == TRUE)
	  fprintf (stdout, " %s", crypt_string);
#endif /* APG_USE_CRYPT */
	if (spell_present == TRUE)
	 {
	  spell_pass_string = spell_word(pass_string, spell_pass_string);
	  fprintf (stdout, (" %s"), spell_pass_string);
	  free((void*)spell_pass_string);
	 }
        if ( delimiter_flag_present == FALSE )
	   fprintf (stdout, "\n");
	fflush (stdout);
#else /* CLISERV */
        if (hyph_req_present == TRUE)
         snprintf(out_pass, max_pass_length*19 + 4, "%s (%s)", pass_string, hyph_pass_string);
	else
         snprintf(out_pass, max_pass_length*19 + 4, "%s", pass_string);	    
	write (0, (void*) out_pass, strlen(out_pass));
	write (0, (void*)&delim[0],2);
#endif /* CLISERV */
	i++;
       }
    }
   /***************************************
   ** ALGORITHM = 1
   ****************************************/
   else if (algorithm == 1)
    {
     if (gen_rand_pass(pass_string, min_pass_length,
                       max_pass_length, mode.pass) == -1)
        err_app_fatal("apg","wrong password length parameter");
#ifndef CLISERV
#ifdef APG_USE_CRYPT
     if (show_crypt_text == TRUE)
         (void)memcpy ((void *)crypt_string,
	               (void *)crypt_passstring(pass_string), 255);
#endif /* APG_USE_CRYPT */
#endif /* CLISERV */
     /***************************************
     ** ALGORITHM = 1 RESTRICTIONS = PRESENT
     ****************************************/
     if ( (restrictions_present == TRUE))
       {
        /* Filter check */
        if (filter_restrict_present == TRUE)
	  restrict_res = filter_check_pass(pass_string, mode.filter);
	/* Bloom-filter check */
	if (restrict_res == 0)
	 {
	  if (bloom_restrict_present == TRUE)
	   {
	    if(paranoid_bloom_restrict_present != TRUE)
              restrict_res = bloom_check_pass(pass_string, restrictions_file);
	    else
	      restrict_res = paranoid_bloom_check_pass(pass_string, restrictions_file, min_substr_len);
	   }
	 }
	 /* Dictionary check */
	 if (restrict_res == 0)
	  if (plain_restrictions_present == TRUE)
            restrict_res = check_pass(pass_string, plain_restrictions_file);


        switch (restrict_res)
	  {
	  case 0:
#ifndef CLISERV
#ifdef APG_USE_CRYPT
            if (show_crypt_text==TRUE)
	      fprintf (stdout, "%s %s", pass_string, crypt_string);
	    else
#endif /* APG_USE_CRYPT */
	      fprintf (stdout, "%s", pass_string);
	    if (spell_present == TRUE)
	     {
	      spell_pass_string = spell_word(pass_string, spell_pass_string);
	      fprintf (stdout, (" %s"), spell_pass_string);
	      free((void*)spell_pass_string);
	     }
	    if ( delimiter_flag_present == FALSE )
	       fprintf (stdout, "\n");
	    fflush (stdout);
#else /* CLISERV */
	    write (0, (void*)pass_string, strlen(pass_string));
	    write (0, (void*)&delim[0],2);
#endif /* CLISERV */
	    i++;
	    break;
	  case 1:
	    break;
	  case -1:
	    err_sys_fatal ("check_pass");
	  default:
	    break;
	  } /* switch */
       }
     /***************************************
     ** ALGORITHM = 1 RESTRICTIONS = PRESENT
     ****************************************/
     else
       {
#ifndef CLISERV
#ifdef APG_USE_CRYPT
        if (show_crypt_text==TRUE)
          fprintf (stdout, "%s %s", pass_string, crypt_string);
	else
#endif /* APG_USE_CRYPT */
          fprintf (stdout, "%s", pass_string);
	if (spell_present == TRUE)
	 {
	  spell_pass_string = spell_word(pass_string, spell_pass_string);
	  fprintf (stdout, (" %s"), spell_pass_string);
	  free((void*)spell_pass_string);
	 }
	if ( delimiter_flag_present == FALSE )
	   fprintf (stdout, "\n");
	fflush (stdout);
#else /* CLISERV */
	write (0, (void*)pass_string, strlen(pass_string));
	write (0, (void*)&delim[0],2);
#endif /* CLISERV */
	i++;
       }
    } /* end of if (algorithm == 1) */
   else
     err_app_fatal ("apg","wrong algorithm type");

   restrict_res = 0;
  } /* end of while (i <= number_of_pass) */
 free((void*)pass_string);
 free((void*)hyph_pass_string);
#ifndef CLISERV
#ifdef APG_USE_CRYPT
 if (show_crypt_text==TRUE)
    free((void*)crypt_string);
#endif /* APG_USE_CRYPT */
#endif /* CLISERV */
#ifdef CLISERV
 free ((void *)out_pass);
 free ((void *)cliaddr);
 close (0);
 closelog();
#endif /* CLISERV */
 return(0);
} /* end of main */

#ifndef CLISERV
#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32__)
/*
** get_user_seq() - Routine that gets user random sequense
** and generates sutable random seed according to it.
** INPUT:
**   void
** OUTPUT:
**   UINT32 - random seed
** NOTES:
**   none
*/
UINT32
get_user_seq (void)
{
 char * seq;
 UINT32 prom[2] = { 0L, 0L };
 UINT32 sdres = 0L;
 printf ("\nPlease enter some random data (only first %d are significant)\n", sizeof(prom));
 seq = (char *)getpass("(eg. your old password):>");
 if (strlen(seq) < sizeof(prom))
  (void)memcpy((void *)&prom[0], (void *)seq, (int)strlen(seq));
 else
  (void)memcpy((void *)&prom[0], (void *)seq, sizeof(prom));
 sdres = prom[0]^prom[1];
 return (sdres);
}
#endif /* WIN32 */
/*
** com_line_user_seq() - Routine that gets user random sequense
** from command line and generates sutable random seed according to it
** INPUT:
**   char * - command line seed
** OUTPUT:
**   UINT32 - random seed
** NOTES:
**   none
*/
UINT32
com_line_user_seq (char * seq)
{
 UINT32 prom[2] = { 0L, 0L };
 UINT32 sdres = 0L;
 if (strlen(seq) < sizeof (prom))
  (void)memcpy((void *)&prom[0], (void *)seq, (int)strlen(seq));
 else
  (void)memcpy((void *)&prom[0], (void *)seq, sizeof(prom));
 sdres = prom[0]^prom[1];
 return (sdres);
}

/*
** print_help() - print help :)))
** INPUT:
**   none.
** OUTPUT:
**   help info to the stdout.
** NOTES:
**   none.
*/
void
print_help (void)
{
 printf ("\napg   Automated Password Generator\n");
 printf ("        Copyright (c) Adel I. Mirzazhanov\n");
 printf ("\napg   [-a algorithm] [-r file] \n");
 printf ("      [-M mode] [-E char_string] [-n num_of_pass] [-m min_pass_len]\n");
 printf ("      [-x max_pass_len] [-c cl_seed] [-d] [-s] [-h] [-y] [-q]\n");
 printf ("\n-M mode         new style password modes\n");
 printf ("-E char_string  exclude characters from password generation process\n");
 printf ("-r file         apply dictionary check against file\n");
 printf ("-b filter_file  apply bloom filter check against filter_file\n");
 printf ("                (filter_file should be created with apgbfm(1) utility)\n");
 printf ("-p substr_len   paranoid modifier for bloom filter check\n");
 printf ("-a algorithm    choose algorithm\n");
 printf ("                 1 - random password generation according to\n");
 printf ("                     password modes\n");
 printf ("                 0 - pronounceable password generation\n");
 printf ("-n num_of_pass  generate num_of_pass passwords\n");
 printf ("-m min_pass_len minimum password length\n");
 printf ("-x max_pass_len maximum password length\n");
#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32__)
 printf ("-s              ask user for a random seed for password\n");
 printf ("                generation\n");
#endif /* WIN32 */
 printf ("-c cl_seed      use cl_seed as a random seed for password\n");
 printf ("-d              do NOT use any delimiters between generated passwords\n");
 printf ("-l              spell generated password\n");
 printf ("-t              print pronunciation for generated pronounceable password\n");
#ifdef APG_USE_CRYPT
 printf ("-y              print crypted passwords\n");
#endif /* APG_USE_CRYPT */
 printf ("-q              quiet mode (do not print warnings)\n");
 printf ("-h              print this help screen\n");
 printf ("-v              print version information\n");
}

#ifdef APG_USE_CRYPT
/*
** crypt_passstring() - produce crypted password.
** INPUT:
**   const char * - password string
** OUTPUT:
**   char * - crypted password 
** NOTES:
**   none.
*/
char * crypt_passstring (const char *p)
{
 char salt[11];
 gen_rand_pass (salt, 10, 10, S_SL|S_CL|S_NB);
 return (crypt(p, salt));
}
#endif /* APG_USE_CRYPT */
#endif /* CLISERV */

/*
** checkopt() - check options.
** INPUT:
**   char * - options string.
** OUTPUT:
**   none.
** NOTES:
**   option should contain only numeral symbols.
*/
void
checkopt(char *opt)
{
 int i;

 for(i=0; i < strlen(opt);i++)
  if(opt[i] != '0' && opt[i] != '1' && opt[i] != '2' && opt[i] != '3' &&
     opt[i] != '4' && opt[i] != '5' && opt[i] != '6' && opt[i] != '7' &&
     opt[i] != '8' && opt[i] != '9')
      err_app_fatal ("checkopt", "wrong option format");
}

/*
** construct_mode() - construct mode for password
** generation from string.
** INPUT:
**   char * - string mode.
** OUTPUT:
**   int - return code.
**     0 - OK
**    -1 - ERROR
** NOTES:
**   none.
*/
int construct_mode(char *s_mode, struct  pass_m * mde)
{
 unsigned int mode = 0;
 unsigned int filter = 0;
 int ch = 0;
 int i = 0;
 int str_length = 0;
 
 str_length = strlen(s_mode);
 
 if (str_length > MAX_MODE_LENGTH)
     return(-1);
 for (i=0; i < str_length; i++)
  {
   ch = (int)*s_mode;
   switch(ch)
    {
     case 'S':
      mode = mode | S_SS;
      filter = filter | S_SS;
      break;
     case 'N':
      mode = mode | S_NB;
      filter = filter | S_NB;
      break;
     case 'C':
      mode = mode | S_CL;
      filter = filter | S_CL;
      break;
     case 'L':
      mode = mode | S_SL;
      filter = filter | S_SL;
      break;
     case 's':
      mode = mode | S_SS;
      break;
     case 'n':
      mode = mode | S_NB;
      break;
     case 'c':
      mode = mode | S_CL;
      break;
     case 'l':
      mode = mode | S_SL;
      break;
     default:
      return(-1);
      break;
    }
   s_mode++;
  }
 mde->pass = mode;
 mde->filter = filter;
 return (0);
}