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"
58 char *mrarg[3], buf[BUFSIZ];
60 char *address, *uname, *machine, *motd;
62 char *canonicalize_hostname();
63 int get_pobox(), scream();
64 int c, setflag, prevpop, usageflag, status;
69 c = setflag = prevpop = usageflag = 0;
70 address = uname = (char *) NULL;
73 if ((whoami = strrchr(argv[0], '/')) == NULL)
82 while ((c = getopt(argc, argv, "s:pu:")) != EOF)
102 uname = strsave(optarg);
108 if (argc == 2 && optind == 1 && !uname) {
109 uname = argv[optind++];
111 if (usageflag || optind != argc) {
115 if ((uname = getlogin()) == NULL) {
118 if (uname[0] == '\0') {
119 pwd = getpwuid((int) u);
120 (void) strcpy(uname, pwd->pw_name);
125 status = mr_connect(NULL);
127 com_err(whoami, status, " while connecting to Moira");
131 status = mr_motd(&motd);
134 com_err(whoami, status, " unable to check server status");
138 fprintf(stderr, "The Moira server is currently unavailable:\n%s\n", motd);
143 status = mr_auth("chpobox");
145 com_err(whoami, status, " while authenticating -- run \"kinit\" and try again.");
151 /* Address is of form user@host. Split it up. */
153 fprintf(stderr, "%s: no address was specified.\n", whoami);
156 machine = strchr(address, '@');
158 *machine++ = '\0'; /* get rid of the @ sign */
159 machine = strtrim(machine); /* get rid of whitespace */
161 fprintf(stderr, "%s: no at sign (@) in address \"%s\"\n",
165 mrarg[2] = canonicalize_hostname(strsave(machine));
166 mrarg[1] = potype(mrarg[2]);
167 if (!strcmp(mrarg[1], "POP")) {
168 if (strcmp(address, uname)) {
170 "%s: the name on the POP box must match the username\n",
174 } else if (!strcmp(mrarg[1], "LOCAL")) {
175 strcat(address, "@");
176 strcat(address, mrarg[2]);
178 if ((address = strchr(address, '@')) &&
179 (address = strchr(address, '.')))
181 strcat(mrarg[2], ".LOCAL");
183 } else if (!strcmp(mrarg[1], "MAILHUB")) {
184 if (!strcmp(address, uname)) {
186 "Error: this will set a mail forwarding loop.\n");
188 "Use \"%s -p\" to set a local post office server.\n",
192 fprintf(stderr, "Error: \"%s@%s\" is a local mail address.\n",
194 fprintf(stderr, "Your mail drop must be on a post office server or an external mail address.\n");
196 } else if (mrarg[1]) {
197 if (*machine != '"' && strcasecmp(mrarg[2], machine))
198 fprintf(stderr, "Warning: hostname %s canonicalized to %s\n",
200 strcat(address, "@");
201 strcat(address, mrarg[2]);
205 status = mr_query("set_pobox", 3, mrarg, scream, NULL);
207 com_err(whoami, status,
208 " while setting pobox for %s to type %s, box %s",
209 mrarg[0], mrarg[1], mrarg[2]);
210 } else if (prevpop) {
211 status = mr_query("set_pobox_pop", 1, mrarg, scream, NULL);
212 if (status == MR_MACHINE) {
214 "Moira has no record of a previous POP box for %s\n", uname);
215 } else if (status != 0)
216 com_err(whoami, status, " while setting pobox");
223 status = mr_query("get_pobox", 1, mrarg, get_pobox, NULL);
224 if (status == MR_NO_MATCH)
225 printf("User %s has no pobox.\n", uname);
226 else if (status != 0)
227 com_err(whoami, status, " while retrieving current mailbox");
235 com_err(whoami, 0, "Unexpected return value from Moira -- programmer botch");
241 * get_pobox gets all your poboxes and displays them.
246 get_pobox(argc, argv, callarg)
248 char **argv, *callarg;
250 if (!strcmp(argv[1], "POP"))
251 printf("User %s, Type %s, Box: %s@%s\n",
252 argv[0], argv[1], argv[0], argv[2]);
254 printf("User %s, Type %s, Box: %s\n",
255 argv[0], argv[1], argv[2]);
256 printf(" Modified by %s on %s with %s\n", argv[4], argv[3], argv[5]);
261 * given a canonicalized machine name, ask the MR server if it is of type
262 * pop, or of type local -- if neither, we assume that it's of type foreign.
268 char *service[1], *argv[3];
269 int check_match(), check_match3(), status;
273 status = mr_query("get_server_locations", 1, service,
274 check_match, machine);
275 if (status && (status != MR_NO_MATCH)) {
276 com_err(whoami, status, " while reading list of POP servers");
282 service[0] = "LOCAL";
283 status = mr_query("get_server_locations", 1, service,
284 check_match, machine);
285 if (status && (status != MR_NO_MATCH)) {
286 com_err(whoami, status, " while reading list of LOCAL servers");
295 status = mr_query("get_alias", 3, argv, check_match3, machine);
296 if (status && (status != MR_NO_MATCH)) {
297 com_err(whoami, status, " while reading list of MAILHUB servers");
308 check_match(argc, argv, callback)
310 char **argv, *callback;
315 if (strcasecmp(argv[1], callback) == 0)
323 check_match3(argc, argv, callback)
325 char **argv, *callback;
330 if (strcasecmp(argv[2], callback) == 0)
338 fprintf(stderr, "Usage: %s [-s address] [-p] [-u user]\n", whoami);