From de15aeb614e438bf12d2f68127d6ef2186187e5c Mon Sep 17 00:00:00 2001 From: nalin Date: Jul 24 2001 15:42:00 +0000 Subject: add lpasswd app, make sure all apps start up popt with the right names --- diff --git a/apps/Makefile.am b/apps/Makefile.am index af17e95..71727c6 100644 --- a/apps/Makefile.am +++ b/apps/Makefile.am @@ -1,4 +1,4 @@ -sbin_PROGRAMS = lchage luseradd lusermod luserdel lgroupadd lgroupmod lgroupdel +sbin_PROGRAMS = lchage luseradd lusermod luserdel lgroupadd lgroupmod lgroupdel lpasswd include $(top_srcdir)/Makefile.extra @@ -36,3 +36,6 @@ lgroupdel_SOURCES = \ apputil.c \ apputil.h \ lgroupdel.c + +lpasswd_SOURCES = \ + lpasswd.c diff --git a/apps/lchage.c b/apps/lchage.c index e86ca65..c06d028 100644 --- a/apps/lchage.c +++ b/apps/lchage.c @@ -100,7 +100,7 @@ main(int argc, const char **argv) textdomain(PACKAGE); setlocale(LC_ALL, ""); - popt = poptGetContext("chage", argc, argv, options, 0); + popt = poptGetContext("lchage", argc, argv, options, 0); poptSetOtherOptionHelp(popt, _("[OPTION...] user")); c = poptGetNextOpt(popt); g_return_val_if_fail(c == -1, 0); diff --git a/apps/lgroupadd.c b/apps/lgroupadd.c index fc82368..3ab431d 100644 --- a/apps/lgroupadd.c +++ b/apps/lgroupadd.c @@ -57,7 +57,7 @@ main(int argc, const char **argv) textdomain(PACKAGE); setlocale(LC_ALL, ""); - popt = poptGetContext("groupadd", argc, argv, options, 0); + popt = poptGetContext("lgroupadd", argc, argv, options, 0); poptSetOtherOptionHelp(popt, _("[OPTION...] group")); c = poptGetNextOpt(popt); g_return_val_if_fail(c == -1, 0); diff --git a/apps/lgroupmod.c b/apps/lgroupmod.c index eb7717c..78e5d8e 100644 --- a/apps/lgroupmod.c +++ b/apps/lgroupmod.c @@ -73,7 +73,7 @@ main(int argc, const char **argv) textdomain(PACKAGE); setlocale(LC_ALL, ""); - popt = poptGetContext("groupmod", argc, argv, options, 0); + popt = poptGetContext("lgroupmod", argc, argv, options, 0); poptSetOtherOptionHelp(popt, _("[OPTION...] group")); c = poptGetNextOpt(popt); g_return_val_if_fail(c == -1, 0); diff --git a/apps/lpasswd.c b/apps/lpasswd.c new file mode 100644 index 0000000..d75f00b --- /dev/null +++ b/apps/lpasswd.c @@ -0,0 +1,143 @@ +/* Copyright (C) 2000,2001 Red Hat, Inc. + * + * This is free software; you can redistribute it and/or modify it under + * the terms of the GNU Library General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ident "$Id$" + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include "../include/libuser/user.h" +#include "apputil.h" + +int +main(int argc, const char **argv) +{ + struct lu_context *ctx = NULL; + struct lu_ent *ent = NULL; + struct lu_error *error = NULL; + char *password = NULL, *cryptedPassword = NULL; + const char *user = NULL; + int c; + int plain_fd = -1, crypted_fd = -1; + int interactive = 0, group = 0; + poptContext popt; + struct poptOption options[] = { + {"interactive", 'i', POPT_ARG_NONE, &interactive, 0, + "prompt for all information", NULL}, + {"group", 'g', POPT_ARG_NONE, &group, 0, + "set group password instead of user password", NULL}, + {"plainpassword", 'P', POPT_ARG_STRING, &password, 0, + "new plain password", NULL}, + {"password", 'p', POPT_ARG_STRING, &cryptedPassword, 0, + "new crypted password", NULL}, + {"plainpassword-fd", 'F', POPT_ARG_INT, &plain_fd, 0, + "read new plain password from given descriptor", NULL}, + {"password-fd", 'f', POPT_ARG_INT, &crypted_fd, 0, + "read new crypted password from given descriptor", NULL}, + POPT_AUTOHELP + {NULL, '\0', POPT_ARG_NONE, NULL, 0, NULL}, + }; + + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); + setlocale(LC_ALL, ""); + + popt = poptGetContext("lpasswd", argc, argv, options, 0); + poptSetOtherOptionHelp(popt, _("[OPTION...] user")); + c = poptGetNextOpt(popt); + g_return_val_if_fail(c == -1, 0); + user = poptGetArg(popt); + + if(user == NULL) { + fprintf(stderr, _("No user name specified.\n")); + return 1; + } + + if((password == NULL) && (cryptedPassword == NULL) && (plain_fd == -1) && (crypted_fd == -1)) { + fprintf(stderr, _("None of -P, -p, -F, -f specified.\n")); + return 1; + } + + ctx = lu_start(user, group ? lu_group : lu_user, NULL, NULL, + interactive ? lu_prompt_console:lu_prompt_console_quiet, + NULL, &error); + g_return_val_if_fail(ctx != NULL, 1); + + ent = lu_ent_new(); + + if(!group) { + if(lu_user_lookup_name(ctx, user, ent, &error) == FALSE) { + fprintf(stderr, _("User %s does not exist.\n"), user); + return 2; + } + } else { + if(lu_group_lookup_name(ctx, user, ent, &error) == FALSE) { + fprintf(stderr, _("Group %s does not exist.\n"), user); + return 2; + } + } + + if(plain_fd != -1) { + char buf[LINE_MAX]; + int i; + memset(buf, '\0', sizeof(buf)); + i = read(plain_fd, buf, sizeof(buf)); + while((i > 0) && ((buf[i - 1] == '\r') || (buf[i - 1] == '\n'))) { + buf[--i] = '\0'; + } + password = buf; + } else + if(crypted_fd != -1) { + char buf[LINE_MAX]; + int i; + memset(buf, '\0', sizeof(buf)); + i = read(crypted_fd, buf, sizeof(buf)); + while((i > 0) && ((buf[i - 1] == '\r') || (buf[i - 1] == '\n'))) { + buf[--i] = '\0'; + } + password = g_strconcat("{crypt}", buf, NULL); + } else + if(cryptedPassword != NULL) { + password = g_strconcat("{crypt}", cryptedPassword, NULL); + } + + if(!group) { + if(lu_user_setpass(ctx, ent, password, &error) == FALSE) { + fprintf(stderr, _("Error setting password for user %s: %s.\n"), user, error->string); + return 3; + } + } else { + if(lu_group_setpass(ctx, ent, password, &error) == FALSE) { + fprintf(stderr, _("Error setting password for group %s: %s.\n"), user, error->string); + return 3; + } + } + + lu_ent_free(ent); + + lu_end(ctx); + + return 0; +} diff --git a/apps/luseradd.c b/apps/luseradd.c index 478eab0..45dc963 100644 --- a/apps/luseradd.c +++ b/apps/luseradd.c @@ -80,7 +80,7 @@ main(int argc, const char **argv) textdomain(PACKAGE); setlocale(LC_ALL, ""); - popt = poptGetContext("useradd", argc, argv, options, 0); + popt = poptGetContext("luseradd", argc, argv, options, 0); poptSetOtherOptionHelp(popt, _("[OPTION...] user")); c = poptGetNextOpt(popt); g_return_val_if_fail(c == -1, 0); diff --git a/configure.in b/configure.in index 14e0bc2..9095b55 100644 --- a/configure.in +++ b/configure.in @@ -1,5 +1,5 @@ AC_INIT(configure.in) -AM_INIT_AUTOMAKE(libuser,0.19) +AM_INIT_AUTOMAKE(libuser,0.20) CFLAGS="$CFLAGS -D_GNU_SOURCE" diff --git a/libuser.spec.in b/libuser.spec.in index c138bb8..cec11a6 100644 --- a/libuser.spec.in +++ b/libuser.spec.in @@ -73,6 +73,7 @@ rm -fr $RPM_BUILD_ROOT - use GTrees instead of GHashTables for most internal tables - files: complain properly about unset attributes - files: group passwords are single-valued, not multiple-valued +- add lpasswd app, make sure all apps start up popt with the right names * Mon Jul 23 2001 Nalin Dahyabhai - actually make the new optional arguments optional