2 * Copyright 1987 by the Massachusetts Institute of Technology. For copying
3 * and distribution information, see the file "mit-copyright.h".
12 static char *rcsid_chpobox_c = "$Header$";
16 * Talk to the Moira database to change a person's home mail machine. This may
17 * be an Athena machine, or a completely arbitrary address.
19 * chpobox with no modifiers reports the current mailbox.
21 * chpobox -s [address] means set the mailbox to this address.
23 * chpobox -p restores the pobox to a previous POP box, if there was one.
25 * chpobox -u [user] is needed if you are logged in as one user, but
26 * are trying to change the email address of another. You must have
27 * Kerberos tickets as the person whose address you are trying to
28 * change, or the attempt will fail.
31 #include <sys/types.h>
40 #include <moira_site.h>
41 #include "mit-copyright.h"
53 int main(int argc, char *argv[])
56 char *mrarg[3], buf[BUFSIZ];
58 char *address, *uname, *machine, *motd;
60 char *canonicalize_hostname();
61 int get_pobox(), scream();
62 int c, setflag, prevpop, usageflag, status;
67 c = setflag = prevpop = usageflag = 0;
68 address = uname = (char *) NULL;
71 if ((whoami = strrchr(argv[0], '/')) == NULL)
79 while ((c = getopt(argc, argv, "s:pu:")) != -1)
99 uname = strsave(optarg);
105 if (argc == 2 && optind == 1 && !uname)
106 uname = argv[optind++];
108 if (usageflag || optind != argc)
113 if (!(uname = getlogin()))
116 if (uname[0] == '\0')
119 strcpy(uname, pwd->pw_name);
124 status = mr_connect(NULL);
127 com_err(whoami, status, " while connecting to Moira");
131 status = mr_motd(&motd);
135 com_err(whoami, status, " unable to check server status");
140 fprintf(stderr, "The Moira server is currently unavailable:\n%s\n",
146 status = mr_auth("chpobox");
149 com_err(whoami, status, " while authenticating -- "
150 "run \"kinit\" and try again.");
157 /* Address is of form user@host. Split it up. */
160 fprintf(stderr, "%s: no address was specified.\n", whoami);
163 machine = strchr(address, '@');
166 *machine++ = '\0'; /* get rid of the @ sign */
167 machine = strtrim(machine); /* get rid of whitespace */
171 fprintf(stderr, "%s: no at sign (@) in address \"%s\"\n",
175 mrarg[2] = canonicalize_hostname(strsave(machine));
176 mrarg[1] = potype(mrarg[2]);
177 if (!strcmp(mrarg[1], "POP"))
179 if (strcmp(address, uname))
182 "%s: the name on the POP box must match the username\n",
187 else if (!strcmp(mrarg[1], "LOCAL"))
189 strcat(address, "@");
190 strcat(address, mrarg[2]);
192 if ((address = strchr(address, '@')) &&
193 (address = strchr(address, '.')))
195 strcat(mrarg[2], ".LOCAL");
198 else if (!strcmp(mrarg[1], "MAILHUB"))
200 if (!strcmp(address, uname))
203 "Error: this will set a mail forwarding loop.\n");
205 "Use \"%s -p\" to set a local post office server.\n",
209 fprintf(stderr, "Error: \"%s@%s\" is a local mail address.\n",
211 fprintf(stderr, "Your mail drop must be on a post office server "
212 "or an external mail address.\n");
217 if (*machine != '"' && strcasecmp(mrarg[2], machine))
219 fprintf(stderr, "Warning: hostname %s canonicalized to %s\n",
222 strcat(address, "@");
223 strcat(address, mrarg[2]);
227 status = mr_query("set_pobox", 3, mrarg, scream, NULL);
229 com_err(whoami, status,
230 " while setting pobox for %s to type %s, box %s",
231 mrarg[0], mrarg[1], mrarg[2]);
235 status = mr_query("set_pobox_pop", 1, mrarg, scream, NULL);
236 if (status == MR_MACHINE)
239 "Moira has no record of a previous POP box for %s\n", uname);
241 else if (status != 0)
242 com_err(whoami, status, " while setting pobox");
249 status = mr_query("get_pobox", 1, mrarg, get_pobox, NULL);
250 if (status == MR_NO_MATCH)
251 printf("User %s has no pobox.\n", uname);
252 else if (status != 0)
253 com_err(whoami, status, " while retrieving current mailbox");
261 com_err(whoami, 0, "Unexpected return value from Moira -- programmer botch");
267 * get_pobox gets all your poboxes and displays them.
270 int get_pobox(int argc, char **argv, char *callarg)
272 if (!strcmp(argv[1], "POP"))
273 printf("User %s, Type %s, Box: %s@%s\n",
274 argv[0], argv[1], argv[0], argv[2]);
276 printf("User %s, Type %s, Box: %s\n",
277 argv[0], argv[1], argv[2]);
278 printf(" Modified by %s on %s with %s\n", argv[4], argv[3], argv[5]);
283 * given a canonicalized machine name, ask the Moira server if it is of type
284 * pop, or of type local -- if neither, we assume that it's of type foreign.
286 char *potype(char *machine)
288 char *service[1], *argv[3];
289 int check_match(), check_match3(), status;
293 status = mr_query("get_server_locations", 1, service, check_match, machine);
294 if (status && (status != MR_NO_MATCH))
296 com_err(whoami, status, " while reading list of POP servers");
302 service[0] = "LOCAL";
303 status = mr_query("get_server_locations", 1, service, check_match, machine);
304 if (status && (status != MR_NO_MATCH))
306 com_err(whoami, status, " while reading list of LOCAL servers");
315 status = mr_query("get_alias", 3, argv, check_match3, machine);
316 if (status && (status != MR_NO_MATCH))
318 com_err(whoami, status, " while reading list of MAILHUB servers");
327 int check_match(int argc, char **argv, char *callback)
332 if (!strcasecmp(argv[1], callback))
338 int check_match3(int argc, char **argv, char *callback)
343 if (!strcasecmp(argv[2], callback))
351 fprintf(stderr, "Usage: %s [-s address] [-p] [-u user]\n", whoami);