]> granicus.if.org Git - apache/blobdiff - support/htdigest.c
Some LDAP servers (wrongly) return LDAP_CONSTRAINT_VIOLATION if a user is
[apache] / support / htdigest.c
index f90a672c592c2ecdec786e82430cc0816944f0e6..6a0e26f123f95feb5edc7addb41999e1ec69b494 100644 (file)
@@ -1,9 +1,9 @@
-/* Copyright 1999-2005 The Apache Software Foundation or its licensors, as
- * applicable.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
@@ -96,7 +96,7 @@ static int get_line(char *s, int n, apr_file_t *f)
     char ch;
     apr_status_t rv = APR_EINVAL;
 
-    while (i < (n - 1) && 
+    while (i < (n - 1) &&
            ((rv = apr_file_getc(&ch, f)) == APR_SUCCESS) && (ch != '\n')) {
         s[i++] = ch;
     }
@@ -104,7 +104,7 @@ static int get_line(char *s, int n, apr_file_t *f)
         s[i++] = ch;
     s[i] = '\0';
 
-    if (rv != APR_SUCCESS) 
+    if (rv != APR_SUCCESS)
         return 1;
 
     return 0;
@@ -124,7 +124,7 @@ static void add_password(const char *user, const char *realm, apr_file_t *f)
     char *pw;
     apr_md5_ctx_t context;
     unsigned char digest[16];
-    char string[MAX_STRING_LEN];
+    char string[3 * MAX_STRING_LEN]; /* this includes room for 2 * ':' + '\0' */
     char pwin[MAX_STRING_LEN];
     char pwv[MAX_STRING_LEN];
     unsigned int i;
@@ -144,7 +144,7 @@ static void add_password(const char *user, const char *realm, apr_file_t *f)
     apr_file_printf(f, "%s:%s:", user, realm);
 
     /* Do MD5 stuff */
-    sprintf(string, "%s:%s:%s", user, realm, pw);
+    apr_snprintf(string, sizeof(string), "%s:%s:%s", user, realm, pw);
 
     apr_md5_init(&context);
 #if APR_CHARSET_EBCDIC
@@ -188,14 +188,14 @@ int main(int argc, const char * const argv[])
     char *dirname;
     char user[MAX_STRING_LEN];
     char realm[MAX_STRING_LEN];
-    char line[MAX_STRING_LEN];
-    char l[MAX_STRING_LEN];
+    char line[3 * MAX_STRING_LEN];
+    char l[3 * MAX_STRING_LEN];
     char w[MAX_STRING_LEN];
     char x[MAX_STRING_LEN];
     int found;
-   
+
     apr_app_initialize(&argc, &argv, NULL);
-    atexit(terminate); 
+    atexit(terminate);
     apr_pool_create(&cntxt, NULL);
     apr_file_open_stderr(&errfile, cntxt);
 
@@ -207,7 +207,7 @@ int main(int argc, const char * const argv[])
         exit(1);
     }
 #endif
-    
+
     apr_signal(SIGINT, (void (*)(int)) interrupted);
     if (argc == 5) {
         if (strcmp(argv[1], "-c"))
@@ -222,9 +222,11 @@ int main(int argc, const char * const argv[])
                     apr_strerror(rv, errmsg, sizeof errmsg));
             exit(1);
         }
-        apr_file_printf(errfile, "Adding password for %s in realm %s.\n", 
-                    argv[4], argv[3]);
-        add_password(argv[4], argv[3], f);
+        apr_cpystrn(user, argv[4], sizeof(user));
+        apr_cpystrn(realm, argv[3], sizeof(realm));
+        apr_file_printf(errfile, "Adding password for %s in realm %s.\n",
+                    user, realm);
+        add_password(user, realm, f);
         apr_file_close(f);
         exit(0);
     }
@@ -253,7 +255,7 @@ int main(int argc, const char * const argv[])
     apr_cpystrn(realm, argv[2], sizeof(realm));
 
     found = 0;
-    while (!(get_line(line, MAX_STRING_LEN, f))) {
+    while (!(get_line(line, sizeof(line), f))) {
         if (found || (line[0] == '#') || (!line[0])) {
             putline(tfp, line);
             continue;
@@ -266,7 +268,7 @@ int main(int argc, const char * const argv[])
             continue;
         }
         else {
-            apr_file_printf(errfile, "Changing password for user %s in realm %s\n", 
+            apr_file_printf(errfile, "Changing password for user %s in realm %s\n",
                     user, realm);
             add_password(user, realm, tfp);
             found = 1;
@@ -282,7 +284,7 @@ int main(int argc, const char * const argv[])
      */
     if (apr_file_copy(dirname, argv[1], APR_FILE_SOURCE_PERMS, cntxt) !=
                 APR_SUCCESS) {
-        apr_file_printf(errfile, "%s: unable to update file %s\n", 
+        apr_file_printf(errfile, "%s: unable to update file %s\n",
                         argv[0], argv[1]);
     }
     apr_file_close(tfp);