-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.c
117 lines (98 loc) · 2.63 KB
/
utils.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
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "utils.h"
char* strtrimcpy(char *src, char *dest)
{
size_t size;
char *end;
char * s = src;
int strip_len;
size = strlen(src);
if (size > 0)
{
int i = 0;
end = s + size - 1;
while (end >= s && isspace(*end))
end--;
while (*s && isspace(*s))
s++;
strip_len = (int)(end - s);
if(strip_len > 0)
{
strncpy(dest, s, strip_len+1);
dest[strip_len+1] = '\0';
return dest;
}
}
return NULL;
}
void read_configuration(struct crowd_config *cfg)
{
char buffer[1024];
FILE* fp = fopen(CFG_FILE, "r");
memset(cfg, '\0', sizeof(struct crowd_config));
strcpy(cfg->client_cert_type, "PEM");
while(fgets(buffer, 1024, fp) != NULL)
{
char *tokptr;
char key[30];
char *value;
if(strtrimcpy(strtok_r(buffer, "=", &tokptr), key) != NULL)
{
value = strtok_r(NULL, "=", &tokptr);
if(strcmp(key, CFG_CROWD_BASE) == 0)
{
int end = -1;
strtrimcpy(value, cfg->base_url);
end = strlen(cfg->base_url)-1;
if(cfg->base_url[end] == '/')
{
cfg->base_url[end] = '\0';
}
}
else if(strcmp(key, CFG_CROWD_APP) == 0)
{
strtrimcpy(value, cfg->application);
}
else if(strcmp(key, CFG_CROWD_PWD) == 0)
{
strtrimcpy(value, cfg->password);
}
else if(strcmp(key, CFG_CLIENT_CERT) == 0)
{
strtrimcpy(value, cfg->client_cert);
}
else if(strcmp(key, CFG_CLIENT_KEY) == 0)
{
strtrimcpy(value, cfg->client_cert_key);
}
else if(strcmp(key, CFG_CLIENT_PWD) == 0)
{
strtrimcpy(value, cfg->client_cert_pwd);
}
else if(strcmp(key, CFG_CLIENT_TYPE) == 0)
{
strtrimcpy(value, cfg->client_cert_type);
}
}
}
}
json_t* get_auth_body(const char* pwd)
{
json_t* body = json_object();
if(body != NULL)
{
json_t* j_pwd = json_string(pwd);
if(j_pwd != NULL)
{
int errno = json_object_set_new(body, "value", j_pwd);
if(errno != 0)
{
json_decref(body);
return NULL;
}
}
}
return body;
}