-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod_CsvtoGenieacs.c
321 lines (278 loc) · 8.59 KB
/
mod_CsvtoGenieacs.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
** mod_CsvtoGenieacs.c -- Apache sample CsvtoGenieacs module
** [Autogenerated via ``apxs -n CsvtoGenieacs -g'']
**
** To play with this sample module first compile it into a
** DSO file and install it into Apache's modules directory
** by running:
**
** $ apxs -c -i mod_CsvtoGenieacs.c
**
** Then activate it in Apache's apache2.conf file for instance
** for the URL /CsvtoGenieacs in as follows:
**
** # apache2.conf
** LoadModule CsvtoGenieacs_module modules/mod_CsvtoGenieacs.so
** <Location /CsvtoGenieacs>
** SetHandler CsvtoGenieacs
** </Location>
**
** Then after restarting Apache via
**
** $ apachectl restart
**
** you immediately can request the URL /CsvtoGenieacs and watch for the
** output of this module. This can be achieved for instance via:
**
** $ lynx -mime_header http://localhost/CsvtoGenieacs
**
** The output should be similar to the following one:
**
** HTTP/1.1 200 OK
** Date: Tue, 31 Mar 1998 14:42:22 GMT
** Server: Apache/1.3.4 (Unix)
** Connection: close
** Content-Type: text/html
**
** The sample page from mod_CsvtoGenieacs.c
*/
/*
Parts of code can be found at
https://httpd.apache.org/docs/2.4/developer/modguide.html
*/
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
#include <json-c/json.h>
#include <stdio.h>
#include <stdlib.h>
///////////////////////////////////////////////////////////////////////
typedef struct {
const char *path;
const char *token;
} struct_config;
static struct_config config;
const char *set_path(cmd_parms *cmd, void *cfg, const char *arg)
{
config.path = arg;
return NULL;
}
const char *set_token(cmd_parms *cmd, void *cfg, const char *arg)
{
config.token = arg;
return NULL;
}
static const command_rec get_config[] =
{
AP_INIT_TAKE1("Token", set_token, NULL, RSRC_CONF, "Set required token"),
AP_INIT_TAKE1("Path", set_path, NULL, RSRC_CONF, "The path to configuration"),
{ NULL }
};
/* The sample content handler */
static int CsvtoGenieacs_handler(request_rec *r)
{
if (strcmp(r->handler, "csvtogenieacs")) {
return DECLINED;
}
r->content_type = "text/html";
/* Check token */
const char *password;
password = apr_table_get(r->headers_in, "AUTHORIZATION");
if((password!=NULL && strcmp(password, config.token)!=0) || password==NULL)
{
ap_rputs("401 Unauthorized", r);
return OK;
}
/////////////////
const char *serial=NULL;
const char *deviceid=NULL;
const char *const fileext=".csv";
char buffer[100];
struct json_object *jobj;
/* how to read php://input in C */
int ret_code = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR);
if (ret_code == OK) {
if (ap_should_client_block(r)) {
int dataBytesRead = ap_get_client_block(r, buffer, 100);
/* example copy data*/
//char *json[dataBytesRead];
//memcpy(json, buffer, dataBytesRead);
jobj = json_tokener_parse(buffer);
char *jsonarray= (char *)json_object_get_array(jobj);
/* check what we get in body and take what we need */
json_object_object_foreach(jobj, key, val) {
if(strcmp(key,"serial")==0)
{
serial=(char *) json_object_get_string(val);
}
if(strcmp(key,"cpeid")==0)
{
deviceid=(char *) json_object_get_string(val);
}
}
}
}
///////////
/* if we didn't get serial in body let's try from GET*/
if(serial==NULL)
{
apr_table_t*GET;
ap_args_to_table(r, &GET);
serial = apr_table_get(GET, "serial");
}
/*checking strlen on null is bad idea */
if(!serial)
{
serial="";
}
size_t s1len = strlen (config.path);
size_t s2len = strlen (serial);
size_t s3len = strlen (fileext);
size_t sumlen=s1len+s2len+s3len+1;
char filename[sumlen];
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
/*
create filename to read, using strcat risks overflow and overwriting other varibles
*/
// strcat(filename, serial);
// strcat(filename, ".csv");
snprintf (filename, sumlen, "%s%s%s", config.path, serial, fileext);
/* open file based on serial/deviceid*/
fp = fopen(filename, "r");
if (fp == NULL)
{
if(deviceid)
{
size_t s2len = strlen (deviceid);
snprintf (filename, s1len+s2len+s3len+1, "%s%s%s", config.path, deviceid, fileext);
fp = fopen(filename, "r");
}
if (fp == NULL)
{
ap_rputs("No configuration found", r);
return OK;
// exit(EXIT_FAILURE);
}
}
struct json_object *obj1, *res, *sub_obj1;
obj1 = json_object_new_object();
const char *a[5];
int i=0;
a[0]="name";
a[1]="taskname";
a[2]="type";
a[3]="param";
a[4]="value";
res = json_object_new_array();
while ((read = getline(&line, &len, fp)) != -1) {
i=0;
char *task = strtok(line, ";");
while (task != NULL)
{
task[strcspn(task, "\n")] = 0;
json_object_object_add(obj1, a[i], json_object_new_string(task));
task = strtok(NULL, ";");
i+=1;
}
json_object_array_add(res, json_object_get(obj1));
}
fclose(fp);
/* return file content in form of json */
ap_rprintf(r, "%s", json_object_to_json_string(res));
return OK;
}
/* struct for geting POST data from apache documentation */
typedef struct {
const char *key;
const char *value;
} keyValuePair;
keyValuePair *readPost(request_rec *r) {
apr_array_header_t *pairs = NULL;
apr_off_t len;
apr_size_t size;
int res;
int i = 0;
char *buffer;
keyValuePair *kvp;
res = ap_parse_form_data(r, NULL, &pairs, -1, HUGE_STRING_LEN);
if (res != OK || !pairs) return NULL;
kvp = apr_pcalloc(r->pool, sizeof(keyValuePair) * (pairs->nelts + 1));
while (pairs && !apr_is_empty_array(pairs)) {
ap_form_pair_t *pair = (ap_form_pair_t *) apr_array_pop(pairs);
apr_brigade_length(pair->value, 1, &len);
size = (apr_size_t) len;
buffer = apr_palloc(r->pool, size + 1);
apr_brigade_flatten(pair->value, buffer, &size);
buffer[len] = 0;
kvp[i].key = pair->name;
kvp[i].value = buffer;
i++;
}
return kvp;
}
static int CsvtoGenieacs_file(request_rec *r)
{
if (strcmp(r->handler, "csvtogenieacsfile")) {
return DECLINED;
}
char *cpename=NULL;
char *body;
char *filename;
filename=config.path;
FILE * fp;
/*~~~~~~~~~~~~~~~~~~~~~~*/
keyValuePair *formData;
/*~~~~~~~~~~~~~~~~~~~~~~*/
r->content_type = "text/html";
/* read POST */
formData = readPost(r);
if (formData) {
int i;
for (i = 0; &formData[i]; i++) {
if (formData[i].key && formData[i].value) {
if(strcmp(formData[i].key,"serial")==0){
cpename=formData[i].value;
strcat(filename, formData[i].value);
}
if(strcmp(formData[i].key,"body")==0){
body=formData[i].value;
}
} else {
break;
}
}
}
if(cpename)
{
strcat(filename, ".csv");
fp = fopen (filename, "w");
fprintf(fp, "%s", body);
fclose(fp);
}
ap_rputs("<h2>Create new configuration file</h2>", r);
ap_rputs("<form action='' method='post' ><span>Serial/Device ID:<input type='text' id='serial' name='serial'></span><BR>", r);
ap_rputs("<span>Body<textarea name='body' rows='4' cols='90'></textarea></span><BR>", r);
ap_rputs("<input type='submit'></form>", r);
return OK;
}
static void CsvtoGenieacs_register_hooks(apr_pool_t *p)
{
config.path = "/tmp";
config.token = "abcd1";
ap_hook_handler(CsvtoGenieacs_handler, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_handler(CsvtoGenieacs_file, NULL, NULL, APR_HOOK_MIDDLE);
}
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA CsvtoGenieacs_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
get_config, /* table of config file commands */
CsvtoGenieacs_register_hooks /* register hooks */
};