3 * Talk to the Moira database to change a person's GECOS information.
5 * chfn with no modifiers changes the information of the user who is
8 * If a commandline argument is given, it is taken to be the username
9 * of the user whose information is to be changed.
11 * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology
12 * For copying and distribution information, please see the file
16 #include <mit-copyright.h>
18 #include <moira_site.h>
44 int chfn(char *uname);
45 int get_user_info(int argc, char *argv[], void *message);
46 char *ask(char *question, char *def_val, int phone_num);
47 void get_new_info(struct finger_info *old_info, struct finger_info *new_info);
49 int main(int argc, char *argv[])
53 if ((whoami = strrchr(argv[0], '/')) == NULL)
65 uname = mrcl_krb_user();
75 int status; /* general purpose exit status */
76 int q_argc; /* argc for mr_query */
77 char *q_argv[F_END]; /* argv for mr_query */
78 char *motd; /* for Moira server status */
81 struct finger_info old_info;
82 struct finger_info new_info;
84 if (mrcl_connect(NULL, "chsh", 2, 1) != MRCL_SUCCESS)
87 /* First, do an access check. */
89 q_argv[F_NAME] = uname;
90 for (i = F_NAME + 1; i < F_MODTIME; i++)
92 q_argc = F_MODTIME; /* one more than the last updatable field */
94 if ((status = mr_access("update_finger_by_login", q_argc, q_argv)))
96 com_err(whoami, status, "; finger\ninformation not changed.");
100 printf("Changing finger information for %s.\n", uname);
102 /* Get information */
104 q_argv[NAME] = uname;
106 if ((status = mr_query("get_finger_by_login", q_argc, q_argv,
107 get_user_info, &old_info)))
109 com_err(whoami, status, "while getting user information.");
113 /* Get the new information from the user */
115 get_new_info(&old_info, &new_info);
119 printf("Changing finger information...\n");
121 q_argv[F_NAME] = uname;
122 q_argv[F_FULLNAME] = new_info.fullname;
123 q_argv[F_NICKNAME] = new_info.nickname;
124 q_argv[F_HOME_ADDR] = new_info.home_address;
125 q_argv[F_HOME_PHONE] = new_info.home_phone;
126 q_argv[F_OFFICE_ADDR] = new_info.office_address;
127 q_argv[F_OFFICE_PHONE] = new_info.office_phone;
128 q_argv[F_MIT_DEPT] = new_info.mit_department;
129 q_argv[F_MIT_AFFIL] = new_info.mit_year;
130 q_argc = F_MODTIME; /* First non-update query argument */
132 if ((status = mr_query("update_finger_by_login", q_argc, q_argv,
135 com_err(whoami, status, "while updating finger information.");
139 printf("Finger information updated succesfully.\n");
144 int get_user_info(int argc, char *argv[], void *message)
146 struct finger_info *old_info = message;
150 fprintf(stderr, "Some internal error occurred; try again.\n");
154 printf("Info last changed on %s by user %s with %s.\n",
155 argv[F_MODTIME], argv[F_MODBY], argv[F_MODWITH]);
157 old_info->fullname = strdup(argv[F_FULLNAME]);
158 old_info->nickname = strdup(argv[F_NICKNAME]);
159 old_info->home_address = strdup(argv[F_HOME_ADDR]);
160 old_info->home_phone = strdup(argv[F_HOME_PHONE]);
161 old_info->office_address = strdup(argv[F_OFFICE_ADDR]);
162 old_info->office_phone = strdup(argv[F_OFFICE_PHONE]);
163 old_info->mit_department = strdup(argv[F_MIT_DEPT]);
164 old_info->mit_year = strdup(argv[F_MIT_AFFIL]);
166 /* Only pay attention to the first match since login names are
167 unique in the database. */
171 char *ask(char *question, char *def_val, int phone_num)
173 static char buf[BUFSIZ];
184 printf("%s [%s]: ", question, def_val);
185 if (!fgets(buf, sizeof(buf), stdin))
187 buf[strlen(buf) - 1] = '\0';
188 if (strlen(buf) == 0)
190 else if (!strcasecmp(buf, BLANK))
195 for (i = 0; i < strlen(buf); i++)
200 printf("'\"' is not allowed.\n");
204 printf("',' is not allowed.\n");
208 printf("':' is not allowed.\n");
214 printf("Control characters are not allowed.\n");
225 for (i = 0; i < strlen(result); i++)
227 if (!isdigit(result[i]) && (result[i] != '-'))
229 printf("Phone numbers can contain only digits.\n");
233 if (result[i] == '-')
239 /* Remove dashes if necessary */
240 if (dashes && result == buf)
243 tmp1 = tmp2 = (char *)buf;
255 void get_new_info(struct finger_info *old_info, struct finger_info *new_info)
257 printf("Default values are printed inside of '[]'.\n");
258 printf("To accept the default, type <return>.\n");
259 printf("To have a blank entry, type the word '%s'.\n\n", BLANK);
261 #define GETINFO(m, v, n) new_info->v = strdup(ask(m, old_info->v, n))
263 GETINFO("Full name", fullname, FALSE);
264 GETINFO("Nickname", nickname, FALSE);
265 GETINFO("Home address (Ex: EC Bemis 514)", home_address, FALSE);
266 GETINFO("Home phone number (Ex: 3141592)", home_phone, TRUE);
267 GETINFO("Office address (Exs: E40-342 or 2-108)",
268 office_address, FALSE);
269 GETINFO("Office phone (Ex: 3-7619)", office_phone, TRUE);
270 GETINFO("MIT department (Exs: 9, Biology, Information Services)",
271 mit_department, FALSE);
272 GETINFO("MIT year (Exs: 1989, '91, Faculty, Grad)", mit_year, FALSE);
277 fprintf(stderr, "Usage: %s [user]\n", whoami);