]> granicus.if.org Git - apache/blob - support/htdigest.c
APR-ize the CHARSET_EBCDIC support in the SHA1 routines. The app (e.g.,
[apache] / support / htdigest.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  *    if any, must include the following acknowledgment:
21  *       "This product includes software developed by the
22  *        Apache Software Foundation (http://www.apache.org/)."
23  *    Alternately, this acknowledgment may appear in the software itself,
24  *    if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" must
27  *    not be used to endorse or promote products derived from this
28  *    software without prior written permission. For written
29  *    permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  *    nor may "Apache" appear in their name, without prior written
33  *    permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation.  For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  */
54
55 /******************************************************************************
56  ******************************************************************************
57  * NOTE! This program is not safe as a setuid executable!  Do not make it
58  * setuid!
59  ******************************************************************************
60  *****************************************************************************/
61 /*
62  * htdigest.c: simple program for manipulating digest passwd file for Apache
63  *
64  * by Alexei Kosut, based on htpasswd.c, by Rob McCool
65  */
66
67 #include "apr_lib.h"
68 #include "apr_md5.h"
69
70 #ifdef HAVE_SYS_TYPES_H
71  #include <sys/types.h>
72 #endif
73
74 #ifdef HAVE_SYS_SIGNAL_H
75 #include <sys/signal.h>
76 #endif
77
78 #ifdef HAVE_SIGNAL_H
79 #include <signal.h>
80 #endif
81
82 #include <stdlib.h>
83
84 #ifdef WIN32
85 #include <conio.h>
86 #endif
87
88 #if 'A' == 0xC1
89 #define CHARSET_EBCDIC
90 #endif
91
92 #ifdef CHARSET_EBCDIC
93 #define LF '\n'
94 #define CR '\r'
95 #else
96 #define LF 10
97 #define CR 13
98 #endif /* CHARSET_EBCDIC */
99
100 #define MAX_STRING_LEN 256
101
102 char *tn;
103 ap_pool_t *cntxt;
104 #ifdef CHARSET_EBCDIC
105 ap_xlate_t *to_ascii;
106 #endif
107
108 static void getword(char *word, char *line, char stop)
109 {
110     int x = 0, y;
111
112     for (x = 0; ((line[x]) && (line[x] != stop)); x++)
113         word[x] = line[x];
114
115     word[x] = '\0';
116     if (line[x])
117         ++x;
118     y = 0;
119
120     while ((line[y++] = line[x++]));
121 }
122
123 static int getline(char *s, int n, ap_file_t *f)
124 {
125     register int i = 0;
126     char ch;
127
128     while (1) {
129         ap_getc(&ch, f);
130             s[i] = ch;
131
132         if (s[i] == CR)
133             ap_getc(&ch, f);
134             s[i] = ch;
135
136         if ((s[i] == 0x4) || (s[i] == LF) || (i == (n - 1))) {
137             s[i] = '\0';
138             if (ap_eof(f) == APR_EOF) {
139                 return 1;
140             }
141             return 0;
142         }
143         ++i;
144     }
145 }
146
147 static void putline(ap_file_t *f, char *l)
148 {
149     int x;
150
151     for (x = 0; l[x]; x++)
152         ap_putc(l[x], f);
153     ap_putc('\n', f);
154 }
155
156
157 static void add_password(char *user, char *realm, ap_file_t *f)
158 {
159     char *pw;
160     ap_md5_ctx_t context;
161     unsigned char digest[16];
162     char string[MAX_STRING_LEN];
163     char pwin[MAX_STRING_LEN];
164     char pwv[MAX_STRING_LEN];
165     unsigned int i;
166     size_t len = sizeof(pwin);
167
168     if (ap_getpass("New password: ", pwin, &len) != APR_SUCCESS) {
169         fprintf(stderr, "password too long");
170         exit(5);
171     }
172     len = sizeof(pwin);
173     ap_getpass("Re-type new password: ", pwv, &len);
174     if (strcmp(pwin, pwv) != 0) {
175         fprintf(stderr, "They don't match, sorry.\n");
176         if (tn) {
177             ap_remove_file(tn, cntxt);
178         }
179         exit(1);
180     }
181     pw = pwin;
182     ap_fprintf(f, "%s:%s:", user, realm);
183
184     /* Do MD5 stuff */
185     sprintf(string, "%s:%s:%s", user, realm, pw);
186
187     ap_MD5Init(&context);
188 #ifdef CHARSET_EBCDIC
189     ap_MD5SetXlate(&context, to_ascii);
190 #endif
191     ap_MD5Update(&context, (unsigned char *) string, strlen(string));
192     ap_MD5Final(digest, &context);
193
194     for (i = 0; i < 16; i++)
195         ap_fprintf(f, "%02x", digest[i]);
196
197     ap_fprintf(f, "\n");
198 }
199
200 static void usage(void)
201 {
202     fprintf(stderr, "Usage: htdigest [-c] passwordfile realm username\n");
203     fprintf(stderr, "The -c flag creates a new file.\n");
204     exit(1);
205 }
206
207 static void interrupted(void)
208 {
209     fprintf(stderr, "Interrupted.\n");
210     if (tn)
211         ap_remove_file(tn, cntxt);
212     exit(1);
213 }
214
215 int main(int argc, char *argv[])
216 {
217     ap_file_t *tfp = NULL, *f;
218     ap_status_t rv;
219     char user[MAX_STRING_LEN];
220     char realm[MAX_STRING_LEN];
221     char line[MAX_STRING_LEN];
222     char l[MAX_STRING_LEN];
223     char w[MAX_STRING_LEN];
224     char x[MAX_STRING_LEN];
225     char command[MAX_STRING_LEN];
226     int found;
227    
228     rv = ap_initialize();
229     if (rv) {
230         fprintf(stderr, "ap_initialize(): %s (%d)\n",
231                 ap_strerror(rv, line, sizeof(line)), rv);
232         exit(1);
233     }
234     atexit(ap_terminate); 
235     ap_create_pool(&cntxt, NULL);
236
237 #ifdef CHARSET_EBCDIC
238     rv = ap_xlate_open(&to_ascii, "ISO8859-1", APR_DEFAULT_CHARSET, cntxt);
239     if (rv) {
240         fprintf(stderr, "ap_xlate_open(): %s (%d)\n",
241                 ap_strerror(rv, line, sizeof(line)), rv);
242         exit(1);
243     }
244 #endif
245     
246     tn = NULL;
247     ap_signal(SIGINT, (void (*)(int)) interrupted);
248     if (argc == 5) {
249         if (strcmp(argv[1], "-c"))
250             usage();
251         if (ap_open(&tfp, argv[2], APR_WRITE | APR_CREATE, -1, cntxt) != APR_SUCCESS) {
252             fprintf(stderr, "Could not open passwd file %s for writing.\n",
253                     argv[2]);
254             perror("ap_open");
255             exit(1);
256         }
257         printf("Adding password for %s in realm %s.\n", argv[4], argv[3]);
258         add_password(argv[4], argv[3], tfp);
259         ap_close(tfp);
260         exit(0);
261     }
262     else if (argc != 4)
263         usage();
264
265     tn = tmpnam(NULL);
266     if (ap_open(&tfp, tn, APR_WRITE | APR_CREATE, -1, cntxt)!= APR_SUCCESS) {
267         fprintf(stderr, "Could not open temp file.\n");
268         exit(1);
269     }
270
271     if (ap_open(&f, argv[1], APR_READ, -1, cntxt) != APR_SUCCESS) {
272         fprintf(stderr,
273                 "Could not open passwd file %s for reading.\n", argv[1]);
274         fprintf(stderr, "Use -c option to create new one.\n");
275         exit(1);
276     }
277     strcpy(user, argv[3]);
278     strcpy(realm, argv[2]);
279
280     found = 0;
281     while (!(getline(line, MAX_STRING_LEN, f))) {
282         if (found || (line[0] == '#') || (!line[0])) {
283             putline(tfp, line);
284             continue;
285         }
286         strcpy(l, line);
287         getword(w, l, ':');
288         getword(x, l, ':');
289         if (strcmp(user, w) || strcmp(realm, x)) {
290             putline(tfp, line);
291             continue;
292         }
293         else {
294             printf("Changing password for user %s in realm %s\n", user, realm);
295             add_password(user, realm, tfp);
296             found = 1;
297         }
298     }
299     if (!found) {
300         printf("Adding user %s in realm %s\n", user, realm);
301         add_password(user, realm, tfp);
302     }
303     ap_close(f);
304     ap_close(tfp);
305 #if defined(OS2) || defined(WIN32)
306     sprintf(command, "copy \"%s\" \"%s\"", tn, argv[1]);
307 #else
308     sprintf(command, "cp %s %s", tn, argv[1]);
309 #endif
310     system(command);
311     ap_remove_file(tn, cntxt);
312     return 0;
313 }