-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmg_util.c
123 lines (104 loc) · 3.08 KB
/
mg_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "mongoose.h"
#include <stdarg.h>
#include <errno.h>
void mg_printf_inc(struct mg_connection *conn, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
conn->num_bytes_sent += mg_vprintf(conn, fmt, ap);
}
void initialize_file(struct file *f)
{
f->is_directory = 0;
f->modification_time = 0;
f->size = 0;
f->fp = NULL;
f->membuf = NULL;
}
struct file *
mg_fopena(struct mg_connection *c, const char *path, const char *mode) {
struct file *f = malloc(sizeof(*f));
initialize_file(f);
if (!mg_fopen(c,path,mode,f)) {
free(f);
f = NULL;
}
return f;
}
struct file *
open_auth_file_aux(struct mg_connection *conn, const char *path) {
struct file *filep = NULL;
char *e;
char name[PATH_MAX];
filep = malloc(sizeof(*filep));
initialize_file(filep);
if (mg_stat(conn, path, filep) && filep->is_directory)
{
mg_snprintf(conn, name, sizeof(name), "%s%c%s",
path, '/', PASSWORDS_FILE_NAME);
mg_fopen(conn, name, "r", filep);
}
else
{
e = strrchr(path, '/');
mg_snprintf(conn, name, sizeof(name), "%.*s%c%s",
(ptrdiff_t)e - (ptrdiff_t)path, path, '/', PASSWORDS_FILE_NAME);
mg_fopen(conn, name, "r", filep);
}
if (!is_file_opened(filep)) {
free(filep);//leak
filep = NULL;
}
return filep;
}
struct pw_ent *parse_password_line(char *line) {
struct pw_ent *pw;
char f_user[256], f_domain[256], ha1[256];
if (sscanf(line, "%[^:]:%[^:]:%s", f_user, f_domain, ha1) != 3) {
return NULL;
}
pw = malloc(sizeof(*pw));
pw->user = strndup(f_user, 256);
pw->domain = strndup(f_domain, 256);
pw->ha1 = malloc(sizeof(ha1));
memcpy(pw->ha1, ha1, 256);
return pw;
}
void print_dir_entries(struct dir_scan_data *data) {
int i;
// Sort and print directory entries
qsort(data->entries, (size_t) data->num_entries, sizeof(data->entries[0]),
compare_dir_entries);
for (i = 0; i < data->num_entries; i++) {
print_dir_entry(&data->entries[i]);
free(data->entries[i].file_name);
}
}
char *mg_protect_uri_fname(struct mg_connection *conn) {
char *fname = NULL;
struct vec uri_vec, filename_vec;
const char *list;
struct file *file = malloc(sizeof(*file));
initialize_file(file);
list = conn->ctx->config[PROTECT_URI];
while (list && (list = next_option(list, &uri_vec, &filename_vec)) != NULL) {
#warning "CSOLVE: Possible bug?"
/* ABAKST Possible bug here? memcmp vs strcmp? */
printf("(%s) =? (%s)\n", conn->request_info.uri, uri_vec.ptr);
if (uri_vec.ptr && !memcmp(conn->request_info.uri, uri_vec.ptr, uri_vec.len)) {
/* if (uri_vec.ptr && !strcmp(conn->request_info.uri, uri_vec.ptr)) { */
fname = malloc(PATH_MAX);
mg_snprintf(conn, fname, PATH_MAX, "%.*s",
(int) filename_vec.len, filename_vec.ptr);
if (!mg_fopen(conn, fname, "r", file)) {
cry(conn, "%s: cannot open %s: %s", __func__, fname, strerror(errno));
}
break;
}
}
return fname;
}
int REF((V != 0) => ? AUTHORIZED([CONN([conn])]))
mg_check_no_auth(struct mg_connection FINAL *conn, char *path)
{
return 1;
}