From 8eec5dc67baaf10f7a449ee4fe7d71da4caeeef7 Mon Sep 17 00:00:00 2001 From: Imran Zaman Date: Fri, 15 Nov 2013 15:52:50 +0200 Subject: [PATCH] added documentation for file functions --- docs/Makefile.am | 5 +- include/gum/common/gum-file.h | 26 +++--- src/common/gum-file.c | 199 +++++++++++++++++++++++++++++------------ src/common/gum-string-utils.c | 2 + src/common/gum-utils.c | 1 + src/common/gum-validate.c | 1 + src/daemon/gumd-daemon-group.c | 108 +++++++++++----------- src/daemon/gumd-daemon-user.c | 54 +++++------ test/common/commontest.c | 2 +- 9 files changed, 251 insertions(+), 147 deletions(-) diff --git a/docs/Makefile.am b/docs/Makefile.am index 7c07bd0..84b7be3 100644 --- a/docs/Makefile.am +++ b/docs/Makefile.am @@ -70,7 +70,10 @@ gum-group-service.h\ gum-dbus-user-gen.h\ gum-dbus-user-service-gen.h\ gum-dbus-group-gen.h\ -gum-dbus-group-service-gen.h +gum-dbus-group-service-gen.h \ +gum-defines.h \ +gum-dbus.h + # Images to copy into HTML directory. # e.g. HTML_IMAGES=$(top_srcdir)/gtk/stock-icons/stock_about_24.png diff --git a/include/gum/common/gum-file.h b/include/gum/common/gum-file.h index 85aa078..f84fa7c 100644 --- a/include/gum/common/gum-file.h +++ b/include/gum/common/gum-file.h @@ -44,11 +44,11 @@ typedef enum { GUM_OPTYPE_MODIFY = 3 } GumOpType; -typedef gboolean (*GumFileUpdateFunc) ( +typedef gboolean (*GumFileUpdateCB) ( GObject *object, GumOpType op, - FILE *origf, - FILE *newf, + FILE *source_file, + FILE *dup_file, gpointer user_data, GError **error); @@ -56,25 +56,25 @@ gboolean gum_file_update ( GObject *object, GumOpType op, - GumFileUpdateFunc update_func, - const gchar *origfn, + GumFileUpdateCB callback, + const gchar *source_file_path, gpointer user_data, GError **error); gboolean gum_file_open_db_files ( - const gchar *origfn, - const gchar *newfn, - FILE **origf, - FILE **newf, + const gchar *source_file_path, + const gchar *dup_file_path, + FILE **source_file, + FILE **dup_file, GError **error); gboolean gum_file_close_db_files ( - const gchar *origfn, - const gchar *newfn, - FILE *orig, - FILE *newf, + const gchar *source_file_path, + const gchar *dup_file_path, + FILE *source_file, + FILE *dup_file, GError **error); struct passwd * diff --git a/src/common/gum-file.c b/src/common/gum-file.c index 2346aab..8d7e97a 100644 --- a/src/common/gum-file.c +++ b/src/common/gum-file.c @@ -47,12 +47,56 @@ * @title: Gum File * @include: gum/common/gum-file.h * + * Below is the code snippet, which demonstrate how can file update function be + * used to add, remove or modify an entry in the user/group database file + * (e.g. /etc/passwd). * * |[ * + * gboolean _custom_update_file_entries (GObject *obj, GumOpType op, + * FILE *source_file, FILE *dup_file, gpointer user_data, GError **error) + * { + * //loop through the file entries and modify as per operation type + * return TRUE; + * } + * + * void update_file_entries () + * { + * GObject* obj = NULL; + * GError *error = NULL; + * const gchar *source_file = "/tmp/passwd"; + * gum_file_update (obj, GUM_OPTYPE_ADD, + * (GumFileUpdateCB)_custom_update_file_entries, source_file, NULL, + * &error); + * } + * * ]| */ +/** + * GumOpType: + * @GUM_OPTYPE_ADD: add an entry + * @GUM_OPTYPE_DELETE: delete an entry + * @GUM_OPTYPE_MODIFY: modify an entry + * + * This enumeration lists the operations on file entry. + */ + +/** + * GumFileUpdateCB: + * @object: (transfer none): the instance of #GObject + * @op: (transfer none): the #GumOpType operation to be done on the file entry + * @source_file: (transfer none): the source file pointer + * @dup_file: (transfer none): the duplicate file pointer + * @user_data: the user data + * @error: (transfer none): the #GError which is set in case of an error + * + * Callback can be used for adding, deleting or modifying file entries. It is + * invoked in #gum_file_update function. + * + * Returns: TRUE if successful, FALSE otherwise and @error is set. + */ + #define GUM_PERM 0777 static FILE * @@ -120,30 +164,49 @@ _copy_file_attributes ( return ret; } +/** + * gum_file_open_db_files: + * @source_file_path: (transfer none): the path to source file + * @dup_file_path: (transfer none): the path to duplicate file, created from + * the source file + * @source_file: (transfer none): the file pointer created when source file + * is opened in read mode + * @dup_file: (transfer none): the file pointer created when duplicate file is + * opened in write mode + * @error: (transfer none): the #GError which is set in case of an error + * + * Opens the source file @source_file_path in read mode. Then creates the + * duplicate file @dup_file_path in write mode and copies source file attributes + * to the duplicate file. Open file handles are set in @source_file and + * @dup_file. + * + * Returns: TRUE if successful, FALSE otherwise and @error is set. + */ gboolean gum_file_open_db_files ( - const gchar *origfn, - const gchar *newfn, - FILE **origf, - FILE **newf, + const gchar *source_file_path, + const gchar *dup_file_path, + FILE **source_file, + FILE **dup_file, GError **error) { - if (!origfn || !origf || !(*origf = _open_file (origfn, "r"))) { - DBG("origfn %s --- orig %p", origfn ? origfn : "NULL", origf); + if (!source_file_path || !source_file || + !(*source_file = _open_file (source_file_path, "r"))) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_OPEN, "Unable to open orig file", error, FALSE); } - if (!newfn || !newf || !(*newf = _open_file (newfn, "w+"))) { - if (*origf) fclose (*origf); + if (!dup_file_path || !dup_file || + !(*dup_file = _open_file (dup_file_path, "w+"))) { + if (*source_file) fclose (*source_file); GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_OPEN, "Unable to open new file", error, FALSE); } - if (!_copy_file_attributes (origfn, newfn)) { - if (*origf) fclose (*origf); - if (*newf) fclose (*newf); - g_unlink (newfn); + if (!_copy_file_attributes (source_file_path, dup_file_path)) { + if (*source_file) fclose (*source_file); + if (*dup_file) fclose (*dup_file); + g_unlink (dup_file_path); GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_ATTRIBUTE, "Unable to get/set file attributes", error, FALSE); } @@ -151,30 +214,44 @@ gum_file_open_db_files ( return TRUE; } +/** + * gum_file_close_db_files: + * @source_file_path: (transfer none): the path to source file + * @dup_file_path: (transfer none): the path to duplicate file + * @source_file: (transfer none): the source file pointer + * @dup_file: (transfer none): the duplicate file pointer + * @error: (transfer none): the #GError which is set in case of an error + * + * Closes the duplicate file @dup_file_path after flushing all the data. Backup + * of the source file @source_file_path is created and duplicate file is renamed + * to as the source file. + * + * Returns: TRUE if successful, FALSE otherwise and @error is set. + */ gboolean gum_file_close_db_files ( - const gchar *origfn, - const gchar *newfn, - FILE *origf, - FILE *newf, + const gchar *source_file_path, + const gchar *dup_file_path, + FILE *source_file, + FILE *dup_file, GError **error) { gboolean retval = TRUE; gchar *old_file = NULL; - if (!newf || - !newfn || - fflush (newf) != 0 || - fsync (fileno (newf)) != 0 || - fclose (newf) != 0) { - GUM_SET_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, retval, - FALSE); + if (!dup_file || + !dup_file_path || + fflush (dup_file) != 0 || + fsync (fileno (dup_file)) != 0 || + fclose (dup_file) != 0) { + GUM_SET_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, + retval, FALSE); goto _close_new; } - newf = NULL; + dup_file = NULL; - /* Move original file to old file and new file as updated file */ - old_file = g_strdup_printf ("%s.old", origfn); + /* Move source file to old file and dup file as updated file */ + old_file = g_strdup_printf ("%s.old", source_file_path); if (!old_file) { GUM_SET_ERROR (GUM_ERROR_FILE_MOVE, "Unable to create old file", error, retval, FALSE); @@ -182,69 +259,81 @@ gum_file_close_db_files ( } g_unlink (old_file); - if (link (origfn, old_file) != 0 || - g_rename (newfn, origfn) != 0) { - GUM_SET_ERROR (GUM_ERROR_FILE_MOVE, "Unable to move file", error, retval, - FALSE); + if (link (source_file_path, old_file) != 0 || + g_rename (dup_file_path, source_file_path) != 0) { + GUM_SET_ERROR (GUM_ERROR_FILE_MOVE, "Unable to move file", error, + retval, FALSE); } _close_new: - if (newf) fclose (newf); - g_unlink (newfn); + if (dup_file) fclose (dup_file); + g_unlink (dup_file_path); - if (origf) fclose (origf); + if (source_file) fclose (source_file); g_free (old_file); return retval; } +/** + * gum_file_update: + * @object: (transfer none): the instance of #GObject; can be NULL + * @op: (transfer none): the #GumOpType operation to be done on file entry + * the source file + * @callback: (transfer none): the callback #GumFileUpdateCB to be invoked + * when the source and duplicate files are opened to be handled + * @source_file_path: (transfer none): the source file path + * @user_data: user data to be passed on to the @callback + * @error: (transfer none): the #GError which is set in case of an error + * + * Opens the files and invokes the callback to do the required operation. + * Finally files are flushed and closed. + * + * Returns: TRUE if successful, FALSE otherwise and @error is set. + */ gboolean gum_file_update ( GObject *object, GumOpType op, - GumFileUpdateFunc update_func, - const gchar *origfn, + GumFileUpdateCB callback, + const gchar *source_file_path, gpointer user_data, GError **error) { - /* open files - * read (current) file - ** write 1-by-1 line to new file along with the - ** modification (add/remove/modify) - * rename files and move original file to old file - * close files - * */ gboolean retval = TRUE; - FILE *origf = NULL, *newf = NULL; - gchar *newfn = NULL; + FILE *source_file = NULL, *dup_file = NULL; + gchar *dup_file_path = NULL; - newfn = g_strdup_printf ("%s-tmp.%lu", origfn, (unsigned long)getpid ()); - retval = gum_file_open_db_files (origfn, newfn, &origf, &newf, error); + dup_file_path = g_strdup_printf ("%s-tmp.%lu", source_file_path, + (unsigned long)getpid ()); + retval = gum_file_open_db_files (source_file_path, dup_file_path, + &source_file, &dup_file, error); if (!retval) goto _finished; /* Update, sync and close file */ - if (!update_func) { - GUM_SET_ERROR (GUM_ERROR_FILE_WRITE, "File write function not specified", - error, retval, FALSE); + if (!callback) { + GUM_SET_ERROR (GUM_ERROR_FILE_WRITE, + "File write function not specified", error, retval, FALSE); goto _close; } - retval = (*update_func) (object, op, origf, newf, user_data, error); + retval = (*callback) (object, op, source_file, dup_file, user_data, error); if (!retval) { goto _close; } - retval = gum_file_close_db_files (origfn, newfn, origf, newf, error); + retval = gum_file_close_db_files (source_file_path, dup_file_path, + source_file, dup_file, error); _close: if (!retval) { - if (newf) fclose (newf); - g_unlink (newfn); - if (origf) fclose (origf); + if (dup_file) fclose (dup_file); + g_unlink (dup_file_path); + if (source_file) fclose (source_file); } _finished: - g_free (newfn); + g_free (dup_file_path); return retval; } diff --git a/src/common/gum-string-utils.c b/src/common/gum-string-utils.c index 7e12e20..1de8212 100644 --- a/src/common/gum-string-utils.c +++ b/src/common/gum-string-utils.c @@ -35,6 +35,8 @@ * @title: Gum String Utils * @include: gum/common/gum-string-utils.h * + * String utility functions + * */ /** diff --git a/src/common/gum-utils.c b/src/common/gum-utils.c index a0809e0..6eac8c6 100644 --- a/src/common/gum-utils.c +++ b/src/common/gum-utils.c @@ -40,6 +40,7 @@ * @title: Gum Utils * @include: gum/common/gum-utils.h * + * Utility functions */ typedef struct __nonce_ctx_t diff --git a/src/common/gum-validate.c b/src/common/gum-validate.c index 055c374..acb4600 100644 --- a/src/common/gum-validate.c +++ b/src/common/gum-validate.c @@ -38,6 +38,7 @@ * @title: Gum Validate * @include: gum/common/gum-validate.h * + * Name/secret validation functions */ /** diff --git a/src/daemon/gumd-daemon-group.c b/src/daemon/gumd-daemon-group.c index 55b54cf..d136305 100644 --- a/src/daemon/gumd-daemon-group.c +++ b/src/daemon/gumd-daemon-group.c @@ -432,20 +432,20 @@ static gboolean _update_daemon_group_entry ( GumdDaemonGroup *self, GumOpType op, - FILE *origf, - FILE *newf, + FILE *source_file, + FILE *dup_file, gpointer user_data, GError **error) { gboolean done = FALSE; struct group *entry; - while ((entry = fgetgrent (origf)) != NULL) { + while ((entry = fgetgrent (source_file)) != NULL) { if (!done) { switch (op) { case GUM_OPTYPE_ADD: if (self->priv->group->gr_gid < entry->gr_gid) { - if (putgrent (self->priv->group, newf) < 0) { + if (putgrent (self->priv->group, dup_file) < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, FALSE); } @@ -465,7 +465,7 @@ _update_daemon_group_entry ( self->priv->group->gr_name; if (self->priv->group->gr_gid == entry->gr_gid && g_strcmp0 (old_name, entry->gr_name) == 0) { - if (putgrent (self->priv->group, newf) < 0) { + if (putgrent (self->priv->group, dup_file) < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, FALSE); } @@ -478,7 +478,7 @@ _update_daemon_group_entry ( break; } } - if (putgrent (entry, newf) < 0) { + if (putgrent (entry, dup_file) < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, FALSE); } @@ -486,7 +486,7 @@ _update_daemon_group_entry ( /* Write entry to file in case it is first entry in the file */ if (!done && op == GUM_OPTYPE_ADD) { - if (putgrent (self->priv->group, newf) < 0) { + if (putgrent (self->priv->group, dup_file) < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, FALSE); } @@ -505,15 +505,15 @@ static gboolean _update_gshadow_entry ( GumdDaemonGroup *self, GumOpType op, - FILE *origf, - FILE *newf, + FILE *source_file, + FILE *dup_file, gpointer user_data, GError **error) { gboolean done = FALSE; struct sgrp *entry = NULL; - while ((entry = fgetsgent (origf)) != NULL) { + while ((entry = fgetsgent (source_file)) != NULL) { if (!done) { switch (op) { case GUM_OPTYPE_ADD: @@ -534,7 +534,7 @@ _update_gshadow_entry ( gchar *old_name = user_data ? (gchar *)user_data : self->priv->gshadow->sg_namp; if (g_strcmp0 (old_name, entry->sg_namp) == 0) { - if (putsgent (self->priv->gshadow, newf) < 0) { + if (putsgent (self->priv->gshadow, dup_file) < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, FALSE); } @@ -547,7 +547,7 @@ _update_gshadow_entry ( break; } } - if (putsgent (entry, newf) < 0) { + if (putsgent (entry, dup_file) < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, FALSE); } @@ -555,7 +555,7 @@ _update_gshadow_entry ( /* Write entry to file in case it is first entry in the file */ if (!done && op == GUM_OPTYPE_ADD) { - if (putsgent (self->priv->gshadow, newf) < 0) { + if (putsgent (self->priv->gshadow, dup_file) < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, FALSE); } @@ -837,7 +837,7 @@ gumd_daemon_group_add ( } if (!gum_file_update (G_OBJECT (self), GUM_OPTYPE_ADD, - (GumFileUpdateFunc)_update_daemon_group_entry, + (GumFileUpdateCB)_update_daemon_group_entry, gum_config_get_string (self->priv->config, GUM_CONFIG_GENERAL_GROUP_FILE), NULL, error)) { gum_lock_pwdf_unlock (); @@ -848,7 +848,7 @@ gumd_daemon_group_add ( GUM_CONFIG_GENERAL_GSHADOW_FILE); if (g_file_test (shadow_file, G_FILE_TEST_EXISTS) && !gum_file_update (G_OBJECT (self), GUM_OPTYPE_ADD, - (GumFileUpdateFunc)_update_gshadow_entry, shadow_file, NULL, + (GumFileUpdateCB)_update_gshadow_entry, shadow_file, NULL, error)) { gum_lock_pwdf_unlock (); return FALSE; @@ -904,7 +904,7 @@ gumd_daemon_group_delete ( } if (!gum_file_update (G_OBJECT (self), GUM_OPTYPE_DELETE, - (GumFileUpdateFunc)_update_daemon_group_entry, + (GumFileUpdateCB)_update_daemon_group_entry, gum_config_get_string (self->priv->config, GUM_CONFIG_GENERAL_GROUP_FILE), NULL, error)) { gum_lock_pwdf_unlock (); @@ -915,7 +915,7 @@ gumd_daemon_group_delete ( GUM_CONFIG_GENERAL_GSHADOW_FILE); if (g_file_test (shadow_file, G_FILE_TEST_EXISTS) && !gum_file_update (G_OBJECT (self), GUM_OPTYPE_DELETE, - (GumFileUpdateFunc)_update_gshadow_entry, shadow_file, NULL, + (GumFileUpdateCB)_update_gshadow_entry, shadow_file, NULL, error)) { gum_lock_pwdf_unlock (); return FALSE; @@ -981,7 +981,7 @@ gumd_daemon_group_update ( GUM_STR_DUP (grp->gr_name, old_name); if (!gum_file_update (G_OBJECT (self), GUM_OPTYPE_MODIFY, - (GumFileUpdateFunc)_update_daemon_group_entry, + (GumFileUpdateCB)_update_daemon_group_entry, gum_config_get_string (self->priv->config, GUM_CONFIG_GENERAL_GROUP_FILE), old_name, error)) { g_free (old_name); @@ -993,7 +993,7 @@ gumd_daemon_group_update ( GUM_CONFIG_GENERAL_GSHADOW_FILE); if (g_file_test (shadow_file, G_FILE_TEST_EXISTS) && !gum_file_update (G_OBJECT (self), GUM_OPTYPE_MODIFY, - (GumFileUpdateFunc)_update_gshadow_entry, + (GumFileUpdateCB)_update_gshadow_entry, shadow_file, old_name, error)) { g_free (old_name); gum_lock_pwdf_unlock (); @@ -1065,7 +1065,7 @@ gumd_daemon_group_add_member ( } if (!gum_file_update (G_OBJECT (self), GUM_OPTYPE_MODIFY, - (GumFileUpdateFunc)_update_daemon_group_entry, + (GumFileUpdateCB)_update_daemon_group_entry, gum_config_get_string (self->priv->config, GUM_CONFIG_GENERAL_GROUP_FILE), NULL, error)) { gum_lock_pwdf_unlock (); @@ -1076,7 +1076,7 @@ gumd_daemon_group_add_member ( GUM_CONFIG_GENERAL_GSHADOW_FILE); if (g_file_test (shadow_file, G_FILE_TEST_EXISTS) && !gum_file_update (G_OBJECT (self), GUM_OPTYPE_MODIFY, - (GumFileUpdateFunc)_update_gshadow_entry, shadow_file, + (GumFileUpdateCB)_update_gshadow_entry, shadow_file, NULL, error)) { gum_lock_pwdf_unlock (); return FALSE; @@ -1144,7 +1144,7 @@ gumd_daemon_group_delete_member ( } if (!gum_file_update (G_OBJECT (self), GUM_OPTYPE_MODIFY, - (GumFileUpdateFunc)_update_daemon_group_entry, + (GumFileUpdateCB)_update_daemon_group_entry, gum_config_get_string (self->priv->config, GUM_CONFIG_GENERAL_GROUP_FILE), NULL, error)) { gum_lock_pwdf_unlock (); @@ -1155,7 +1155,7 @@ gumd_daemon_group_delete_member ( GUM_CONFIG_GENERAL_GSHADOW_FILE); if (g_file_test (shadow_file, G_FILE_TEST_EXISTS) && !gum_file_update (G_OBJECT (self), GUM_OPTYPE_MODIFY, - (GumFileUpdateFunc)_update_gshadow_entry, shadow_file, NULL, + (GumFileUpdateCB)_update_gshadow_entry, shadow_file, NULL, error)) { gum_lock_pwdf_unlock (); return FALSE; @@ -1172,9 +1172,9 @@ gumd_daemon_group_delete_user_membership ( GError **error) { gboolean retval = TRUE; - FILE *origf = NULL, *newf = NULL; - gchar *newfn = NULL; - const gchar *origfn = NULL; + FILE *source_file = NULL, *dup_file = NULL; + gchar *dup_file_path = NULL; + const gchar *source_file_path = NULL; if (!config || !user_name) { GUM_RETURN_WITH_ERROR (GUM_ERROR_GROUP_INVALID_DATA, @@ -1191,14 +1191,17 @@ gumd_daemon_group_delete_user_membership ( } /* update group entries */ - origfn = gum_config_get_string (config, GUM_CONFIG_GENERAL_GROUP_FILE); - newfn = g_strdup_printf ("%s-tmp.%lu", origfn, (unsigned long)getpid ()); + source_file_path = gum_config_get_string (config, + GUM_CONFIG_GENERAL_GROUP_FILE); + dup_file_path = g_strdup_printf ("%s-tmp.%lu", source_file_path, + (unsigned long)getpid ()); - retval = gum_file_open_db_files (origfn, newfn, &origf, &newf, error); + retval = gum_file_open_db_files (source_file_path, dup_file_path, + &source_file, &dup_file, error); if (!retval) goto _finished; struct group *gent = NULL; - while ((gent = fgetgrent (origf)) != NULL) { + while ((gent = fgetgrent (source_file)) != NULL) { if (gum_string_utils_search_stringv (gent->gr_mem, user_name)) { gint status = 0; @@ -1207,7 +1210,7 @@ gumd_daemon_group_delete_user_membership ( GUM_STR_FREEV (gdest->gr_mem); gdest->gr_mem = gum_string_utils_delete_string (gent->gr_mem, user_name); - status = putgrent (gdest, newf); + status = putgrent (gdest, dup_file); _free_daemon_group_entry (gdest); if (status < 0) { GUM_SET_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", @@ -1215,33 +1218,37 @@ gumd_daemon_group_delete_user_membership ( break; } - } else if (putgrent (gent, newf) < 0) { + } else if (putgrent (gent, dup_file) < 0) { GUM_SET_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, retval, FALSE); break; } } if (!retval) { - fclose (newf); - g_unlink (newfn); - fclose (origf); + fclose (dup_file); + g_unlink (dup_file_path); + fclose (source_file); } else { - retval = gum_file_close_db_files (origfn, newfn, origf, newf, error); + retval = gum_file_close_db_files (source_file_path, dup_file_path, + source_file, dup_file, error); } - newf = NULL; origf = NULL; - g_free (newfn); newfn = NULL; + dup_file = NULL; source_file = NULL; + g_free (dup_file_path); dup_file_path = NULL; /* update gshadow entries */ - origfn = gum_config_get_string (config, GUM_CONFIG_GENERAL_GSHADOW_FILE); - if (!g_file_test (origfn, G_FILE_TEST_EXISTS)) goto _finished; + source_file_path = gum_config_get_string (config, + GUM_CONFIG_GENERAL_GSHADOW_FILE); + if (!g_file_test (source_file_path, G_FILE_TEST_EXISTS)) goto _finished; - newfn = g_strdup_printf ("%s-tmp.%lu", origfn, (unsigned long)getpid ()); + dup_file_path = g_strdup_printf ("%s-tmp.%lu", source_file_path, + (unsigned long)getpid ()); - retval = gum_file_open_db_files (origfn, newfn, &origf, &newf, error); + retval = gum_file_open_db_files (source_file_path, dup_file_path, + &source_file, &dup_file, error); if (!retval) goto _finished; struct sgrp *gsent = NULL; - while ((gsent = fgetsgent (origf)) != NULL) { + while ((gsent = fgetsgent (source_file)) != NULL) { gboolean is_mem = gum_string_utils_search_stringv (gsent->sg_mem, user_name); @@ -1263,7 +1270,7 @@ gumd_daemon_group_delete_user_membership ( user_name); } - status = putsgent (gsdest, newf); + status = putsgent (gsdest, dup_file); _free_gshadow_entry (gsdest); if (status < 0) { GUM_SET_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", @@ -1271,7 +1278,7 @@ gumd_daemon_group_delete_user_membership ( break; } - } else if (putsgent (gsent, newf) < 0) { + } else if (putsgent (gsent, dup_file) < 0) { GUM_SET_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, retval, FALSE); break; @@ -1279,15 +1286,16 @@ gumd_daemon_group_delete_user_membership ( } if (!retval) { - fclose (newf); - g_unlink (newfn); - fclose (origf); + fclose (dup_file); + g_unlink (dup_file_path); + fclose (source_file); } else { - retval = gum_file_close_db_files (origfn, newfn, origf, newf, error); + retval = gum_file_close_db_files (source_file_path, dup_file_path, + source_file, dup_file, error); } _finished: - g_free (newfn); + g_free (dup_file_path); gum_lock_pwdf_unlock (); return retval; diff --git a/src/daemon/gumd-daemon-user.c b/src/daemon/gumd-daemon-user.c index 9398f7d..6b97a80 100644 --- a/src/daemon/gumd-daemon-user.c +++ b/src/daemon/gumd-daemon-user.c @@ -837,8 +837,8 @@ static gboolean _update_passwd_entry ( GumdDaemonUser *self, GumOpType op, - FILE *origf, - FILE *newf, + FILE *source_file, + FILE *dup_file, gpointer user_data, GError **error) { @@ -846,12 +846,12 @@ _update_passwd_entry ( gboolean done = FALSE; struct passwd *entry; - while ((entry = fgetpwent (origf)) != NULL) { + while ((entry = fgetpwent (source_file)) != NULL) { if (!done) { switch (op) { case GUM_OPTYPE_ADD: if (self->priv->pw->pw_uid < entry->pw_uid) { - if (putpwent (self->priv->pw, newf) < 0) { + if (putpwent (self->priv->pw, dup_file) < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, FALSE); } @@ -872,7 +872,7 @@ _update_passwd_entry ( if (self->priv->pw->pw_uid == entry->pw_uid && self->priv->pw->pw_gid == entry->pw_gid && g_strcmp0 (old_name, entry->pw_name) == 0) { - if (putpwent (self->priv->pw, newf) < 0) { + if (putpwent (self->priv->pw, dup_file) < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, FALSE); } @@ -885,7 +885,7 @@ _update_passwd_entry ( break; } } - if (putpwent (entry, newf) < 0) { + if (putpwent (entry, dup_file) < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, FALSE); } @@ -893,7 +893,7 @@ _update_passwd_entry ( /* Write entry to file in case it is first entry in the file */ if (!done && op == GUM_OPTYPE_ADD) { - if (putpwent (self->priv->pw, newf) < 0) { + if (putpwent (self->priv->pw, dup_file) < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, "Add entry failure", error, FALSE); } @@ -935,8 +935,8 @@ static gboolean _lock_shadow_entry ( GumdDaemonUser *self, GumOpType op, - FILE *origf, - FILE *newf, + FILE *source_file, + FILE *dup_file, gpointer user_data, GError **error) { @@ -949,7 +949,7 @@ _lock_shadow_entry ( "File write failure", error, FALSE); } - while ((entry = fgetspent (origf)) != NULL) { + while ((entry = fgetspent (source_file)) != NULL) { if (!done) { switch (op) { case GUM_OPTYPE_MODIFY: { @@ -966,7 +966,7 @@ _lock_shadow_entry ( /* entry is locked, unlock it */ spent->sp_pwdp = g_strdup (entry->sp_pwdp+1); } - ret = putspent (spent, newf); + ret = putspent (spent, dup_file); _free_shadow_entry (spent); if (ret < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, @@ -981,7 +981,7 @@ _lock_shadow_entry ( break; } } - if (putspent (entry, newf) < 0) { + if (putspent (entry, dup_file) < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, FALSE); } @@ -999,8 +999,8 @@ static gboolean _update_shadow_entry ( GumdDaemonUser *self, GumOpType op, - FILE *origf, - FILE *newf, + FILE *source_file, + FILE *dup_file, gpointer user_data, GError **error) { @@ -1008,7 +1008,7 @@ _update_shadow_entry ( gboolean done = FALSE; struct spwd *entry = NULL; - while ((entry = fgetspent (origf)) != NULL) { + while ((entry = fgetspent (source_file)) != NULL) { if (!done) { switch (op) { case GUM_OPTYPE_ADD: @@ -1029,7 +1029,7 @@ _update_shadow_entry ( gchar *old_name = user_data ? (gchar *)user_data : self->priv->shadow->sp_namp; if (g_strcmp0 (old_name, entry->sp_namp) == 0) { - if (putspent (self->priv->shadow, newf) < 0) { + if (putspent (self->priv->shadow, dup_file) < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, FALSE); } @@ -1042,7 +1042,7 @@ _update_shadow_entry ( break; } } - if (putspent (entry, newf) < 0) { + if (putspent (entry, dup_file) < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, "File write failure", error, FALSE); } @@ -1050,7 +1050,7 @@ _update_shadow_entry ( /* Write entry to file in case it is first entry in the file */ if (!done && op == GUM_OPTYPE_ADD) { - if (putspent (self->priv->shadow, newf) < 0) { + if (putspent (self->priv->shadow, dup_file) < 0) { GUM_RETURN_WITH_ERROR (GUM_ERROR_FILE_WRITE, "Add entry failure", error, FALSE); } @@ -1479,11 +1479,11 @@ gumd_daemon_user_add ( } if (!gum_file_update (G_OBJECT (self), GUM_OPTYPE_ADD, - (GumFileUpdateFunc)_update_passwd_entry, + (GumFileUpdateCB)_update_passwd_entry, gum_config_get_string (self->priv->config, GUM_CONFIG_GENERAL_PASSWD_FILE), NULL, error) || !gum_file_update (G_OBJECT (self), GUM_OPTYPE_ADD, - (GumFileUpdateFunc)_update_shadow_entry, + (GumFileUpdateCB)_update_shadow_entry, gum_config_get_string (self->priv->config, GUM_CONFIG_GENERAL_SHADOW_FILE), NULL, error)) { gum_lock_pwdf_unlock (); @@ -1548,7 +1548,7 @@ gumd_daemon_user_delete ( /* lock the user */ if (!gum_file_update (G_OBJECT (self), GUM_OPTYPE_MODIFY, - (GumFileUpdateFunc)_lock_shadow_entry, + (GumFileUpdateCB)_lock_shadow_entry, gum_config_get_string (self->priv->config, GUM_CONFIG_GENERAL_SHADOW_FILE), &lock, error)) { gum_lock_pwdf_unlock (); @@ -1560,7 +1560,7 @@ gumd_daemon_user_delete ( /* unlock the user */ lock = FALSE; gum_file_update (G_OBJECT (self), GUM_OPTYPE_MODIFY, - (GumFileUpdateFunc)_lock_shadow_entry, + (GumFileUpdateCB)_lock_shadow_entry, gum_config_get_string (self->priv->config, GUM_CONFIG_GENERAL_SHADOW_FILE), &lock, NULL); gum_lock_pwdf_unlock (); @@ -1569,18 +1569,18 @@ gumd_daemon_user_delete ( } if (!gum_file_update (G_OBJECT (self), GUM_OPTYPE_DELETE, - (GumFileUpdateFunc)_update_passwd_entry, + (GumFileUpdateCB)_update_passwd_entry, gum_config_get_string (self->priv->config, GUM_CONFIG_GENERAL_PASSWD_FILE), NULL, error) || !gum_file_update (G_OBJECT (self), GUM_OPTYPE_DELETE, - (GumFileUpdateFunc)_update_shadow_entry, + (GumFileUpdateCB)_update_shadow_entry, gum_config_get_string (self->priv->config, GUM_CONFIG_GENERAL_SHADOW_FILE), NULL, error)) { /* unlock the user */ lock = FALSE; gum_file_update (G_OBJECT (self), GUM_OPTYPE_MODIFY, - (GumFileUpdateFunc)_lock_shadow_entry, + (GumFileUpdateCB)_lock_shadow_entry, gum_config_get_string (self->priv->config, GUM_CONFIG_GENERAL_SHADOW_FILE), &lock, NULL); gum_lock_pwdf_unlock (); @@ -1685,11 +1685,11 @@ gumd_daemon_user_update ( GUM_STR_DUP (pw->pw_name, old_name); if (!gum_file_update (G_OBJECT (self), GUM_OPTYPE_MODIFY, - (GumFileUpdateFunc)_update_passwd_entry, + (GumFileUpdateCB)_update_passwd_entry, gum_config_get_string (self->priv->config, GUM_CONFIG_GENERAL_PASSWD_FILE), old_name, error) || !gum_file_update (G_OBJECT (self), GUM_OPTYPE_MODIFY, - (GumFileUpdateFunc)_update_shadow_entry, + (GumFileUpdateCB)_update_shadow_entry, gum_config_get_string (self->priv->config, GUM_CONFIG_GENERAL_SHADOW_FILE), old_name, error)) { g_free (old_name); diff --git a/test/common/commontest.c b/test/common/commontest.c index c907117..d8a97eb 100644 --- a/test/common/commontest.c +++ b/test/common/commontest.c @@ -346,7 +346,7 @@ START_TEST (test_file) fail_if (stat (origfn, &origst) < 0); fail_unless (gum_file_update (user, GUM_OPTYPE_ADD, - (GumFileUpdateFunc)_update_file_entries, origfn, NULL, + (GumFileUpdateCB)_update_file_entries, origfn, NULL, &error) == TRUE); fail_unless (error == NULL); -- 2.7.4