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 SMS 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>
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 sms server
90 has been established. */
98 com_err(whoami, 0, "Unexpected return value from SMS -- programmer botch");
105 int status; /* general purpose exit status */
106 int q_argc; /* argc for sms_query */
107 char *q_argv[U_END]; /* argv for sms_query */
109 int got_one = 0; /* have we got a new shell yet? */
110 char shell[BUFSIZ]; /* the new shell */
112 void check_shell(); /* make sure new shell is valid */
114 /* Try each query. If we ever fail, print error message and exit. */
116 status = sms_connect(SMS_SERVER);
118 com_err(whoami, status, " while connecting to SMS");
122 status = sms_auth("chsh"); /* Don't use argv[0] - too easy to fake */
124 com_err(whoami, status,
125 " while authenticating -- run \"kinit\" and try again.");
129 /* First, do an access check */
131 q_argv[USH_NAME] = uname;
132 q_argv[USH_SHELL] = "junk";
135 if (status = sms_access("update_user_shell", q_argc, q_argv))
137 com_err(whoami, status, "; shell not\nchanged.");
141 printf("Changing login shell for %s.\n", uname);
143 /* Display current information */
145 q_argv[NAME] = uname;
148 if ((status = sms_query("get_user_by_login", q_argc, q_argv,
149 get_shell, (char *) uname)))
151 com_err(whoami, status, " while getting user information.");
155 /* Ask for new shell */
158 printf("New shell: ");
159 if (fgets(shell, sizeof(shell), stdin) == NULL)
161 got_one = (strlen(shell) > 1);
164 shell[strlen(shell) - 1] = 0; /* strip off newline */
166 /* Make sure we have a valid shell. This routine could exit */
171 printf("Changing shell to %s...\n", shell);
173 q_argv[USH_NAME] = uname;
174 q_argv[USH_SHELL] = shell;
176 if (status = sms_query("update_user_shell", q_argc, q_argv,
177 scream, (char *) NULL))
179 com_err(whoami, status, " while changing shell.");
183 printf("Shell successfully changed.\n");
189 get_shell(argc, argv, uname)
192 char *uname; /* for sanity checking */
194 /* We'll just take the first information we get since login names
195 cannot be duplicated in the database. */
197 if (strcmp(argv[U_NAME], uname))
199 fprintf(stderr, "Some internal error has occurred. Try again.\n");
203 printf("Account information last changed on %s\n", argv[U_MODTIME]);
204 printf("by user %s with %s.\n", argv[U_MODBY], argv[U_MODWITH]);
205 printf("Current shell for %s is %s.\n", uname, argv[U_SHELL]);
207 return(SMS_ABORT); /* Don't pay attention to other matches. */
210 void check_shell(shell)
216 while (valid_shell = getusershell()) {
217 if (strcmp(shell, valid_shell) == NULL) {
221 else if (strcmp(shell, 1+rindex(valid_shell, '/')) == NULL) {
223 (void) strcpy(shell, valid_shell);
229 if (shell[0] != '/') {
230 fprintf(stderr, "%s it not a standard shell. ", shell);
231 fprintf(stderr, "You may choose to use a nonstandard\n");
232 fprintf(stderr, "shell, but you must specify its complete ");
233 fprintf(stderr, "path name.\n");
236 else if (access(shell, X_OK)) {
237 fprintf(stderr, "%s is not available.\n", shell);
241 printf("%s exists but is an unusual choice.\n", shell);
242 printf("Try again if it is not what you wanted.\n");
249 fprintf(stderr, "Usage: %s [user]\n", whoami);