forked from open-policy-agent/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflag.c
85 lines (71 loc) · 2.35 KB
/
flag.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
#include <stdlib.h>
#include <string.h>
#include "flag.h"
#include "log.h"
#define FLAG_COUNT 6
// Valid flags that can be used as arguments to this PAM module inside /etc/pam.d/ files.
static const char FLAG_STR_OPA_URL[] = "url";
static const char FLAG_STR_OPA_SOCK[] = "sock";
static const char FLAG_STR_PULL_ENDPOINT[] = "pull_endpoint";
static const char FLAG_STR_DISPLAY_ENDPOINT[] = "display_endpoint";
static const char FLAG_STR_AUTHZ_ENDPOINT[] = "authz_endpoint";
static const char FLAG_STR_LOG_LEVEL[] = "log_level";
// Default values of exposed variables.
char flag_opa_url[MAX_FLAG_SIZE] = "";
char flag_opa_sock[MAX_FLAG_SIZE] = "";
char flag_pull_endpoint[MAX_FLAG_SIZE] = "";
char flag_display_endpoint[MAX_FLAG_SIZE] = "";
char flag_authz_endpoint[MAX_FLAG_SIZE] = "";
char flag_log_level[MAX_FLAG_SIZE] = "";
static const struct FlagStrToVar {
const char *flag_str;
const char *var;
} FLAG_STR_TO_VAR[FLAG_COUNT] = {
{FLAG_STR_OPA_URL, flag_opa_url},
{FLAG_STR_OPA_SOCK, flag_opa_sock},
{FLAG_STR_PULL_ENDPOINT, flag_pull_endpoint},
{FLAG_STR_DISPLAY_ENDPOINT, flag_display_endpoint},
{FLAG_STR_AUTHZ_ENDPOINT, flag_authz_endpoint},
{FLAG_STR_LOG_LEVEL, flag_log_level},
};
static const char *var_for_flag_str(const char* flag_str) {
int i;
for (i = 0; i < FLAG_COUNT; i++) {
if (strcmp(FLAG_STR_TO_VAR[i].flag_str, flag_str) == 0) {
return FLAG_STR_TO_VAR[i].var;
}
}
return NULL;
}
void initialize_flags(int argc, const char **argv) {
int i;
for (i = 0; i < argc; i++) {
pam_log(LOG_PRIORITY_INFO, "Parsing arg: %s", argv[i]);
char *arg, *tofree;
arg = tofree = strdup(argv[i]);
if (arg == NULL) {
pam_log(LOG_PRIORITY_ERROR, "Error reading arg: insufficient memory.");
continue;
}
char *flag = strsep(&arg, "=");
char *value = strsep(&arg, "=");
if (arg != NULL) {
pam_log(LOG_PRIORITY_ERROR, "Got arg with multiple '=': %s", argv[i]);
free(tofree);
continue;
}
if (flag == NULL || value == NULL) {
pam_log(LOG_PRIORITY_ERROR, "Could not parse arg: %s", argv[i]);
free(tofree);
continue;
}
const char *flag_var = var_for_flag_str(flag);
if (flag_var == NULL) {
pam_log(LOG_PRIORITY_ERROR, "Got unknown flag: %s", flag);
free(tofree);
continue;
}
strcpy((char *)flag_var, value);
free(tofree);
}
}