Remove direct passwd & group file manipulation 97/185997/1 accepted/tizen_5.0_unified accepted/tizen/5.0/unified/20181102.021839 accepted/tizen/unified/20180914.073219 submit/tizen/20180913.140552 submit/tizen_5.0/20181101.000004
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Mon, 6 Aug 2018 09:44:50 +0000 (11:44 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Mon, 6 Aug 2018 09:44:50 +0000 (11:44 +0200)
This commit removes direct /etc/passwd & /etc/group
file manipulation in favour to using POSIX (libc-
standard) APIs.

This change is needed to allow splitting databases
into read-only /etc/ (upgraded with Tizen) and writable
/opt/etc/ (user-managed).

Change-Id: Icda87b57a8b2da6b4481d4e43dc2b3e53fa746a7

src/passwd.c

index 629664d..4ac561f 100644 (file)
 #include <string.h>
 #include <sys/types.h>
 #include <errno.h>
+#include <pwd.h>
 
 #include "heap.h"
 #include "passwd.h"
 
-#ifndef NOT_PASSWD_ONLY
-
-#include "buffer.h"
-
-/* index of fields */
-enum { iname, ipasswd, iuid, igid, icmt, idir, ishell };
-
-static const char pwfile[] = "/etc/passwd";
-
-#define is(s, i) (!strncmp(s, context->pw.starts[i], context->pw.lengths[i]) && !s[context->pw.lengths[i]])
-
-/* open the passwd file */
-static int oppw(struct tzplatform_context *context)
-{
-       context->pw.pos = 0;
-       return buffer_create(&context->pw.buffer, pwfile);
-}
-
-/* close the passwd file */
-static void clpw(struct tzplatform_context *context)
-{
-       buffer_destroy(&context->pw.buffer);
-}
-
-/* read the passwd file */
-static int rdpw(struct tzplatform_context *context)
-{
-       int col = 0;
-       struct pw *pw = &context->pw;
-       const char *head = pw->buffer.buffer + pw->pos;
-       const char *end = pw->buffer.buffer + pw->buffer.length;
-       while (head != end) {
-               pw->starts[col] = head;
-               while (head != end && *head != ':' && *head != '\n') head++;
-               pw->lengths[col] = head - pw->starts[col];
-               col++;
-               if (col == 7) {
-                       if (head == end) {
-                               pw->pos = head - pw->buffer.buffer;
-                               return 1;
-                       }
-                       if (*head == '\n') {
-                               head++;
-                               pw->pos = head - pw->buffer.buffer;
-                               return 1;
-                       }
-                       while (head != end && *head != '\n') head++;
-                       if (head != end) head++;
-                       col = 0;
-               } else {
-                       if (head != end) {
-                               if (*head++ == '\n')
-                                       col = 0;
-                       }
-               }
-       }
-       pw->pos = head - pw->buffer.buffer;
-       return 0;
-}
-
-int pw_get(struct tzplatform_context *context, struct heap *heap, struct pwget **items)
-{
-       size_t user, home;
-       int result;
-       int i, n, s;
-       struct pw *pw = &context->pw;
-
-       for (n = 0 ; items[n] != NULL ; n++)
-               items[n]->set = 0;
-
-       result = oppw(context);
-       if (result == 0) {
-               s = n;
-               while (s && rdpw(context)) {
-                       user = home = HNULL;
-                       for (i = 0 ; i < n ; i++) {
-                               if (!items[i]->set && is(items[i]->id, iuid)) {
-                                       if (user == HNULL) {
-                                               user = heap_strndup(heap,
-                                                       pw->starts[iname], pw->lengths[iname]);
-                                               home = heap_strndup(heap,
-                                                       pw->starts[idir], pw->lengths[idir]);
-                                       }
-                                       items[i]->set = 1;
-                                       items[i]->user = user;
-                                       items[i]->home = home;
-                                       s--;
-                               }
-                       }
-               }
-               clpw(context);
-       }
-       return result;
-}
-
-int pw_has_uid(struct tzplatform_context *context, uid_t uid)
-{
-       struct pw *pw = &context->pw;
-
-       if (oppw(context) == 0) {
-               while (rdpw(context)) {
-                       if (pw->lengths[iuid] && (int)uid == atoi(pw->starts[iuid])) {
-                               clpw(context);
-                               return 1;
-                       }
-               }
-               clpw(context);
-       }
-       return 0;
-}
-
-int pw_get_uid(struct tzplatform_context *context, const char *name, uid_t *uid)
-{
-       int result = oppw(context);
-       if (result == 0) {
-               while (rdpw(context)) {
-                       if (is(name, iname)) {
-                               *uid = (uid_t)atoi(context->pw.starts[iuid]);
-                               clpw(context);
-                               return 0;
-                       }
-               }
-               clpw(context);
-               result = -1;
-               errno = EEXIST;
-       }
-       return result;
-}
-
-int pw_get_gid(struct tzplatform_context *context, const char *name, gid_t *gid)
-{
-       int result = oppw(context);
-       if (result == 0) {
-               while (rdpw(context)) {
-                       if (is(name, iname)) {
-                               *gid = (gid_t)atoi(context->pw.starts[igid]);
-                               clpw(context);
-                               return 0;
-                       }
-               }
-               clpw(context);
-               result = -1;
-               errno = EEXIST;
-       }
-       return result;
-}
-
-#else
-
-#include <pwd.h>
-
 #define BUFSIZE  4096
 
-int pw_get(struct heap *heap, struct pwget **items)
+int pw_get(struct tzplatform_context *context, struct heap *heap, struct pwget **items)
 {
        char buffer[BUFSIZE];
        struct passwd entry, *pe;
@@ -214,7 +64,7 @@ int pw_get(struct heap *heap, struct pwget **items)
        return 0;
 }
 
-int pw_has_uid(uid_t uid)
+int pw_has_uid(struct tzplatform_context *context, uid_t uid)
 {
        char buffer[BUFSIZE];
        struct passwd entry, *pe;
@@ -224,7 +74,7 @@ int pw_has_uid(uid_t uid)
        return !result && pe;
 }
 
-int pw_get_uid(const char *name, uid_t *uid)
+int pw_get_uid(struct tzplatform_context *context, const char *name, uid_t *uid)
 {
        char buffer[BUFSIZE];
        struct passwd entry, *pe;
@@ -240,7 +90,7 @@ int pw_get_uid(const char *name, uid_t *uid)
        return result;
 }
 
-int pw_get_gid(const char *name, gid_t *gid)
+int pw_get_gid(struct tzplatform_context *context, const char *name, gid_t *gid)
 {
        char buffer[BUFSIZE];
        struct passwd entry, *pe;
@@ -256,8 +106,6 @@ int pw_get_gid(const char *name, gid_t *gid)
        return result;
 }
 
-#endif
-
 #ifdef TEST_PASSWD
 #include <stdio.h>
 int main(int argc, char**argv)
@@ -297,5 +145,3 @@ int main(int argc, char**argv)
        return 0;
 }
 #endif
-
-