Parcourir la source

Changed API: function mg_modify_passwords_file(). Instead of passing context, a domain name is passed, thus making this function completely mongoose-agnostic.

valenok il y a 14 ans
Parent
commit
29716fa22e
3 fichiers modifiés avec 7 ajouts et 25 suppressions
  1. 1 18
      main.c
  2. 2 5
      mongoose.c
  3. 4 2
      mongoose.h

+ 1 - 18
main.c

@@ -85,23 +85,6 @@ static void die(const char *fmt, ...) {
   exit(EXIT_FAILURE);
 }
 
-/*
- * Edit the passwords file.
- */
-static int mg_edit_passwords(const char *fname, const char *domain,
-                             const char *user, const char *pass) {
-  struct mg_context *ctx;
-  const char *options[] = {"authentication_domain", NULL, NULL};
-  int success;
-
-  options[1] = domain;
-  ctx = mg_start(NULL, NULL, options);
-  success = mg_modify_passwords_file(ctx, fname, user, pass);
-  mg_stop(ctx);
-
-  return success;
-}
-
 static void show_usage_and_exit(void) {
   const char **names;
   int i;
@@ -240,7 +223,7 @@ static void start_mongoose(int argc, char *argv[]) {
     if (argc != 6) {
       show_usage_and_exit();
     }
-    exit(mg_edit_passwords(argv[2], argv[3], argv[4], argv[5]) ?
+    exit(mg_modify_passwords_file(argv[2], argv[3], argv[4], argv[5]) ?
          EXIT_SUCCESS : EXIT_FAILURE);
   }
 

+ 2 - 5
mongoose.c

@@ -2226,16 +2226,14 @@ static int is_authorized_for_put(struct mg_connection *conn) {
   return ret;
 }
 
-int mg_modify_passwords_file(struct mg_context *ctx, const char *fname,
+int mg_modify_passwords_file(const char *fname, const char *domain,
                              const char *user, const char *pass) {
   int found;
   char line[512], u[512], d[512], ha1[33], tmp[PATH_MAX];
-  const char *domain;
   FILE *fp, *fp2;
 
   found = 0;
   fp = fp2 = NULL;
-  domain = ctx->config[AUTHENTICATION_DOMAIN];
 
   // Regard empty password as no password - remove user record.
   if (pass[0] == '\0') {
@@ -2251,10 +2249,9 @@ int mg_modify_passwords_file(struct mg_context *ctx, const char *fname,
 
   // Open the given file and temporary file
   if ((fp = mg_fopen(fname, "r")) == NULL) {
-    cry(fc(ctx), "Cannot open %s: %s", fname, strerror(errno));
     return 0;
   } else if ((fp2 = mg_fopen(tmp, "w+")) == NULL) {
-    cry(fc(ctx), "Cannot open %s: %s", tmp, strerror(errno));
+    fclose(fp);
     return 0;
   }
 

+ 4 - 2
mongoose.h

@@ -138,8 +138,10 @@ const char **mg_get_valid_option_names(void);
 //
 // Return:
 //   1 on success, 0 on error.
-int mg_modify_passwords_file(struct mg_context *ctx, 
-    const char *passwords_file_name, const char *user, const char *password);
+int mg_modify_passwords_file(const char *passwords_file_name,
+                             const char *domain,
+                             const char *user,
+                             const char *password);
 
 // Send data to the client.
 int mg_write(struct mg_connection *, const void *buf, size_t len);