6 static const char *user
;
8 static uint num_cgroups
, cgroup_idx
;
9 static hash_table
*auth_hosts
, *auth_services
;
10 static int is_host_root
= 1, is_service_root
= 1;
12 int auth_host_ok(const char *host
)
17 return !!hash_find(auth_hosts
, host
);
20 int auth_service_ok(const char *host
, const char *svc
)
22 if (is_service_root
|| is_host_root
)
25 if ((hash_find2(auth_services
, host
, svc
)) || auth_host_ok(host
))
31 static int list_has_entry(const char *list
, const char *ent
)
38 for (p
= list
- 1; p
; p
= strchr(++p
, ',')) {
40 if (!strncmp(p
, ent
, len
) && (p
[len
] == ',' || !p
[len
])) {
48 static int list_has_any_entry(const char *list
, char **ents
, int nents
)
52 for (i
= 0; i
< nents
; i
++) {
53 if (list_has_entry(list
, ents
[i
])) {
60 static int grok_host(struct cfg_comp
*obj
)
63 char *host_name
= NULL
;
64 char *contacts
= NULL
;
67 for (i
= 0; i
< obj
->vars
; i
++) {
68 struct cfg_var
*v
= obj
->vlist
[i
];
69 if (!strcmp(v
->key
, "host_name")) {
71 } else if (!strcmp(v
->key
, "contacts")) {
73 } else if (!strcmp(v
->key
, "contact_groups")) {
78 if ((contacts
&& list_has_entry(contacts
, user
)) ||
79 (groups
&& list_has_any_entry(groups
, cgroups
, cgroup_idx
)))
81 hash_add(auth_hosts
, host_name
, (void *)user
);
90 static int grok_service(struct cfg_comp
*obj
)
93 char *host_name
= NULL
;
94 char *contacts
= NULL
;
96 char *service_description
= NULL
;
98 for (i
= 0; i
< obj
->vars
; i
++) {
99 struct cfg_var
*v
= obj
->vlist
[i
];
100 if (!strcmp(v
->key
, "host_name")) {
101 host_name
= v
->value
;
102 } else if (!strcmp(v
->key
, "contacts")) {
104 } else if (!strcmp(v
->key
, "contact_groups")) {
106 } else if (!strcmp(v
->key
, "service_description")) {
107 service_description
= v
->value
;
111 if ((contacts
&& list_has_entry(contacts
, user
)) ||
112 (groups
&& list_has_any_entry(groups
, cgroups
, cgroup_idx
)))
114 hash_add2(auth_services
, host_name
, service_description
, (void *)user
);
120 static int grok_contactgroup(struct cfg_comp
*obj
)
124 char *members
= NULL
;
126 for (i
= 0; i
< obj
->vars
; i
++) {
127 struct cfg_var
*v
= obj
->vlist
[i
];
128 if (!strcmp(v
->key
, "contactgroup_name")) {
130 } else if (!strcmp(v
->key
, "members")) {
135 if (list_has_entry(members
, user
)) {
136 if (cgroup_idx
>= num_cgroups
- 1) {
137 cgroups
= realloc(cgroups
, (num_cgroups
+ 5) * sizeof(char *));
139 printf("Failed to realloc(cgroups)\n");
144 cgroups
[cgroup_idx
++] = name
;
150 static int grok_object(struct cfg_comp
*conf
, const char *str
, int (*handler
)(struct cfg_comp
*))
154 for (i
= 0; i
< conf
->nested
; i
++) {
155 struct cfg_comp
*obj
;
157 if (!strcmp(obj
->name
, str
)) {
165 void auth_set_user(const char *username
)
170 const char *auth_get_user(void)
175 void auth_parse_permission(const char *key
, const char *value
)
179 if (!user
|| prefixcmp(key
, "authorized_for_"))
182 val
= list_has_entry(value
, user
) | list_has_entry(value
, "*");
183 if (!prefixcmp(key
, "authorized_for_all_services")) {
184 is_service_root
= val
;
186 if (!prefixcmp(key
, "authorized_for_all_hosts")) {
188 is_service_root
= val
;
192 static struct cfg_comp
*conf
;
193 int auth_init(const char *path
)
195 int host_buckets
, service_buckets
;
198 * if the user can see all hosts (and thus all services),
199 * we don't need to read the configuration
204 cgroups
= calloc(20, sizeof(char *));
209 conf
= cfg_parse_file(path
);
211 printf("Failed to parse %s for some reason\n", path
);
215 grok_object(conf
, "define contactgroup ", grok_contactgroup
);
217 host_buckets
= conf
->nested
/ 10;
218 auth_hosts
= hash_init(host_buckets
);
219 grok_object(conf
, "define host ", grok_host
);
222 if (!is_service_root
) {
223 service_buckets
= conf
->nested
/ 2;
224 auth_services
= hash_init(service_buckets
);
225 grok_object(conf
, "define service ", grok_service
);
230 void auth_deinit(void)
233 cfg_destroy_compound(conf
);