2 * Copyright 1988 by the Massachusetts Institute of Technology. For copying
3 * and distribution information, see the file "mit-copyright.h".
12 static char *rcsid_chsh_c = "$Header$";
16 * Talk to the MOIRA database to change a person's login shell. The chosen
17 * shell must exist. A warning will be issued if the shell is not in
20 * chsh with no modifiers changes the shell of the user who is running
23 * If a commandline argument is given, it is taken to be the username
24 * of the user whose shell is to be changed.
28 #include <sys/types.h>
38 #include <moira_site.h>
39 #include "mit-copyright.h"
44 extern char *krb_err_txt[];
55 if ((whoami = rindex(argv[0], '/')) == NULL)
68 /* Do it right; get name from kerberos ticket file rather than
71 if (k_errno = tf_init(TKT_FILE, R_TKT_FIL)) {
72 fprintf(stderr, "%s: %s\n", whoami, krb_err_txt[k_errno]);
76 if (k_errno = tf_get_pname(pname)) {
77 fprintf(stderr, "%s: %s\n", whoami, krb_err_txt[k_errno]);
89 /* This should be called rather than exit once connection to moira server
90 has been established. */
98 com_err(whoami, 0, "Unexpected return value from Moira -- programmer botch");
105 int status; /* general purpose exit status */
106 int q_argc; /* argc for mr_query */
107 char *q_argv[U_END]; /* argv for mr_query */
108 char *motd; /* determine MR server status */
110 int got_one = 0; /* have we got a new shell yet? */
111 char shell[BUFSIZ]; /* the new shell */
113 void check_shell(); /* make sure new shell is valid */
115 /* Try each query. If we ever fail, print error message and exit. */
117 status = mr_connect(NULL);
119 com_err(whoami, status, " while connecting to Moira");
123 status = mr_motd(&motd);
125 com_err(whoami, status, " unable to check server status");
129 fprintf(stderr, "The Moira server is currently unavailable:\n%s\n", motd);
133 status = mr_auth("chsh"); /* Don't use argv[0] - too easy to fake */
135 com_err(whoami, status,
136 " while authenticating -- run \"kinit\" and try again.");
140 /* First, do an access check */
142 q_argv[USH_NAME] = uname;
143 q_argv[USH_SHELL] = "junk";
146 if (status = mr_access("update_user_shell", q_argc, q_argv))
148 com_err(whoami, status, "; shell not\nchanged.");
152 printf("Changing login shell for %s.\n", uname);
154 /* Display current information */
156 q_argv[NAME] = uname;
159 if ((status = mr_query("get_user_by_login", q_argc, q_argv,
160 get_shell, (char *) uname)))
162 com_err(whoami, status, " while getting user information.");
166 /* Ask for new shell */
169 printf("New shell: ");
170 if (fgets(shell, sizeof(shell), stdin) == NULL)
172 got_one = (strlen(shell) > 1);
175 shell[strlen(shell) - 1] = 0; /* strip off newline */
177 /* Make sure we have a valid shell. This routine could exit */
182 printf("Changing shell to %s...\n", shell);
184 q_argv[USH_NAME] = uname;
185 q_argv[USH_SHELL] = shell;
187 if (status = mr_query("update_user_shell", q_argc, q_argv,
188 scream, (char *) NULL))
190 com_err(whoami, status, " while changing shell.");
194 printf("Shell successfully changed.\n");
200 get_shell(argc, argv, uname)
203 char *uname; /* for sanity checking */
205 /* We'll just take the first information we get since login names
206 cannot be duplicated in the database. */
208 if (strcmp(argv[U_NAME], uname))
210 fprintf(stderr, "Some internal error has occurred. Try again.\n");
214 printf("Account information last changed on %s\n", argv[U_MODTIME]);
215 printf("by user %s with %s.\n", argv[U_MODBY], argv[U_MODWITH]);
216 printf("Current shell for %s is %s.\n", uname, argv[U_SHELL]);
218 return(MR_ABORT); /* Don't pay attention to other matches. */
221 void check_shell(shell)
227 while (valid_shell = getusershell()) {
228 if (strcmp(shell, valid_shell) == NULL) {
232 else if (strcmp(shell, 1+rindex(valid_shell, '/')) == NULL) {
234 (void) strcpy(shell, valid_shell);
240 if (shell[0] != '/') {
241 fprintf(stderr, "%s it not a standard shell. ", shell);
242 fprintf(stderr, "You may choose to use a nonstandard\n");
243 fprintf(stderr, "shell, but you must specify its complete ");
244 fprintf(stderr, "path name.\n");
247 else if (access(shell, X_OK)) {
248 fprintf(stderr, "%s is not available.\n", shell);
252 printf("%s exists but is an unusual choice.\n", shell);
253 printf("Try again if it is not what you wanted.\n");
260 fprintf(stderr, "Usage: %s [user]\n", whoami);
264 #if defined(ultrix) || defined(AIX386)
267 static int count = 1;
278 return(getusershell());