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.
This commit is contained in:
Ting-Wei Lan 2019-08-29 11:59:55 +08:00 committed by Georges Basile Stavracas Neto
parent 54caab7b07
commit 564aa72b5e

View file

@ -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 |