X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-cred.c;h=88c29c5e17ab0859f6b3d7d66cd90e769990b712;hb=ca0f859d96599c8ae295c85f825030c891969de1;hp=87661f19985726445a73bec6525fcc4ba8b23b34;hpb=c623236073ec6cba38a5c02582cc24ad2f5e51c4;p=src%2Fapp-framework-binder.git diff --git a/src/afb-cred.c b/src/afb-cred.c index 87661f19..88c29c5e 100644 --- a/src/afb-cred.c +++ b/src/afb-cred.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 "IoT.bzh" + * Copyright (C) 2017-2019 "IoT.bzh" * Author: José Bollo * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,6 +18,8 @@ #define _GNU_SOURCE #include +#include +#include #include #include #include @@ -26,9 +28,14 @@ #include #include "afb-cred.h" +#include "verbose.h" #define MAX_LABEL_LENGTH 1024 +#if !defined(NO_DEFAULT_PEERCRED) && !defined(ADD_DEFAULT_PEERCRED) +# define NO_DEFAULT_PEERCRED +#endif + #if !defined(DEFAULT_PEERSEC_LABEL) # define DEFAULT_PEERSEC_LABEL "NoLabel" #endif @@ -42,6 +49,9 @@ # define DEFAULT_PEERCRED_PID 0 /* no process */ #endif +static char export_format[] = "%x:%x:%x-%s"; +static char import_format[] = "%x:%x:%x-%n"; + static struct afb_cred *current; static struct afb_cred *mkcred(uid_t uid, gid_t gid, pid_t pid, const char *label, size_t size) @@ -66,6 +76,7 @@ static struct afb_cred *mkcred(uid_t uid, gid_t gid, pid_t pid, const char *labe cred->uid = uid; cred->gid = gid; cred->pid = pid; + cred->exported = NULL; dest = (char*)(&cred[1]); cred->user = dest; while(i) @@ -117,15 +128,15 @@ struct afb_cred *afb_cred_create_for_socket(int fd) /* get the credentials */ length = (socklen_t)(sizeof ucred); rc = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &length); - if (rc < 0 || length != (socklen_t)(sizeof ucred)) { + if (rc < 0 || length != (socklen_t)(sizeof ucred) || !~ucred.uid) { #if !defined(NO_DEFAULT_PEERCRED) - if (!rc) - errno = EINVAL; - return NULL; -#else ucred.uid = DEFAULT_PEERCRED_UID; ucred.gid = DEFAULT_PEERCRED_GID; ucred.pid = DEFAULT_PEERCRED_PID; +#else + if (!rc) + errno = EINVAL; + return NULL; #endif } @@ -157,10 +168,12 @@ struct afb_cred *afb_cred_addref(struct afb_cred *cred) void afb_cred_unref(struct afb_cred *cred) { if (cred && !__atomic_sub_fetch(&cred->refcount, 1, __ATOMIC_RELAXED)) { - if (cred != current) - free(cred); - else + if (cred == current) cred->refcount = 1; + else { + free((void*)cred->exported); + free(cred); + } } } @@ -171,3 +184,36 @@ struct afb_cred *afb_cred_current() return afb_cred_addref(current); } +const char *afb_cred_export(struct afb_cred *cred) +{ + int rc; + + if (!cred->exported) { + rc = asprintf((char**)&cred->exported, + export_format, + (int)cred->uid, + (int)cred->gid, + (int)cred->pid, + cred->label); + if (rc < 0) { + errno = ENOMEM; + cred->exported = NULL; + } + } + return cred->exported; +} + +struct afb_cred *afb_cred_import(const char *string) +{ + struct afb_cred *cred; + int rc, uid, gid, pid, pos; + + rc = sscanf(string, import_format, &uid, &gid, &pid, &pos); + if (rc == 3) + cred = afb_cred_create((uid_t)uid, (gid_t)gid, (pid_t)pid, &string[pos]); + else { + errno = EINVAL; + cred = NULL; + } + return cred; +}