2 * Copyright (C) 2017-2019 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <sys/types.h>
28 #include <sys/socket.h>
31 #include "afb-context.h"
32 #include "afb-token.h"
35 #define MAX_LABEL_LENGTH 1024
37 #if !defined(NO_DEFAULT_PEERCRED) && !defined(ADD_DEFAULT_PEERCRED)
38 # define NO_DEFAULT_PEERCRED
41 #if !defined(DEFAULT_PEERSEC_LABEL)
42 # define DEFAULT_PEERSEC_LABEL "NoLabel"
44 #if !defined(DEFAULT_PEERCRED_UID)
45 # define DEFAULT_PEERCRED_UID 99 /* nobody */
47 #if !defined(DEFAULT_PEERCRED_GID)
48 # define DEFAULT_PEERCRED_GID 99 /* nobody */
50 #if !defined(DEFAULT_PEERCRED_PID)
51 # define DEFAULT_PEERCRED_PID 0 /* no process */
54 static char export_format[] = "%x:%x:%x-%s";
55 static char import_format[] = "%x:%x:%x-%n";
57 static struct afb_cred *current;
59 static struct afb_cred *mkcred(uid_t uid, gid_t gid, pid_t pid, const char *label, size_t size)
61 struct afb_cred *cred;
69 user[i++] = (char)('0' + u % 10);
71 } while(u && i < sizeof user);
73 cred = malloc(2 + i + size + sizeof *cred);
81 cred->exported = NULL;
82 dest = (char*)(&cred[1]);
89 memcpy(dest, label, size);
91 dest = strrchr(dest, ':');
98 static struct afb_cred *mkcurrent()
100 char label[MAX_LABEL_LENGTH];
104 fd = open("/proc/self/attr/current", O_RDONLY);
108 rc = read(fd, label, sizeof label);
114 return mkcred(getuid(), getgid(), getpid(), label, (size_t)rc);
117 struct afb_cred *afb_cred_create(uid_t uid, gid_t gid, pid_t pid, const char *label)
119 label = label ? : DEFAULT_PEERSEC_LABEL;
120 return mkcred(uid, gid, pid, label, strlen(label));
123 struct afb_cred *afb_cred_create_for_socket(int fd)
128 char label[MAX_LABEL_LENGTH];
130 /* get the credentials */
131 length = (socklen_t)(sizeof ucred);
132 rc = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &length);
133 if (rc < 0 || length != (socklen_t)(sizeof ucred) || !~ucred.uid) {
134 #if !defined(NO_DEFAULT_PEERCRED)
135 ucred.uid = DEFAULT_PEERCRED_UID;
136 ucred.gid = DEFAULT_PEERCRED_GID;
137 ucred.pid = DEFAULT_PEERCRED_PID;
145 /* get the security label */
146 length = (socklen_t)(sizeof label);
147 rc = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, label, &length);
148 if (rc < 0 || length > (socklen_t)(sizeof label)) {
149 #if !defined(NO_DEFAULT_PEERSEC)
150 length = (socklen_t)strlen(DEFAULT_PEERSEC_LABEL);
151 strcpy (label, DEFAULT_PEERSEC_LABEL);
159 /* makes the result */
160 return mkcred(ucred.uid, ucred.gid, ucred.pid, label, (size_t)length);
163 struct afb_cred *afb_cred_addref(struct afb_cred *cred)
166 __atomic_add_fetch(&cred->refcount, 1, __ATOMIC_RELAXED);
170 void afb_cred_unref(struct afb_cred *cred)
172 if (cred && !__atomic_sub_fetch(&cred->refcount, 1, __ATOMIC_RELAXED)) {
176 free((void*)cred->exported);
182 struct afb_cred *afb_cred_current()
185 current = mkcurrent();
186 return afb_cred_addref(current);
189 const char *afb_cred_export(struct afb_cred *cred)
193 if (!cred->exported) {
194 rc = asprintf((char**)&cred->exported,
202 cred->exported = NULL;
205 return cred->exported;
208 struct afb_cred *afb_cred_import(const char *string)
210 struct afb_cred *cred;
211 int rc, uid, gid, pid, pos;
213 rc = sscanf(string, import_format, &uid, &gid, &pid, &pos);
215 cred = afb_cred_create((uid_t)uid, (gid_t)gid, (pid_t)pid, &string[pos]);
223 /*********************************************************************************/
224 static const char *token_of_context(struct afb_context *context)
226 return context && context->token ? afb_token_string(context->token) : "X";
229 /*********************************************************************************/
230 #ifdef BACKEND_PERMISSION_IS_CYNARA
233 #include <cynara-client.h>
235 static cynara *handle;
236 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
238 int afb_cred_has_permission(struct afb_cred *cred, const char *permission, struct afb_context *context)
243 /* case of permission for self */
247 ERROR("Got a null permission!");
251 /* cynara isn't reentrant */
252 pthread_mutex_lock(&mutex);
254 /* lazy initialisation */
256 rc = cynara_initialize(&handle, NULL);
257 if (rc != CYNARA_API_SUCCESS) {
259 ERROR("cynara initialisation failed with code %d", rc);
264 /* query cynara permission */
265 rc = cynara_check(handle, cred->label, token_of_context(context), cred->user, permission);
267 pthread_mutex_unlock(&mutex);
268 return rc == CYNARA_API_ACCESS_ALLOWED;
271 /*********************************************************************************/
273 int afb_cred_has_permission(struct afb_cred *cred, const char *permission, struct afb_context *context)
275 WARNING("Granting permission %s by default of backend", permission ?: "(null)");