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 #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b))
47 int get_user_info(int argc, char *argv[], void *message);
48 char *ask(char *question, char *def_val, int phone_num);
49 void get_new_info(struct finger_info *old_info, struct finger_info *new_info);
51 int main(int argc, char *argv[])
53 int status; /* general purpose exit status */
54 int q_argc; /* argc for mr_query */
55 char *q_argv[F_END]; /* argv for mr_query */
57 struct finger_info old_info;
58 struct finger_info new_info;
62 if ((whoami = strrchr(argv[0], '/')) == NULL)
67 /* parse our command line options */
68 while (++arg - argv < argc)
72 if (argis("db", "database"))
74 if (arg - argv < argc - 1)
83 else if (username == NULL)
91 username = mrcl_krb_user();
96 if (mrcl_connect(server, "chsh", 2, 1) != MRCL_SUCCESS)
99 /* First, do an access check. */
101 q_argv[F_NAME] = username;
102 for (i = F_NAME + 1; i < F_MODTIME; i++)
104 q_argc = F_MODTIME; /* one more than the last updatable field */
106 if ((status = mr_access("update_finger_by_login", q_argc, q_argv)))
108 com_err(whoami, status, "; finger\ninformation not changed.");
112 printf("Changing finger information for %s.\n", username);
114 /* Get information */
116 q_argv[NAME] = username;
118 if ((status = mr_query("get_finger_by_login", q_argc, q_argv,
119 get_user_info, &old_info)))
121 com_err(whoami, status, "while getting user information.");
125 /* Get the new information from the user */
127 get_new_info(&old_info, &new_info);
131 printf("Changing finger information...\n");
133 q_argv[F_NAME] = username;
134 q_argv[F_FULLNAME] = new_info.fullname;
135 q_argv[F_NICKNAME] = new_info.nickname;
136 q_argv[F_HOME_ADDR] = new_info.home_address;
137 q_argv[F_HOME_PHONE] = new_info.home_phone;
138 q_argv[F_OFFICE_ADDR] = new_info.office_address;
139 q_argv[F_OFFICE_PHONE] = new_info.office_phone;
140 q_argv[F_MIT_DEPT] = new_info.mit_department;
141 q_argv[F_MIT_AFFIL] = new_info.mit_year;
142 q_argc = F_MODTIME; /* First non-update query argument */
144 if ((status = mr_query("update_finger_by_login", q_argc, q_argv,
147 com_err(whoami, status, "while updating finger information.");
151 printf("Finger information updated succesfully.\n");
158 int get_user_info(int argc, char *argv[], void *message)
160 struct finger_info *old_info = message;
164 fprintf(stderr, "Some internal error occurred; try again.\n");
168 printf("Info last changed on %s by user %s with %s.\n",
169 argv[F_MODTIME], argv[F_MODBY], argv[F_MODWITH]);
171 old_info->fullname = strdup(argv[F_FULLNAME]);
172 old_info->nickname = strdup(argv[F_NICKNAME]);
173 old_info->home_address = strdup(argv[F_HOME_ADDR]);
174 old_info->home_phone = strdup(argv[F_HOME_PHONE]);
175 old_info->office_address = strdup(argv[F_OFFICE_ADDR]);
176 old_info->office_phone = strdup(argv[F_OFFICE_PHONE]);
177 old_info->mit_department = strdup(argv[F_MIT_DEPT]);
178 old_info->mit_year = strdup(argv[F_MIT_AFFIL]);
180 /* Only pay attention to the first match since login names are
181 unique in the database. */
185 char *ask(char *question, char *def_val, int phone_num)
187 static char buf[BUFSIZ];
199 printf("%s [%s]: ", question, def_val);
200 if (!fgets(buf, sizeof(buf), stdin))
202 buf[strlen(buf) - 1] = '\0';
203 if (strlen(buf) == 0)
205 else if (!strcasecmp(buf, BLANK))
211 for (i = 0; i < len; i++)
216 printf("'\"' is not allowed.\n");
220 printf("',' is not allowed.\n");
224 printf("':' is not allowed.\n");
230 printf("Control characters are not allowed.\n");
242 void get_new_info(struct finger_info *old_info, struct finger_info *new_info)
244 printf("Default values are printed inside of '[]'.\n");
245 printf("To accept the default, type <return>.\n");
246 printf("To have a blank entry, type the word '%s'.\n\n", BLANK);
248 #define GETINFO(m, v, n) new_info->v = strdup(ask(m, old_info->v, n))
250 GETINFO("Full name", fullname, FALSE);
251 GETINFO("Nickname", nickname, FALSE);
252 GETINFO("Home address (Ex: EC Bemis 514)", home_address, FALSE);
253 GETINFO("Home phone number (Ex: 3141592)", home_phone, TRUE);
254 GETINFO("Office address (Exs: E40-342 or 2-108)",
255 office_address, FALSE);
256 GETINFO("Office phone (Ex: 3-7619)", office_phone, TRUE);
257 GETINFO("MIT department (Exs: 9, Biology, Information Services)",
258 mit_department, FALSE);
259 GETINFO("MIT year (Exs: 1989, '91, Faculty, Grad)", mit_year, FALSE);
264 fprintf(stderr, "Usage: %s [user]\n", whoami);