]>
Commit | Line | Data |
---|---|---|
c441a31a | 1 | /* $Id$ |
7283d000 | 2 | * |
7f5ddc6e | 3 | * Talk to the Moira database to change a person's GECOS information. |
5eaef520 | 4 | * |
5 | * chfn with no modifiers changes the information of the user who is | |
7283d000 | 6 | * running the program. |
5eaef520 | 7 | * |
7283d000 | 8 | * If a commandline argument is given, it is taken to be the username |
9 | * of the user whose information is to be changed. | |
10 | * | |
7ac48069 | 11 | * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology |
12 | * For copying and distribution information, please see the file | |
13 | * <mit-copyright.h>. | |
7283d000 | 14 | */ |
15 | ||
7ac48069 | 16 | #include <mit-copyright.h> |
17 | #include <moira.h> | |
18 | #include <moira_site.h> | |
d018521f | 19 | #include <mrclient.h> |
7283d000 | 20 | |
7ac48069 | 21 | #include <ctype.h> |
7283d000 | 22 | #include <stdio.h> |
f071d8a7 | 23 | #include <string.h> |
7ac48069 | 24 | |
7ac48069 | 25 | RCSID("$Header$"); |
26 | ||
27 | #define FALSE 0 | |
28 | #define TRUE 1 | |
7283d000 | 29 | |
30 | char *whoami; | |
31 | ||
7283d000 | 32 | struct finger_info { |
5eaef520 | 33 | char *fullname; |
34 | char *nickname; | |
35 | char *home_address; | |
36 | char *home_phone; | |
37 | char *office_address; | |
38 | char *office_phone; | |
39 | char *mit_department; | |
40 | char *mit_year; | |
7283d000 | 41 | }; |
42 | ||
3f738519 | 43 | void usage(void); |
7ac48069 | 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); | |
48 | ||
5eaef520 | 49 | int main(int argc, char *argv[]) |
7283d000 | 50 | { |
d018521f | 51 | char *uname; |
ca1102ea | 52 | |
5eaef520 | 53 | if ((whoami = strrchr(argv[0], '/')) == NULL) |
54 | whoami = argv[0]; | |
55 | else | |
56 | whoami++; | |
7283d000 | 57 | |
5eaef520 | 58 | if (argc > 2) |
59 | usage(); | |
60 | ||
61 | if (argc == 2) | |
62 | uname = argv[1]; | |
63 | else | |
7283d000 | 64 | { |
d018521f | 65 | uname = mrcl_krb_user(); |
66 | if (!uname) | |
67 | exit(1); | |
7283d000 | 68 | } |
5eaef520 | 69 | |
70 | exit(chfn(uname)); | |
7283d000 | 71 | } |
72 | ||
5eaef520 | 73 | int chfn(char *uname) |
7283d000 | 74 | { |
5eaef520 | 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 */ | |
5eaef520 | 78 | int i; |
7283d000 | 79 | |
5eaef520 | 80 | struct finger_info old_info; |
81 | struct finger_info new_info; | |
7283d000 | 82 | |
acde26f0 | 83 | if (mrcl_connect(NULL, "chsh", 2, 1) != MRCL_SUCCESS) |
d018521f | 84 | exit(1); |
7283d000 | 85 | |
5eaef520 | 86 | /* First, do an access check. */ |
87 | ||
88 | q_argv[F_NAME] = uname; | |
89 | for (i = F_NAME + 1; i < F_MODTIME; i++) | |
90 | q_argv[i] = "junk"; | |
91 | q_argc = F_MODTIME; /* one more than the last updatable field */ | |
7283d000 | 92 | |
5eaef520 | 93 | if ((status = mr_access("update_finger_by_login", q_argc, q_argv))) |
94 | { | |
95 | com_err(whoami, status, "; finger\ninformation not changed."); | |
d018521f | 96 | exit(2); |
7283d000 | 97 | } |
98 | ||
5eaef520 | 99 | printf("Changing finger information for %s.\n", uname); |
7283d000 | 100 | |
5eaef520 | 101 | /* Get information */ |
7283d000 | 102 | |
5eaef520 | 103 | q_argv[NAME] = uname; |
104 | q_argc = NAME + 1; | |
105 | if ((status = mr_query("get_finger_by_login", q_argc, q_argv, | |
7ac48069 | 106 | get_user_info, &old_info))) |
7283d000 | 107 | { |
5eaef520 | 108 | com_err(whoami, status, "while getting user information."); |
d018521f | 109 | exit(2); |
7283d000 | 110 | } |
111 | ||
5eaef520 | 112 | /* Get the new information from the user */ |
7283d000 | 113 | |
5eaef520 | 114 | get_new_info(&old_info, &new_info); |
7283d000 | 115 | |
5eaef520 | 116 | /* Do the update */ |
7283d000 | 117 | |
5eaef520 | 118 | printf("Changing finger information...\n"); |
7283d000 | 119 | |
5eaef520 | 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 */ | |
7283d000 | 130 | |
5eaef520 | 131 | if ((status = mr_query("update_finger_by_login", q_argc, q_argv, |
7ac48069 | 132 | NULL, NULL))) |
7283d000 | 133 | { |
5eaef520 | 134 | com_err(whoami, status, "while updating finger information."); |
d018521f | 135 | exit(1); |
7283d000 | 136 | } |
137 | ||
5eaef520 | 138 | printf("Finger information updated succesfully.\n"); |
7283d000 | 139 | |
5eaef520 | 140 | return 0; |
7283d000 | 141 | } |
142 | ||
7ac48069 | 143 | int get_user_info(int argc, char *argv[], void *message) |
7283d000 | 144 | { |
7ac48069 | 145 | struct finger_info *old_info = message; |
5eaef520 | 146 | |
147 | if (argc != F_END) | |
148 | { | |
149 | fprintf(stderr, "Some internal error occurred; try again.\n"); | |
d018521f | 150 | exit(3); |
7283d000 | 151 | } |
5eaef520 | 152 | |
153 | printf("Info last changed on %s by user %s with %s.\n", | |
154 | argv[F_MODTIME], argv[F_MODBY], argv[F_MODWITH]); | |
155 | ||
7ac48069 | 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]); | |
5eaef520 | 164 | |
165 | /* Only pay attention to the first match since login names are | |
166 | unique in the database. */ | |
167 | return MR_ABORT; | |
7283d000 | 168 | } |
169 | ||
5eaef520 | 170 | char *ask(char *question, char *def_val, int phone_num) |
7283d000 | 171 | { |
5eaef520 | 172 | static char buf[BUFSIZ]; |
173 | int ok = FALSE; | |
174 | char *result; | |
175 | int i; | |
176 | int dashes = FALSE; | |
533bacb3 | 177 | int len; |
5eaef520 | 178 | |
7283d000 | 179 | #define BLANK "none" |
5eaef520 | 180 | |
181 | while (!ok) | |
7283d000 | 182 | { |
5eaef520 | 183 | ok = TRUE; |
184 | printf("%s [%s]: ", question, def_val); | |
185 | if (!fgets(buf, sizeof(buf), stdin)) | |
d018521f | 186 | exit(0); |
5eaef520 | 187 | buf[strlen(buf) - 1] = '\0'; |
188 | if (strlen(buf) == 0) | |
189 | result = def_val; | |
190 | else if (!strcasecmp(buf, BLANK)) | |
191 | result = ""; | |
192 | else | |
193 | result = buf; | |
194 | ||
533bacb3 | 195 | len = strlen(buf); |
196 | for (i = 0; i < len; i++) | |
7283d000 | 197 | { |
5eaef520 | 198 | switch (buf[i]) |
7283d000 | 199 | { |
5eaef520 | 200 | case '"': |
201 | printf("'\"' is not allowed.\n"); | |
202 | ok = FALSE; | |
203 | break; | |
204 | case ',': | |
205 | printf("',' is not allowed.\n"); | |
206 | ok = FALSE; | |
207 | break; | |
208 | case ':': | |
209 | printf("':' is not allowed.\n"); | |
210 | ok = FALSE; | |
211 | break; | |
212 | default: | |
213 | if (iscntrl(buf[i])) | |
214 | { | |
215 | printf("Control characters are not allowed.\n"); | |
216 | ok = FALSE; | |
217 | break; | |
7283d000 | 218 | } |
219 | } | |
5eaef520 | 220 | if (!ok) |
221 | break; | |
7283d000 | 222 | } |
5eaef520 | 223 | |
224 | if (phone_num && ok) | |
225 | { | |
533bacb3 | 226 | len = strlen(result); |
227 | for (i = 0; i < len; i++) | |
5eaef520 | 228 | { |
229 | if (!isdigit(result[i]) && (result[i] != '-')) | |
230 | { | |
231 | printf("Phone numbers can contain only digits.\n"); | |
232 | ok = FALSE; | |
233 | break; | |
7283d000 | 234 | } |
5eaef520 | 235 | if (result[i] == '-') |
236 | dashes = TRUE; | |
7283d000 | 237 | } |
238 | } | |
239 | } | |
5eaef520 | 240 | |
241 | /* Remove dashes if necessary */ | |
242 | if (dashes && result == buf) | |
243 | { | |
244 | char *tmp1, *tmp2; | |
245 | tmp1 = tmp2 = (char *)buf; | |
246 | do | |
247 | { | |
248 | if (*tmp1 != '-') | |
249 | *tmp2++ = *tmp1; | |
7283d000 | 250 | } |
5eaef520 | 251 | while (*tmp1++); |
7283d000 | 252 | } |
5eaef520 | 253 | |
254 | return result; | |
7283d000 | 255 | } |
5eaef520 | 256 | |
257 | void get_new_info(struct finger_info *old_info, struct finger_info *new_info) | |
7283d000 | 258 | { |
5eaef520 | 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); | |
262 | ||
7ac48069 | 263 | #define GETINFO(m, v, n) new_info->v = strdup(ask(m, old_info->v, n)) |
5eaef520 | 264 | |
265 | GETINFO("Full name", fullname, FALSE); | |
266 | GETINFO("Nickname", nickname, FALSE); | |
7f5ddc6e | 267 | GETINFO("Home address (Ex: EC Bemis 514)", home_address, FALSE); |
5eaef520 | 268 | GETINFO("Home phone number (Ex: 3141592)", home_phone, TRUE); |
269 | GETINFO("Office address (Exs: E40-342 or 2-108)", | |
270 | office_address, FALSE); | |
7f5ddc6e | 271 | GETINFO("Office phone (Ex: 3-7619)", office_phone, TRUE); |
5eaef520 | 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); | |
7283d000 | 275 | } |
276 | ||
3f738519 | 277 | void usage(void) |
7283d000 | 278 | { |
5eaef520 | 279 | fprintf(stderr, "Usage: %s [user]\n", whoami); |
280 | exit(1); | |
7283d000 | 281 | } |