Setting and checking LOA
[src/app-framework-binder.git] / include / afb / afb-plugin.h
1 /*
2  * Copyright (C) 2016 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #pragma once
19
20 /*****************************************************************************
21  * This files is the main file to include for writing plugins dedicated to
22  *
23  *                      AFB-DAEMON
24  *
25  * Functions of plugins of afb-daemon are accessible by authorized clients
26  * through the apis module of afb-daemon.
27  *
28  * A plugin is a shared library. This shared library must have at least one
29  * exported symbol for being registered in afb-daemon.
30  * For the current version of afb-daemon, the function exported MUST be named
31  *
32  *                  pluginAfbV1Register
33  */
34
35 /*
36  * Some function of the library are exported to afb-daemon.
37  */
38
39 #include <afb/afb-req-itf.h>
40 #include <afb/afb-event-sender-itf.h>
41
42 /*
43  * Definition of the versions of the plugin.
44  * The definition uses hashes.
45  */
46 enum  AFB_plugin_version
47 {
48        AFB_PLUGIN_VERSION_1 = 123456789        /* version 1 */
49 };
50
51 /*
52  * Enum for Session/Token/Authentication middleware.
53  * This enumeration is valid for plugins of type 1
54  */
55 enum AFB_session_v1
56 {
57        AFB_SESSION_NONE = 0,   /* no session and no authentification required */
58        AFB_SESSION_CREATE = 1, /* requires authentification and first call of the session */
59        AFB_SESSION_CLOSE = 2,  /* closes the session after authentification */
60        AFB_SESSION_RENEW = 4,  /* refreshes the token after authentification */
61        AFB_SESSION_CHECK = 8,  /* enforce authentification */
62
63        AFB_SESSION_LOA_GE = 16, /* check that the LOA is greater or equal to the given value */
64        AFB_SESSION_LOA_LE = 32, /* check that the LOA is lesser or equal to the given value */
65        AFB_SESSION_LOA_EQ = 48, /* check that the LOA is equal to the given value */
66
67        AFB_SESSION_LOA_SHIFT = 6, /* shift for LOA */
68        AFB_SESSION_LOA_MASK = 3,  /* mask for LOA */
69
70        AFB_SESSION_LOA_0 = 0,   /* value for LOA of 0 */
71        AFB_SESSION_LOA_1 = 64,  /* value for LOA of 1 */
72        AFB_SESSION_LOA_2 = 128, /* value for LOA of 2 */
73        AFB_SESSION_LOA_3 = 192, /* value for LOA of 3 */
74
75        AFB_SESSION_LOA_LE_0 = AFB_SESSION_LOA_LE | AFB_SESSION_LOA_0, /* check LOA <= 0 */
76        AFB_SESSION_LOA_LE_1 = AFB_SESSION_LOA_LE | AFB_SESSION_LOA_1, /* check LOA <= 1 */
77        AFB_SESSION_LOA_LE_2 = AFB_SESSION_LOA_LE | AFB_SESSION_LOA_2, /* check LOA <= 2 */
78        AFB_SESSION_LOA_LE_3 = AFB_SESSION_LOA_LE | AFB_SESSION_LOA_3, /* check LOA <= 3 */
79
80        AFB_SESSION_LOA_GE_0 = AFB_SESSION_LOA_GE | AFB_SESSION_LOA_0, /* check LOA >= 0 */
81        AFB_SESSION_LOA_GE_1 = AFB_SESSION_LOA_GE | AFB_SESSION_LOA_1, /* check LOA >= 1 */
82        AFB_SESSION_LOA_GE_2 = AFB_SESSION_LOA_GE | AFB_SESSION_LOA_2, /* check LOA >= 2 */
83        AFB_SESSION_LOA_GE_3 = AFB_SESSION_LOA_GE | AFB_SESSION_LOA_3, /* check LOA >= 3 */
84
85        AFB_SESSION_LOA_EQ_0 = AFB_SESSION_LOA_EQ | AFB_SESSION_LOA_0, /* check LOA == 0 */
86        AFB_SESSION_LOA_EQ_1 = AFB_SESSION_LOA_EQ | AFB_SESSION_LOA_1, /* check LOA == 1 */
87        AFB_SESSION_LOA_EQ_2 = AFB_SESSION_LOA_EQ | AFB_SESSION_LOA_2, /* check LOA == 2 */
88        AFB_SESSION_LOA_EQ_3 = AFB_SESSION_LOA_EQ | AFB_SESSION_LOA_3  /* check LOA == 3 */
89 };
90
91 /*
92  * Description of one verb of the API provided by the plugin
93  * This enumeration is valid for plugins of type 1
94  */
95 struct AFB_verb_desc_v1
96 {
97        const char *name;                       /* name of the verb */
98        enum AFB_session_v1 session;            /* authorisation and session requirements of the verb */
99        void (*callback)(struct afb_req req);   /* callback function implementing the verb */
100        const char *info;                       /* textual description of the verb */
101 };
102
103 /*
104  * Description of the plugins of type 1
105  */
106 struct AFB_plugin_desc_v1
107 {
108        const char *info;                       /* textual information about the plugin */
109        const char *prefix;                     /* required prefix name for the plugin */
110        const struct AFB_verb_desc_v1 *verbs;   /* array of descriptions of verbs terminated by a NULL name */
111 };
112
113 /*
114  * Description of a plugin
115  */
116 struct AFB_plugin
117 {
118        enum AFB_plugin_version type; /* type of the plugin */
119        union {
120                struct AFB_plugin_desc_v1 v1;   /* description of the plugin of type 1 */
121        };
122 };
123
124 /*
125  * config mode
126  */
127 enum AFB_Mode {
128        AFB_MODE_LOCAL = 0,     /* run locally */
129        AFB_MODE_REMOTE,        /* run remotely */
130        AFB_MODE_GLOBAL         /* run either remotely or locally (DONT USE! reserved for future) */
131 };
132
133 /* declaration of features of libsystemd */
134 struct sd_event;
135 struct sd_bus;
136
137 /*
138  * Definition of the facilities provided by the daemon.
139  */
140 struct afb_daemon_itf {
141        struct afb_event_sender (*get_event_sender)(void *closure);           /* get the event manager */
142        struct sd_event *(*get_event_loop)(void *closure);      /* get the common systemd's event loop */
143        struct sd_bus *(*get_user_bus)(void *closure);          /* get the common systemd's user d-bus */
144        struct sd_bus *(*get_system_bus)(void *closure);        /* get the common systemd's system d-bus */
145 };
146
147 /*
148  * Structure for accessing daemon.
149  * See also: afb_daemon_get_event_sender, afb_daemon_get_event_loop, afb_daemon_get_user_bus, afb_daemon_get_system_bus
150  */
151 struct afb_daemon {
152        const struct afb_daemon_itf *itf;       /* the interfacing functions */
153        void *closure;                          /* the closure when calling these functions */
154 };
155
156 /*
157  * Interface between the daemon and the plugin.
158  */
159 struct AFB_interface
160 {
161        struct afb_daemon daemon;       /* access to the daemon facilies */
162        int verbosity;                  /* level of verbosity */
163        enum AFB_Mode mode;             /* run mode (local or remote) */
164 };
165
166 /*
167  * Function for registering the plugin
168  */
169 extern const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *interface);
170
171 /*
172  * Retrieves the event sender of AFB
173  * 'daemon' MUST be the daemon given in interface when activating the plugin.
174  */
175 static inline struct afb_event_sender afb_daemon_get_event_sender(struct afb_daemon daemon)
176 {
177         return daemon.itf->get_event_sender(daemon.closure);
178 }
179
180 /*
181  * Retrieves the common systemd's event loop of AFB
182  * 'daemon' MUST be the daemon given in interface when activating the plugin.
183  */
184 static inline struct sd_event *afb_daemon_get_event_loop(struct afb_daemon daemon)
185 {
186         return daemon.itf->get_event_loop(daemon.closure);
187 }
188
189 /*
190  * Retrieves the common systemd's user/session d-bus of AFB
191  * 'daemon' MUST be the daemon given in interface when activating the plugin.
192  */
193 static inline struct sd_bus *afb_daemon_get_user_bus(struct afb_daemon daemon)
194 {
195         return daemon.itf->get_user_bus(daemon.closure);
196 }
197
198 /*
199  * Retrieves the common systemd's system d-bus of AFB
200  * 'daemon' MUST be the daemon given in interface when activating the plugin.
201  */
202 static inline struct sd_bus *afb_daemon_get_system_bus(struct afb_daemon daemon)
203 {
204         return daemon.itf->get_system_bus(daemon.closure);
205 }
206
207