]> granicus.if.org Git - apache/blob - support/htdigest.c
Merge the htdigest.c file from the apr/test directory to the support
[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 #ifdef CHARSET_EBCDIC
89 #define LF '\n'
90 #define CR '\r'
91 #else
92 #define LF 10
93 #define CR 13
94 #endif /* CHARSET_EBCDIC */
95
96 #define MAX_STRING_LEN 256
97
98 char *tn;
99 ap_pool_t *cntxt;
100
101 static void getword(char *word, char *line, char stop)
102 {
103     int x = 0, y;
104
105     for (x = 0; ((line[x]) && (line[x] != stop)); x++)
106         word[x] = line[x];
107
108     word[x] = '\0';
109     if (line[x])
110         ++x;
111     y = 0;
112
113     while ((line[y++] = line[x++]));
114 }
115
116 static int getline(char *s, int n, ap_file_t *f)
117 {
118     register int i = 0;
119     char ch;
120
121     while (1) {
122         ap_getc(&ch, f);
123             s[i] = ch;
124
125         if (s[i] == CR)
126             ap_getc(&ch, f);
127             s[i] = ch;
128
129         if ((s[i] == 0x4) || (s[i] == LF) || (i == (n - 1))) {
130             s[i] = '\0';
131             if (ap_eof(f) == APR_EOF) {
132                 return 1;
133             }
134             return 0;
135         }
136         ++i;
137     }
138 }
139
140 static void putline(ap_file_t *f, char *l)
141 {
142     int x;
143
144     for (x = 0; l[x]; x++)
145         ap_putc(l[x], f);
146     ap_putc('\n', f);
147 }
148
149
150 static void add_password(char *user, char *realm, ap_file_t *f)
151 {
152     char *pw;
153     ap_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 (ap_getpass("New password: ", pwin, &len) != APR_SUCCESS) {
162         fprintf(stderr, "password too long");
163         exit(5);
164     }
165     len = sizeof(pwin);
166     ap_getpass("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             ap_remove_file(tn, cntxt);
171         }
172         exit(1);
173     }
174     pw = pwin;
175     ap_fprintf(f, "%s:%s:", user, realm);
176
177     /* Do MD5 stuff */
178     sprintf(string, "%s:%s:%s", user, realm, pw);
179
180     ap_MD5Init(&context);
181     ap_MD5Update(&context, (unsigned char *) string, strlen(string));
182     ap_MD5Final(digest, &context);
183
184     for (i = 0; i < 16; i++)
185         ap_fprintf(f, "%02x", digest[i]);
186
187     ap_fprintf(f, "\n");
188 }
189
190 static void usage(void)
191 {
192     fprintf(stderr, "Usage: htdigest [-c] passwordfile realm username\n");
193     fprintf(stderr, "The -c flag creates a new file.\n");
194     exit(1);
195 }
196
197 static void interrupted(void)
198 {
199     fprintf(stderr, "Interrupted.\n");
200     if (tn)
201         ap_remove_file(tn, cntxt);
202     exit(1);
203 }
204
205 int main(int argc, char *argv[])
206 {
207     ap_file_t *tfp, *f;
208     char user[MAX_STRING_LEN];
209     char realm[MAX_STRING_LEN];
210     char line[MAX_STRING_LEN];
211     char l[MAX_STRING_LEN];
212     char w[MAX_STRING_LEN];
213     char x[MAX_STRING_LEN];
214     char command[MAX_STRING_LEN];
215     int found;
216     
217     ap_create_pool(&cntxt, NULL);
218
219     tn = NULL;
220     ap_signal(SIGINT, (void (*)(int)) interrupted);
221     if (argc == 5) {
222         if (strcmp(argv[1], "-c"))
223             usage();
224         if (ap_open(&tfp, argv[2], APR_WRITE | APR_CREATE, -1, cntxt) != APR_SUCCESS) {
225             fprintf(stderr, "Could not open passwd file %s for writing.\n",
226                     argv[2]);
227             perror("ap_open");
228             exit(1);
229         }
230         printf("Adding password for %s in realm %s.\n", argv[4], argv[3]);
231         add_password(argv[4], argv[3], tfp);
232         ap_close(tfp);
233         exit(0);
234     }
235     else if (argc != 4)
236         usage();
237
238     tn = tmpnam(NULL);
239     if (ap_open(&tfp, tn, APR_WRITE | APR_CREATE, -1, cntxt)!= APR_SUCCESS) {
240         fprintf(stderr, "Could not open temp file.\n");
241         exit(1);
242     }
243
244     if (ap_open(&f, argv[1], APR_READ, -1, cntxt) != APR_SUCCESS) {
245         fprintf(stderr,
246                 "Could not open passwd file %s for reading.\n", argv[1]);
247         fprintf(stderr, "Use -c option to create new one.\n");
248         exit(1);
249     }
250     strcpy(user, argv[3]);
251     strcpy(realm, argv[2]);
252
253     found = 0;
254     while (!(getline(line, MAX_STRING_LEN, f))) {
255         if (found || (line[0] == '#') || (!line[0])) {
256             putline(tfp, line);
257             continue;
258         }
259         strcpy(l, line);
260         getword(w, l, ':');
261         getword(x, l, ':');
262         if (strcmp(user, w) || strcmp(realm, x)) {
263             putline(tfp, line);
264             continue;
265         }
266         else {
267             printf("Changing password for user %s in realm %s\n", user, realm);
268             add_password(user, realm, tfp);
269             found = 1;
270         }
271     }
272     if (!found) {
273         printf("Adding user %s in realm %s\n", user, realm);
274         add_password(user, realm, tfp);
275     }
276     ap_close(f);
277     ap_close(tfp);
278 #if defined(OS2) || defined(WIN32)
279     sprintf(command, "copy \"%s\" \"%s\"", tn, argv[1]);
280 #else
281     sprintf(command, "cp %s %s", tn, argv[1]);
282 #endif
283     system(command);
284     ap_remove_file(tn, cntxt);
285     return 0;
286 }