From 29716fa22e6ad149738cbc802756600eec334f57 Mon Sep 17 00:00:00 2001 From: valenok Date: Fri, 18 Feb 2011 16:40:58 +0000 Subject: [PATCH] Changed API: function mg_modify_passwords_file(). Instead of passing context, a domain name is passed, thus making this function completely mongoose-agnostic. --- main.c | 19 +------------------ mongoose.c | 7 ++----- mongoose.h | 6 ++++-- 3 files changed, 7 insertions(+), 25 deletions(-) diff --git a/main.c b/main.c index 4559b4ba..327367b9 100644 --- a/main.c +++ b/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); } diff --git a/mongoose.c b/mongoose.c index a3e7c01f..48512d3c 100644 --- a/mongoose.c +++ b/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; } diff --git a/mongoose.h b/mongoose.h index f31d894d..c212411a 100644 --- a/mongoose.h +++ b/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);