3 * This is the file printer.c for the Moira Client, which allows users
4 * to quickly and easily maintain most parts of the Moira database.
5 * It Contains: Functions for handling the printers.
10 * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
11 * For copying and distribution information, please see the file
15 #include <mit-copyright.h>
17 #include <moira_site.h>
28 void RealDeletePrn(char **info, Bool one_item);
29 void ChangePrn(char **info, Bool one_item);
30 extern int GetAliasValue(int argc, char **argv, void *retval);
31 int StoreHWAddr(int argc, char **argv, void *retval);
40 #define DEFAULT_LOGHOST "WSLOGGER.MIT.EDU"
42 int StoreHWAddr(int argc, char **argv, void *retval)
49 /* Function Name: SetDefaults
50 * Description: sets the default values for filesystem additions.
51 * Arguments: info - an array of char pointers to recieve defaults.
52 * Returns: char ** (this array, now filled).
55 static char **SetDefaults(char **info, char *name)
57 info[PRN_NAME] = strdup(name);
58 info[PRN_TYPE] = strdup("PRIVATE");
59 info[PRN_HWTYPE] = strdup("HP");
60 info[PRN_DUPLEXNAME] = strdup("");
61 info[PRN_HOSTNAME] = strdup(name);
62 info[PRN_LOGHOST] = strdup(DEFAULT_LOGHOST);
63 info[PRN_RM] = strdup("[ANY]");
64 info[PRN_RP] = strdup(name);
65 info[PRN_RQ] = strdup("[NONE]");
66 info[PRN_KA] = strdup("1");
67 info[PRN_PC] = strdup("10");
68 info[PRN_AC] = strdup("[none]");
69 info[PRN_LPC_ACL] = strdup("[none]");
70 info[PRN_BANNER] = strdup("1");
71 info[PRN_LOCATION] = strdup("");
72 info[PRN_CONTACT] = strdup("");
73 info[PRN_MODTIME] = info[PRN_MODBY] = info[PRN_MODWITH] = NULL;
79 /* Function Name: GetPrnInfo
80 * Description: Stores the info in a queue.
81 * Arguments: name - name of the item to get information on.
82 * Returns: a pointer to the first element in the queue or null
83 * if printer not found.
86 static struct mqelem *GetPrnInfo(char *name, int how)
89 struct mqelem *elem = NULL;
94 stat = do_mr_query("get_printer", 1, &name, StoreInfo, &elem);
95 if (stat == MR_NO_MATCH)
97 stat = do_mr_query("get_printer_by_duplexname", 1, &name,
102 stat = do_mr_query("get_printer_by_ethernet", 1, &name,
106 name = canonicalize_hostname(strdup(name));
107 stat = do_mr_query("get_printer_by_hostname", 1, &name,
112 name = canonicalize_hostname(strdup(name));
113 stat = do_mr_query("get_printer_by_rm", 1, &name,
118 stat = do_mr_query("get_printer_by_location", 1, &name,
122 stat = do_mr_query("get_printer_by_contact", 1, &name,
129 com_err(program_name, stat, " in GetPrnInfo");
132 return QueueTop(elem);
135 /* Function Name: PrintPrnInfo
136 * Description: Yet another specialized print function.
137 * Arguments: info - all info about this Printer.
138 * Returns: printer name
141 static char *PrintPrnInfo(char **info)
143 char buf[BUFSIZ], *hwaddr;
144 int status, banner = atoi(info[PRN_BANNER]);
146 if (!info) /* If no informaion */
148 Put_message("PrintPrnInfo called with null info!");
152 sprintf(buf, "Printer: %-18s Duplex queue: %-18s", info[PRN_NAME],
153 *info[PRN_DUPLEXNAME] ? info[PRN_DUPLEXNAME] : "[none]");
155 sprintf(buf, "Type: %-10s Hardware type: %-10s Hardware address: ",
156 info[PRN_TYPE], info[PRN_HWTYPE]);
157 status = do_mr_query("get_host_hwaddr", 1, &info[PRN_HOSTNAME],
158 StoreHWAddr, &hwaddr);
159 if (status == MR_SUCCESS)
167 sprintf(buf, "Printer hostname: %s", info[PRN_HOSTNAME]);
169 sprintf(buf, "Printer log host: %s", info[PRN_LOGHOST]);
171 sprintf(buf, "Spool host: %s", info[PRN_RM]);
173 sprintf(buf, "Remote Printer Name: %-38s Banner page: %s", info[PRN_RP],
174 banner ? ( banner == PRN_BANNER_FIRST ? "Yes" : "Last" ) : "No");
176 sprintf(buf, "Authentication: %-3s Price/page: %-3s Quota Server: %s",
177 atoi(info[PRN_KA]) ? "yes" : "no", info[PRN_PC], info[PRN_RQ]);
179 sprintf(buf, "Restrict list: %-23s LPC ACL: %-23s",
180 info[PRN_AC], info[PRN_LPC_ACL]);
182 sprintf(buf, "Location: %s", info[PRN_LOCATION]);
184 sprintf(buf, "Contact: %s", info[PRN_CONTACT]);
186 sprintf(buf, MOD_FORMAT, info[PRN_MODBY], info[PRN_MODTIME],
190 return info[PRN_NAME];
193 /* Function Name: AskPrnInfo.
194 * Description: This function askes the user for information about a
195 * printer and saves it into a structure.
196 * Arguments: info - a pointer the the structure to put the
201 static char **AskPrnInfo(char **info)
203 char temp_buf[BUFSIZ];
204 char *args[3], *lpc_acl;
209 sprintf(temp_buf, "Printer entry for %s.", info[PRN_NAME]);
210 Put_message(temp_buf);
213 if (GetTypeFromUser("Type of Printer", "printertype", &info[PRN_TYPE]) ==
216 if (GetTypeFromUser("Hardware Type", "printerhwtype", &info[PRN_HWTYPE]) ==
219 if (GetValueFromUser("Duplex spool name", &info[PRN_DUPLEXNAME]) ==
222 if (GetValueFromUser("Printer hostname (or [none])", &info[PRN_HOSTNAME]) ==
225 info[PRN_HOSTNAME] = canonicalize_hostname(info[PRN_HOSTNAME]);
226 if (GetValueFromUser("Log host", &info[PRN_LOGHOST]) == SUB_ERROR)
228 info[PRN_LOGHOST] = canonicalize_hostname(info[PRN_LOGHOST]);
229 if (GetValueFromUser("Spool host (or [any])", &info[PRN_RM]) == SUB_ERROR)
231 info[PRN_RM] = canonicalize_hostname(info[PRN_RM]);
232 if (GetValueFromUser("Remote printer name", &info[PRN_RP]) == SUB_ERROR)
234 if (GetValueFromUser("Quota server", &info[PRN_RQ]) == SUB_ERROR)
236 info[PRN_RQ] = canonicalize_hostname(info[PRN_RQ]);
237 if (GetYesNoValueFromUser("Kerberos authenticated", &info[PRN_KA]) ==
240 if (GetValueFromUser("Price per page", &info[PRN_PC]) == SUB_ERROR)
242 if (GetValueFromUser("Restrict list", &info[PRN_AC]) == SUB_ERROR)
244 args[0] = info[PRN_TYPE];
247 status = do_mr_query("get_alias", 3, args, GetAliasValue, &lpc_acl);
248 if (status == MR_SUCCESS)
250 free(info[PRN_LPC_ACL]);
251 info[PRN_LPC_ACL] = lpc_acl;
253 if (GetValueFromUser("LPC ACL", &info[PRN_LPC_ACL]) == SUB_ERROR)
255 if (GetYesNoValueFromUser("Banner page", &info[PRN_BANNER]) == SUB_ERROR)
257 if (GetValueFromUser("Location", &info[PRN_LOCATION]) == SUB_ERROR)
259 if (GetValueFromUser("Contact", &info[PRN_CONTACT]) == SUB_ERROR)
262 FreeAndClear(&info[PRN_MODTIME], TRUE);
263 FreeAndClear(&info[PRN_MODBY], TRUE);
264 FreeAndClear(&info[PRN_MODWITH], TRUE);
269 /* ---------------- Printer Menu ------------------ */
271 /* Function Name: GetPrn
272 * Description: Get Printer information
273 * Arguments: argc, argv - name of printer in argv[1].
274 * Returns: DM_NORMAL.
277 int GetPrn(int argc, char **argv)
281 top = GetPrnInfo(argv[1], BY_NAME); /* get info. */
282 Loop(top, (void (*)(char **)) PrintPrnInfo);
283 FreeQueue(top); /* clean the queue. */
287 int GetPrnByEthernet(int argc, char **argv)
291 top = GetPrnInfo(argv[1], BY_ETHERNET); /* get info. */
292 Loop(top, (void (*)(char **)) PrintPrnInfo);
293 FreeQueue(top); /* clean the queue. */
297 int GetPrnByHostname(int argc, char **argv)
301 top = GetPrnInfo(argv[1], BY_HOSTNAME); /* get info. */
302 Loop(top, (void (*)(char **)) PrintPrnInfo);
303 FreeQueue(top); /* clean the queue. */
307 int GetPrnByRM(int argc, char **argv)
311 top = GetPrnInfo(argv[1], BY_RM); /* get info. */
312 Loop(top, (void (*)(char **)) PrintPrnInfo);
313 FreeQueue(top); /* clean the queue. */
317 int GetPrnByLocation(int argc, char **argv)
321 top = GetPrnInfo(argv[1], BY_LOCATION); /* get info. */
322 Loop(top, (void (*)(char **)) PrintPrnInfo);
323 FreeQueue(top); /* clean the queue. */
327 int GetPrnByContact(int argc, char **argv)
331 top = GetPrnInfo(argv[1], BY_CONTACT); /* get info. */
332 Loop(top, (void (*)(char **)) PrintPrnInfo);
333 FreeQueue(top); /* clean the queue. */
338 /* Function Name: RealDeletePrn
339 * Description: Does the real deletion work.
340 * Arguments: info - array of char *'s containing all useful info.
341 * one_item - a Boolean that is true if only one item
342 * in queue that dumped us here.
346 void RealDeletePrn(char **info, Bool one_item)
350 if ((stat = do_mr_query("delete_printer", 1, &info[PRN_NAME], NULL, NULL)))
351 com_err(program_name, stat, " printer not deleted.");
353 Put_message("Printer deleted.");
356 /* Function Name: DeletePrn
357 * Description: Delete a printer given its name.
358 * Arguments: argc, argv - argv[1] is the name of the printer.
362 int DeletePrn(int argc, char **argv)
364 struct mqelem *elem = GetPrnInfo(argv[1], BY_NAME);
365 QueryLoop(elem, PrintPrnInfo, RealDeletePrn, "Delete Printer");
371 /* Function Name: AddPrn
372 * Description: Add a printer
373 * Arguments: arc, argv - name of printer in argv[1].
374 * Returns: DM_NORMAL.
377 int AddPrn(int argc, char **argv)
379 char *info[MAX_ARGS_SIZE], **args;
382 if (!ValidName(argv[1]))
385 if (!(stat = do_mr_query("get_printer", 1, argv + 1, NULL, NULL)) ||
386 !(stat = do_mr_query("get_printer_by_duplexname", 1, argv + 1,
389 Put_message ("A Printer by that name already exists.");
392 else if (stat != MR_NO_MATCH)
394 com_err(program_name, stat, " in AddPrn");
398 args = AskPrnInfo(SetDefaults(info, argv[1]));
401 Put_message("Aborted.");
405 if ((stat = do_mr_query("add_printer", CountArgs(args), args, NULL, NULL)))
406 com_err(program_name, stat, " in AddPrn");
408 if (stat == MR_SUCCESS && strcasecmp(info[PRN_HOSTNAME], "[NONE]"))
409 UpdateHWAddr(2, &info[PRN_HOSTNAME - 1]);
416 /* Function Name: ChangePrn
417 * Description: Do the work of changing a Prn
418 * Arguments: argc, argv - printcap info
422 void ChangePrn(char **info, Bool one_item)
427 oldinfo = CopyInfo(info);
428 if (!AskPrnInfo(info))
430 if ((stat = do_mr_query("delete_printer", 1, &info[PRN_NAME], NULL, NULL)))
432 com_err(program_name, stat, " printer not updated.");
435 if ((stat = do_mr_query("add_printer", CountArgs(info), info, NULL, NULL)))
437 com_err(program_name, stat, " in ChngPrn");
438 if ((stat = do_mr_query("add_printer", CountArgs(oldinfo) - 3,
439 oldinfo, NULL, NULL)))
440 com_err(program_name, stat, " while attempting to put old info back");
447 /* Function Name: ChngPrn
448 * Description: Update the printcap information
449 * Arguments: argc, argv - name of printer in argv[1].
450 * Returns: DM_NORMAL.
453 int ChngPrn(int argc, char **argv)
455 struct mqelem *elem = GetPrnInfo(argv[1], BY_NAME);
456 QueryLoop(elem, NullPrint, ChangePrn, "Change the printer");
462 int UpdateHWAddr(int argc, char **argv)
465 char *name, *hwaddr, *s, *d, *uargv[2];
467 name = canonicalize_hostname(strdup(argv[1]));
468 stat = do_mr_query("get_host_hwaddr", 1, &name, StoreHWAddr, &hwaddr);
469 if (stat != MR_SUCCESS)
472 com_err(program_name, stat, " checking host ethernet address");
476 if (GetValueFromUser("Hardware ethernet address", &hwaddr) == SUB_ERROR)
492 if ((stat = do_mr_query("update_host_hwaddr", 2, uargv, NULL, NULL)))
493 com_err(program_name, stat, " updating ethernet address.");