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 */
80 struct finger_info old_info;
81 struct finger_info new_info;
83 if (mrcl_connect(NULL, "chsh", 2, 1) != MRCL_SUCCESS)
86 /* First, do an access check. */
88 q_argv[F_NAME] = uname;
89 for (i = F_NAME + 1; i < F_MODTIME; i++)
91 q_argc = F_MODTIME; /* one more than the last updatable field */
93 if ((status = mr_access("update_finger_by_login", q_argc, q_argv)))
95 com_err(whoami, status, "; finger\ninformation not changed.");
99 printf("Changing finger information for %s.\n", uname);
101 /* Get information */
103 q_argv[NAME] = uname;
105 if ((status = mr_query("get_finger_by_login", q_argc, q_argv,
106 get_user_info, &old_info)))
108 com_err(whoami, status, "while getting user information.");
112 /* Get the new information from the user */
114 get_new_info(&old_info, &new_info);
118 printf("Changing finger information...\n");
120 q_argv[F_NAME] = uname;
121 q_argv[F_FULLNAME] = new_info.fullname;
122 q_argv[F_NICKNAME] = new_info.nickname;
123 q_argv[F_HOME_ADDR] = new_info.home_address;
124 q_argv[F_HOME_PHONE] = new_info.home_phone;
125 q_argv[F_OFFICE_ADDR] = new_info.office_address;
126 q_argv[F_OFFICE_PHONE] = new_info.office_phone;
127 q_argv[F_MIT_DEPT] = new_info.mit_department;
128 q_argv[F_MIT_AFFIL] = new_info.mit_year;
129 q_argc = F_MODTIME; /* First non-update query argument */
131 if ((status = mr_query("update_finger_by_login", q_argc, q_argv,
134 com_err(whoami, status, "while updating finger information.");
138 printf("Finger information updated succesfully.\n");
143 int get_user_info(int argc, char *argv[], void *message)
145 struct finger_info *old_info = message;
149 fprintf(stderr, "Some internal error occurred; try again.\n");
153 printf("Info last changed on %s by user %s with %s.\n",
154 argv[F_MODTIME], argv[F_MODBY], argv[F_MODWITH]);
156 old_info->fullname = strdup(argv[F_FULLNAME]);
157 old_info->nickname = strdup(argv[F_NICKNAME]);
158 old_info->home_address = strdup(argv[F_HOME_ADDR]);
159 old_info->home_phone = strdup(argv[F_HOME_PHONE]);
160 old_info->office_address = strdup(argv[F_OFFICE_ADDR]);
161 old_info->office_phone = strdup(argv[F_OFFICE_PHONE]);
162 old_info->mit_department = strdup(argv[F_MIT_DEPT]);
163 old_info->mit_year = strdup(argv[F_MIT_AFFIL]);
165 /* Only pay attention to the first match since login names are
166 unique in the database. */
170 char *ask(char *question, char *def_val, int phone_num)
172 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))
196 for (i = 0; i < len; i++)
201 printf("'\"' is not allowed.\n");
205 printf("',' is not allowed.\n");
209 printf("':' is not allowed.\n");
215 printf("Control characters are not allowed.\n");
226 len = strlen(result);
227 for (i = 0; i < len; i++)
229 if (!isdigit(result[i]) && (result[i] != '-'))
231 printf("Phone numbers can contain only digits.\n");
235 if (result[i] == '-')
241 /* Remove dashes if necessary */
242 if (dashes && result == buf)
245 tmp1 = tmp2 = (char *)buf;
257 void get_new_info(struct finger_info *old_info, struct finger_info *new_info)
259 printf("Default values are printed inside of '[]'.\n");
260 printf("To accept the default, type <return>.\n");
261 printf("To have a blank entry, type the word '%s'.\n\n", BLANK);
263 #define GETINFO(m, v, n) new_info->v = strdup(ask(m, old_info->v, n))
265 GETINFO("Full name", fullname, FALSE);
266 GETINFO("Nickname", nickname, FALSE);
267 GETINFO("Home address (Ex: EC Bemis 514)", home_address, FALSE);
268 GETINFO("Home phone number (Ex: 3141592)", home_phone, TRUE);
269 GETINFO("Office address (Exs: E40-342 or 2-108)",
270 office_address, FALSE);
271 GETINFO("Office phone (Ex: 3-7619)", office_phone, TRUE);
272 GETINFO("MIT department (Exs: 9, Biology, Information Services)",
273 mit_department, FALSE);
274 GETINFO("MIT year (Exs: 1989, '91, Faculty, Grad)", mit_year, FALSE);
279 fprintf(stderr, "Usage: %s [user]\n", whoami);