X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fapp-framework-binder.git;a=blobdiff_plain;f=src%2Fafb-cred.c;h=09a4803ed46306e80ebfe95a75c5849a3712f158;hp=763c926536ea4b0aeea1b7690248567f8b03c910;hb=65353dce81a629e042800bb7b86fcd869a76727e;hpb=9952955d440866fa061d78fe923cb8282f778667 diff --git a/src/afb-cred.c b/src/afb-cred.c index 763c9265..09a4803e 100644 --- a/src/afb-cred.c +++ b/src/afb-cred.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 "IoT.bzh" + * Copyright (C) 2015-2020 "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,30 @@ #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 +#if !defined(DEFAULT_PEERCRED_UID) +# define DEFAULT_PEERCRED_UID 99 /* nobody */ +#endif +#if !defined(DEFAULT_PEERCRED_GID) +# define DEFAULT_PEERCRED_GID 99 /* nobody */ +#endif +#if !defined(DEFAULT_PEERCRED_PID) +# 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) @@ -53,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) @@ -90,7 +114,7 @@ static struct afb_cred *mkcurrent() struct afb_cred *afb_cred_create(uid_t uid, gid_t gid, pid_t pid, const char *label) { - label = label ? : ""; + label = label ? : DEFAULT_PEERSEC_LABEL; return mkcred(uid, gid, pid, label, strlen(label)); } @@ -104,19 +128,30 @@ 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) + ucred.uid = DEFAULT_PEERCRED_UID; + ucred.gid = DEFAULT_PEERCRED_GID; + ucred.pid = DEFAULT_PEERCRED_PID; +#else if (!rc) errno = EINVAL; return NULL; +#endif } /* get the security label */ length = (socklen_t)(sizeof label); rc = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, label, &length); if (rc < 0 || length > (socklen_t)(sizeof label)) { +#if !defined(NO_DEFAULT_PEERSEC) + length = (socklen_t)strlen(DEFAULT_PEERSEC_LABEL); + strcpy (label, DEFAULT_PEERSEC_LABEL); +#else if (!rc) errno = EINVAL; return NULL; +#endif } /* makes the result */ @@ -133,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); + } } } @@ -147,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; +}