]> granicus.if.org Git - apache/blob - support/htdigest.c
Remove the old version of the error docs.
[apache] / support / htdigest.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2001 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.h"
68 #include "apr_file_io.h"
69 #include "apr_md5.h"
70 #include "apr_lib.h"            /* for apr_getpass() */
71 #include "apr_general.h"
72 #include "apr_signal.h"
73
74 #define APR_WANT_STDIO
75 #define APR_WANT_STRFUNC
76 #include "apr_want.h"
77
78 #if APR_HAVE_SYS_TYPES_H
79 #include <sys/types.h>
80 #endif
81 #if APR_HAVE_STDLIB_H
82 #include <stdlib.h>
83 #endif
84
85 #ifdef WIN32
86 #include <conio.h>
87 #endif
88
89
90 #if APR_CHARSET_EBCDIC
91 #define LF '\n'
92 #define CR '\r'
93 #else
94 #define LF 10
95 #define CR 13
96 #endif /* APR_CHARSET_EBCDIC */
97
98 #define MAX_STRING_LEN 256
99
100 char *tn;
101 apr_pool_t *cntxt;
102 #if APR_CHARSET_EBCDIC
103 apr_xlate_t *to_ascii;
104 #endif
105
106 static void getword(char *word, char *line, char stop)
107 {
108     int x = 0, y;
109
110     for (x = 0; ((line[x]) && (line[x] != stop)); x++)
111         word[x] = line[x];
112
113     word[x] = '\0';
114     if (line[x])
115         ++x;
116     y = 0;
117
118     while ((line[y++] = line[x++]));
119 }
120
121 static int getline(char *s, int n, apr_file_t *f)
122 {
123     register int i = 0;
124     char ch;
125     apr_status_t rv = APR_EINVAL;
126
127     while (i < (n - 1) && 
128            ((rv = apr_file_getc(&ch, f)) == APR_SUCCESS) && (ch != '\n')) {
129         s[i++] = ch;
130     }
131     if (ch == '\n')
132         s[i++] = ch;
133     s[i] = '\0';
134
135     if (rv != APR_SUCCESS) 
136         return 1;
137
138     return 0;
139 }
140
141 static void putline(apr_file_t *f, char *l)
142 {
143     int x;
144
145     for (x = 0; l[x]; x++)
146         apr_file_putc(l[x], f);
147 }
148
149
150 static void add_password(char *user, char *realm, apr_file_t *f)
151 {
152     char *pw;
153     apr_md5_ctx_t context;
154     unsigned char digest[16];
155     char string[MAX_STRING_LEN];
156     char pwin[MAX_STRING_LEN];
157     char pwv[MAX_STRING_LEN];
158     unsigned int i;
159     size_t len = sizeof(pwin);
160
161     if (apr_password_get("New password: ", pwin, &len) != APR_SUCCESS) {
162         fprintf(stderr, "password too long");
163         exit(5);
164     }
165     len = sizeof(pwin);
166     apr_password_get("Re-type new password: ", pwv, &len);
167     if (strcmp(pwin, pwv) != 0) {
168         fprintf(stderr, "They don't match, sorry.\n");
169         if (tn) {
170             apr_file_remove(tn, cntxt);
171         }
172         exit(1);
173     }
174     pw = pwin;
175     apr_file_printf(f, "%s:%s:", user, realm);
176
177     /* Do MD5 stuff */
178     sprintf(string, "%s:%s:%s", user, realm, pw);
179
180     apr_md5_init(&context);
181 #if APR_CHARSET_EBCDIC
182     apr_md5_set_xlate(&context, to_ascii);
183 #endif
184     apr_md5_update(&context, (unsigned char *) string, strlen(string));
185     apr_md5_final(digest, &context);
186
187     for (i = 0; i < 16; i++)
188         apr_file_printf(f, "%02x", digest[i]);
189
190     apr_file_printf(f, "\n");
191 }
192
193 static void usage(void)
194 {
195     fprintf(stderr, "Usage: htdigest [-c] passwordfile realm username\n");
196     fprintf(stderr, "The -c flag creates a new file.\n");
197     exit(1);
198 }
199
200 static void interrupted(void)
201 {
202     fprintf(stderr, "Interrupted.\n");
203     if (tn)
204         apr_file_remove(tn, cntxt);
205     exit(1);
206 }
207
208 static void terminate(void)
209 {
210     apr_terminate();
211 }
212
213 int main(int argc, char *argv[])
214 {
215     apr_file_t *tfp = NULL, *f;
216     apr_status_t rv;
217     char user[MAX_STRING_LEN];
218     char realm[MAX_STRING_LEN];
219     char line[MAX_STRING_LEN];
220     char l[MAX_STRING_LEN];
221     char w[MAX_STRING_LEN];
222     char x[MAX_STRING_LEN];
223     char command[MAX_STRING_LEN];
224     int found;
225    
226     rv = apr_initialize();
227     if (rv) {
228         fprintf(stderr, "apr_initialize(): %s (%d)\n",
229                 apr_strerror(rv, line, sizeof(line)), rv);
230         exit(1);
231     }
232     atexit(terminate); 
233     apr_pool_create(&cntxt, NULL);
234
235 #if APR_CHARSET_EBCDIC
236     rv = apr_xlate_open(&to_ascii, "ISO8859-1", APR_DEFAULT_CHARSET, cntxt);
237     if (rv) {
238         fprintf(stderr, "apr_xlate_open(): %s (%d)\n",
239                 apr_strerror(rv, line, sizeof(line)), rv);
240         exit(1);
241     }
242 #endif
243     
244     tn = NULL;
245     apr_signal(SIGINT, (void (*)(int)) interrupted);
246     if (argc == 5) {
247         if (strcmp(argv[1], "-c"))
248             usage();
249         rv = apr_file_open(&tfp, argv[2], APR_WRITE | APR_CREATE, -1, cntxt);
250         if (rv != APR_SUCCESS) {
251             char errmsg[120];
252
253             fprintf(stderr, "Could not open passwd file %s for writing: %s\n",
254                     argv[2],
255                     apr_strerror(rv, errmsg, sizeof errmsg));
256             exit(1);
257         }
258         printf("Adding password for %s in realm %s.\n", argv[4], argv[3]);
259         add_password(argv[4], argv[3], tfp);
260         apr_file_close(tfp);
261         exit(0);
262     }
263     else if (argc != 4)
264         usage();
265
266     tn = tmpnam(NULL);
267     if (apr_file_open(&tfp, tn, APR_WRITE | APR_CREATE, -1, cntxt)!= APR_SUCCESS) {
268         fprintf(stderr, "Could not open temp file.\n");
269         exit(1);
270     }
271
272     if (apr_file_open(&f, argv[1], APR_READ, -1, cntxt) != APR_SUCCESS) {
273         fprintf(stderr,
274                 "Could not open passwd file %s for reading.\n", argv[1]);
275         fprintf(stderr, "Use -c option to create new one.\n");
276         exit(1);
277     }
278     strcpy(user, argv[3]);
279     strcpy(realm, argv[2]);
280
281     found = 0;
282     while (!(getline(line, MAX_STRING_LEN, f))) {
283         if (found || (line[0] == '#') || (!line[0])) {
284             putline(tfp, line);
285             continue;
286         }
287         strcpy(l, line);
288         getword(w, l, ':');
289         getword(x, l, ':');
290         if (strcmp(user, w) || strcmp(realm, x)) {
291             putline(tfp, line);
292             continue;
293         }
294         else {
295             printf("Changing password for user %s in realm %s\n", user, realm);
296             add_password(user, realm, tfp);
297             found = 1;
298         }
299     }
300     if (!found) {
301         printf("Adding user %s in realm %s\n", user, realm);
302         add_password(user, realm, tfp);
303     }
304     apr_file_close(f);
305     apr_file_close(tfp);
306 #if defined(OS2) || defined(WIN32)
307     sprintf(command, "copy \"%s\" \"%s\"", tn, argv[1]);
308 #else
309     sprintf(command, "cp %s %s", tn, argv[1]);
310 #endif
311     system(command);
312     apr_file_remove(tn, cntxt);
313     return 0;
314 }