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.
29 #include <sys/types.h>
39 #include <moira_site.h>
40 #include "mit-copyright.h"
45 int main(int argc, char *argv[])
51 initialize_krb_error_table();
53 if ((whoami = strrchr(argv[0], '/')) == NULL)
65 /* Do it right; get name from kerberos ticket file rather than
68 if ((k_errno = tf_init(TKT_FILE, R_TKT_FIL)))
70 com_err(whoami, k_errno, "reading ticket file");
74 if ((k_errno = tf_get_pname(pname)))
76 com_err(whoami, k_errno, "getting kerberos principal name");
86 /* This should be called rather than exit once connection to moira server
87 has been established. */
96 com_err(whoami, 0, "Unexpected return value from Moira -- programmer botch");
100 int chsh(char *uname)
102 int status; /* general purpose exit status */
103 int q_argc; /* argc for mr_query */
104 char *q_argv[U_END]; /* argv for mr_query */
105 char *motd; /* determine MR server status */
107 int got_one = 0; /* have we got a new shell yet? */
108 char shell[BUFSIZ]; /* the new shell */
110 void check_shell(); /* make sure new shell is valid */
112 /* Try each query. If we ever fail, print error message and exit. */
114 status = mr_connect(NULL);
117 com_err(whoami, status, " while connecting to Moira");
121 status = mr_motd(&motd);
124 com_err(whoami, status, " unable to check server status");
129 fprintf(stderr, "The Moira server is currently unavailable:\n%s\n",
134 status = mr_auth("chsh"); /* Don't use argv[0] - too easy to fake */
137 com_err(whoami, status,
138 " while authenticating -- run \"kinit\" and try again.");
142 /* First, do an access check */
144 q_argv[USH_NAME] = uname;
145 q_argv[USH_SHELL] = "junk";
148 if ((status = mr_access("update_user_shell", q_argc, q_argv)))
150 com_err(whoami, status, "; shell not\nchanged.");
154 printf("Changing login shell for %s.\n", uname);
156 /* Display current information */
158 q_argv[NAME] = uname;
161 if ((status = mr_query("get_user_account_by_login", q_argc, q_argv,
162 get_shell, (char *) uname)))
164 com_err(whoami, status, " while getting user information.");
168 /* Ask for new shell */
171 printf("New shell: ");
172 if (!fgets(shell, sizeof(shell), stdin))
174 got_one = (strlen(shell) > 1);
177 shell[strlen(shell) - 1] = 0; /* strip off newline */
179 /* Make sure we have a valid shell. This routine could exit */
184 printf("Changing shell to %s...\n", shell);
186 q_argv[USH_NAME] = uname;
187 q_argv[USH_SHELL] = shell;
189 if ((status = mr_query("update_user_shell", q_argc, q_argv,
192 com_err(whoami, status, " while changing shell.");
196 printf("Shell successfully changed.\n");
202 int get_shell(int argc, char **argv, char *uname)
204 /* We'll just take the first information we get since login names
205 cannot be duplicated in the database. */
207 if (argc < U_END || strcmp(argv[U_NAME], uname))
209 fprintf(stderr, "Some internal error has occurred. Try again.\n");
213 printf("Account information last changed on %s\n", argv[U_MODTIME]);
214 printf("by user %s with %s.\n", argv[U_MODBY], argv[U_MODWITH]);
215 printf("Current shell for %s is %s.\n", uname, argv[U_SHELL]);
217 return MR_ABORT; /* Don't pay attention to other matches. */
220 void check_shell(char *shell)
225 while ((valid_shell = getusershell()))
227 if (!strcmp(shell, valid_shell))
232 else if (!strcmp(shell, 1 + strrchr(valid_shell, '/')))
235 strcpy(shell, valid_shell);
244 fprintf(stderr, "%s is not a standard shell. ", shell);
245 fprintf(stderr, "You may choose to use a nonstandard\n");
246 fprintf(stderr, "shell, but you must specify its complete ");
247 fprintf(stderr, "path name.\n");
250 else if (access(shell, X_OK))
252 fprintf(stderr, "%s is not available.\n", shell);
257 printf("%s exists but is an unusual choice.\n", shell);
258 printf("Try again if it is not what you wanted.\n");
265 fprintf(stderr, "Usage: %s [user]\n", whoami);
269 #ifdef NEED_GETUSERSHELL
272 static int count = 1;
281 return "/bin/athena/tcsh";
286 return getusershell();