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 = rindex(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 = index(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 = index(address, '@')) &&
179 (address = index(address, '.')))
181 strcat(mrarg[2], ".LOCAL");
183 } else if (mrarg[1]) {
184 if (*machine != '"' && strcasecmp(mrarg[2], machine))
185 printf("Warning: hostname %s canonicalized to %s\n",
187 strcat(address, "@");
188 strcat(address, mrarg[2]);
192 status = mr_query("set_pobox", 3, mrarg, scream, NULL);
194 com_err(whoami, status,
195 " while setting pobox for %s to type %s, box %s",
196 mrarg[0], mrarg[1], mrarg[2]);
197 } else if (prevpop) {
198 status = mr_query("set_pobox_pop", 1, mrarg, scream, NULL);
199 if (status == MR_MACHINE) {
201 "Moira has no record of a previous POP box for %s\n", uname);
202 } else if (status != 0)
203 com_err(whoami, status, " while setting pobox");
210 status = mr_query("get_pobox", 1, mrarg, get_pobox, NULL);
211 if (status == MR_NO_MATCH)
212 printf("User %s has no pobox.\n", uname);
213 else if (status != 0)
214 com_err(whoami, status, " while retrieving current mailbox");
222 com_err(whoami, 0, "Unexpected return value from Moira -- programmer botch");
228 * get_pobox gets all your poboxes and displays them.
233 get_pobox(argc, argv, callarg)
235 char **argv, *callarg;
237 if (!strcmp(argv[1], "POP"))
238 printf("User %s, Type %s, Box: %s@%s\n",
239 argv[0], argv[1], argv[0], argv[2]);
241 printf("User %s, Type %s, Box: %s\n",
242 argv[0], argv[1], argv[2]);
243 printf(" Modified by %s on %s with %s\n", argv[4], argv[3], argv[5]);
248 * given a canonicalized machine name, ask the MR server if it is of type
249 * pop, or of type local -- if neither, we assume that it's of type foreign.
256 int check_match(), status;
260 status = mr_query("get_server_locations", 1, service,
261 check_match, machine);
262 if (status && (status != MR_NO_MATCH)) {
263 com_err(whoami, status, " while reading list of POP servers");
269 service[0] = "LOCAL";
270 status = mr_query("get_server_locations", 1, service,
271 check_match, machine);
272 if (status && (status != MR_NO_MATCH)) {
273 com_err(whoami, status, " while reading list of LOCAL servers");
284 check_match(argc, argv, callback)
286 char **argv, *callback;
291 if (strcasecmp(argv[1], callback) == 0)
299 fprintf(stderr, "Usage: %s [-s address] [-p] [-u user]\n", whoami);