Skip to content

Commit

Permalink
wip: finished conversion to pointer array
Browse files Browse the repository at this point in the history
- Finished conversion to pointer array, the only reamining
is the sorting logic and written to the file logic. Then the testing ofc
  • Loading branch information
peterbaouoft committed Jun 11, 2018
1 parent a5d0abc commit c6bf9c2
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile-rpm-ostree.am
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ rpm_ostree_SOURCES = src/app/main.c \
src/app/rpmostree-polkit-agent.c \
src/app/rpmostree-polkit-agent.h \
src/app/rpmostree-builtin-kargs.c \
$(NULL)
$(NULL)

if BUILDOPT_COMPOSE_TOOLING
rpm_ostree_SOURCES += \
Expand Down
5 changes: 5 additions & 0 deletions Makefile-tests.am
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ tests_check_test_kargs_CPPFLAGS = $(testbin_cppflags)
tests_check_test_kargs_CFLAGS = $(testbin_cflags)
tests_check_test_kargs_LDADD = $(testbin_ldadd) libtest.la

tests_check_test_sysusers_CPPFLAGS = $(testbin_cppflags)
tests_check_test_sysusers_CFLAGS = $(testbin_cflags)
tests_check_test_sysusers_LDADD = $(testbin_ldadd) libtest.la

uninstalled_test_programs = \
tests/check/jsonutil \
tests/check/postprocess \
tests/check/test-utils \
tests/check/test-kargs \
tests/check/test-sysusers \
$(NULL)

uninstalled_test_scripts = \
Expand Down
3 changes: 3 additions & 0 deletions src/app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ static RpmOstreeCommand commands[] = {
{ "refresh-md", 0,
"Generate rpm repo metadata",
rpmostree_builtin_refresh_md },
//{ "temp-convert", RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD,
//"temp command to convert passwd/group conversion",
// rpmostree_builtin_temp_convert },
/* Legacy aliases */
{ "pkg-add", RPM_OSTREE_BUILTIN_FLAG_HIDDEN,
NULL, rpmostree_builtin_install },
Expand Down
1 change: 1 addition & 0 deletions src/app/rpmostree-builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ BUILTINPROTO(uninstall);
BUILTINPROTO(override);
BUILTINPROTO(start_daemon);
BUILTINPROTO(ex);
// BUILTINPROTO(temp_convert);

#undef BUILTINPROTO

Expand Down
16 changes: 14 additions & 2 deletions src/app/rpmostree-compose-builtin-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1259,15 +1259,27 @@ impl_commit_tree (RpmOstreeTreeComposeContext *self,
if (self->treefile)
{
g_autoptr(GFile) treefile_dirpath = g_file_get_parent (self->treefile_path);
g_autoptr(GHashTable) sysusers_table = NULL;
if (!rpmostree_check_passwd (self->repo, self->rootfs_dfd, treefile_dirpath, self->treefile,
self->previous_checksum,
self->previous_checksum, &sysusers_table,
cancellable, error))
return glnx_prefix_error (error, "Handling passwd db");

if (!rpmostree_check_groups (self->repo, self->rootfs_dfd, treefile_dirpath, self->treefile,
self->previous_checksum,
self->previous_checksum, &sysusers_table,
cancellable, error))
return glnx_prefix_error (error, "Handling group db");
if (sysusers_table)
{
/* Do conversion and write files here */
g_autofree gchar* sysuser_content = NULL;
//if (!rpmostree_passwd_sysusers2char (sysusers_table,
// &sysuser_content, error))
// return glnx_prefix_error (error, "Handling sysusers conversion");
/* Write to the sysusers directory */
;
/* May be delete the /usr/lib/passwd generated? */
}
}

/* See comment above */
Expand Down
141 changes: 134 additions & 7 deletions src/libpriv/rpmostree-passwd-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ conv_passwd_ent_free (void *vptr)
g_free (ptr->name);
g_free (ptr->pw_gecos);
g_free (ptr->pw_dir);
g_free (ptr->pw_shell);
g_free (ptr);
}

Expand All @@ -159,8 +160,9 @@ rpmostree_passwd_data2passwdents (const char *data)
convent->uid = ent->pw_uid;
convent->gid = ent->pw_gid;
/* Want to add anymore, like dir? */
convent->pw_gecos = g_strdup(ent->pw_gecos);
convent->pw_dir = g_strdup(ent->pw_dir);
convent->pw_gecos = g_strdup (ent->pw_gecos);
convent->pw_dir = g_strdup (ent->pw_dir);
convent->pw_shell = g_strdup (ent->pw_shell);
g_ptr_array_add (ret, convent);
}

Expand All @@ -185,6 +187,17 @@ conv_group_ent_free (void *vptr)
g_free (ptr);
}

static void
sysuser_ent_free (void *vptr)
{
struct sysuser_ent *ptr = vptr;
g_free (ptr->name);
g_free (ptr->id);
g_free (ptr->gecos);
g_free (ptr->dir);
g_free (ptr->shell);
}

GPtrArray *
rpmostree_passwd_data2groupents (const char *data)
{
Expand Down Expand Up @@ -216,6 +229,112 @@ compare_group_ents (gconstpointer a, gconstpointer b)
return strcmp ((*sa)->name, (*sb)->name);
}

rpmostree_passwd_ents2sysusers (gboolean is_passwd,
GPtrArray *input_ents,
GHashTable **out_sysusers_table,
GError **error)
{
gboolean ret;
if (is_passwd)
ret = FALSE;
//ret = rpmostree_passwdents2sysusers (input_ents, out_sysusers_table, error);
else
ret = FALSE;
return ret;
}

gboolean
rpmostree_passwdents2sysusers (GPtrArray *passwd_ents,
GPtrArray **out_sysusers_entries,
GError **error)
{
/* Do the assignment inside the function so we don't need to handle visibility
* issues from different files any more */
GPtrArray *sysusers_array = NULL;
sysusers_array = *out_sysusers_entries ?: g_ptr_array_new_with_free_func (sysuser_ent_free);
for (int counter=0; counter < passwd_ents->len; counter++)
{
struct conv_passwd_ent *convent = passwd_ents->pdata[counter];
struct sysuser_ent *sysent = g_new (struct sysuser_ent, 1);

// Note, sysuser support uid:gid format when uid is not equal
// to gid, and that allows sysusers to add both group and user entries
if (convent->uid != convent->gid)
sysent->id = g_strdup_printf ("%u:%u", convent->uid, convent->gid);
else
sysent->id = g_strdup_printf ("%u", convent->uid);

sysent->type = "u";
sysent->name = g_strdup (convent->name);

sysent->gecos = (g_str_equal (convent->pw_gecos, "")) ? NULL :
g_strdup_printf ("\"%s\"", convent->pw_gecos);
sysent->dir = (g_str_equal (convent->pw_dir, ""))? NULL :
g_steal_pointer (&convent->pw_dir);
sysent->shell = g_steal_pointer (&convent->pw_shell);

g_ptr_array_add (sysusers_array, sysent);
}
/* Do the assignment at the end if the sysusers_table was not initialized */
if (*out_sysusers_entries == NULL)
*out_sysusers_entries = g_steal_pointer (&sysusers_array);

return TRUE;
}

gboolean
rpmostree_groupents2sysusers (GPtrArray *group_ents,
GPtrArray **out_sysusers_entries,
GError **error)
{
/* Do the assignment inside the function so we don't need to handle visibility
* issues from different files any more */
GPtrArray *sysusers_array = NULL;
sysusers_array = *out_sysusers_entries ?: g_ptr_array_new_with_free_func (sysuser_ent_free);
for (int counter=0; counter < group_ents->len; counter++)
{
struct conv_group_ent *convent = group_ents->pdata[counter];
struct sysuser_ent *sysent = g_new (struct sysuser_ent, 1);

sysent->type = "g";
sysent->name = g_steal_pointer (&convent->name);
sysent->id = g_strdup_printf ("%u", convent->gid);
sysent->gecos = NULL;
sysent->dir = NULL;
sysent->shell = NULL;

g_ptr_array_add (sysusers_array, sysent);
}
/* Do the assignment at the end if the sysusers_table was not initialized */
if (*out_sysusers_entries == NULL)
*out_sysusers_entries = g_steal_pointer (&sysusers_array);

return TRUE;
}

gboolean
rpmostree_passwd_sysusers2char (GPtrArray *sysusers_entries,
char **out_content,
GError **error)
{

GString* sysuser_content = g_string_new (NULL);
for (int counter = 0; counter < sysusers_entries->len; counter++)
{
struct sysuser_ent *sysent = sysusers_entries->pdata[counter];
const char *shell = sysent->shell ?: "-";
const char *gecos = sysent->gecos ?: "-";
const char *dir = sysent->dir ?: "-";
g_autofree gchar* line_content = g_strjoin (" ", sysent->type, sysent->name,
sysent->id, gecos, dir, shell, NULL);
g_string_append_printf (sysuser_content, "%s\n", line_content);
}
if (out_content)
*out_content = g_string_free(sysuser_content, FALSE);

return TRUE;
}

/* See "man 5 passwd" We just make sure the name and uid/gid match,
and that none are missing. don't care about GECOS/dir/shell.
*/
Expand All @@ -226,6 +345,7 @@ rpmostree_check_passwd_groups (gboolean passwd,
GFile *treefile_dirpath,
JsonObject *treedata,
const char *previous_commit,
GHashTable **out_hashtable,
GCancellable *cancellable,
GError **error)
{
Expand Down Expand Up @@ -253,7 +373,7 @@ rpmostree_check_passwd_groups (gboolean passwd,
return TRUE; /* Note early return */
else if (g_str_equal (chk_type, "previous"))
; /* Handled below */
else if (g_str_equal (chk_type, "file"))
else if (g_str_equal (chk_type, "file") || g_str_equal (chk_type, "sysusers"))
{
direct = _rpmostree_jsonutil_object_require_string_member (chk,
"filename",
Expand Down Expand Up @@ -358,7 +478,7 @@ rpmostree_check_passwd_groups (gboolean passwd,
return TRUE;
}
}
else if (g_str_equal (chk_type, "file"))
else if (g_str_equal (chk_type, "file") || g_str_equal (chk_type, "sysusers"))
{
old_path = g_file_resolve_relative_path (treefile_dirpath, direct);
old_contents = glnx_file_get_contents_utf8_at (AT_FDCWD, gs_file_get_path_cached (old_path), NULL,
Expand All @@ -367,7 +487,7 @@ rpmostree_check_passwd_groups (gboolean passwd,
return FALSE;
}

if (g_str_equal (chk_type, "previous") || g_str_equal (chk_type, "file"))
if (g_str_equal (chk_type, "previous") || g_str_equal (chk_type, "file") || g_str_equal (chk_type, "sysusers"))
{
if (passwd)
old_ents = rpmostree_passwd_data2passwdents (old_contents);
Expand Down Expand Up @@ -548,6 +668,11 @@ rpmostree_check_passwd_groups (gboolean passwd,
}
}

/* Now, at the end of checking, if all goes correctly, we put entries into
* hashtable for sysusers */
if (g_str_equal (chk_type, "sysusers") &&
!rpmostree_passwd_ents2sysusers (passwd, new_ents, out_hashtable, error))
return FALSE;
return TRUE;
}

Expand All @@ -560,11 +685,12 @@ rpmostree_check_passwd (OstreeRepo *repo,
GFile *treefile_dirpath,
JsonObject *treedata,
const char *previous_commit,
GHashTable **out_hashtable,
GCancellable *cancellable,
GError **error)
{
return rpmostree_check_passwd_groups (TRUE, repo, rootfs_fd, treefile_dirpath,
treedata, previous_commit,
treedata, previous_commit, out_hashtable,
cancellable, error);
}

Expand All @@ -577,11 +703,12 @@ rpmostree_check_groups (OstreeRepo *repo,
GFile *treefile_dirpath,
JsonObject *treedata,
const char *previous_commit,
GHashTable **out_hashtable,
GCancellable *cancellable,
GError **error)
{
return rpmostree_check_passwd_groups (TRUE, repo, rootfs_fd, treefile_dirpath,
treedata, previous_commit,
treedata, previous_commit, out_hashtable,
cancellable, error);
}

Expand Down
32 changes: 32 additions & 0 deletions src/libpriv/rpmostree-passwd-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ rpmostree_check_passwd (OstreeRepo *repo,
GFile *treefile_path,
JsonObject *treedata,
const char *previous_commit,
GHashTable **out_hashtable,
GCancellable *cancellable,
GError **error);

Expand All @@ -39,6 +40,7 @@ rpmostree_check_groups (OstreeRepo *repo,
GFile *treefile_path,
JsonObject *treedata,
const char *previous_commit,
GHashTable **out_hashtable,
GCancellable *cancellable,
GError **error);

Expand Down Expand Up @@ -89,12 +91,22 @@ gboolean
rpmostree_passwd_complete_rpm_layering (int rootfs_dfd,
GError **error);

struct sysuser_ent {
const char *type; /* type of sysuser entry, can be 1: u (user) 2: g (group) 3: m (mixed) 4: r (ranged ids) */
char *name;
char *id; /* id in sysuser entry, can be in the form of 1: uid 2:gid 3: uid:gid, or we can separate this into uid_t and gid_t*/
char *gecos; /* user information */
char *dir; /* home directory */
char *shell; /* Login shell, defaulted to /sbin/nologin */
};

struct conv_passwd_ent {
char *name;
uid_t uid;
gid_t gid;
char *pw_gecos; /* user information */
char *pw_dir; /* home directory */
char *pw_shell; /* login shell */
};

struct conv_group_ent {
Expand All @@ -107,3 +119,23 @@ rpmostree_passwd_data2passwdents (const char *data);

GPtrArray *
rpmostree_passwd_data2groupents (const char *data);

gboolean
rpmostree_passwdents2sysusers (GPtrArray *passwd_ents,
GPtrArray **out_sysusers_entries,
GError **error);
gboolean
rpmostree_groupents2sysusers (GPtrArray *group_ents,
GPtrArray **out_sysusers_entries,
GError **error);

gboolean
rpmostree_passwd_sysusers2char (GPtrArray *sysusers_entries,
char **out_content,
GError **error);

gboolean
rpmostree_passwd_ents2sysusers (gboolean is_passwd,
GPtrArray *input_ents,
GHashTable **out_sysusers_table,
GError **error);
Loading

0 comments on commit c6bf9c2

Please sign in to comment.