3 * Talk to the Moira database to change a person's login shell. The chosen
4 * shell must exist. A warning will be issued if the shell is not in
7 * chsh with no modifiers changes the shell of the user who is running
10 * If a commandline argument is given, it is taken to be the username
11 * of the user whose shell is to be changed.
13 * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology
14 * For copying and distribution information, please see the file
18 #include <mit-copyright.h>
20 #include <moira_site.h>
30 int chsh(char *uname);
31 int get_shell(int argc, char **argv, void *uname);
32 int get_fmodtime(int argc, char **argv, void *uname);
33 void check_shell(char *shell);
34 #ifndef HAVE_GETUSERSHELL
35 char *getusershell(void);
40 int main(int argc, char *argv[])
44 if ((whoami = strrchr(argv[0], '/')) == NULL)
56 uname = mrcl_krb_user();
66 int status; /* general purpose exit status */
67 int q_argc; /* argc for mr_query */
68 char *q_argv[U_END]; /* argv for mr_query */
69 char *motd; /* determine Moira server status */
71 int got_one = 0; /* have we got a new shell yet? */
72 char shell[BUFSIZ]; /* the new shell */
74 if (mrcl_connect(NULL, "chsh", 2, 1) != MRCL_SUCCESS)
77 /* First, do an access check */
79 q_argv[USH_NAME] = uname;
80 q_argv[USH_SHELL] = "junk";
83 if ((status = mr_access("update_user_shell", q_argc, q_argv)))
85 com_err(whoami, status, "; shell not\nchanged.");
89 printf("Changing login shell for %s.\n", uname);
91 /* Display current information */
96 if ((status = mr_query("get_finger_by_login", q_argc, q_argv,
97 get_fmodtime, uname)))
99 com_err(whoami, status, "while getting user information.");
103 if ((status = mr_query("get_user_account_by_login", q_argc, q_argv,
106 com_err(whoami, status, "while getting user information.");
110 /* Ask for new shell */
113 printf("New shell: ");
114 if (!fgets(shell, sizeof(shell), stdin))
116 got_one = (strlen(shell) > 1);
119 shell[strlen(shell) - 1] = 0; /* strip off newline */
121 /* Make sure we have a valid shell. This routine could exit */
126 printf("Changing shell to %s...\n", shell);
128 q_argv[USH_NAME] = uname;
129 q_argv[USH_SHELL] = shell;
131 if ((status = mr_query("update_user_shell", q_argc, q_argv, NULL, NULL)))
133 com_err(whoami, status, "while changing shell.");
137 printf("Shell successfully changed.\n");
143 int get_shell(int argc, char **argv, void *uname)
145 /* We'll just take the first information we get since login names
146 cannot be duplicated in the database. */
148 if (argc < U_END || strcmp(argv[U_NAME], uname))
150 fprintf(stderr, "Some internal error has occurred. Try again.\n");
154 printf("Current shell for %s is %s.\n", (char *)uname, argv[U_SHELL]);
156 return MR_ABORT; /* Don't pay attention to other matches. */
159 int get_fmodtime(int argc, char **argv, void *uname)
161 /* We'll just take the first information we get since login names
162 cannot be duplicated in the database. */
164 if (argc < F_END || strcmp(argv[F_NAME], uname))
166 fprintf(stderr, "Some internal error has occurred. Try again.\n");
170 printf("Finger information last changed on %s\n", argv[F_MODTIME]);
171 printf("by user %s with %s.\n", argv[F_MODBY], argv[F_MODWITH]);
173 return MR_ABORT; /* Don't pay attention to other matches. */
176 void check_shell(char *shell)
181 while ((valid_shell = getusershell()))
183 if (!strcmp(shell, valid_shell))
188 else if (!strcmp(shell, 1 + strrchr(valid_shell, '/')))
191 strcpy(shell, valid_shell);
200 fprintf(stderr, "%s is not a standard shell. ", shell);
201 fprintf(stderr, "You may choose to use a nonstandard\n");
202 fprintf(stderr, "shell, but you must specify its complete ");
203 fprintf(stderr, "path name.\n");
206 else if (access(shell, X_OK))
208 fprintf(stderr, "%s is not available.\n", shell);
213 printf("%s exists but is an unusual choice.\n", shell);
214 printf("Try again if it is not what you wanted.\n");
221 fprintf(stderr, "Usage: %s [user]\n", whoami);
225 #ifndef HAVE_GETUSERSHELL
226 #include <sys/param.h>
228 char *getusershell(void)
230 static FILE *shells = NULL;
232 /* In a sane universe, no shell will have a length longer than
233 * MAXPATHLEN. If any line in /etc/shells does, we'll lose, but
236 static char buf[MAXPATHLEN];
241 shells = fopen("/etc/shells", "r");
244 fprintf(stderr, "%s: Can't open /etc/shells. Unable to determine if "
245 "this is a normal shell.\n\n", whoami);
252 if (!fgets(buf, sizeof(buf), shells))
262 p = strchr(buf, '\n');