Improve handling of verbosity in bindings
[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
21 #include <stdlib.h>
22
23 #include <afb/afb-auth.h>
24
25 #include "afb-auth.h"
26 #include "afb-context.h"
27 #include "afb-xreq.h"
28 #include "afb-cred.h"
29 #include "verbose.h"
30
31 int afb_auth_check(struct afb_xreq *xreq, const struct afb_auth *auth)
32 {
33         switch (auth->type) {
34         default:
35         case afb_auth_No:
36                 return 0;
37
38         case afb_auth_Token:
39                 return afb_context_check(&xreq->context);
40
41         case afb_auth_LOA:
42                 return afb_context_check_loa(&xreq->context, auth->loa);
43
44         case afb_auth_Permission:
45                 return afb_auth_has_permission(xreq, auth->text);
46
47         case afb_auth_Or:
48                 return afb_auth_check(xreq, auth->first) || afb_auth_check(xreq, auth->next);
49
50         case afb_auth_And:
51                 return afb_auth_check(xreq, auth->first) && afb_auth_check(xreq, auth->next);
52
53         case afb_auth_Not:
54                 return !afb_auth_check(xreq, auth->first);
55
56         case afb_auth_Yes:
57                 return 1;
58         }
59 }
60
61 /*********************************************************************************/
62 #ifdef BACKEND_PERMISSION_IS_CYNARA
63
64 #include <pthread.h>
65 #include <cynara-client.h>
66
67 static cynara *handle;
68 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
69
70 int afb_auth_has_permission(struct afb_xreq *xreq, const char *permission)
71 {
72         int rc;
73
74         if (!xreq->cred) {
75                 /* case of permission for self */
76                 return 1;
77         }
78         if (!permission) {
79                 ERROR("Got a null permission!");
80                 return 0;
81         }
82
83         /* cynara isn't reentrant */
84         pthread_mutex_lock(&mutex);
85
86         /* lazy initialisation */
87         if (!handle) {
88                 rc = cynara_initialize(&handle, NULL);
89                 if (rc != CYNARA_API_SUCCESS) {
90                         handle = NULL;
91                         ERROR("cynara initialisation failed with code %d", rc);
92                         return 0;
93                 }
94         }
95
96         /* query cynara permission */
97         rc = cynara_check(handle, xreq->cred->label, afb_context_uuid(&xreq->context), xreq->cred->user, permission);
98
99         pthread_mutex_unlock(&mutex);
100         return rc == CYNARA_API_ACCESS_ALLOWED;
101 }
102
103 /*********************************************************************************/
104 #else
105 int afb_auth_has_permission(struct afb_xreq *xreq, const char *permission)
106 {
107         WARNING("Granting permission %s by default of backend", permission ?: "(null)");
108         return !!permission;
109 }
110 #endif
111