[gnome-control-center/wip/lantw/fix-user-panel-problem-on-freebsd: 3/4] user-accounts: Use 'pw usershow' to check username validity on FreeBSD



commit dc040cb598120318cb9dbdc9211b2d6b21a0b78b
Author: Ting-Wei Lan <lantw src gnome org>
Date:   Thu Aug 29 11:59:55 2019 +0800

    user-accounts: Use 'pw usershow' to check username validity on FreeBSD
    
    FreeBSD has no standalone 'usermod' command. Most user and group
    management functions are provided as subcommands of 'pw' tool.

 panels/user-accounts/user-utils.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
---
diff --git a/panels/user-accounts/user-utils.c b/panels/user-accounts/user-utils.c
index 72401e40b..ce1815cc3 100644
--- a/panels/user-accounts/user-utils.c
+++ b/panels/user-accounts/user-utils.c
@@ -29,6 +29,10 @@
 #include <utmpx.h>
 #include <pwd.h>
 
+#ifdef __FreeBSD__
+#include <sysexits.h>
+#endif
+
 #include <gio/gio.h>
 #include <gio/gunixoutputstream.h>
 #include <glib/gi18n.h>
@@ -403,10 +407,17 @@ is_valid_username_data_free (isValidUsernameData *data)
         g_free (data);
 }
 
+#ifdef __FreeBSD__
+/* Taken from pw(8) man page. */
+#define E_SUCCESS EX_OK
+#define E_BAD_ARG EX_DATAERR
+#define E_NOTFOUND EX_NOUSER
+#else
 /* Taken from usermod.c in shadow-utils. */
 #define E_SUCCESS 0
 #define E_BAD_ARG 3
 #define E_NOTFOUND 6
+#endif
 
 static void
 is_valid_username_child_watch_cb (GPid pid,
@@ -481,6 +492,17 @@ is_valid_username_async (const gchar *username,
                 return;
         }
 
+#ifdef __FreeBSD__
+        /* Abuse "pw usershow -n <name>" in the same way as the code below. We
+         * don't use "pw usermod -n <name> -N -l <newname>" here because it has
+         * a special case for "root" to reject changes to the root user.
+         */
+        argv[0] = "pw";
+        argv[1] = "usershow";
+        argv[2] = "-n";
+        argv[3] = data->username;
+        argv[4] = NULL;
+#else
         /* "usermod --login" is meant to be used to change a username, but the
          * exit codes can be safely abused to check the validity of username.
          * However, the current "usermod" implementation may change in the
@@ -493,6 +515,7 @@ is_valid_username_async (const gchar *username,
         argv[3] = "--";
         argv[4] = data->username;
         argv[5] = NULL;
+#endif
 
         if (!g_spawn_async (NULL, argv, NULL,
                             G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD |


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]