[1537] in Moira
chsh rewrite
daemon@ATHENA.MIT.EDU (Garry Zacheiss)
Tue Apr 25 20:46:32 2000
Message-Id: <200004260046.UAA08170@sweet-transvestite.mit.edu>
To: moiradev@MIT.EDU
Date: Tue, 25 Apr 2000 20:46:14 -0400
From: Garry Zacheiss <zacheiss@MIT.EDU>
This changes chsh to accept a -w flag to change the user's
Windows shell. While I was at it, I also added -db support for
connecting to a non-default Moira server.
Index: chsh.c
===================================================================
RCS file: /afs/athena.mit.edu/astaff/project/moiradev/repository/moira/clients/chsh/chsh.c,v
retrieving revision 1.1
diff -c -r1.1 chsh.c
*** chsh.c 2000/03/16 06:03:59 1.1
--- chsh.c 2000/04/25 23:46:22
***************
*** 24,34 ****
--- 24,37 ----
#include <string.h>
#include <unistd.h>
+ #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b))
+
RCSID("$Header: /afs/athena.mit.edu/astaff/project/moiradev/repository/moira/clients/chsh/chsh.c,v 1.1 2000/03/16 06:03:59 zacheiss Exp $");
void usage(void);
int chsh(char *uname);
int get_shell(int argc, char **argv, void *uname);
+ int get_winshell(int argc, char **argv, void *uname);
int get_fmodtime(int argc, char **argv, void *uname);
void check_shell(char *shell);
#ifndef HAVE_GETUSERSHELL
***************
*** 36,77 ****
#endif
char *whoami;
int main(int argc, char *argv[])
{
! char *uname;
if ((whoami = strrchr(argv[0], '/')) == NULL)
whoami = argv[0];
else
whoami++;
! if (argc > 2)
! usage();
! if (argc == 2)
! uname = argv[1];
! else
{
uname = mrcl_krb_user();
if (!uname)
exit(1);
}
! exit(chsh(uname));
! }
!
! int chsh(char *uname)
! {
! int status; /* general purpose exit status */
! int q_argc; /* argc for mr_query */
! char *q_argv[U_END]; /* argv for mr_query */
! char *motd; /* determine Moira server status */
!
! int got_one = 0; /* have we got a new shell yet? */
! char shell[BUFSIZ]; /* the new shell */
! if (mrcl_connect(NULL, "chsh", 2, 1) != MRCL_SUCCESS)
exit(1);
/* First, do an access check */
--- 39,103 ----
#endif
char *whoami;
+ char *uname;
int main(int argc, char *argv[])
{
! int status; /* general purpose exit status */
! int q_argc; /* argc for mr_query */
! char *q_argv[U_END]; /* argv for mr_query */
! char *motd; /* determine Moira server status */
! int got_one = 0; /* Have we got a shell yet? */
! char shell[BUFSIZ]; /* Our new shell */
! int windowsflg = 0, unixflg = 0;
! char **arg = argv;
! char *server = NULL;
if ((whoami = strrchr(argv[0], '/')) == NULL)
whoami = argv[0];
else
whoami++;
! /* parse our command line options */
! while (++arg - argv < argc)
! {
! if (**arg == '-')
! {
! if (argis("w", "winshell"))
! windowsflg++;
! else if (argis("u", "unixshell"))
! unixflg++;
! else if (argis("db", "database"))
! {
! if (arg - argv < argc - 1)
! {
! ++arg;
! server = *arg;
! }
! else
! usage();
! }
! }
! else if (uname == NULL)
! uname = *arg;
! else
! usage();
! }
! if (!uname)
{
uname = mrcl_krb_user();
if (!uname)
exit(1);
}
! if (!unixflg && !windowsflg)
! unixflg++;
! if (unixflg && windowsflg)
! usage();
!
! if (mrcl_connect(server, "chsh", 3, 1) != MRCL_SUCCESS)
exit(1);
/* First, do an access check */
***************
*** 89,98 ****
printf("Changing login shell for %s.\n", uname);
/* Display current information */
!
q_argv[NAME] = uname;
q_argc = NAME + 1;
!
if ((status = mr_query("get_finger_by_login", q_argc, q_argv,
get_fmodtime, uname)))
{
--- 115,124 ----
printf("Changing login shell for %s.\n", uname);
/* Display current information */
!
q_argv[NAME] = uname;
q_argc = NAME + 1;
!
if ((status = mr_query("get_finger_by_login", q_argc, q_argv,
get_fmodtime, uname)))
{
***************
*** 100,146 ****
exit(2);
}
! if ((status = mr_query("get_user_account_by_login", q_argc, q_argv,
! get_shell, uname)))
{
! com_err(whoami, status, "while getting user information.");
! exit(2);
}
!
! /* Ask for new shell */
! while (!got_one)
{
! printf("New shell: ");
! if (!fgets(shell, sizeof(shell), stdin))
! exit(0);
! got_one = (strlen(shell) > 1);
! }
!
! shell[strlen(shell) - 1] = 0; /* strip off newline */
!
! /* Make sure we have a valid shell. This routine could exit */
! check_shell(shell);
! /* Change shell */
! printf("Changing shell to %s...\n", shell);
! q_argv[USH_NAME] = uname;
! q_argv[USH_SHELL] = shell;
! q_argc = USH_END;
! if ((status = mr_query("update_user_shell", q_argc, q_argv, NULL, NULL)))
{
! com_err(whoami, status, "while changing shell.");
! exit(2);
}
! printf("Shell successfully changed.\n");
! mr_disconnect();
! return 0;
}
! int get_shell(int argc, char **argv, void *uname)
{
/* We'll just take the first information we get since login names
cannot be duplicated in the database. */
--- 126,228 ----
exit(2);
}
! if (unixflg)
{
! if ((status = mr_query("get_user_account_by_login", q_argc, q_argv,
! get_shell, uname)))
! {
! com_err(whoami, status, "while getting user information.");
! exit(2);
! }
!
! /* Ask for new shell */
! while (!got_one)
! {
! printf("New shell: ");
! if (!fgets(shell, sizeof(shell), stdin))
! exit(0);
! got_one = (strlen(shell) > 1);
! }
!
! shell[strlen(shell) - 1] = 0; /* strip off newline */
!
! /* Make sure we have a valid shell. This routine could exit */
! check_shell(shell);
!
! /* Change shell */
!
! printf("Changing shell to %s...\n", shell);
!
! q_argv[USH_NAME] = uname;
! q_argv[USH_SHELL] = shell;
! q_argc = USH_END;
! if ((status = mr_query("update_user_shell", q_argc, q_argv, NULL, NULL)))
! {
! com_err(whoami, status, "while changing shell.");
! exit(2);
! }
!
! printf("Shell successfully changed.\n");
}
! else if (windowsflg)
{
! if ((status = mr_query("get_user_account_by_login", q_argc, q_argv,
! get_winshell, uname)))
! {
! com_err(whoami, status, "while getting user information.");
! exit(2);
! }
!
! /* Ask for new Windows shell */
! while(!got_one)
! {
! printf("New Windows shell: ");
! if (!fgets(shell, sizeof(shell), stdin))
! exit(0);
! got_one = (strlen(shell) > 1);
! }
!
! shell[strlen(shell) - 1] = 0; /* strip off newline */
! /* Change shell */
!
! printf("Changing Windows shell to %s...\n", shell);
!
! q_argv[USH_NAME] = uname;
! q_argv[USH_SHELL] = shell;
! q_argc = USH_END;
! if ((status = mr_query("update_user_windows_shell", q_argc, q_argv,
! NULL, NULL)))
! {
! com_err(whoami, status, "while changing Windows shell.");
! exit(2);
! }
!
! printf("Windows shell successfully changed.\n");
! }
!
! mr_disconnect();
!
! return 0;
! }
! int get_shell(int argc, char **argv, void *uname)
! {
! /* We'll just take the first information we get since login names
! cannot be duplicated in the database. */
! if (argc < U_END || strcmp(argv[U_NAME], uname))
{
! fprintf(stderr, "Some internal error has occurred. Try again.\n");
! exit(3);
}
! printf("Current shell for %s is %s.\n", (char *)uname, argv[U_SHELL]);
! return MR_ABORT; /* Don't pay attention to other matches. */
}
! int get_winshell(int argc, char **argv, void *uname)
{
/* We'll just take the first information we get since login names
cannot be duplicated in the database. */
***************
*** 151,157 ****
exit(3);
}
! printf("Current shell for %s is %s.\n", (char *)uname, argv[U_SHELL]);
return MR_ABORT; /* Don't pay attention to other matches. */
}
--- 233,240 ----
exit(3);
}
! printf("Current Windows shell for %s is %s.\n", (char *)uname,
! argv[U_WINCONSOLESHELL]);
return MR_ABORT; /* Don't pay attention to other matches. */
}
***************
*** 178,184 ****
char *valid_shell;
int ok = 0;
! while ((valid_shell = getusershell()))
{
if (!strcmp(shell, valid_shell))
{
--- 261,267 ----
char *valid_shell;
int ok = 0;
! while ((valid_shell = (char *)getusershell()))
{
if (!strcmp(shell, valid_shell))
{
***************
*** 218,224 ****
void usage(void)
{
! fprintf(stderr, "Usage: %s [user]\n", whoami);
exit(1);
}
--- 301,307 ----
void usage(void)
{
! fprintf(stderr, "Usage: %s [-w|-u] [user]\n", whoami);
exit(1);
}