Adding signature checking 02/27902/2 accepted/tizen/common/20140924.112708 accepted/tizen/ivi/20140927.095037 submit/tizen_common/20140923.154519 submit/tizen_common/20140924.073135 submit/tizen_ivi/20140926.000000
authorJosé Bollo <jose.bollo@open.eurogiciel.org>
Thu, 18 Sep 2014 14:41:16 +0000 (16:41 +0200)
committerJosé Bollo <jose.bollo@open.eurogiciel.org>
Tue, 23 Sep 2014 07:46:04 +0000 (09:46 +0200)
The couple tizen-platform-wrapper/tizen-platform-config is
intended to be a foundation of what is the tizen configuration.

As such, each change of the set of keys SHOULD implies a full
rebuild of tizen. It is what normally happens via the set
of build dependencies. But some developper encountered problems
because they were taking part of the system, having different
versions of configuration. Especially, it happened was installing
a fresh rpm built with GBS on an old image. This problem is reported
in the following file:

Bug-Tizen: TC-1626

This commit modify the behaviour of the generated library but
doesn't modify its API. The new principle is to add a signup that
it linked to the client and checked by the shared library. If the
client's signup misfits library's one, a critical error is printed
to using syslog and the program is aborted.

Also, the modularity of the source is improved.

Change-Id: I5c2e5a990ebf17f893b669c272fda99d0d0b2de1
Signed-off-by: José Bollo <jose.bollo@open.eurogiciel.org>
33 files changed:
configure.ac
packaging/tizen-platform-wrapper.spec
src/Makefile.am
src/buffer.c
src/buffer.h
src/build.sh [new file with mode: 0755]
src/context.c [new file with mode: 0644]
src/context.h [new file with mode: 0644]
src/foreign.c
src/foreign.h
src/hashing.c [new file with mode: 0644]
src/hashing.h [new file with mode: 0644]
src/heap.c
src/heap.h
src/init.c [new file with mode: 0644]
src/init.h [new file with mode: 0644]
src/parser.c
src/parser.h
src/passwd.c
src/passwd.h
src/scratch.c
src/scratch.h
src/sha256sum.c [new file with mode: 0644]
src/sha256sum.h [new file with mode: 0644]
src/shared-api.c [new file with mode: 0644]
src/shared-api.h [new file with mode: 0644]
src/static-api.c [new file with mode: 0644]
src/toolbox.c
src/tzplatform_config.c [deleted file]
src/tzplatform_config.h
src/tzplatform_config.sym [new file with mode: 0644]
src/tzplatform_get.c
tizen-platform-wrapper.pc.in

index 7b4e1cec190174c84aca1ee03c0dfd772311bdf0..3e4ad62332dd196662e4093e8f0b61c696608d88 100644 (file)
@@ -2,7 +2,7 @@
 # Process this file with autoconf to produce a configure script.
 
 AC_PREREQ([2.69])
-AC_INIT([tizen-platform-wrapper], [1.0])
+AC_INIT([tizen-platform-wrapper], [2.0])
 AM_INIT_AUTOMAKE([-Wall -Werror foreign])
 AC_CONFIG_SRCDIR([src/parser.h])
 AC_CONFIG_HEADERS([src/config.h])
index 027a699253cba0a74c100c6c3b678cbbcbf79ee8..d0178533d283bcbb5a429181bee1f7467d13fc75 100644 (file)
@@ -1,5 +1,5 @@
 Name:           tizen-platform-wrapper
-Version:        1.0
+Version:        2.0
 Release:        0
 License:        LGPL-2.0
 Summary:        A toolkit to generate the libtizen-platform-config library
@@ -8,6 +8,7 @@ Group:          Development/Tools
 Source:         %{name}-%{version}.tar.bz2
 Source1001:     %{name}.manifest
 Requires:       gperf
+Requires:       /usr/bin/sha256sum
 
 %description
 A toolkit to generate the libtizen-platform-config library in tizen-platform-config.
index bf994a3af1689fca8314bc6b0d09e2d2896ddf4c..987247aecfcda08a0ed75213d7254dfefea9e16d 100644 (file)
@@ -4,6 +4,7 @@ tzplatform_tool_SOURCES = buffer.c \
                           foreign.c \
                           heap.c \
                           parser.c \
+                          sha256sum.c \
                           toolbox.c
 
 dist_pkgdata_DATA = buffer.c \
@@ -16,7 +17,16 @@ dist_pkgdata_DATA = buffer.c \
                     parser.h \
                     scratch.c \
                     scratch.h \
-                    tzplatform_config.c \
+                    context.c \
+                    context.h \
+                    hashing.c \
+                    hashing.h \
+                    init.c \
+                    init.h \
+                    shared-api.c \
+                    shared-api.h \
+                    static-api.c \
+                    tzplatform_config.sym \
                     tzplatform_config.h \
                     tzplatform_get.c \
                     passwd.h \
index 1a72e76654c09035d10900cb0a46798532113e24..92e9bf3d2ce31bf852dede81365e49f0c22c8fd9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2013-2014 Intel Corporation.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
index cd86040da7e16723e8029a7a83884921b8e4da28..28b43be503b166b59d4ebc2762fad83cb80f5fb9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2013-2014 Intel Corporation.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
diff --git a/src/build.sh b/src/build.sh
new file mode 100755 (executable)
index 0000000..2ccaa48
--- /dev/null
@@ -0,0 +1,20 @@
+#!/bin/bash
+
+# useful build script for purpose of testing
+
+f="-O -Wall -DCONFIGPATH=\"meta\" -fPIC"
+
+e() { echo error: "$@" >&2; exit 1; }
+d() { echo running: "$@" >&2; "$@" || e "$@"; }
+
+[ -f meta ] || e no file meta
+
+d gcc $f -o toolbox toolbox.c parser.c buffer.c foreign.c sha256sum.c
+d ./toolbox h > tzplatform_variables.h
+d ./toolbox c > hash.inc
+d ./toolbox signup > signup.inc
+d gcc $f -c *.c
+d ld -shared --version-script=tzplatform_config.sym -o libtzplatform-config.so buffer.o   foreign.o  heap.o  parser.o  scratch.o context.o  hashing.o  init.o  passwd.o  shared-api.o
+d gcc -o get tzplatform_get.o static-api.o -L. -ltzplatform-config
+
+
diff --git a/src/context.c b/src/context.c
new file mode 100644 (file)
index 0000000..4b1e49d
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+
+
+
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <unistd.h>
+
+#ifndef NOT_MULTI_THREAD_SAFE
+#include <pthread.h>
+#endif
+
+#include "tzplatform_variables.h"
+#include "heap.h"
+#include "foreign.h"
+#include "context.h"
+
+
+inline uid_t get_uid(struct tzplatform_context *context)
+{
+    uid_t result;
+
+    result = context->user;
+    if (result == _USER_NOT_SET_)
+        result = getuid();
+
+    return result;
+}
+
+#if _FOREIGN_HAS_(EUID)
+inline uid_t get_euid(struct tzplatform_context *context)
+{
+    uid_t result;
+
+    result = context->user;
+    if (result == _USER_NOT_SET_)
+        result = geteuid();
+
+    return result;
+}
+#endif
+
+#if _FOREIGN_HAS_(GID)
+inline gid_t get_gid(struct tzplatform_context *context)
+{
+    return getgid();
+}
+#endif
+
diff --git a/src/context.h b/src/context.h
new file mode 100644 (file)
index 0000000..2612f19
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#ifndef CONTEXT_H
+#define CONTEXT_H
+
+#ifndef HEAP_H
+#error "you should include heap.h"
+#endif
+
+#ifndef FOREIGN_H
+#error "you should include foreign.h"
+#endif
+
+enum STATE { RESET=0, ERROR, VALID };
+
+#define _USER_NOT_SET_  ((uid_t)-1)
+
+struct tzplatform_context {
+#ifndef NOT_MULTI_THREAD_SAFE
+    pthread_mutex_t mutex;
+#endif
+    enum STATE state;
+    uid_t user;
+    struct heap heap;
+    const char *values[_TZPLATFORM_VARIABLES_COUNT_];
+};
+
+inline uid_t get_uid(struct tzplatform_context *context);
+
+#if _FOREIGN_HAS_(EUID)
+inline uid_t get_euid(struct tzplatform_context *context);
+#endif
+
+#if _FOREIGN_HAS_(GID)
+inline gid_t get_gid(struct tzplatform_context *context);
+#endif
+
+#endif
+
index dab1c217348f826a7336ea71560230e9dff54d82..a19f6ad865e70000062efa98e289f696e568a3fd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2013-2014 Intel Corporation.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
index 3c9066fad3957722b9aecc5f4a6a528dfef9f7b1..646a41dff4bb7751024a63ae6b108edb9bb82c83 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2013-2014 Intel Corporation.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
diff --git a/src/hashing.c b/src/hashing.c
new file mode 100644 (file)
index 0000000..c22b4c0
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <string.h>
+#include <assert.h>
+
+#include "tzplatform_variables.h"
+#include "hash.inc"
+
+static const char *var_names[_TZPLATFORM_VARIABLES_COUNT_];
+
+inline int hashid(const char *text, unsigned int len)
+{
+    const struct varassoc *vara = hashvar(text, len);
+    return vara ? vara->id : -1;
+}
+
+const char *keyname(int id)
+{
+    const struct varassoc *iter, *end;
+
+    assert(0 <= id && id < _TZPLATFORM_VARIABLES_COUNT_);
+    if (!var_names[0]) {
+        iter = namassoc;
+        end = iter + (sizeof namassoc / sizeof namassoc[0]);
+        while (iter != end) {
+            if (iter->offset >= 0) {
+                assert(0 <= iter->id && iter->id < _TZPLATFORM_VARIABLES_COUNT_);
+                var_names[iter->id] = varpool + iter->offset;
+            }
+            iter++;
+        }
+    }
+    return var_names[id];
+}
diff --git a/src/hashing.h b/src/hashing.h
new file mode 100644 (file)
index 0000000..5ce816e
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#ifndef HASHING_H
+#define HASHING_H
+
+inline int hashid(const char *text, unsigned int len);
+const char *keyname(int id);
+
+#endif
+
index 2e4365d4624893bc582b74f141285cfc117d3b36..d091f51fbf2bf3689b846ce649dd6eb0787a635a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2013-2014 Intel Corporation.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
index d9713e5c2fc22d46f8392f8195788ba0deefb255..744e6c0905514764f438e3322634502e7e278374 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2013-2014 Intel Corporation.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
diff --git a/src/init.c b/src/init.c
new file mode 100644 (file)
index 0000000..76868cc
--- /dev/null
@@ -0,0 +1,431 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <pthread.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <stdarg.h>
+#include <alloca.h>
+#include <pwd.h>
+#include <assert.h>
+
+#ifndef NOT_MULTI_THREAD_SAFE
+#include <pthread.h>
+#endif
+
+#ifndef CONFIGPATH
+#define CONFIGPATH "/etc/tizen-platform.conf"
+#endif
+
+#include "tzplatform_variables.h"
+#include "tzplatform_config.h"
+#include "parser.h"
+#include "heap.h"
+#include "buffer.h"
+#include "foreign.h"
+#include "scratch.h"
+#include "passwd.h"
+#include "context.h"
+#include "hashing.h"
+#include "init.h"
+
+#define _HAS_IDS_   (  _FOREIGN_HAS_(UID)  \
+                    || _FOREIGN_HAS_(EUID) \
+                    || _FOREIGN_HAS_(GID)  )
+
+#define _HAS_PWS_   (  _FOREIGN_HAS_(HOME)  \
+                    || _FOREIGN_HAS_(USER)  \
+                    || _FOREIGN_HAS_(EHOME) \
+                    || _FOREIGN_HAS_(EUSER) )
+
+/* local and static variables */
+static const char metafilepath[] = CONFIGPATH;
+static const char emptystring[] = "";
+
+/* structure for reading config files */
+struct reading {
+    int errcount;
+    struct tzplatform_context *context;
+    size_t dynvars[_FOREIGN_COUNT_];
+    size_t offsets[_TZPLATFORM_VARIABLES_COUNT_];
+};
+
+/* write the error message */
+static void writerror( const char *format, ...)
+{
+    va_list ap;
+    fprintf( stderr, "tzplatform_config ERROR: ");
+    va_start(ap, format);
+    vfprintf(stderr, format, ap);
+    va_end(ap);
+    fprintf( stderr, "\n");
+}
+
+#if _HAS_IDS_
+/* fill the foreign variables for ids */
+static void foreignid( struct reading *reading)
+{
+    int n;
+    char buffer[50];
+
+#if _FOREIGN_HAS_(UID)
+    /* set the uid */
+    if (reading->dynvars[UID] == HNULL) {
+        n = snprintf( buffer, sizeof buffer, "%d", (int)get_uid(reading->context));
+        if (0 < n && n < (int)(sizeof buffer))
+            reading->dynvars[UID] = heap_strndup( &reading->context->heap, buffer, (size_t)n);
+    }
+#endif
+
+#if _FOREIGN_HAS_(EUID)
+    /* set the euid */
+    if (reading->dynvars[EUID] == HNULL) {
+        n = snprintf( buffer, sizeof buffer, "%d", (int)get_euid(reading->context));
+        if (0 < n && n < (int)(sizeof buffer))
+            reading->dynvars[EUID] = heap_strndup( &reading->context->heap, buffer, (size_t)n);
+    }
+#endif
+
+#if _FOREIGN_HAS_(GID)
+    /* set the gid */
+    if (reading->dynvars[GID] == HNULL) {
+        n = snprintf( buffer, sizeof buffer, "%d", (int)get_gid(reading->context));
+        if (0 < n && n < (int)(sizeof buffer))
+            reading->dynvars[GID] = heap_strndup( &reading->context->heap, buffer, (size_t)n);
+    }
+#endif
+}
+#endif
+
+#if _HAS_PWS_
+/* fill the foreign variables for home and user */
+static void foreignpw( struct reading *reading)
+{
+    int n = 0;
+    struct pwget *array[3];
+#if _FOREIGN_HAS_(HOME) || _FOREIGN_HAS_(USER)
+    struct pwget uid;
+    char suid[50];
+#endif
+#if _FOREIGN_HAS_(EHOME) || _FOREIGN_HAS_(EUSER)
+    struct pwget euid;
+    char seuid[50];
+#endif
+
+#if _FOREIGN_HAS_(HOME) || _FOREIGN_HAS_(USER)
+    if (
+#if _FOREIGN_HAS_(HOME)
+        reading->dynvars[HOME] == HNULL
+#endif
+#if _FOREIGN_HAS_(HOME) && _FOREIGN_HAS_(USER)
+        ||
+#endif
+#if _FOREIGN_HAS_(USER)
+        reading->dynvars[USER] == HNULL
+#endif
+    ) {
+        snprintf( suid, sizeof suid, "%u", (unsigned)get_uid(reading->context));
+        uid.id = suid;
+        array[n++] = &uid;
+    }
+    else {
+        uid.set = 0;
+    }
+#endif
+
+#if _FOREIGN_HAS_(EHOME) || _FOREIGN_HAS_(EUSER)
+    if (
+#if _FOREIGN_HAS_(EHOME)
+        reading->dynvars[EHOME] == HNULL
+#endif
+#if _FOREIGN_HAS_(EHOME) && _FOREIGN_HAS_(USER)
+        ||
+#endif
+#if _FOREIGN_HAS_(EUSER)
+        reading->dynvars[EUSER] == HNULL
+#endif
+    ) {
+        snprintf( seuid, sizeof seuid, "%u", (unsigned)get_euid(reading->context));
+        euid.id = seuid;
+        array[n++] = &euid;
+    }
+    else {
+        euid.set = 0;
+    }
+#endif
+
+    if (n) {
+        array[n] = NULL;
+        if (pw_get( &reading->context->heap, array) == 0) {
+#if _FOREIGN_HAS_(HOME)
+            if (uid.set)
+                reading->dynvars[HOME] = uid.home;
+#endif
+#if _FOREIGN_HAS_(USER)
+            if (uid.set)
+                reading->dynvars[USER] = uid.user;
+#endif
+#if _FOREIGN_HAS_(EHOME)
+            if (euid.set)
+                reading->dynvars[EHOME] = euid.home;
+#endif
+#if _FOREIGN_HAS_(EUSER)
+            if (euid.set)
+                reading->dynvars[EUSER] = euid.user;
+#endif
+        }
+    }
+}
+#endif
+
+/* get the foreign variable */
+static const char *foreignvar( struct reading *reading, 
+                                            const char *name, size_t length)
+{
+    enum fkey key = foreign( name, length);
+    size_t offset;
+
+    switch (key) {
+#if _HAS_PWS_
+#if _FOREIGN_HAS_(HOME)
+    case HOME:
+#endif
+#if _FOREIGN_HAS_(USER)
+    case USER:
+#endif
+#if _FOREIGN_HAS_(EHOME)
+    case EHOME:
+#endif
+#if _FOREIGN_HAS_(EUSER)
+    case EUSER:
+#endif
+        foreignpw( reading);
+        break;
+#endif
+#if _HAS_IDS_
+#if _FOREIGN_HAS_(UID)
+    case UID:
+#endif
+#if _FOREIGN_HAS_(GID)
+    case GID:
+#endif
+#if _FOREIGN_HAS_(EUID)
+    case EUID:
+#endif
+        foreignid( reading);
+        break;
+#endif
+
+    default:
+        return NULL;
+    }
+    offset = reading->dynvars[key];
+    return offset==HNULL ? NULL : heap_address( &reading->context->heap, offset);
+}
+
+/* callback for parsing errors */
+static int errcb( struct parsing *parsing, 
+            size_t position, const char *message)
+{
+    struct parsinfo info;
+    struct reading *reading = parsing->data;
+
+    /* count the error */
+    reading->errcount++;
+
+    /* print the error */
+    parse_utf8_info( parsing, &info, position);
+    writerror( "%s (file %s line %d)", message, metafilepath, info.lino);
+
+    /* continue to parse */
+    return 0;
+}
+
+/* callback for solving variables */
+static const char *getcb( struct parsing *parsing, 
+            const char *key, size_t length,
+            size_t begin_pos, size_t end_pos)
+{
+    struct parsinfo info;
+    const char *result;
+    size_t offset;
+    struct reading *reading = parsing->data;
+    int id;
+
+    /* try to find a tzplatform variable */
+    id = hashid(key, length);
+    if (id >= 0) {
+        /* found: try to use it */
+        offset = reading->offsets[id];
+        if (offset != HNULL)
+            result = heap_address( &reading->context->heap, offset);
+        else 
+            result = NULL;
+    }
+    else {
+        /* that is a foreign variable */
+        result = foreignvar( reading, key, length);
+    }
+
+    /* emit the error and then return */
+    if (result == NULL) {
+        reading->errcount++;
+        parse_utf8_info( parsing, &info, begin_pos);
+        writerror( "undefined value for %.*s (file %s line %d)",
+            length, key, metafilepath, info.lino);
+        result = emptystring; /* avoid error of the parser */
+    }
+    return result;
+}
+
+/* callback to define variables */
+static int putcb( struct parsing *parsing, 
+            const char *key, size_t key_length, 
+            const char *value, size_t value_length,
+            size_t begin_pos, size_t end_pos)
+{
+    struct parsinfo info;
+    size_t offset;
+    char *string;
+    struct reading *reading = parsing->data;
+    int id;
+
+    /* try to find a tzplatform variable */
+    id = hashid( key, key_length);
+    if (id >= 0) {
+        /* check that the variable isn't already defined */
+        offset = reading->offsets[id];
+        if (offset != HNULL) {
+            reading->errcount++;
+            parse_utf8_info( parsing, &info, begin_pos);
+            writerror( "redefinition of variable %.*s (file %s line %d)",
+                    key_length, key, metafilepath, info.lino);
+        }
+
+        /* allocate the variable value */
+        offset = heap_alloc( &reading->context->heap, value_length+1);
+        if (offset == HNULL) {
+            /* error of allocation */
+            reading->errcount++;
+            writerror( "out of memory");
+        }
+        else {
+            /* record the variable value */
+            reading->offsets[id] = offset;
+            string = heap_address( &reading->context->heap, offset);
+            memcpy( string, value, value_length);
+            string[value_length] = 0;
+        }
+    }
+    else {
+        /* forbidden variable */
+        parse_utf8_info( parsing, &info, begin_pos);
+        writerror( "forbidden variable name %.*s (file %s line %d)",
+            key_length, key, metafilepath, info.lino);
+        
+    }
+
+    /* continue to parse */
+    return 0;
+}
+
+/* initialize the environment */
+inline void initialize(struct tzplatform_context *context)
+{
+    struct buffer buffer;
+    struct parsing parsing;
+    struct reading reading;
+    size_t offset;
+    int i, result;
+
+    /* clear the variables */
+    reading.errcount = 0;
+    reading.context = context;
+    for (i = 0 ; i < (int)_FOREIGN_COUNT_ ; i++) {
+        reading.dynvars[i] = HNULL;
+    }
+    for (i = 0 ; i < (int)_TZPLATFORM_VARIABLES_COUNT_ ; i++) {
+        context->values[i] = NULL;
+        reading.offsets[i] = HNULL;
+    }
+
+    /* read the configuration file */
+    result = buffer_create( &buffer, metafilepath);
+    if (result != 0) {
+        writerror( "can't read file %s",metafilepath);
+        context->state = ERROR;
+        return;
+    }
+
+    /* create the heap */
+    result = heap_create( &context->heap, 1);
+    if (result != 0) {
+        buffer_destroy( &buffer);
+        writerror( "out of memory");
+        context->state = ERROR;
+        return;
+    }
+
+    /* read the file */
+    parsing.buffer = buffer.buffer;
+    parsing.length = buffer.length;
+    parsing.maximum_data_size = 0;
+    parsing.should_escape = 0;
+    parsing.data = &reading;
+    parsing.get = getcb;
+    parsing.put = putcb;
+    parsing.error = errcb;
+    result = parse_utf8_config( &parsing);
+    buffer_destroy( &buffer);
+    if (result != 0 || reading.errcount != 0) {
+        writerror( "%d errors while parsing file %s",
+                                            reading.errcount, metafilepath);
+    }
+
+    /* set the variables */
+    heap_read_only( &context->heap);
+    for (i = 0 ; i < (int)_TZPLATFORM_VARIABLES_COUNT_ ; i++) {
+        offset = reading.offsets[i];
+        if (offset != HNULL)
+            context->values[i] = heap_address( &context->heap, offset);
+        else
+            writerror( "the variable %s isn't defined in file %s",
+                keyname(i), metafilepath);
+            /* TODO undefined variable */;
+    }
+    context->state = VALID;
+}
+
diff --git a/src/init.h b/src/init.h
new file mode 100644 (file)
index 0000000..446431c
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#ifndef INIT_H
+#define INIT_H
+
+inline void initialize(struct tzplatform_context *context);
+
+#endif
+
index 0b959161cb46a4284d01941142741d05dc3dd940..fa8d94d8bc4fbd27bb62f9e5429b30bea8cc97d3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2013-2014 Intel Corporation.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
index d8390f45b05c6fc91cde53d218732dc943bf74c7..0ee30e8779492f3976d792b9b09ee18c7393c89f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2013-2014 Intel Corporation.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
index 5aa32b8741b70905c0ff67de53a81aa73ea9134d..287d375dc0834fb2d2e535807f6abf61c8ffed3f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2013-2014 Intel Corporation.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
index d3c1d1c2d9e3001b1aba3736b6ffc8cc7662c8fa..f9de75c4c0eb75148912c0e769c7afd6ddc1ab7f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2013-2014 Intel Corporation.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
index 7c71da479362d925f32e0ea50b5a301ddfee660b..6f25b544f473a4101ec049ea80f0de00d99a4d51 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2013-2014 Intel Corporation.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
index c571348732e039c76f87a77944049a1277900984..3b15bcb064e0affb0f159b3d91ff4ebcc1a1745e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2013-2014 Intel Corporation.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
diff --git a/src/sha256sum.c b/src/sha256sum.c
new file mode 100644 (file)
index 0000000..9b399d1
--- /dev/null
@@ -0,0 +1,250 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <fcntl.h>
+#include <string.h>
+
+#include "sha256sum.h"
+
+static const char *arg[2] = { "/usr/bin/sha256sum", NULL };
+
+struct sha256sum {
+    enum { RUNNING, FAILED, SUCCESSED } state;
+    pid_t pid;
+    int tofd;
+    int fromfd;
+    char result[32];
+};
+
+struct sha256sum *sha256sum_create()
+{
+    int fds1[2];
+    int fds2[2];
+    pid_t pid;
+    int sts;
+    struct sha256sum *result;
+
+    result = malloc(sizeof * result);
+    if (result == NULL)
+        return NULL;
+
+    sts = pipe(fds1);
+    if (sts != 0) {
+        free(result);
+        return NULL;
+    }
+
+    sts = pipe(fds2);
+    if (sts != 0) {
+        close( fds1[0]);
+        close( fds1[1]);
+        free(result);
+        return NULL;
+    }
+
+    pid = fork();
+    if (pid == -1) {
+        close( fds1[0]);
+        close( fds1[1]);
+        close( fds2[0]);
+        close( fds2[1]);
+        free(result);
+        return NULL;
+    }
+
+    if (pid == 0) {
+        dup2( fds1[0], 0);
+        dup2( fds2[1], 1);
+        close( fds1[0]);
+        close( fds1[1]);
+        close( fds2[0]);
+        close( fds2[1]);
+        execve( arg[0], (char**)arg, environ);
+        exit(1);
+    }
+
+    close( fds1[0]);
+    close( fds2[1]);
+    result->state = RUNNING;
+    result->pid = pid;
+    result->tofd = fds1[1];
+    result->fromfd = fds2[0];
+    return result;
+}
+
+void sha256sum_destroy(struct sha256sum *s)
+{
+    int sts;
+
+    if (s->state == RUNNING) {
+        close(s->fromfd);
+        close(s->tofd);
+        waitpid(s->pid, &sts, 0);
+    }
+    free(s);
+}
+
+int sha256sum_add_data(struct sha256sum *s, const void *data, size_t length)
+{
+    ssize_t sl;
+    int sts;
+
+    if (s->state != RUNNING)
+        return -1;
+
+    while (length) {
+        do {
+            sl = write(s->tofd, data, length);
+        } while (sl == -1 && (errno == EINTR || errno == EAGAIN));
+        if (sl == -1) {
+            s->state = FAILED;
+            close(s->fromfd);
+            close(s->tofd);
+            waitpid(s->pid, &sts, 0);
+            return -1;
+        }
+        length -= (size_t)sl;
+        data = (const void*)((const char*)data + (size_t)sl);
+    }
+    return 0;
+}
+
+int sha256sum_add_file(struct sha256sum *s, const char *filename)
+{
+    char buffer[16384];
+    int fd;
+    ssize_t rd;
+
+    fd = open(filename, O_RDONLY);
+    if (fd < 0)
+        return -1;
+
+    for (;;) {
+        do {
+            rd = read(fd, buffer, sizeof buffer);
+        } while (rd == -1 && (errno == EINTR || errno == EAGAIN));
+        if (rd == -1) {
+            close(fd);
+            return -1;
+        }
+        if (rd == 0) {
+            close(fd);
+            return 0;
+        }
+        if (sha256sum_add_data(s, buffer, (size_t)rd)) {
+            close(fd);
+            return -1;
+        }
+    }
+}
+
+int sha256sum_get(struct sha256sum *s, char result[32])
+{
+    int sts;
+    int j;
+    char buffer[65];
+    char c1;
+    char c2;
+
+    if (s->state == RUNNING) {
+        s->state = FAILED;
+        close(s->tofd);
+        waitpid(s->pid, &sts, 0);
+        if (WIFEXITED(sts) && WEXITSTATUS(sts) == 0) {
+            sts = read(s->fromfd, buffer, 65);
+            if (sts == 65 && buffer[64] == ' ') {
+                s->state = SUCCESSED;
+                for (j = 0 ; j < 32 ; j++) {
+                    c1 = buffer[j+j];
+                    if ('0' <= c1 && c1 <= '9')
+                        c1 = c1 - '0';
+                    else if ('a' <= c1 && c1 <= 'f')
+                        c1 = c1 - 'a' + '\012';
+                    else if ('A' <= c1 && c1 <= 'F')
+                        c1 = c1 - 'A' + '\012';
+                    else {
+                        s->state = FAILED;
+                        break;
+                    }
+                    c2 = buffer[j+j+1];
+                    if ('0' <= c2 && c2 <= '9')
+                        c2 = c2 - '0';
+                    else if ('a' <= c2 && c2 <= 'f')
+                        c2 = c2 - 'a' + '\012';
+                    else if ('A' <= c2 && c2 <= 'F')
+                        c2 = c2 - 'A' + '\012';
+                    else {
+                        s->state = FAILED;
+                        break;
+                    }
+                    s->result[j] = (c1 << 4) | c2;
+                }
+            }
+        }
+        close(s->fromfd);
+    }
+
+    if (s->state == FAILED)
+        return -1;
+
+    memcpy(result, s->result, 32);
+
+    return 0;
+}
+
+#ifdef TEST
+#include <stdio.h>
+#include <assert.h>
+int main(int argc, char **argv)
+{
+    char sum[32];
+    int j, r;
+    struct sha256sum *s;
+
+    while (*++argv) {
+        s = sha256sum_create();
+        assert(s != NULL);
+        r = sha256sum_add_file(s, *argv);
+        assert(r == 0);
+        r = sha256sum_get(s, sum);
+        assert(r == 0);
+        sha256sum_destroy(s);
+        for (j=0 ; j < 32 ; j++)
+            printf("%02x", (int)(unsigned char)sum[j]);
+        printf("  %s\n", *argv);
+    }
+    return 0;
+}
+#endif
+
diff --git a/src/sha256sum.h b/src/sha256sum.h
new file mode 100644 (file)
index 0000000..9494594
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#ifndef SHA256SUM_H
+#define SHA256SUM_H
+
+struct sha256sum;
+
+struct sha256sum *sha256sum_create();
+void sha256sum_destroy(struct sha256sum *s);
+int sha256sum_add_data(struct sha256sum *s, const void *data, size_t length);
+int sha256sum_add_file(struct sha256sum *s, const char *filename);
+int sha256sum_get(struct sha256sum *s, char result[32]);
+
+#endif
diff --git a/src/shared-api.c b/src/shared-api.c
new file mode 100644 (file)
index 0000000..742e69a
--- /dev/null
@@ -0,0 +1,386 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <assert.h>
+#include <syslog.h>
+
+#ifndef NOT_MULTI_THREAD_SAFE
+#include <pthread.h>
+#endif
+
+#include "tzplatform_variables.h"
+#include "tzplatform_config.h"
+#include "heap.h"
+#include "scratch.h"
+#include "passwd.h"
+#include "foreign.h"
+#include "context.h"
+#include "hashing.h"
+#include "init.h"
+#include "shared-api.h"
+
+
+/* the global context */
+static struct tzplatform_context global_context = {
+#ifndef NOT_MULTI_THREAD_SAFE
+    .mutex = PTHREAD_MUTEX_INITIALIZER,
+#endif
+    .state = RESET,
+    .user = _USER_NOT_SET_
+};
+
+/* the signup of names */
+#include "signup.inc"
+
+/* validate the signup */
+static void validate_signup(char signup[33])
+{
+    if (memcmp(signup+1, tizen_platform_config_signup+1, 32)) {
+        syslog(LOG_CRIT, "Bad signup of the client of tizen-platform-config");
+        abort();
+    }
+    signup[0] = 1;
+}
+
+/* check the signup */
+static inline void check_signup(char signup[33])
+{
+    if (!signup[0])
+        validate_signup(signup);
+}
+
+/* locks the context */
+inline static void lock(struct tzplatform_context *context)
+{
+#ifndef NOT_MULTI_THREAD_SAFE
+    pthread_mutex_lock( &context->mutex);
+#endif
+}
+
+/* unlock the context */
+inline static void unlock(struct tzplatform_context *context)
+{
+#ifndef NOT_MULTI_THREAD_SAFE
+    pthread_mutex_unlock( &context->mutex);
+#endif
+}
+
+static inline const char *get_lock(int id, struct tzplatform_context *context)
+{
+    lock( context);
+
+    if (id < 0 || (int)_TZPLATFORM_VARIABLES_COUNT_ <= id)
+        return NULL;
+
+    if (context->state == RESET)
+        initialize( context);
+
+    return context->state == ERROR ? NULL : context->values[id];
+}
+
+/*************** PUBLIC API begins here **************/
+
+int tzplatform_context_create(struct tzplatform_context **result)
+{
+    struct tzplatform_context *context;
+
+    context = malloc( sizeof * context);
+    *result = context;
+    if (context == NULL)
+        return -1;
+
+    context->state = RESET;
+    context->user = _USER_NOT_SET_;
+#ifndef NOT_MULTI_THREAD_SAFE
+    pthread_mutex_init( &context->mutex, NULL);
+#endif
+    return 0;
+}
+
+void tzplatform_context_destroy(struct tzplatform_context *context)
+{
+    if (context->state == VALID)
+            heap_destroy( &context->heap);
+    context->state = ERROR;
+    free( context);
+}
+
+void tzplatform_reset()
+{
+    tzplatform_context_reset(&global_context);
+}
+
+void tzplatform_context_reset(struct tzplatform_context *context)
+{
+    lock( context);
+    if (context->state != RESET) {
+        if (context->state == VALID)
+            heap_destroy( &context->heap);
+        context->state = RESET;
+    }
+    unlock( context);
+}
+
+uid_t tzplatform_get_user(char signup[33])
+{
+    return tzplatform_context_get_user( &global_context);
+}
+
+uid_t tzplatform_context_get_user(struct tzplatform_context *context)
+{
+    uid_t result;
+
+    lock( context);
+    result = get_uid( context);
+    unlock( context);
+
+    return result;
+}
+
+void tzplatform_reset_user()
+{
+    tzplatform_context_reset_user( &global_context);
+}
+
+void tzplatform_context_reset_user(struct tzplatform_context *context)
+{
+    tzplatform_context_set_user( context, _USER_NOT_SET_);
+}
+
+int tzplatform_set_user(uid_t uid)
+{
+    return tzplatform_context_set_user( &global_context, uid);
+}
+
+int tzplatform_context_set_user(struct tzplatform_context *context, uid_t uid)
+{
+    lock( context);
+    if (context->user != uid) {
+        if (uid != _USER_NOT_SET_ && !pw_has_uid( uid)) {
+            unlock( context);
+            return -1;
+       }
+        else {
+            if (context->state == VALID)
+                heap_destroy( &context->heap);
+            context->state = RESET;
+            context->user = uid;
+        }
+    }
+    unlock( context);
+
+    return 0;
+}
+
+/*************** PUBLIC INTERNAL API begins here **************/
+
+const char* _getname_tzplatform_(int id, char signup[33])
+{
+    check_signup(signup);
+    return 0 <= id && id < _TZPLATFORM_VARIABLES_COUNT_ ? keyname(id) : NULL;
+}
+
+int _getid_tzplatform_(const char *name, char signup[33])
+{
+    check_signup(signup);
+    return hashid(name, strlen(name));
+}
+
+const char* _getenv_tzplatform_(int id, char signup[33]) 
+{
+    return _context_getenv_tzplatform_(id, signup, &global_context);
+}
+
+const char* _context_getenv_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
+{
+    const char *array[2];
+    const char *result;
+
+    check_signup(signup);
+    result = get_lock(id, context);
+    if (result != NULL) {
+        array[0] = result;
+        array[1] = NULL;
+        result = scratchcat( 0, array);
+    }
+    unlock( context);
+    return result;
+}
+
+int _getenv_int_tzplatform_(int id, char signup[33])
+{
+    return _context_getenv_int_tzplatform_(id, signup, &global_context);
+}
+
+int _context_getenv_int_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
+{
+    const char *value;
+    int result;
+
+    check_signup(signup);
+    value = get_lock(id, context);
+    result = value==NULL ? -1 : atoi(value);
+    unlock( context);
+    return result;
+}
+
+const char* _mkstr_tzplatform_(int id, const char * str, char signup[33])
+{
+    return _context_mkstr_tzplatform_(id, str, signup,  &global_context);
+}
+
+const char* _context_mkstr_tzplatform_(int id, const char *str, char signup[33], struct tzplatform_context *context)
+{
+    const char *array[3];
+    const char *result;
+
+    check_signup(signup);
+    result = get_lock(id, context);
+    if (result != NULL) {
+        array[0] = result;
+        array[1] = str;
+        array[2] = NULL;
+        result = scratchcat( 0, array);
+    }
+    unlock( context);
+    return result;
+}
+
+const char* _mkpath_tzplatform_(int id, const char * path, char signup[33])
+{
+    return _context_mkpath_tzplatform_(id, path, signup,  &global_context);
+}
+
+const char* _context_mkpath_tzplatform_(int id, const char *path, char signup[33], struct tzplatform_context *context)
+{
+    const char *array[3];
+    const char *result;
+
+    check_signup(signup);
+    result = get_lock(id, context);
+    if (result != NULL) {
+        array[0] = result;
+        array[1] = path;
+        array[2] = NULL;
+        result = scratchcat( 1, array);
+    }
+    unlock( context);
+    return result;
+}
+
+const char* _mkpath3_tzplatform_(int id, const char * path, const char* path2, char signup[33])
+{
+    return _context_mkpath3_tzplatform_( id, path, path2, signup,  &global_context);
+}
+
+const char* _context_mkpath3_tzplatform_(int id, const char *path, const char *path2, char signup[33], struct tzplatform_context *context)
+{
+    const char *array[4];
+    const char *result;
+
+    check_signup(signup);
+    result = get_lock(id, context);
+    if (result != NULL) {
+        array[0] = result;
+        array[1] = path;
+        array[2] = path2;
+        array[3] = NULL;
+        result = scratchcat( 1, array);
+    }
+    unlock( context);
+    return result;
+}
+
+const char* _mkpath4_tzplatform_(int id, const char * path, const char* path2, const char *path3, char signup[33])
+{
+    return _context_mkpath4_tzplatform_( id, path, path2, path3, signup,  &global_context);
+}
+
+const char* _context_mkpath4_tzplatform_(int id, const char *path, const char *path2, const char *path3, char signup[33], struct tzplatform_context *context)
+{
+    const char *array[5];
+    const char *result;
+
+    check_signup(signup);
+    result = get_lock(id, context);
+    if (result != NULL) {
+        array[0] = result;
+        array[1] = path;
+        array[2] = path2;
+        array[3] = path3;
+        array[4] = NULL;
+        result = scratchcat( 1, array);
+    }
+    unlock( context);
+    return result;
+}
+
+uid_t _getuid_tzplatform_(int id, char signup[33])
+{
+    return _context_getuid_tzplatform_( id, signup,  &global_context);
+}
+
+uid_t _context_getuid_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
+{
+    uid_t result;
+    const char *value;
+
+    check_signup(signup);
+    result = (uid_t)-1;
+    value = get_lock(id, context);
+    if (value != NULL) {
+        pw_get_uid( value, &result);
+    }
+    unlock( context);
+    return result;
+}
+
+gid_t _getgid_tzplatform_(int id, char signup[33])
+{
+    return _context_getgid_tzplatform_( id, signup,  &global_context);
+}
+
+gid_t _context_getgid_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
+{
+    gid_t result;
+    const char *value;
+
+    check_signup(signup);
+    result = (uid_t)-1;
+    value = get_lock(id, context);
+    if (value != NULL) {
+        pw_get_gid( value, &result);
+    }
+    unlock( context);
+    return result;
+}
+
diff --git a/src/shared-api.h b/src/shared-api.h
new file mode 100644 (file)
index 0000000..8735026
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+
+#ifndef SHARED_API_H
+#define SHARED_API_H
+
+extern const char* _getname_tzplatform_(int id, char signup[33]);
+extern int _getid_tzplatform_(const char *name, char signup[33]);
+extern const char* _getenv_tzplatform_(int id, char signup[33]) ;
+extern const char* _context_getenv_tzplatform_(int id, char signup[33], struct tzplatform_context *context);
+extern int _getenv_int_tzplatform_(int id, char signup[33]);
+extern int _context_getenv_int_tzplatform_(int id, char signup[33], struct tzplatform_context *context);
+extern const char* _mkstr_tzplatform_(int id, const char * str, char signup[33]);
+extern const char* _context_mkstr_tzplatform_(int id, const char *str, char signup[33], struct tzplatform_context *context);
+extern const char* _mkpath_tzplatform_(int id, const char * path, char signup[33]);
+extern const char* _context_mkpath_tzplatform_(int id, const char *path, char signup[33], struct tzplatform_context *context);
+extern const char* _mkpath3_tzplatform_(int id, const char * path, const char* path2, char signup[33]);
+extern const char* _context_mkpath3_tzplatform_(int id, const char *path, const char *path2, char signup[33], struct tzplatform_context *context);
+extern const char* _mkpath4_tzplatform_(int id, const char * path, const char* path2, const char *path3, char signup[33]);
+extern const char* _context_mkpath4_tzplatform_(int id, const char *path, const char *path2, const char *path3, char signup[33], struct tzplatform_context *context);
+extern uid_t _getuid_tzplatform_(int id, char signup[33]);
+extern uid_t _context_getuid_tzplatform_(int id, char signup[33], struct tzplatform_context *context);
+extern gid_t _getgid_tzplatform_(int id, char signup[33]);
+extern gid_t _context_getgid_tzplatform_(int id, char signup[33], struct tzplatform_context *context);
+
+#endif
+
diff --git a/src/static-api.c b/src/static-api.c
new file mode 100644 (file)
index 0000000..054d093
--- /dev/null
@@ -0,0 +1,184 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <unistd.h>
+#include "tzplatform_variables.h"
+#include "tzplatform_config.h"
+
+#include "shared-api.h"
+
+#include "signup.inc"
+
+int tzplatform_getcount()
+{
+    return _TZPLATFORM_VARIABLES_COUNT_;
+}
+
+const char* tzplatform_getname(enum tzplatform_variable id)
+{
+    return _getname_tzplatform_(id, tizen_platform_config_signup);
+}
+
+enum tzplatform_variable tzplatform_getid(const char *name)
+{
+    return _getid_tzplatform_(name, tizen_platform_config_signup);
+}
+
+const char* tzplatform_getenv(enum tzplatform_variable id) 
+{
+    return _getenv_tzplatform_(id, tizen_platform_config_signup);
+}
+
+const char* tzplatform_context_getenv(struct tzplatform_context *context, enum tzplatform_variable id)
+{
+    return _context_getenv_tzplatform_(id, tizen_platform_config_signup, context);
+}
+
+int tzplatform_getenv_int(enum tzplatform_variable id)
+{
+    return _getenv_int_tzplatform_(id, tizen_platform_config_signup);
+}
+
+int tzplatform_context_getenv_int(struct tzplatform_context *context, enum tzplatform_variable id)
+{
+    return _context_getenv_int_tzplatform_(id, tizen_platform_config_signup, context);
+}
+
+const char* tzplatform_mkstr(enum tzplatform_variable id, const char * str)
+{
+    return _mkstr_tzplatform_(id, str, tizen_platform_config_signup);
+}
+
+const char* tzplatform_context_mkstr(struct tzplatform_context *context, enum tzplatform_variable id, const char *str)
+{
+    return _context_mkstr_tzplatform_(id, str, tizen_platform_config_signup, context);
+}
+
+const char* tzplatform_mkpath(enum tzplatform_variable id, const char * path)
+{
+    return _mkpath_tzplatform_(id, path, tizen_platform_config_signup);
+}
+
+const char* tzplatform_context_mkpath(struct tzplatform_context *context, enum tzplatform_variable id, const char *path)
+{
+    return _context_mkpath_tzplatform_(id, path, tizen_platform_config_signup, context);
+}
+
+const char* tzplatform_mkpath3(enum tzplatform_variable id, const char * path, const char* path2)
+{
+    return _mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup);
+}
+
+const char* tzplatform_context_mkpath3(struct tzplatform_context *context, enum tzplatform_variable id, const char *path, const char *path2)
+{
+    return _context_mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup, context);
+}
+
+const char* tzplatform_mkpath4(enum tzplatform_variable id, const char * path, const char* path2, const char *path3)
+{
+    return _mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup);
+}
+
+const char* tzplatform_context_mkpath4(struct tzplatform_context *context, enum tzplatform_variable id, const char *path, const char *path2, const char *path3)
+{
+    return _context_mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup, context);
+}
+
+uid_t tzplatform_getuid(enum tzplatform_variable id)
+{
+    return _getuid_tzplatform_(id, tizen_platform_config_signup);
+}
+
+uid_t tzplatform_context_getuid(struct tzplatform_context *context, enum tzplatform_variable id)
+{
+    return _context_getuid_tzplatform_(id, tizen_platform_config_signup, context);
+}
+
+gid_t tzplatform_getgid(enum tzplatform_variable id)
+{
+    return _getgid_tzplatform_(id, tizen_platform_config_signup);
+}
+
+gid_t tzplatform_context_getgid(struct tzplatform_context *context, enum tzplatform_variable id)
+{
+    return _context_getgid_tzplatform_(id, tizen_platform_config_signup, context);
+}
+
+#ifdef TEST
+#include <stdlib.h>
+#include <stdio.h>
+
+int main() {
+    int i;
+    struct tzplatform_context *context;
+    enum tzplatform_variable id;
+    const char *name;
+    const char *value;
+    int xid;
+    uid_t uid;
+
+    i = 0;
+    while(i != tzplatform_getcount()) {
+        id = (enum tzplatform_variable)i;
+        name = tzplatform_getname(id);
+        value = tzplatform_getenv(id);
+        xid = (int)tzplatform_getid(name);
+        printf("%d=%d\t%s=%s\n",i,xid,name,value?value:"<null>");
+        i++;
+    }
+
+    printf("------------------------\n");
+    i = tzplatform_context_create(&context);
+    if (i) {
+        printf("error while creating context %d\n",i);
+        return 1;
+    }
+
+    uid = (uid_t)0;
+    i = tzplatform_context_set_user(context, uid);
+    if (i) {
+        printf("error %d while switching to user %d\n",i,(int)uid);
+        return 1;
+    }
+    i = 0;
+    while(i != tzplatform_getcount()) {
+        id = (enum tzplatform_variable)i;
+        name = tzplatform_getname(id);
+        value = tzplatform_context_getenv(context, id);
+        xid = (int)tzplatform_getid(name);
+        printf("%d=%d\t%s=%s\n",i,xid,name,value?value:"<null>");
+        i++;
+    }
+    tzplatform_context_destroy(context);
+
+    return 0;
+}
+#endif
+
+
index bc7911dbfd1341b51327b11dde664621d9caeda3..0c48f362576ccab3d20c3146a297a15b0c8c529e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2013-2014 Intel Corporation.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -38,6 +38,7 @@
 #include "heap.h"
 #include "buffer.h"
 #include "foreign.h"
+#include "sha256sum.h"
 
 /*======================================================================*/
 
@@ -87,6 +88,7 @@ pretty     Pretty print of the 'file' (the normalized format)\n\
 h          Produce the C header with the enumeration of the variables\n\
 c          Produce the C code to hash the variable names\n\
 rpm        Produce the macro file to use with RPM\n\
+signup     Produce the signup data for the proxy linked statically\n\
 \n\
 ";
 
@@ -113,7 +115,7 @@ static char genh_tail[] = "\
 static char gperf_head[] = "\
 struct varassoc {\n\
   int offset;\n\
-  enum tzplatform_variable id;\n\
+  int id;\n\
 };\n\
 %%\n\
 ";
@@ -132,6 +134,16 @@ static char rpm_head[] = "\
 \n\
 ";
 
+static char signup_head[] = "\
+/* I'm generated. Dont edit me! */\n\
+static char tizen_platform_config_signup[33] = {\n\
+    '\\x00',\n\
+";
+
+static char signup_tail[] = "\
+  };\n\
+";
+
 /*== GLOBALS VARIABLES =================================================*/
 
 /* name of the meta file to process */
@@ -150,7 +162,7 @@ static int errcount = 0;
 static int dependant = 0;
 
 /* action to perform */
-static enum { CHECK, PRETTY, GENC, GENH, RPM } action = CHECK;
+static enum { CHECK, PRETTY, GENC, GENH, RPM, SIGNUP } action = CHECK;
 
 /* output of error */
 static int notstderr = 0;
@@ -477,6 +489,51 @@ static const char *getcb( struct parsing *parsing,
 
 /*======================================================================*/
 
+/* compare two keys */
+static int keycmp(const void *a, const void *b)
+{
+    const struct key *ka = *(const struct key **)a;
+    const struct key *kb = *(const struct key **)b;
+    return strcmp(ka->name, kb->name);
+}
+
+/* sort the keys and return their count */
+static int sortkeys()
+{
+    struct key *key = keys, **array;
+    int count = 0, index;
+
+    while (key) {
+        key = key->next;
+        count++;
+    }
+
+    array = malloc( count * sizeof * array);
+    if (array == NULL)
+        return -1;
+
+    key = keys;
+    index = 0;
+    
+    while (key) {
+        array[index++] = key;
+        key = key->next;
+    }
+
+    qsort(array, count, sizeof * array, keycmp);
+
+    while (index) {
+       array[--index]->next = key;
+        key = array[index];
+    }
+    keys = key;
+    free( array);
+
+    return count;
+}
+
+/*======================================================================*/
+
 /* pretty print the read file */
 static int pretty( const char *buffer, size_t length, FILE *output)
 {
@@ -512,6 +569,12 @@ static int genh( FILE *output)
     struct key *key;
     int status;
 
+#ifndef NO_SORT_KEYS
+    status = sortkeys();
+    if (status < 0)
+        return status;
+#endif
+
     status = fprintf( output, "%s", genh_head);
     if (status < 0)
         return status;
@@ -535,6 +598,12 @@ static int genc(FILE *output)
     int result, sts;
     size_t l;
 
+#ifndef NO_SORT_KEYS
+    sts = sortkeys();
+    if (sts < 0)
+        return sts;
+#endif
+
     result = pipe(fds);
     if (result != 0)
         return result;
@@ -592,6 +661,12 @@ static int rpm( FILE *output)
     struct key *key;
     int status;
 
+#ifndef NO_SORT_KEYS
+    status = sortkeys();
+    if (status < 0)
+        return status;
+#endif
+
     status = fprintf( output, "%s", rpm_head);
     if (status < 0)
         return status;
@@ -605,6 +680,58 @@ static int rpm( FILE *output)
     return 0;
 }
 
+/* generate the signup */
+static int signup( FILE *output)
+{
+    struct key *key;
+    int status;
+    int i;
+    struct sha256sum *sum;
+    char term;
+    char signup[32];
+
+#ifndef NO_SORT_KEYS
+    status = sortkeys();
+    if (status < 0)
+        return status;
+#endif
+
+    sum = sha256sum_create();
+    if (sum == NULL)
+        return -1;
+
+    term = ';';
+    for (key = keys ; key != NULL ; key = key->next) {
+        status = sha256sum_add_data(sum, key->name, strlen(key->name));
+        if (status < 0)
+            return status;
+        status = sha256sum_add_data(sum, &term, 1);
+        if (status < 0)
+            return status;
+    }
+
+    status = sha256sum_get(sum, signup);
+    if (status < 0)
+        return status;
+
+    status = fprintf( output, "%s", signup_head);
+    if (status < 0)
+        return status;
+
+    for (i=0 ; i<32 ; i++) {
+        status = fprintf( output, "%s'\\x%02x'%s",
+                    (i & 7) ? " " : "    ",
+                    (int)(unsigned char)signup[i],
+                    (i & 7) < 7 ? "," : i == 31 ? "\n" : ",\n");
+        if (status < 0)
+            return status;
+    }
+    status = fprintf( output, "%s", signup_tail);
+    if (status < 0)
+        return status;
+    return 0;
+}
+
 /* main of processing */
 static int process()
 {
@@ -657,6 +784,9 @@ static int process()
     case RPM:
         rpm( stdout);
         break;
+    case SIGNUP:
+        signup( stdout);
+        break;
     }
 
     buffer_destroy( &buffer);
@@ -699,7 +829,11 @@ static int arguments( char **argv)
             action = RPM;
             argv++;
         }
-        else if (0 == strcmp( *argv, "help")) {
+        else if (0 == strcmp( *argv, "signup")) {
+            action = SIGNUP;
+            argv++;
+        }
+        else if (0 == strcmp( *argv, "help") || 0 == strcmp( *argv, "--help")) {
             printf("%s", help);
             exit(0);
             return -1;
diff --git a/src/tzplatform_config.c b/src/tzplatform_config.c
deleted file mode 100644 (file)
index 2b27401..0000000
+++ /dev/null
@@ -1,841 +0,0 @@
-/*
- * Copyright (C) 2013 Intel Corporation.
- * 
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
- *
- * Authors:
- *   José Bollo <jose.bollo@open.eurogiciel.org>
- *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
- *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
- *
- */
-#define _GNU_SOURCE
-
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdint.h>
-#include <string.h>
-#include <pthread.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <ctype.h>
-#include <stdarg.h>
-#include <alloca.h>
-#include <pwd.h>
-
-#ifndef NOT_MULTI_THREAD_SAFE
-#include <pthread.h>
-#endif
-
-#ifndef CONFIGPATH
-#define CONFIGPATH "/etc/tizen-platform.conf"
-#endif
-
-#include "tzplatform_variables.h"
-#include "tzplatform_config.h"
-#include "parser.h"
-#include "heap.h"
-#include "buffer.h"
-#include "foreign.h"
-#include "scratch.h"
-#include "passwd.h"
-
-#define _HAS_IDS_   (  _FOREIGN_HAS_(UID)  \
-                    || _FOREIGN_HAS_(EUID) \
-                    || _FOREIGN_HAS_(GID)  )
-
-#define _HAS_PWS_   (  _FOREIGN_HAS_(HOME)  \
-                    || _FOREIGN_HAS_(USER)  \
-                    || _FOREIGN_HAS_(EHOME) \
-                    || _FOREIGN_HAS_(EUSER) )
-
-#define _USER_NOT_SET_  ((uid_t)-1)
-
-/* local and static variables */
-static const char metafilepath[] = CONFIGPATH;
-static const char emptystring[] = "";
-static const char *var_names[_TZPLATFORM_VARIABLES_COUNT_];
-
-enum STATE { RESET=0, ERROR, VALID };
-
-struct tzplatform_context {
-#ifndef NOT_MULTI_THREAD_SAFE
-    pthread_mutex_t mutex;
-#endif
-    enum STATE state;
-    uid_t user;
-    struct heap heap;
-    const char *values[_TZPLATFORM_VARIABLES_COUNT_];
-};
-
-/* structure for reading config files */
-struct reading {
-    int errcount;
-    struct tzplatform_context *context;
-    size_t dynvars[_FOREIGN_COUNT_];
-    size_t offsets[_TZPLATFORM_VARIABLES_COUNT_];
-};
-
-static struct tzplatform_context global_context = {
-#ifndef NOT_MULTI_THREAD_SAFE
-    .mutex = PTHREAD_MUTEX_INITIALIZER,
-#endif
-    .state = RESET,
-    .user = _USER_NOT_SET_
-};
-
-#include "hash.inc"
-
-/* write the error message */
-static void writerror( const char *format, ...)
-{
-    va_list ap;
-    fprintf( stderr, "tzplatform_config ERROR: ");
-    va_start(ap, format);
-    vfprintf(stderr, format, ap);
-    va_end(ap);
-    fprintf( stderr, "\n");
-}
-
-static uid_t get_uid(struct tzplatform_context *context)
-{
-    uid_t result;
-
-    result = context->user;
-    if (result == _USER_NOT_SET_)
-        result = getuid();
-
-    return result;
-}
-
-#if _FOREIGN_HAS_(EUID)
-static uid_t get_euid(struct tzplatform_context *context)
-{
-    uid_t result;
-
-    result = context->user;
-    if (result == _USER_NOT_SET_)
-        result = geteuid();
-
-    return result;
-}
-#endif
-
-#if _FOREIGN_HAS_(GID)
-static gid_t get_gid(struct tzplatform_context *context)
-{
-    return getgid();
-}
-#endif
-
-#if _HAS_IDS_
-/* fill the foreign variables for ids */
-static void foreignid( struct reading *reading)
-{
-    int n;
-    char buffer[50];
-
-#if _FOREIGN_HAS_(UID)
-    /* set the uid */
-    if (reading->dynvars[UID] == HNULL) {
-        n = snprintf( buffer, sizeof buffer, "%d", (int)get_uid(reading->context));
-        if (0 < n && n < (int)(sizeof buffer))
-            reading->dynvars[UID] = heap_strndup( &reading->context->heap, buffer, (size_t)n);
-    }
-#endif
-
-#if _FOREIGN_HAS_(EUID)
-    /* set the euid */
-    if (reading->dynvars[EUID] == HNULL) {
-        n = snprintf( buffer, sizeof buffer, "%d", (int)get_euid(reading->context));
-        if (0 < n && n < (int)(sizeof buffer))
-            reading->dynvars[EUID] = heap_strndup( &reading->context->heap, buffer, (size_t)n);
-    }
-#endif
-
-#if _FOREIGN_HAS_(GID)
-    /* set the gid */
-    if (reading->dynvars[GID] == HNULL) {
-        n = snprintf( buffer, sizeof buffer, "%d", (int)get_gid(reading->context));
-        if (0 < n && n < (int)(sizeof buffer))
-            reading->dynvars[GID] = heap_strndup( &reading->context->heap, buffer, (size_t)n);
-    }
-#endif
-}
-#endif
-
-#if _HAS_PWS_
-/* fill the foreign variables for home and user */
-static void foreignpw( struct reading *reading)
-{
-    int n = 0;
-    struct pwget *array[3];
-#if _FOREIGN_HAS_(HOME) || _FOREIGN_HAS_(USER)
-    struct pwget uid;
-    char suid[50];
-#endif
-#if _FOREIGN_HAS_(EHOME) || _FOREIGN_HAS_(EUSER)
-    struct pwget euid;
-    char seuid[50];
-#endif
-
-#if _FOREIGN_HAS_(HOME) || _FOREIGN_HAS_(USER)
-    if (
-#if _FOREIGN_HAS_(HOME)
-        reading->dynvars[HOME] == HNULL
-#endif
-#if _FOREIGN_HAS_(HOME) && _FOREIGN_HAS_(USER)
-        ||
-#endif
-#if _FOREIGN_HAS_(USER)
-        reading->dynvars[USER] == HNULL
-#endif
-    ) {
-        snprintf( suid, sizeof suid, "%u", (unsigned)get_uid(reading->context));
-        uid.id = suid;
-        array[n++] = &uid;
-    }
-    else {
-        uid.set = 0;
-    }
-#endif
-
-#if _FOREIGN_HAS_(EHOME) || _FOREIGN_HAS_(EUSER)
-    if (
-#if _FOREIGN_HAS_(EHOME)
-        reading->dynvars[EHOME] == HNULL
-#endif
-#if _FOREIGN_HAS_(EHOME) && _FOREIGN_HAS_(USER)
-        ||
-#endif
-#if _FOREIGN_HAS_(EUSER)
-        reading->dynvars[EUSER] == HNULL
-#endif
-    ) {
-        snprintf( seuid, sizeof seuid, "%u", (unsigned)get_euid(reading->context));
-        euid.id = seuid;
-        array[n++] = &euid;
-    }
-    else {
-        euid.set = 0;
-    }
-#endif
-
-    if (n) {
-        array[n] = NULL;
-        if (pw_get( &reading->context->heap, array) == 0) {
-#if _FOREIGN_HAS_(HOME)
-            if (uid.set)
-                reading->dynvars[HOME] = uid.home;
-#endif
-#if _FOREIGN_HAS_(USER)
-            if (uid.set)
-                reading->dynvars[USER] = uid.user;
-#endif
-#if _FOREIGN_HAS_(EHOME)
-            if (euid.set)
-                reading->dynvars[EHOME] = euid.home;
-#endif
-#if _FOREIGN_HAS_(EUSER)
-            if (euid.set)
-                reading->dynvars[EUSER] = euid.user;
-#endif
-        }
-    }
-}
-#endif
-
-/* get the foreign variable */
-static const char *foreignvar( struct reading *reading, 
-                                            const char *name, size_t length)
-{
-    enum fkey key = foreign( name, length);
-    size_t offset;
-
-    switch (key) {
-#if _HAS_PWS_
-#if _FOREIGN_HAS_(HOME)
-    case HOME:
-#endif
-#if _FOREIGN_HAS_(USER)
-    case USER:
-#endif
-#if _FOREIGN_HAS_(EHOME)
-    case EHOME:
-#endif
-#if _FOREIGN_HAS_(EUSER)
-    case EUSER:
-#endif
-        foreignpw( reading);
-        break;
-#endif
-#if _HAS_IDS_
-#if _FOREIGN_HAS_(UID)
-    case UID:
-#endif
-#if _FOREIGN_HAS_(GID)
-    case GID:
-#endif
-#if _FOREIGN_HAS_(EUID)
-    case EUID:
-#endif
-        foreignid( reading);
-        break;
-#endif
-
-    default:
-        return NULL;
-    }
-    offset = reading->dynvars[key];
-    return offset==HNULL ? NULL : heap_address( &reading->context->heap, offset);
-}
-
-/* callback for parsing errors */
-static int errcb( struct parsing *parsing, 
-            size_t position, const char *message)
-{
-    struct parsinfo info;
-    struct reading *reading = parsing->data;
-
-    /* count the error */
-    reading->errcount++;
-
-    /* print the error */
-    parse_utf8_info( parsing, &info, position);
-    writerror( "%s (file %s line %d)", message, metafilepath, info.lino);
-
-    /* continue to parse */
-    return 0;
-}
-
-/* callback for solving variables */
-static const char *getcb( struct parsing *parsing, 
-            const char *key, size_t length,
-            size_t begin_pos, size_t end_pos)
-{
-    struct parsinfo info;
-    const char *result;
-    const struct varassoc *vara;
-    size_t offset;
-    struct reading *reading = parsing->data;
-
-    /* try to find a tzplatform variable */
-    vara = hashvar( key, length);
-    if (vara) {
-        /* found: try to use it */
-        offset = reading->offsets[(int)vara->id];
-        if (offset != HNULL)
-            result = heap_address( &reading->context->heap, offset);
-        else 
-            result = NULL;
-    }
-    else {
-        /* that is a foreign variable */
-        result = foreignvar( reading, key, length);
-    }
-
-    /* emit the error and then return */
-    if (result == NULL) {
-        reading->errcount++;
-        parse_utf8_info( parsing, &info, begin_pos);
-        writerror( "undefined value for %.*s (file %s line %d)",
-            length, key, metafilepath, info.lino);
-        result = emptystring; /* avoid error of the parser */
-    }
-    return result;
-}
-
-/* callback to define variables */
-static int putcb( struct parsing *parsing, 
-            const char *key, size_t key_length, 
-            const char *value, size_t value_length,
-            size_t begin_pos, size_t end_pos)
-{
-    struct parsinfo info;
-    const struct varassoc *vara;
-    size_t offset;
-    char *string;
-    struct reading *reading = parsing->data;
-
-    /* try to find a tzplatform variable */
-    vara = hashvar( key, key_length);
-    if (vara) {
-        /* check that the variable isn't already defined */
-        offset = reading->offsets[(int)vara->id];
-        if (offset != HNULL) {
-            reading->errcount++;
-            parse_utf8_info( parsing, &info, begin_pos);
-            writerror( "redefinition of variable %.*s (file %s line %d)",
-                    key_length, key, metafilepath, info.lino);
-        }
-
-        /* allocate the variable value */
-        offset = heap_alloc( &reading->context->heap, value_length+1);
-        if (offset == HNULL) {
-            /* error of allocation */
-            reading->errcount++;
-            writerror( "out of memory");
-        }
-        else {
-            /* record the variable value */
-            reading->offsets[(int)vara->id] = offset;
-            string = heap_address( &reading->context->heap, offset);
-            memcpy( string, value, value_length);
-            string[value_length] = 0;
-        }
-    }
-    else {
-        /* forbidden variable */
-        parse_utf8_info( parsing, &info, begin_pos);
-        writerror( "forbidden variable name %.*s (file %s line %d)",
-            key_length, key, metafilepath, info.lino);
-        
-    }
-
-    /* continue to parse */
-    return 0;
-}
-
-/* initialize the environment */
-static void initialize(struct tzplatform_context *context)
-{
-    struct buffer buffer;
-    struct parsing parsing;
-    struct reading reading;
-    size_t offset;
-    int i, result;
-
-    /* clear the variables */
-    reading.errcount = 0;
-    reading.context = context;
-    for (i = 0 ; i < (int)_FOREIGN_COUNT_ ; i++) {
-        reading.dynvars[i] = HNULL;
-    }
-    for (i = 0 ; i < (int)_TZPLATFORM_VARIABLES_COUNT_ ; i++) {
-        context->values[i] = NULL;
-        reading.offsets[i] = HNULL;
-    }
-
-    /* read the configuration file */
-    result = buffer_create( &buffer, metafilepath);
-    if (result != 0) {
-        writerror( "can't read file %s",metafilepath);
-        context->state = ERROR;
-        return;
-    }
-
-    /* create the heap */
-    result = heap_create( &context->heap, 1);
-    if (result != 0) {
-        buffer_destroy( &buffer);
-        writerror( "out of memory");
-        context->state = ERROR;
-        return;
-    }
-
-    /* read the file */
-    parsing.buffer = buffer.buffer;
-    parsing.length = buffer.length;
-    parsing.maximum_data_size = 0;
-    parsing.should_escape = 0;
-    parsing.data = &reading;
-    parsing.get = getcb;
-    parsing.put = putcb;
-    parsing.error = errcb;
-    result = parse_utf8_config( &parsing);
-    buffer_destroy( &buffer);
-    if (result != 0 || reading.errcount != 0) {
-        writerror( "%d errors while parsing file %s",
-                                            reading.errcount, metafilepath);
-    }
-
-    /* set the variables */
-    heap_read_only( &context->heap);
-    for (i = 0 ; i < (int)_TZPLATFORM_VARIABLES_COUNT_ ; i++) {
-        offset = reading.offsets[i];
-        if (offset != HNULL)
-            context->values[i] = heap_address( &context->heap, offset);
-        else
-            writerror( "the variable %s isn't defined in file %s",
-                tzplatform_getname((enum tzplatform_variable)i), metafilepath);
-            /* TODO undefined variable */;
-    }
-    context->state = VALID;
-}
-
-inline static void lock(struct tzplatform_context *context)
-{
-#ifndef NOT_MULTI_THREAD_SAFE
-    pthread_mutex_lock( &context->mutex);
-#endif
-}
-
-inline static void unlock(struct tzplatform_context *context)
-{
-#ifndef NOT_MULTI_THREAD_SAFE
-    pthread_mutex_unlock( &context->mutex);
-#endif
-}
-
-static const char *get_lock(struct tzplatform_context *context, enum tzplatform_variable id)
-{
-    const char *result;
-    int offset;
-
-    lock( context);
-    offset = (int)id;
-    if (offset < 0 || (int)_TZPLATFORM_VARIABLES_COUNT_ <= offset) {
-        /*error("invalid id"); TODO*/
-        result = NULL;
-    }
-    else {
-        if (context->state == RESET)
-            initialize( context);
-        result = context->state == ERROR ? NULL : context->values[offset];
-    }
-    return result;
-}
-
-int tzplatform_context_create(struct tzplatform_context **result)
-{
-    struct tzplatform_context *context;
-
-    context = malloc( sizeof * context);
-    *result = context;
-    if (context == NULL)
-        return -1;
-
-    context->state = RESET;
-    context->user = _USER_NOT_SET_;
-#ifndef NOT_MULTI_THREAD_SAFE
-    pthread_mutex_init( &context->mutex, NULL);
-#endif
-    return 0;
-}
-
-void tzplatform_context_destroy(struct tzplatform_context *context)
-{
-    if (context->state == VALID)
-            heap_destroy( &context->heap);
-    context->state = ERROR;
-    free( context);
-}
-
-void tzplatform_reset()
-{
-    tzplatform_context_reset( &global_context);
-}
-
-void tzplatform_context_reset(struct tzplatform_context *context)
-{
-    lock( context);
-    if (context->state != RESET) {
-        if (context->state == VALID)
-            heap_destroy( &context->heap);
-        context->state = RESET;
-    }
-    unlock( context);
-}
-
-int tzplatform_getcount()
-{
-    return (int)_TZPLATFORM_VARIABLES_COUNT_;
-}
-
-const char* tzplatform_getname(enum tzplatform_variable id)
-{
-    const struct varassoc *iter, *end;
-    const char *result;
-    int offset;
-
-    offset = (int)id;
-    if (offset < 0 || (int)_TZPLATFORM_VARIABLES_COUNT_ <= offset) {
-        /*error("invalid id"); TODO*/
-        result = NULL;
-    }
-    else {
-        if (!var_names[0]) {
-            iter = namassoc;
-            end = iter + (sizeof namassoc / sizeof namassoc[0]);
-            while (iter != end) {
-                if (iter->offset >= 0) 
-                    var_names[(int)iter->id] = varpool + iter->offset;
-                iter++;
-            }
-        }
-        result = var_names[offset];
-    }
-    return result;
-}
-
-enum tzplatform_variable tzplatform_getid(const char *name)
-{
-    const struct varassoc *vara = hashvar( name, strlen(name));
-    return vara ? vara->id : _TZPLATFORM_VARIABLES_INVALID_;
-}
-
-const char* tzplatform_getenv(enum tzplatform_variable id) 
-{
-    return tzplatform_context_getenv( &global_context, id);
-}
-
-const char* tzplatform_context_getenv(struct tzplatform_context *context, enum tzplatform_variable id)
-{
-    const char *array[2];
-    const char *result = get_lock( context, id);
-    if (result != NULL) {
-        array[0] = result;
-        array[1] = NULL;
-        result = scratchcat( 0, array);
-    }
-    unlock( context);
-    return result;
-}
-
-int tzplatform_getenv_int(enum tzplatform_variable id)
-{
-    return tzplatform_context_getenv_int( &global_context, id);
-}
-
-int tzplatform_context_getenv_int(struct tzplatform_context *context, enum tzplatform_variable id)
-{
-    const char *value = get_lock( context, id);
-    int result = value==NULL ? -1 : atoi(value);
-    unlock( context);
-    return result;
-}
-
-const char* tzplatform_mkstr(enum tzplatform_variable id, const char * str)
-{
-    return tzplatform_context_mkstr( &global_context, id, str);
-}
-
-const char* tzplatform_context_mkstr(struct tzplatform_context *context, enum tzplatform_variable id, const char *str)
-{
-    const char *array[3];
-    const char *result = get_lock( context, id);
-    if (result != NULL) {
-        array[0] = result;
-        array[1] = str;
-        array[2] = NULL;
-        result = scratchcat( 0, array);
-    }
-    unlock( context);
-    return result;
-}
-
-const char* tzplatform_mkpath(enum tzplatform_variable id, const char * path)
-{
-    return tzplatform_context_mkpath( &global_context, id, path);
-}
-
-const char* tzplatform_context_mkpath(struct tzplatform_context *context, enum tzplatform_variable id, const char *path)
-{
-    const char *array[3];
-    const char *result = get_lock( context, id);
-    if (result != NULL) {
-        array[0] = result;
-        array[1] = path;
-        array[2] = NULL;
-        result = scratchcat( 1, array);
-    }
-    unlock( context);
-    return result;
-}
-
-const char* tzplatform_mkpath3(enum tzplatform_variable id, const char * path,
-                                                        const char* path2)
-{
-    return tzplatform_context_mkpath3( &global_context, id, path, path2);
-}
-
-const char* tzplatform_context_mkpath3(struct tzplatform_context *context, enum tzplatform_variable id, const char *path,
-                                                            const char *path2)
-{
-    const char *array[4];
-    const char *result = get_lock( context, id);
-    if (result != NULL) {
-        array[0] = result;
-        array[1] = path;
-        array[2] = path2;
-        array[3] = NULL;
-        result = scratchcat( 1, array);
-    }
-    unlock( context);
-    return result;
-}
-
-const char* tzplatform_mkpath4(enum tzplatform_variable id, const char * path,
-                                          const char* path2, const char *path3)
-{
-    return tzplatform_context_mkpath4( &global_context, id, path, path2, path3);
-}
-
-const char* tzplatform_context_mkpath4(struct tzplatform_context *context, enum tzplatform_variable id, const char *path,
-                                        const char *path2, const char *path3)
-{
-    const char *array[5];
-    const char *result = get_lock( context, id);
-    if (result != NULL) {
-        array[0] = result;
-        array[1] = path;
-        array[2] = path2;
-        array[3] = path3;
-        array[4] = NULL;
-        result = scratchcat( 1, array);
-    }
-    unlock( context);
-    return result;
-}
-
-uid_t tzplatform_getuid(enum tzplatform_variable id)
-{
-    return tzplatform_context_getuid( &global_context, id);
-}
-
-uid_t tzplatform_context_getuid(struct tzplatform_context *context, enum tzplatform_variable id)
-{
-    uid_t result = (uid_t)-1;
-    const char *value = get_lock( context, id);
-    if (value != NULL) {
-        pw_get_uid( value, &result);
-    }
-    unlock( context);
-    return result;
-}
-
-gid_t tzplatform_getgid(enum tzplatform_variable id)
-{
-    return tzplatform_context_getgid( &global_context, id);
-}
-
-gid_t tzplatform_context_getgid(struct tzplatform_context *context, enum tzplatform_variable id)
-{
-    gid_t result = (uid_t)-1;
-    const char *value = get_lock( context, id);
-    if (value != NULL) {
-        pw_get_gid( value, &result);
-    }
-    unlock( context);
-    return result;
-}
-
-int tzplatform_set_user(uid_t uid)
-{
-    return tzplatform_context_set_user( &global_context, uid);
-}
-
-int tzplatform_context_set_user(struct tzplatform_context *context, uid_t uid)
-{
-    int result;
-
-    result = 0;
-    lock( context);
-    if (context->user != uid) {
-           if (uid != _USER_NOT_SET_ && !pw_has_uid( uid))
-            result = -1;
-        else {
-            if (context->state == VALID)
-                heap_destroy( &context->heap);
-            context->state = RESET;
-            context->user = uid;
-        }
-    }
-    unlock( context);
-
-    return result;
-}
-
-uid_t tzplatform_get_user()
-{
-    return tzplatform_context_get_user( &global_context);
-}
-
-uid_t tzplatform_context_get_user(struct tzplatform_context *context)
-{
-    uid_t result;
-
-    lock( context);
-    result = get_uid( context);
-    unlock( context);
-
-    return result;
-}
-
-void tzplatform_reset_user()
-{
-    tzplatform_context_reset_user( &global_context);
-}
-
-void tzplatform_context_reset_user(struct tzplatform_context *context)
-{
-    tzplatform_context_set_user( context, _USER_NOT_SET_);
-}
-
-#ifdef TEST
-int main() {
-    int i;
-    struct tzplatform_context *context;
-    enum tzplatform_variable id;
-    const char *name;
-    const char *value;
-    int xid;
-    uid_t uid;
-
-    i = 0;
-    while(i != tzplatform_getcount()) {
-        id = (enum tzplatform_variable)i;
-        name = tzplatform_getname(id);
-        value = tzplatform_getenv(id);
-        xid = (int)tzplatform_getid(name);
-        printf("%d=%d\t%s=%s\n",i,xid,name,value?value:"<null>");
-        i++;
-    }
-
-    printf("------------------------\n");
-    i = tzplatform_context_create(&context);
-    if (i) {
-        printf("error while creating context %d\n",i);
-        return 1;
-    }
-
-    uid = (uid_t)0;
-    i = tzplatform_context_set_user(context, uid);
-    if (i) {
-        printf("error %d while switching to user %d\n",i,(int)uid);
-        return 1;
-    }
-    i = 0;
-    while(i != tzplatform_getcount()) {
-        id = (enum tzplatform_variable)i;
-        name = tzplatform_getname(id);
-        value = tzplatform_context_getenv(context, id);
-        xid = (int)tzplatform_getid(name);
-        printf("%d=%d\t%s=%s\n",i,xid,name,value?value:"<null>");
-        i++;
-    }
-    tzplatform_context_destroy(context);
-
-    return 0;
-}
-#endif
-
-
index ca78b35c283bb826a8cb9a597b09f1f6d8f3c931..5a982265dc4e5c4b2ec44a5615b4416f3fff99ec 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2013-2014 Intel Corporation.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -230,8 +230,6 @@ void tzplatform_context_destroy(struct tzplatform_context *context);
 
 /*
  Enforces the removal of the previously evaluated tizen platform variables.
-
- Call this function in case of changing of user inside the application.
 */
 extern
 void tzplatform_context_reset(struct tzplatform_context *context);
@@ -247,6 +245,7 @@ extern
 int tzplatform_context_set_user(struct tzplatform_context *context, uid_t uid);
 
 /*
+ Get the user set to the context.
 */
 extern
 uid_t tzplatform_context_get_user(struct tzplatform_context *context);
diff --git a/src/tzplatform_config.sym b/src/tzplatform_config.sym
new file mode 100644 (file)
index 0000000..49146b8
--- /dev/null
@@ -0,0 +1,38 @@
+TPC {
+       global:
+               _context_getenv_int_tzplatform_;
+               _context_getenv_tzplatform_;
+               _context_getgid_tzplatform_;
+               _context_getuid_tzplatform_;
+               _context_mkpath_tzplatform_;
+               _context_mkpath3_tzplatform_;
+               _context_mkpath4_tzplatform_;
+               _context_mkstr_tzplatform_;
+               _getenv_int_tzplatform_;
+               _getenv_tzplatform_;
+               _getgid_tzplatform_;
+               _getid_tzplatform_;
+               _getname_tzplatform_;
+               _getuid_tzplatform_;
+               _mkpath_tzplatform_;
+               _mkpath3_tzplatform_;
+               _mkpath4_tzplatform_;
+               _mkstr_tzplatform_;
+
+
+               tzplatform_context_create;
+               tzplatform_context_destroy;
+               tzplatform_context_get_user;
+               tzplatform_context_reset;
+               tzplatform_context_reset_user;
+               tzplatform_context_set_user;
+               tzplatform_getname;
+               tzplatform_get_user;
+               tzplatform_reset;
+               tzplatform_reset_user;
+               tzplatform_set_user;
+
+       local:
+               *;
+};
+
index f254f54510f0ba49ec07fcfaf3d285fdd883c73a..427346029402e5e94ac0829a65bb9b761579ec7f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2013-2014 Intel Corporation.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
index 3df2ca4c5d2a762962b9ce7204c08be793bb8ba8..59224d9fd6371ec425c9f200bd082e4491f4e898 100644 (file)
@@ -2,10 +2,11 @@ prefix=@prefix@
 exec_prefix=@exec_prefix@
 libdir=@libdir@
 includedir=@includedir@
-srcdir=@datadir@
+datarootdir=@datarootdir@
+datadir=@datadir@
 Name: @PACKAGE_NAME@
 Description: Tizen Platform Wrapper library.
 Version: @PACKAGE_VERSION@
 URL: @PACKAGE_URL@
 Libs: -L${libdir} 
-Cflags: -I${includedir}/
\ No newline at end of file
+Cflags: -I${includedir}/