Commit 29716fa2 authored by valenok's avatar valenok

Changed API: function mg_modify_passwords_file(). Instead of passing context,...

Changed API: function mg_modify_passwords_file(). Instead of passing context, a domain name is passed, thus making this function completely mongoose-agnostic.
parent 27ccf416
...@@ -85,23 +85,6 @@ static void die(const char *fmt, ...) { ...@@ -85,23 +85,6 @@ static void die(const char *fmt, ...) {
exit(EXIT_FAILURE); 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) { static void show_usage_and_exit(void) {
const char **names; const char **names;
int i; int i;
...@@ -240,7 +223,7 @@ static void start_mongoose(int argc, char *argv[]) { ...@@ -240,7 +223,7 @@ static void start_mongoose(int argc, char *argv[]) {
if (argc != 6) { if (argc != 6) {
show_usage_and_exit(); 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); EXIT_SUCCESS : EXIT_FAILURE);
} }
......
...@@ -2226,16 +2226,14 @@ static int is_authorized_for_put(struct mg_connection *conn) { ...@@ -2226,16 +2226,14 @@ static int is_authorized_for_put(struct mg_connection *conn) {
return ret; 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) { const char *user, const char *pass) {
int found; int found;
char line[512], u[512], d[512], ha1[33], tmp[PATH_MAX]; char line[512], u[512], d[512], ha1[33], tmp[PATH_MAX];
const char *domain;
FILE *fp, *fp2; FILE *fp, *fp2;
found = 0; found = 0;
fp = fp2 = NULL; fp = fp2 = NULL;
domain = ctx->config[AUTHENTICATION_DOMAIN];
// Regard empty password as no password - remove user record. // Regard empty password as no password - remove user record.
if (pass[0] == '\0') { if (pass[0] == '\0') {
...@@ -2251,10 +2249,9 @@ int mg_modify_passwords_file(struct mg_context *ctx, const char *fname, ...@@ -2251,10 +2249,9 @@ int mg_modify_passwords_file(struct mg_context *ctx, const char *fname,
// Open the given file and temporary file // Open the given file and temporary file
if ((fp = mg_fopen(fname, "r")) == NULL) { if ((fp = mg_fopen(fname, "r")) == NULL) {
cry(fc(ctx), "Cannot open %s: %s", fname, strerror(errno));
return 0; return 0;
} else if ((fp2 = mg_fopen(tmp, "w+")) == NULL) { } else if ((fp2 = mg_fopen(tmp, "w+")) == NULL) {
cry(fc(ctx), "Cannot open %s: %s", tmp, strerror(errno)); fclose(fp);
return 0; return 0;
} }
......
...@@ -138,8 +138,10 @@ const char **mg_get_valid_option_names(void); ...@@ -138,8 +138,10 @@ const char **mg_get_valid_option_names(void);
// //
// Return: // Return:
// 1 on success, 0 on error. // 1 on success, 0 on error.
int mg_modify_passwords_file(struct mg_context *ctx, int mg_modify_passwords_file(const char *passwords_file_name,
const char *passwords_file_name, const char *user, const char *password); const char *domain,
const char *user,
const char *password);
// Send data to the client. // Send data to the client.
int mg_write(struct mg_connection *, const void *buf, size_t len); int mg_write(struct mg_connection *, const void *buf, size_t len);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment