htdigest.c revision 686cd089cc7f4d7be5752f026490ab3016763e95
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder/* Copyright 1999-2004 The Apache Software Foundation
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder * Licensed under the Apache License, Version 2.0 (the "License");
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder * you may not use this file except in compliance with the License.
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu * You may obtain a copy of the License at
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder * http://www.apache.org/licenses/LICENSE-2.0
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder * Unless required by applicable law or agreed to in writing, software
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder * distributed under the License is distributed on an "AS IS" BASIS,
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder * See the License for the specific language governing permissions and
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder * limitations under the License.
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder/******************************************************************************
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder ******************************************************************************
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder * NOTE! This program is not safe as a setuid executable! Do not make it
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder ******************************************************************************
a3631d87c556bfbce810f6207abeee30ca6a6590Christian Maeder *****************************************************************************/
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder * htdigest.c: simple program for manipulating digest passwd file for Apache
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder * by Alexei Kosut, based on htpasswd.c, by Rob McCool
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#include "apr_lib.h" /* for apr_getpass() */
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder#include "apr_strings.h" /* for apr_pstrdup() */
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#endif /* APR_CHARSET_EBCDIC */
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maederstatic void cleanup_tempfile_and_exit(int rc)
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maederstatic void getword(char *word, char *line, char stop)
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder int x = 0, y;
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder for (x = 0; ((line[x]) && (line[x] != stop)); x++)
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maederstatic int get_line(char *s, int n, apr_file_t *f)
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder register int i = 0;
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder while (i < (n - 1) &&
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder ((rv = apr_file_getc(&ch, f)) == APR_SUCCESS) && (ch != '\n')) {
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder for (x = 0; l[x]; x++)
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maederstatic void add_password(const char *user, const char *realm, apr_file_t *f)
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder unsigned int i;
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder if (apr_password_get("New password: ", pwin, &len) != APR_SUCCESS) {
adda0e6252b14215228e4071b347c49b808894f8Christian Maeder apr_file_printf(errfile, "password too long");
adda0e6252b14215228e4071b347c49b808894f8Christian Maeder apr_password_get("Re-type new password: ", pwv, &len);
adda0e6252b14215228e4071b347c49b808894f8Christian Maeder apr_file_printf(errfile, "They don't match, sorry.\n");
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder /* Do MD5 stuff */
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder sprintf(string, "%s:%s:%s", user, realm, pw);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_md5_update(&context, (unsigned char *) string, strlen(string));
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder for (i = 0; i < 16; i++)
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maederstatic void usage(void)
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_printf(errfile, "Usage: htdigest [-c] passwordfile realm username\n");
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_file_printf(errfile, "The -c flag creates a new file.\n");
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maederstatic void interrupted(void)
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_printf(errfile, "Interrupted.\n");
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maederstatic void terminate(void)
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maederint main(int argc, const char * const argv[])
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder rv = apr_xlate_open(&to_ascii, "ISO8859-1", APR_DEFAULT_CHARSET, cntxt);
31d3d7a2f915455d04aadfd36560d1032942b771Christian Maeder apr_file_printf(errfile, "apr_xlate_open(): %s (%d)\n",
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_signal(SIGINT, (void (*)(int)) interrupted);
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder rv = apr_file_open(&f, argv[2], APR_WRITE | APR_CREATE, -1, cntxt);
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_printf(errfile, "Could not open passwd file %s for writing: %s\n",
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_file_printf(errfile, "Adding password for %s in realm %s.\n",
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder if (apr_temp_dir_get((const char**)&dirname, cntxt) != APR_SUCCESS) {
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_file_printf(errfile, "%s: could not determine temp dir\n",
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder dirname = apr_psprintf(cntxt, "%s/%s", dirname, tn);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder if (apr_file_mktemp(&tfp, dirname, 0, cntxt) != APR_SUCCESS) {
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_file_printf(errfile, "Could not open temp file %s.\n", dirname);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder if (apr_file_open(&f, argv[1], APR_READ, -1, cntxt) != APR_SUCCESS) {
cf39e23ee25b89496d451fcafc70ece1cf760891Christian Maeder "Could not open passwd file %s for reading.\n", argv[1]);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_file_printf(errfile, "Use -c option to create new one.\n");
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_cpystrn(realm, argv[2], sizeof(realm));
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder while (!(get_line(line, MAX_STRING_LEN, f))) {
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder if (found || (line[0] == '#') || (!line[0])) {
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_printf(errfile, "Changing password for user %s in realm %s\n",
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder apr_file_printf(errfile, "Adding user %s in realm %s\n", user, realm);
7bffb8b0e6cae4bb7ecb59b99327add6106c06b9Christian Maeder /* The temporary file has all the data, just copy it to the new location.
1fac054baed931dc57f0e41dd0ade39adac28c49Christian Maeder if (apr_file_copy(dirname, argv[1], APR_FILE_SOURCE_PERMS, cntxt) !=
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_printf(errfile, "%s: unable to update file %s\n",