Implement afb_req_has_permission
[src/app-framework-binder.git] / src / afb-auth.c
1 /*
2  * Copyright (C) 2016, 2017 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  * Author José Bollo <jose.bollo@iot.bzh>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #define _GNU_SOURCE
20 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
21
22 #include <stdlib.h>
23
24 #include <afb/afb-auth.h>
25
26 #include "afb-auth.h"
27 #include "afb-context.h"
28 #include "afb-xreq.h"
29 #include "afb-cred.h"
30 #include "verbose.h"
31
32 int afb_auth_check(struct afb_xreq *xreq, const struct afb_auth *auth)
33 {
34         switch (auth->type) {
35         default:
36         case afb_auth_No:
37                 return 0;
38
39         case afb_auth_Token:
40                 return afb_context_check(&xreq->context);
41
42         case afb_auth_LOA:
43                 return afb_context_check_loa(&xreq->context, auth->loa);
44
45         case afb_auth_Permission:
46                 return afb_auth_has_permission(xreq, auth->text);
47
48         case afb_auth_Or:
49                 return afb_auth_check(xreq, auth->first) || afb_auth_check(xreq, auth->next);
50
51         case afb_auth_And:
52                 return afb_auth_check(xreq, auth->first) && afb_auth_check(xreq, auth->next);
53
54         case afb_auth_Not:
55                 return !afb_auth_check(xreq, auth->first);
56
57         case afb_auth_Yes:
58                 return 1;
59         }
60 }
61
62 /*********************************************************************************/
63 #ifdef BACKEND_PERMISSION_IS_CYNARA
64
65 #include <pthread.h>
66 #include <cynara-client.h>
67
68 static cynara *handle;
69 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
70
71 int afb_auth_has_permission(struct afb_xreq *xreq, const char *permission)
72 {
73         int rc;
74
75         if (!xreq->cred) {
76                 /* case of permission for self */
77                 return 1;
78         }
79         if (!permission) {
80                 ERROR("Got a null permission!");
81                 return 0;
82         }
83
84         /* cynara isn't reentrant */
85         pthread_mutex_lock(&mutex);
86
87         /* lazy initialisation */
88         if (!handle) {
89                 rc = cynara_initialize(&handle, NULL);
90                 if (rc != CYNARA_API_SUCCESS) {
91                         handle = NULL;
92                         ERROR("cynara initialisation failed with code %d", rc);
93                         return 0;
94                 }
95         }
96
97         /* query cynara permission */
98         rc = cynara_check(handle, xreq->cred->label, afb_context_uuid(&xreq->context), xreq->cred->user, permission);
99
100         pthread_mutex_unlock(&mutex);
101         return rc == CYNARA_API_ACCESS_ALLOWED;
102 }
103
104 /*********************************************************************************/
105 #else
106 int afb_auth_has_permission(struct afb_xreq *xreq, const char *permission)
107 {
108         WARNING("Granting permission %s by default of backend", permission ?: "(null)");
109         return !!permission;
110 }
111 #endif
112