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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/*
* Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the
* Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*/
#include "json.h"
#include "cfg.h"
#include "ogAdmServer.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <syslog.h>
static int parse_json_rest(struct og_server_cfg *cfg, json_t *element)
{
const char *key;
json_t *value;
json_object_foreach(element, key, value) {
if (!strcmp(key, "ip")) {
if (og_json_parse_string(value, &cfg->rest.ip) < 0)
return -1;
} else if (!strcmp(key, "port")) {
if (og_json_parse_string(value, &cfg->rest.port) < 0)
return -1;
} else if (!strcmp(key, "api_token")) {
if (og_json_parse_string(value, &cfg->rest.api_token) < 0)
return -1;
} else {
syslog(LOG_ERR, "unknown key `%s' in rest\n", key);
return -1;
}
}
return 0;
}
static int parse_json_db(struct og_server_cfg *cfg, json_t *element)
{
const char *key;
json_t *value;
json_object_foreach(element, key, value) {
if (!strcmp(key, "ip")) {
if (og_json_parse_string(value, &cfg->db.ip) < 0)
return -1;
} else if (!strcmp(key, "user")) {
if (og_json_parse_string(value, &cfg->db.user) < 0)
return -1;
} else if (!strcmp(key, "pass")) {
if (og_json_parse_string(value, &cfg->db.pass) < 0)
return -1;
} else if (!strcmp(key, "name")) {
if (og_json_parse_string(value, &cfg->db.name) < 0)
return -1;
} else if (!strcmp(key, "port")) {
if (og_json_parse_string(value, &cfg->db.port) < 0)
return -1;
} else {
syslog(LOG_ERR, "unknown key `%s' in db\n", key);
return -1;
}
}
return 0;
}
static int parse_json_samba(struct og_server_cfg *cfg, json_t *element)
{
const char *key;
json_t *value;
json_object_foreach(element, key, value) {
if (!strcmp(key, "user")) {
if (og_json_parse_string(value, &cfg->smb.user) < 0)
return -1;
} else if (!strcmp(key, "pass")) {
if (og_json_parse_string(value, &cfg->smb.pass) < 0)
return -1;
} else {
syslog(LOG_ERR, "unknown key `%s' in samba\n", key);
return -1;
}
}
return 0;
}
static int parse_json_wol(struct og_server_cfg *cfg, json_t *element)
{
const char *key;
json_t *value;
json_object_foreach(element, key, value) {
if (!strcmp(key, "interface")) {
if (og_json_parse_string(value, &cfg->wol.interface) < 0)
return -1;
} else {
syslog(LOG_ERR, "unknown key `%s' in wol\n", key);
return -1;
}
}
return 0;
}
static int parse_json_repo(struct og_server_cfg *cfg, json_t *element)
{
const char *key;
json_t *value;
json_object_foreach(element, key, value) {
if (!strcmp(key, "directory")) {
if (og_json_parse_string(value, &cfg->repo.dir) < 0)
return -1;
} else {
syslog(LOG_ERR, "unknown key `%s' in repo\n", key);
return -1;
}
}
return 0;
}
#define OG_SERVER_CFG_REST (1 << 0)
#define OG_SERVER_CFG_DB (1 << 1)
#define OG_SERVER_CFG_WOL (1 << 2)
#define OG_SERVER_CFG_REPO (1 << 3)
#define OG_SERVER_CFG_SAMBA (1 << 4)
int parse_json_config(const char *filename, struct og_server_cfg *cfg)
{
json_t *root, *value;
uint32_t flags = 0;
json_error_t err;
const char *key;
char buf[4096];
int fd, ret;
fd = open(filename, O_RDONLY);
if (fd < 0) {
syslog(LOG_ERR, "Cannot open %s", filename);
return -1;
}
ret = read(fd, buf, sizeof(buf));
if (ret < 0 || ret == sizeof(buf)) {
syslog(LOG_ERR, "Cannot read from %s", filename);
return -1;
}
root = json_loads(buf, 0, &err);
if (!root) {
syslog(LOG_ERR, "Cannot parse malformed json file");
return -1;
}
json_object_foreach(root, key, value) {
if (!strcmp(key, "rest")) {
if (parse_json_rest(cfg, value) < 0) {
ret = -1;
break;
}
flags |= OG_SERVER_CFG_REST;
} else if (!strcmp(key, "wol")) {
if (parse_json_wol(cfg, value) < 0) {
ret = -1;
break;
}
flags |= OG_SERVER_CFG_WOL;
} else if (!strcmp(key, "database")) {
if (parse_json_db(cfg, value) < 0) {
ret = -1;
break;
}
flags |= OG_SERVER_CFG_DB;
} else if (!strcmp(key, "samba")) {
if (parse_json_samba(cfg, value) < 0) {
ret = -1;
break;
}
flags |= OG_SERVER_CFG_SAMBA;
} else if (!strcmp(key, "repository")) {
if (parse_json_repo(cfg, value) < 0)
return -1;
flags |= OG_SERVER_CFG_REPO;
} else {
syslog(LOG_ERR, "unknown key `%s' in %s\n",
key, filename);
ret = -1;
}
}
if (ret < 0)
json_decref(root);
if ((flags & OG_SERVER_CFG_REST) &&
(flags & OG_SERVER_CFG_DB) &&
(flags & OG_SERVER_CFG_WOL)) {
ret = 0;
} else {
syslog(LOG_ERR, "Missing attributes in json file");
ret = -1;
}
if (!(flags & OG_SERVER_CFG_SAMBA))
syslog(LOG_WARNING, "Missing samba configuration in ogserver.json file");
if (ret < 0)
json_decref(root);
else
cfg->json = root;
return ret;
}
|