Release bumped to "gts4".
[tac_plus.git] / generate_passwd.c
1 /*
2    Copyright (c) 1995-1998 by Cisco systems, Inc.
3
4    Permission to use, copy, modify, and distribute this software for
5    any purpose and without fee is hereby granted, provided that this
6    copyright and permission notice appear on all copies of the
7    software and supporting documentation, the name of Cisco Systems,
8    Inc. not be used in advertising or publicity pertaining to
9    distribution of the program without specific prior permission, and
10    notice be given in supporting documentation that modification,
11    copying and distribution is by permission of Cisco Systems, Inc.
12
13    Cisco Systems, Inc. makes no representations about the suitability
14    of this software for any purpose.  THIS SOFTWARE IS PROVIDED ``AS
15    IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
16    WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17    FITNESS FOR A PARTICULAR PURPOSE.
18 */
19
20 /* Program to des encrypt a password like Unix
21    It prompts for the password to encrypt.
22    You can optionally supply a salt to verify a password.
23    Usage: a.out [salt]
24 */
25
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #include <stdlib.h>
36 #include <time.h>
37 #include <stdio.h>
38 #include <string.h>
39
40
41 #ifndef NULL
42 #define NULL ((void *) 0)
43 #endif
44
45 /* Stolen from pbmplus.h of netpbm: */
46
47 #if __STDC__
48 #define TAC_ARGS(alist) alist
49 #else /*__STDC__*/
50 #define TAC_ARGS(alist) ()
51 #endif /*__STDC__*/
52
53
54 int main TAC_ARGS((int argc, char **argv));
55
56 int
57 main(argc, argv)
58 int argc;
59 char **argv;
60 {
61     char pass[25], *salt, buf[24];
62     char *result;
63     int n;
64     char *prompt = "Password to be encrypted: ";
65
66     salt = NULL;
67
68     if (argc == 2) {
69         salt = argv[1];
70     }
71
72     write(1, prompt, strlen(prompt));
73     n = read(0, pass, sizeof(pass));
74     pass[n-1] = 0;
75
76     if (!salt) {
77         int i, r, r1 = 0 /* GCC paranoia */, r2 = 0 /* GCC paranoia */;
78
79         srand(time(0));
80
81         for(i=0; i <= 1; i++) {
82
83             r = rand();
84
85             r = r & 127;
86
87             if (r < 46)
88                 r += 46;
89
90             if (r > 57 && r < 65)
91                 r += 7;
92
93             if (r > 90 && r < 97)
94                 r += 6;
95
96             if (r > 122)
97                 r -= 5;
98
99             if (i == 0)
100                 r1 = r;
101
102             if (i == 1)
103                 r2 = r;
104         }
105
106         sprintf(buf, "%c%c", r1, r2);
107         salt = buf;
108     }
109
110     result = crypt(pass, salt);
111
112     write(1, result, strlen(result));
113     write(1, "\n", 1);
114
115     return (EXIT_SUCCESS);
116 }