]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpadmin.c
Move debug printfs to internal usage only.
[thirdparty/cups.git] / systemv / lpadmin.c
1 /*
2 * "lpadmin" command for CUPS.
3 *
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products.
6 *
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
8 */
9
10 /*
11 * Include necessary headers...
12 */
13
14 #include <cups/cups-private.h>
15 #include <cups/ppd-private.h>
16
17
18 /*
19 * Local functions...
20 */
21
22 static int add_printer_to_class(http_t *http, char *printer, char *pclass);
23 static int default_printer(http_t *http, char *printer);
24 static int delete_printer(http_t *http, char *printer);
25 static int delete_printer_from_class(http_t *http, char *printer,
26 char *pclass);
27 static int delete_printer_option(http_t *http, char *printer,
28 char *option);
29 static int enable_printer(http_t *http, char *printer);
30 static char *get_printer_ppd(const char *uri, char *buffer, size_t bufsize, int *num_options, cups_option_t **options);
31 static cups_ptype_t get_printer_type(http_t *http, char *printer, char *uri,
32 size_t urisize);
33 static int set_printer_options(http_t *http, char *printer,
34 int num_options, cups_option_t *options,
35 char *file, int enable);
36 static int validate_name(const char *name);
37
38
39 /*
40 * 'main()' - Parse options and configure the scheduler.
41 */
42
43 int /* O - Exit status */
44 main(int argc, /* I - Number of command-line arguments */
45 char *argv[]) /* I - Command-line arguments */
46 {
47 int i; /* Looping var */
48 http_t *http; /* Connection to server */
49 char *printer, /* Destination printer */
50 *pclass, /* Printer class name */
51 *opt, /* Option pointer */
52 *val; /* Pointer to allow/deny value */
53 int enable = 0; /* Enable/resume printer? */
54 int num_options; /* Number of options */
55 cups_option_t *options; /* Options */
56 char *file, /* New PPD file */
57 evefile[1024] = ""; /* IPP Everywhere PPD */
58 const char *ppd_name, /* ppd-name value */
59 *device_uri; /* device-uri value */
60
61
62 _cupsSetLocale(argv);
63
64 http = NULL;
65 printer = NULL;
66 num_options = 0;
67 options = NULL;
68 file = NULL;
69
70 for (i = 1; i < argc; i ++)
71 {
72 if (argv[i][0] == '-')
73 {
74 for (opt = argv[i] + 1; *opt; opt ++)
75 {
76 switch (*opt)
77 {
78 case 'c' : /* Add printer to class */
79 if (!http)
80 {
81 http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
82
83 if (http == NULL)
84 {
85 _cupsLangPrintf(stderr, _("lpadmin: Unable to connect to server: %s"), strerror(errno));
86 return (1);
87 }
88 }
89
90 if (printer == NULL)
91 {
92 _cupsLangPuts(stderr,
93 _("lpadmin: Unable to add a printer to the class:\n"
94 " You must specify a printer name first."));
95 return (1);
96 }
97
98 if (opt[1] != '\0')
99 {
100 pclass = opt + 1;
101 opt += strlen(opt) - 1;
102 }
103 else
104 {
105 i ++;
106
107 if (i >= argc)
108 {
109 _cupsLangPuts(stderr, _("lpadmin: Expected class name after \"-c\" option."));
110 return (1);
111 }
112
113 pclass = argv[i];
114 }
115
116 if (!validate_name(pclass))
117 {
118 _cupsLangPuts(stderr,
119 _("lpadmin: Class name can only contain printable "
120 "characters."));
121 return (1);
122 }
123
124 if (add_printer_to_class(http, printer, pclass))
125 return (1);
126 break;
127
128 case 'd' : /* Set as default destination */
129 if (!http)
130 {
131 http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
132
133 if (http == NULL)
134 {
135 _cupsLangPrintf(stderr, _("lpadmin: Unable to connect to server: %s"), strerror(errno));
136 return (1);
137 }
138 }
139
140 if (opt[1] != '\0')
141 {
142 printer = opt + 1;
143 opt += strlen(opt) - 1;
144 }
145 else
146 {
147 i ++;
148
149 if (i >= argc)
150 {
151 _cupsLangPuts(stderr, _("lpadmin: Expected printer name after \"-d\" option."));
152 return (1);
153 }
154
155 printer = argv[i];
156 }
157
158 if (!validate_name(printer))
159 {
160 _cupsLangPuts(stderr, _("lpadmin: Printer name can only contain printable characters."));
161 return (1);
162 }
163
164 if (default_printer(http, printer))
165 return (1);
166
167 i = argc;
168 break;
169
170 case 'h' : /* Connect to host */
171 if (http)
172 {
173 httpClose(http);
174 http = NULL;
175 }
176
177 if (opt[1] != '\0')
178 {
179 cupsSetServer(opt + 1);
180 opt += strlen(opt) - 1;
181 }
182 else
183 {
184 i ++;
185
186 if (i >= argc)
187 {
188 _cupsLangPuts(stderr, _("lpadmin: Expected hostname after \"-h\" option."));
189 return (1);
190 }
191
192 cupsSetServer(argv[i]);
193 }
194 break;
195
196 case 'P' : /* Use the specified PPD file */
197 case 'i' : /* Use the specified PPD file */
198 if (opt[1] != '\0')
199 {
200 file = opt + 1;
201 opt += strlen(opt) - 1;
202 }
203 else
204 {
205 i ++;
206
207 if (i >= argc)
208 {
209 _cupsLangPrintf(stderr, _("lpadmin: Expected PPD after \"-%c\" option."), argv[i - 1][1]);
210 return (1);
211 }
212
213 file = argv[i];
214 }
215
216 if (*opt == 'i')
217 {
218 /*
219 * Check to see that the specified file is, in fact, a PPD...
220 */
221
222 cups_file_t *fp = cupsFileOpen(file, "r");
223 char line[256];
224
225 if (!cupsFileGets(fp, line, sizeof(line)) || strncmp(line, "*PPD-Adobe", 10))
226 {
227 _cupsLangPuts(stderr, _("lpadmin: System V interface scripts are no longer supported for security reasons."));
228 cupsFileClose(fp);
229 return (1);
230 }
231
232 cupsFileClose(fp);
233 }
234 break;
235
236 case 'E' : /* Enable the printer/enable encryption */
237 if (printer == NULL)
238 {
239 #ifdef HAVE_SSL
240 cupsSetEncryption(HTTP_ENCRYPTION_REQUIRED);
241
242 if (http)
243 httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
244 #else
245 _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."), argv[0]);
246 #endif /* HAVE_SSL */
247 break;
248 }
249
250 if (!http)
251 {
252 http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
253
254 if (http == NULL)
255 {
256 _cupsLangPrintf(stderr,
257 _("lpadmin: Unable to connect to server: %s"),
258 strerror(errno));
259 return (1);
260 }
261 }
262
263 enable = 1;
264 break;
265
266 case 'm' : /* Use the specified standard script/PPD file */
267 if (opt[1] != '\0')
268 {
269 num_options = cupsAddOption("ppd-name", opt + 1, num_options, &options);
270 opt += strlen(opt) - 1;
271 }
272 else
273 {
274 i ++;
275
276 if (i >= argc)
277 {
278 _cupsLangPuts(stderr, _("lpadmin: Expected model after \"-m\" option."));
279 return (1);
280 }
281
282 num_options = cupsAddOption("ppd-name", argv[i], num_options, &options);
283 }
284 break;
285
286 case 'o' : /* Set option */
287 if (opt[1] != '\0')
288 {
289 num_options = cupsParseOptions(opt + 1, num_options, &options);
290 opt += strlen(opt) - 1;
291 }
292 else
293 {
294 i ++;
295
296 if (i >= argc)
297 {
298 _cupsLangPuts(stderr, _("lpadmin: Expected name=value after \"-o\" option."));
299 return (1);
300 }
301
302 num_options = cupsParseOptions(argv[i], num_options, &options);
303 }
304 break;
305
306 case 'p' : /* Add/modify a printer */
307 if (opt[1] != '\0')
308 {
309 printer = opt + 1;
310 opt += strlen(opt) - 1;
311 }
312 else
313 {
314 i ++;
315
316 if (i >= argc)
317 {
318 _cupsLangPuts(stderr, _("lpadmin: Expected printer after \"-p\" option."));
319 return (1);
320 }
321
322 printer = argv[i];
323 }
324
325 if (!validate_name(printer))
326 {
327 _cupsLangPuts(stderr, _("lpadmin: Printer name can only contain printable characters."));
328 return (1);
329 }
330 break;
331
332 case 'r' : /* Remove printer from class */
333 if (!http)
334 {
335 http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
336
337 if (http == NULL)
338 {
339 _cupsLangPrintf(stderr,
340 _("lpadmin: Unable to connect to server: %s"),
341 strerror(errno));
342 return (1);
343 }
344 }
345
346 if (printer == NULL)
347 {
348 _cupsLangPuts(stderr,
349 _("lpadmin: Unable to remove a printer from the class:\n"
350 " You must specify a printer name first."));
351 return (1);
352 }
353
354 if (opt[1] != '\0')
355 {
356 pclass = opt + 1;
357 opt += strlen(opt) - 1;
358 }
359 else
360 {
361 i ++;
362
363 if (i >= argc)
364 {
365 _cupsLangPuts(stderr, _("lpadmin: Expected class after \"-r\" option."));
366 return (1);
367 }
368
369 pclass = argv[i];
370 }
371
372 if (!validate_name(pclass))
373 {
374 _cupsLangPuts(stderr, _("lpadmin: Class name can only contain printable characters."));
375 return (1);
376 }
377
378 if (delete_printer_from_class(http, printer, pclass))
379 return (1);
380 break;
381
382 case 'R' : /* Remove option */
383 if (!http)
384 {
385 http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
386
387 if (http == NULL)
388 {
389 _cupsLangPrintf(stderr, _("lpadmin: Unable to connect to server: %s"), strerror(errno));
390 return (1);
391 }
392 }
393
394 if (printer == NULL)
395 {
396 _cupsLangPuts(stderr,
397 _("lpadmin: Unable to delete option:\n"
398 " You must specify a printer name first."));
399 return (1);
400 }
401
402 if (opt[1] != '\0')
403 {
404 val = opt + 1;
405 opt += strlen(opt) - 1;
406 }
407 else
408 {
409 i ++;
410
411 if (i >= argc)
412 {
413 _cupsLangPuts(stderr, _("lpadmin: Expected name after \"-R\" option."));
414 return (1);
415 }
416
417 val = argv[i];
418 }
419
420 if (delete_printer_option(http, printer, val))
421 return (1);
422 break;
423
424 case 'U' : /* Username */
425 if (opt[1] != '\0')
426 {
427 cupsSetUser(opt + 1);
428 opt += strlen(opt) - 1;
429 }
430 else
431 {
432 i ++;
433 if (i >= argc)
434 {
435 _cupsLangPrintf(stderr, _("%s: Error - expected username after \"-U\" option."), argv[0]);
436 return (1);
437 }
438
439 cupsSetUser(argv[i]);
440 }
441 break;
442
443 case 'u' : /* Allow/deny users */
444 if (opt[1] != '\0')
445 {
446 val = opt + 1;
447 opt += strlen(opt) - 1;
448 }
449 else
450 {
451 i ++;
452
453 if (i >= argc)
454 {
455 _cupsLangPuts(stderr, _("lpadmin: Expected allow/deny:userlist after \"-u\" option."));
456 return (1);
457 }
458
459 val = argv[i];
460 }
461
462 if (!_cups_strncasecmp(val, "allow:", 6))
463 num_options = cupsAddOption("requesting-user-name-allowed", val + 6, num_options, &options);
464 else if (!_cups_strncasecmp(val, "deny:", 5))
465 num_options = cupsAddOption("requesting-user-name-denied", val + 5, num_options, &options);
466 else
467 {
468 _cupsLangPrintf(stderr, _("lpadmin: Unknown allow/deny option \"%s\"."), val);
469 return (1);
470 }
471 break;
472
473 case 'v' : /* Set the device-uri attribute */
474 if (opt[1] != '\0')
475 {
476 num_options = cupsAddOption("device-uri", opt + 1, num_options, &options);
477 opt += strlen(opt) - 1;
478 }
479 else
480 {
481 i ++;
482
483 if (i >= argc)
484 {
485 _cupsLangPuts(stderr, _("lpadmin: Expected device URI after \"-v\" option."));
486 return (1);
487 }
488
489 num_options = cupsAddOption("device-uri", argv[i], num_options, &options);
490 }
491 break;
492
493 case 'x' : /* Delete a printer */
494 if (!http)
495 {
496 http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
497
498 if (http == NULL)
499 {
500 _cupsLangPrintf(stderr,
501 _("lpadmin: Unable to connect to server: %s"),
502 strerror(errno));
503 return (1);
504 }
505 }
506
507 if (opt[1] != '\0')
508 {
509 printer = opt + 1;
510 opt += strlen(opt) - 1;
511 }
512 else
513 {
514 i ++;
515
516 if (i >= argc)
517 {
518 _cupsLangPuts(stderr, _("lpadmin: Expected printer or class after \"-x\" option."));
519 return (1);
520 }
521
522 printer = argv[i];
523 }
524
525 if (!validate_name(printer))
526 {
527 _cupsLangPuts(stderr, _("lpadmin: Printer name can only contain printable characters."));
528 return (1);
529 }
530
531 if (delete_printer(http, printer))
532 return (1);
533
534 i = argc;
535 break;
536
537 case 'D' : /* Set the printer-info attribute */
538 if (opt[1] != '\0')
539 {
540 num_options = cupsAddOption("printer-info", opt + 1, num_options, &options);
541 opt += strlen(opt) - 1;
542 }
543 else
544 {
545 i ++;
546
547 if (i >= argc)
548 {
549 _cupsLangPuts(stderr, _("lpadmin: Expected description after \"-D\" option."));
550 return (1);
551 }
552
553 num_options = cupsAddOption("printer-info", argv[i], num_options, &options);
554 }
555 break;
556
557 case 'I' : /* Set the supported file types (ignored) */
558 i ++;
559
560 if (i >= argc)
561 {
562 _cupsLangPuts(stderr, _("lpadmin: Expected file type(s) after \"-I\" option."));
563 return (1);
564 }
565
566 _cupsLangPuts(stderr, _("lpadmin: Warning - content type list ignored."));
567 break;
568
569 case 'L' : /* Set the printer-location attribute */
570 if (opt[1] != '\0')
571 {
572 num_options = cupsAddOption("printer-location", opt + 1, num_options, &options);
573 opt += strlen(opt) - 1;
574 }
575 else
576 {
577 i ++;
578
579 if (i >= argc)
580 {
581 _cupsLangPuts(stderr, _("lpadmin: Expected location after \"-L\" option."));
582 return (1);
583 }
584
585 num_options = cupsAddOption("printer-location", argv[i], num_options, &options);
586 }
587 break;
588
589 default :
590 _cupsLangPrintf(stderr, _("lpadmin: Unknown option \"%c\"."), *opt);
591 return (1);
592 }
593 }
594 }
595 else
596 {
597 _cupsLangPrintf(stderr, _("lpadmin: Unknown argument \"%s\"."), argv[i]);
598 return (1);
599 }
600 }
601
602 /*
603 * Set options as needed...
604 */
605
606 ppd_name = cupsGetOption("ppd-name", num_options, options);
607 device_uri = cupsGetOption("device-uri", num_options, options);
608
609 if (ppd_name && !strcmp(ppd_name, "raw"))
610 {
611 #ifdef __APPLE__
612 _cupsLangPuts(stderr, _("lpadmin: Raw queues are no longer supported on macOS."));
613 #else
614 _cupsLangPuts(stderr, _("lpadmin: Raw queues are deprecated and will stop working in a future version of CUPS."));
615 #endif /* __APPLE__ */
616
617 if (device_uri && (!strncmp(device_uri, "ipp://", 6) || !strncmp(device_uri, "ipps://", 7)) && strstr(device_uri, "/printers/"))
618 _cupsLangPuts(stderr, _("lpadmin: Use the 'everywhere' model for shared printers."));
619
620 #ifdef __APPLE__
621 return (1);
622 #endif /* __APPLE__ */
623 }
624 else if (ppd_name && !strcmp(ppd_name, "everywhere") && device_uri)
625 {
626 if ((file = get_printer_ppd(device_uri, evefile, sizeof(evefile), &num_options, &options)) == NULL)
627 return (1);
628
629 num_options = cupsRemoveOption("ppd-name", num_options, &options);
630 }
631 else if (ppd_name || file)
632 {
633 _cupsLangPuts(stderr, _("lpadmin: Printer drivers are deprecated and will stop working in a future version of CUPS."));
634 }
635
636 if (num_options || file)
637 {
638 if (printer == NULL)
639 {
640 _cupsLangPuts(stderr,
641 _("lpadmin: Unable to set the printer options:\n"
642 " You must specify a printer name first."));
643 return (1);
644 }
645
646 if (!http)
647 {
648 http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC,
649 cupsEncryption(), 1, 30000, NULL);
650
651 if (http == NULL) {
652 _cupsLangPrintf(stderr, _("lpadmin: Unable to connect to server: %s"),
653 strerror(errno));
654 return (1);
655 }
656 }
657
658 if (set_printer_options(http, printer, num_options, options, file, enable))
659 return (1);
660 }
661 else if (enable && enable_printer(http, printer))
662 return (1);
663
664 if (evefile[0])
665 unlink(evefile);
666
667 if (printer == NULL)
668 {
669 _cupsLangPuts(stdout,
670 _("Usage:\n"
671 "\n"
672 " lpadmin [-h server] -d destination\n"
673 " lpadmin [-h server] -x destination\n"
674 " lpadmin [-h server] -p printer [-c add-class] "
675 "[-i interface] [-m model]\n"
676 " [-r remove-class] [-v device] "
677 "[-D description]\n"
678 " [-P ppd-file] [-o name=value]\n"
679 " [-u allow:user,user] "
680 "[-u deny:user,user]"));
681 }
682
683 if (http)
684 httpClose(http);
685
686 return (0);
687 }
688
689
690 /*
691 * 'add_printer_to_class()' - Add a printer to a class.
692 */
693
694 static int /* O - 0 on success, 1 on fail */
695 add_printer_to_class(http_t *http, /* I - Server connection */
696 char *printer, /* I - Printer to add */
697 char *pclass) /* I - Class to add to */
698 {
699 int i; /* Looping var */
700 ipp_t *request, /* IPP Request */
701 *response; /* IPP Response */
702 ipp_attribute_t *attr, /* Current attribute */
703 *members; /* Members in class */
704 char uri[HTTP_MAX_URI]; /* URI for printer/class */
705
706
707 /*
708 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
709 * attributes:
710 *
711 * attributes-charset
712 * attributes-natural-language
713 * printer-uri
714 * requesting-user-name
715 */
716
717 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
718
719 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
720 "localhost", 0, "/classes/%s", pclass);
721 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
722 "printer-uri", NULL, uri);
723 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
724 NULL, cupsUser());
725
726 /*
727 * Do the request and get back a response...
728 */
729
730 response = cupsDoRequest(http, request, "/");
731
732 /*
733 * Build a CUPS-Add-Modify-Class request, which requires the following
734 * attributes:
735 *
736 * attributes-charset
737 * attributes-natural-language
738 * printer-uri
739 * requesting-user-name
740 * member-uris
741 */
742
743 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
744
745 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
746 "printer-uri", NULL, uri);
747 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
748 NULL, cupsUser());
749
750 /*
751 * See if the printer is already in the class...
752 */
753
754 if (response != NULL &&
755 (members = ippFindAttribute(response, "member-names",
756 IPP_TAG_NAME)) != NULL)
757 for (i = 0; i < members->num_values; i ++)
758 if (_cups_strcasecmp(printer, members->values[i].string.text) == 0)
759 {
760 _cupsLangPrintf(stderr,
761 _("lpadmin: Printer %s is already a member of class "
762 "%s."), printer, pclass);
763 ippDelete(request);
764 ippDelete(response);
765 return (0);
766 }
767
768 /*
769 * OK, the printer isn't part of the class, so add it...
770 */
771
772 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
773 "localhost", 0, "/printers/%s", printer);
774
775 if (response != NULL &&
776 (members = ippFindAttribute(response, "member-uris",
777 IPP_TAG_URI)) != NULL)
778 {
779 /*
780 * Add the printer to the existing list...
781 */
782
783 attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_URI,
784 "member-uris", members->num_values + 1, NULL, NULL);
785 for (i = 0; i < members->num_values; i ++)
786 attr->values[i].string.text =
787 _cupsStrAlloc(members->values[i].string.text);
788
789 attr->values[i].string.text = _cupsStrAlloc(uri);
790 }
791 else
792 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI, "member-uris", NULL,
793 uri);
794
795 /*
796 * Then send the request...
797 */
798
799 ippDelete(response);
800
801 ippDelete(cupsDoRequest(http, request, "/admin/"));
802 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
803 {
804 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
805
806 return (1);
807 }
808 else
809 return (0);
810 }
811
812
813 /*
814 * 'default_printer()' - Set the default printing destination.
815 */
816
817 static int /* O - 0 on success, 1 on fail */
818 default_printer(http_t *http, /* I - Server connection */
819 char *printer) /* I - Printer name */
820 {
821 ipp_t *request; /* IPP Request */
822 char uri[HTTP_MAX_URI]; /* URI for printer/class */
823
824
825 /*
826 * Build a CUPS-Set-Default request, which requires the following
827 * attributes:
828 *
829 * attributes-charset
830 * attributes-natural-language
831 * printer-uri
832 * requesting-user-name
833 */
834
835 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
836 "localhost", 0, "/printers/%s", printer);
837
838 request = ippNewRequest(IPP_OP_CUPS_SET_DEFAULT);
839
840 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
841 "printer-uri", NULL, uri);
842 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
843 NULL, cupsUser());
844
845 /*
846 * Do the request and get back a response...
847 */
848
849 ippDelete(cupsDoRequest(http, request, "/admin/"));
850
851 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
852 {
853 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
854
855 return (1);
856 }
857 else
858 return (0);
859 }
860
861
862 /*
863 * 'delete_printer()' - Delete a printer from the system...
864 */
865
866 static int /* O - 0 on success, 1 on fail */
867 delete_printer(http_t *http, /* I - Server connection */
868 char *printer) /* I - Printer to delete */
869 {
870 ipp_t *request; /* IPP Request */
871 char uri[HTTP_MAX_URI]; /* URI for printer/class */
872
873
874 /*
875 * Build a CUPS-Delete-Printer request, which requires the following
876 * attributes:
877 *
878 * attributes-charset
879 * attributes-natural-language
880 * printer-uri
881 * requesting-user-name
882 */
883
884 request = ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER);
885
886 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
887 "localhost", 0, "/printers/%s", printer);
888 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
889 "printer-uri", NULL, uri);
890 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
891 NULL, cupsUser());
892
893 /*
894 * Do the request and get back a response...
895 */
896
897 ippDelete(cupsDoRequest(http, request, "/admin/"));
898
899 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
900 {
901 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
902
903 return (1);
904 }
905 else
906 return (0);
907 }
908
909
910 /*
911 * 'delete_printer_from_class()' - Delete a printer from a class.
912 */
913
914 static int /* O - 0 on success, 1 on fail */
915 delete_printer_from_class(
916 http_t *http, /* I - Server connection */
917 char *printer, /* I - Printer to remove */
918 char *pclass) /* I - Class to remove from */
919 {
920 int i, j, k; /* Looping vars */
921 ipp_t *request, /* IPP Request */
922 *response; /* IPP Response */
923 ipp_attribute_t *attr, /* Current attribute */
924 *members; /* Members in class */
925 char uri[HTTP_MAX_URI]; /* URI for printer/class */
926
927
928 /*
929 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
930 * attributes:
931 *
932 * attributes-charset
933 * attributes-natural-language
934 * printer-uri
935 * requesting-user-name
936 */
937
938 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
939
940 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
941 "localhost", 0, "/classes/%s", pclass);
942 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
943 "printer-uri", NULL, uri);
944 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
945 NULL, cupsUser());
946
947 /*
948 * Do the request and get back a response...
949 */
950
951 if ((response = cupsDoRequest(http, request, "/classes/")) == NULL ||
952 response->request.status.status_code == IPP_STATUS_ERROR_NOT_FOUND)
953 {
954 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
955
956 ippDelete(response);
957
958 return (1);
959 }
960
961 /*
962 * See if the printer is already in the class...
963 */
964
965 if ((members = ippFindAttribute(response, "member-names", IPP_TAG_NAME)) == NULL)
966 {
967 _cupsLangPuts(stderr, _("lpadmin: No member names were seen."));
968
969 ippDelete(response);
970
971 return (1);
972 }
973
974 for (i = 0; i < members->num_values; i ++)
975 if (!_cups_strcasecmp(printer, members->values[i].string.text))
976 break;
977
978 if (i >= members->num_values)
979 {
980 _cupsLangPrintf(stderr,
981 _("lpadmin: Printer %s is not a member of class %s."),
982 printer, pclass);
983
984 ippDelete(response);
985
986 return (1);
987 }
988
989 if (members->num_values == 1)
990 {
991 /*
992 * Build a CUPS-Delete-Class request, which requires the following
993 * attributes:
994 *
995 * attributes-charset
996 * attributes-natural-language
997 * printer-uri
998 * requesting-user-name
999 */
1000
1001 request = ippNewRequest(IPP_OP_CUPS_DELETE_CLASS);
1002
1003 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1004 "printer-uri", NULL, uri);
1005 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1006 "requesting-user-name", NULL, cupsUser());
1007 }
1008 else
1009 {
1010 /*
1011 * Build a IPP_OP_CUPS_ADD_MODIFY_CLASS request, which requires the following
1012 * attributes:
1013 *
1014 * attributes-charset
1015 * attributes-natural-language
1016 * printer-uri
1017 * requesting-user-name
1018 * member-uris
1019 */
1020
1021 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
1022
1023 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1024 "printer-uri", NULL, uri);
1025 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1026 "requesting-user-name", NULL, cupsUser());
1027
1028 /*
1029 * Delete the printer from the class...
1030 */
1031
1032 members = ippFindAttribute(response, "member-uris", IPP_TAG_URI);
1033 attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_URI,
1034 "member-uris", members->num_values - 1, NULL, NULL);
1035
1036 for (j = 0, k = 0; j < members->num_values; j ++)
1037 if (j != i)
1038 attr->values[k ++].string.text =
1039 _cupsStrAlloc(members->values[j].string.text);
1040 }
1041
1042 /*
1043 * Then send the request...
1044 */
1045
1046 ippDelete(response);
1047
1048 ippDelete(cupsDoRequest(http, request, "/admin/"));
1049
1050 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
1051 {
1052 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1053
1054 return (1);
1055 }
1056 else
1057 return (0);
1058 }
1059
1060
1061 /*
1062 * 'delete_printer_option()' - Delete a printer option.
1063 */
1064
1065 static int /* O - 0 on success, 1 on fail */
1066 delete_printer_option(http_t *http, /* I - Server connection */
1067 char *printer, /* I - Printer */
1068 char *option) /* I - Option to delete */
1069 {
1070 ipp_t *request; /* IPP request */
1071 char uri[HTTP_MAX_URI]; /* URI for printer/class */
1072
1073
1074 /*
1075 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1076 * requires the following attributes:
1077 *
1078 * attributes-charset
1079 * attributes-natural-language
1080 * printer-uri
1081 * requesting-user-name
1082 * option with deleteAttr tag
1083 */
1084
1085 if (get_printer_type(http, printer, uri, sizeof(uri)) & CUPS_PRINTER_CLASS)
1086 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
1087 else
1088 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER);
1089
1090 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1091 "printer-uri", NULL, uri);
1092 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1093 "requesting-user-name", NULL, cupsUser());
1094 ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_DELETEATTR, option, 0);
1095
1096 /*
1097 * Do the request and get back a response...
1098 */
1099
1100 ippDelete(cupsDoRequest(http, request, "/admin/"));
1101
1102 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
1103 {
1104 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1105
1106 return (1);
1107 }
1108 else
1109 return (0);
1110 }
1111
1112
1113 /*
1114 * 'enable_printer()' - Enable a printer...
1115 */
1116
1117 static int /* O - 0 on success, 1 on fail */
1118 enable_printer(http_t *http, /* I - Server connection */
1119 char *printer) /* I - Printer to enable */
1120 {
1121 ipp_t *request; /* IPP Request */
1122 char uri[HTTP_MAX_URI]; /* URI for printer/class */
1123
1124
1125 /*
1126 * Send IPP_OP_ENABLE_PRINTER and IPP_OP_RESUME_PRINTER requests, which
1127 * require the following attributes:
1128 *
1129 * attributes-charset
1130 * attributes-natural-language
1131 * printer-uri
1132 * requesting-user-name
1133 */
1134
1135 request = ippNewRequest(IPP_OP_ENABLE_PRINTER);
1136
1137 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1138 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
1139
1140 ippDelete(cupsDoRequest(http, request, "/admin/"));
1141
1142 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
1143 {
1144 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1145
1146 return (1);
1147 }
1148
1149 request = ippNewRequest(IPP_OP_RESUME_PRINTER);
1150
1151 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1152 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
1153
1154 ippDelete(cupsDoRequest(http, request, "/admin/"));
1155
1156 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
1157 {
1158 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1159
1160 return (1);
1161 }
1162
1163 return (0);
1164 }
1165
1166
1167 /*
1168 * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
1169 */
1170
1171 static char * /* O - Filename or NULL */
1172 get_printer_ppd(
1173 const char *uri, /* I - Printer URI */
1174 char *buffer, /* I - Filename buffer */
1175 size_t bufsize, /* I - Size of filename buffer */
1176 int *num_options, /* IO - Number of options */
1177 cups_option_t **options) /* IO - Options */
1178 {
1179 http_t *http; /* Connection to printer */
1180 ipp_t *request, /* Get-Printer-Attributes request */
1181 *response; /* Get-Printer-Attributes response */
1182 ipp_attribute_t *attr; /* Attribute from response */
1183 char resolved[1024], /* Resolved URI */
1184 scheme[32], /* URI scheme */
1185 userpass[256], /* Username:password */
1186 host[256], /* Hostname */
1187 resource[256]; /* Resource path */
1188 int port; /* Port number */
1189 static const char * const pattrs[] = /* Attributes to use */
1190 {
1191 "job-template",
1192 "printer-defaults",
1193 "printer-description",
1194 "media-col-database"
1195 };
1196
1197
1198 /*
1199 * Connect to the printer...
1200 */
1201
1202 if (strstr(uri, "._tcp"))
1203 {
1204 /*
1205 * Resolve URI...
1206 */
1207
1208 if (!_httpResolveURI(uri, resolved, sizeof(resolved), _HTTP_RESOLVE_DEFAULT, NULL, NULL))
1209 {
1210 _cupsLangPrintf(stderr, _("%s: Unable to resolve \"%s\"."), "lpadmin", uri);
1211 return (NULL);
1212 }
1213
1214 uri = resolved;
1215 }
1216
1217 if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), host, sizeof(host), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
1218 {
1219 _cupsLangPrintf(stderr, _("%s: Bad printer URI \"%s\"."), "lpadmin", uri);
1220 return (NULL);
1221 }
1222
1223 http = httpConnect2(host, port, NULL, AF_UNSPEC, !strcmp(scheme, "ipps") ? HTTP_ENCRYPTION_ALWAYS : HTTP_ENCRYPTION_IF_REQUESTED, 1, 30000, NULL);
1224 if (!http)
1225 {
1226 _cupsLangPrintf(stderr, _("%s: Unable to connect to \"%s:%d\": %s"), "lpadmin", host, port, cupsLastErrorString());
1227 return (NULL);
1228 }
1229
1230 /*
1231 * Send a Get-Printer-Attributes request...
1232 */
1233
1234 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
1235 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1236 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]), NULL, pattrs);
1237 response = cupsDoRequest(http, request, resource);
1238
1239 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE)
1240 {
1241 _cupsLangPrintf(stderr, _("%s: Unable to query printer: %s"), "lpadmin", cupsLastErrorString());
1242 buffer[0] = '\0';
1243 }
1244 else if (_ppdCreateFromIPP(buffer, bufsize, response))
1245 {
1246 if (!cupsGetOption("printer-geo-location", *num_options, *options) && (attr = ippFindAttribute(response, "printer-geo-location", IPP_TAG_URI)) != NULL)
1247 *num_options = cupsAddOption("printer-geo-location", ippGetString(attr, 0, NULL), *num_options, options);
1248
1249 if (!cupsGetOption("printer-info", *num_options, *options) && (attr = ippFindAttribute(response, "printer-info", IPP_TAG_TEXT)) != NULL)
1250 *num_options = cupsAddOption("printer-info", ippGetString(attr, 0, NULL), *num_options, options);
1251
1252 if (!cupsGetOption("printer-location", *num_options, *options) && (attr = ippFindAttribute(response, "printer-location", IPP_TAG_TEXT)) != NULL)
1253 *num_options = cupsAddOption("printer-location", ippGetString(attr, 0, NULL), *num_options, options);
1254 }
1255 else
1256 _cupsLangPrintf(stderr, _("%s: Unable to create PPD file: %s"), "lpadmin", strerror(errno));
1257
1258 ippDelete(response);
1259 httpClose(http);
1260
1261 if (buffer[0])
1262 return (buffer);
1263 else
1264 return (NULL);
1265 }
1266
1267
1268 /*
1269 * 'get_printer_type()' - Determine the printer type and URI.
1270 */
1271
1272 static cups_ptype_t /* O - printer-type value */
1273 get_printer_type(http_t *http, /* I - Server connection */
1274 char *printer, /* I - Printer name */
1275 char *uri, /* I - URI buffer */
1276 size_t urisize) /* I - Size of URI buffer */
1277 {
1278 ipp_t *request, /* IPP request */
1279 *response; /* IPP response */
1280 ipp_attribute_t *attr; /* printer-type attribute */
1281 cups_ptype_t type; /* printer-type value */
1282
1283
1284 /*
1285 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1286 * attributes:
1287 *
1288 * attributes-charset
1289 * attributes-natural-language
1290 * printer-uri
1291 * requested-attributes
1292 * requesting-user-name
1293 */
1294
1295 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, (int)urisize, "ipp", NULL, "localhost", ippPort(), "/printers/%s", printer);
1296
1297 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
1298 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1299 "printer-uri", NULL, uri);
1300 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1301 "requested-attributes", NULL, "printer-type");
1302 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1303 "requesting-user-name", NULL, cupsUser());
1304
1305 /*
1306 * Do the request...
1307 */
1308
1309 response = cupsDoRequest(http, request, "/");
1310 if ((attr = ippFindAttribute(response, "printer-type",
1311 IPP_TAG_ENUM)) != NULL)
1312 {
1313 type = (cups_ptype_t)attr->values[0].integer;
1314
1315 if (type & CUPS_PRINTER_CLASS)
1316 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, (int)urisize, "ipp", NULL, "localhost", ippPort(), "/classes/%s", printer);
1317 }
1318 else
1319 type = CUPS_PRINTER_LOCAL;
1320
1321 ippDelete(response);
1322
1323 return (type);
1324 }
1325
1326
1327 /*
1328 * 'set_printer_options()' - Set the printer options.
1329 */
1330
1331 static int /* O - 0 on success, 1 on fail */
1332 set_printer_options(
1333 http_t *http, /* I - Server connection */
1334 char *printer, /* I - Printer */
1335 int num_options, /* I - Number of options */
1336 cups_option_t *options, /* I - Options */
1337 char *file, /* I - PPD file */
1338 int enable) /* I - Enable printer? */
1339 {
1340 ipp_t *request; /* IPP Request */
1341 const char *ppdfile; /* PPD filename */
1342 int ppdchanged = 0; /* PPD changed? */
1343 ppd_file_t *ppd; /* PPD file */
1344 ppd_choice_t *choice; /* Marked choice */
1345 char uri[HTTP_MAX_URI], /* URI for printer/class */
1346 line[1024], /* Line from PPD file */
1347 keyword[1024], /* Keyword from Default line */
1348 *keyptr, /* Pointer into keyword... */
1349 tempfile[1024]; /* Temporary filename */
1350 cups_file_t *in, /* PPD file */
1351 *out; /* Temporary file */
1352 const char *ppdname, /* ppd-name value */
1353 *protocol, /* Old protocol option */
1354 *customval, /* Custom option value */
1355 *boolval; /* Boolean value */
1356 int wrote_ipp_supplies = 0, /* Wrote cupsIPPSupplies keyword? */
1357 wrote_snmp_supplies = 0,/* Wrote cupsSNMPSupplies keyword? */
1358 copied_options = 0; /* Copied options? */
1359
1360
1361 /*
1362 * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
1363 * which requires the following attributes:
1364 *
1365 * attributes-charset
1366 * attributes-natural-language
1367 * printer-uri
1368 * requesting-user-name
1369 * other options
1370 */
1371
1372 if (get_printer_type(http, printer, uri, sizeof(uri)) & CUPS_PRINTER_CLASS)
1373 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
1374 else
1375 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER);
1376
1377 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1378 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
1379
1380 if (enable)
1381 {
1382 ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", IPP_PSTATE_IDLE);
1383 ippAddBoolean(request, IPP_TAG_PRINTER, "printer-is-accepting-jobs", 1);
1384 }
1385
1386 /*
1387 * Add the options...
1388 */
1389
1390 if (file)
1391 ppdfile = file;
1392 else if ((ppdname = cupsGetOption("ppd-name", num_options, options)) != NULL && strcmp(ppdname, "raw") && num_options > 1)
1393 {
1394 if ((ppdfile = cupsGetServerPPD(http, ppdname)) != NULL)
1395 {
1396 /*
1397 * Copy options array and remove ppd-name from it...
1398 */
1399
1400 cups_option_t *temp = NULL, *optr;
1401 int i, num_temp = 0;
1402 for (i = num_options, optr = options; i > 0; i --, optr ++)
1403 if (strcmp(optr->name, "ppd-name"))
1404 num_temp = cupsAddOption(optr->name, optr->value, num_temp, &temp);
1405
1406 copied_options = 1;
1407 ppdchanged = 1;
1408 num_options = num_temp;
1409 options = temp;
1410 }
1411 }
1412 else if (request->request.op.operation_id == IPP_OP_CUPS_ADD_MODIFY_PRINTER)
1413 ppdfile = cupsGetPPD(printer);
1414 else
1415 ppdfile = NULL;
1416
1417 cupsEncodeOptions2(request, num_options, options, IPP_TAG_OPERATION);
1418 cupsEncodeOptions2(request, num_options, options, IPP_TAG_PRINTER);
1419
1420 if ((protocol = cupsGetOption("protocol", num_options, options)) != NULL)
1421 {
1422 if (!_cups_strcasecmp(protocol, "bcp"))
1423 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
1424 NULL, "bcp");
1425 else if (!_cups_strcasecmp(protocol, "tbcp"))
1426 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
1427 NULL, "tbcp");
1428 }
1429
1430 if (ppdfile)
1431 {
1432 /*
1433 * Set default options in the PPD file...
1434 */
1435
1436 if ((ppd = ppdOpenFile(ppdfile)) == NULL)
1437 {
1438 int linenum; /* Line number of error */
1439 ppd_status_t status = ppdLastError(&linenum);
1440 /* Status code */
1441
1442 _cupsLangPrintf(stderr, _("lpadmin: Unable to open PPD \"%s\": %s on line %d."), ppdfile, ppdErrorString(status), linenum);
1443 }
1444
1445 ppdMarkDefaults(ppd);
1446 cupsMarkOptions(ppd, num_options, options);
1447
1448 if ((out = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
1449 {
1450 _cupsLangPrintError(NULL, _("lpadmin: Unable to create temporary file"));
1451 ippDelete(request);
1452 if (ppdfile != file)
1453 unlink(ppdfile);
1454 if (copied_options)
1455 cupsFreeOptions(num_options, options);
1456 return (1);
1457 }
1458
1459 if ((in = cupsFileOpen(ppdfile, "r")) == NULL)
1460 {
1461 _cupsLangPrintf(stderr,
1462 _("lpadmin: Unable to open PPD file \"%s\" - %s"),
1463 ppdfile, strerror(errno));
1464 ippDelete(request);
1465 if (ppdfile != file)
1466 unlink(ppdfile);
1467 if (copied_options)
1468 cupsFreeOptions(num_options, options);
1469 cupsFileClose(out);
1470 unlink(tempfile);
1471 return (1);
1472 }
1473
1474 while (cupsFileGets(in, line, sizeof(line)))
1475 {
1476 if (!strncmp(line, "*cupsIPPSupplies:", 17) &&
1477 (boolval = cupsGetOption("cupsIPPSupplies", num_options,
1478 options)) != NULL)
1479 {
1480 wrote_ipp_supplies = 1;
1481 cupsFilePrintf(out, "*cupsIPPSupplies: %s\n",
1482 (!_cups_strcasecmp(boolval, "true") ||
1483 !_cups_strcasecmp(boolval, "yes") ||
1484 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1485 }
1486 else if (!strncmp(line, "*cupsSNMPSupplies:", 18) &&
1487 (boolval = cupsGetOption("cupsSNMPSupplies", num_options,
1488 options)) != NULL)
1489 {
1490 wrote_snmp_supplies = 1;
1491 cupsFilePrintf(out, "*cupsSNMPSupplies: %s\n",
1492 (!_cups_strcasecmp(boolval, "true") ||
1493 !_cups_strcasecmp(boolval, "yes") ||
1494 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1495 }
1496 else if (strncmp(line, "*Default", 8))
1497 cupsFilePrintf(out, "%s\n", line);
1498 else
1499 {
1500 /*
1501 * Get default option name...
1502 */
1503
1504 strlcpy(keyword, line + 8, sizeof(keyword));
1505
1506 for (keyptr = keyword; *keyptr; keyptr ++)
1507 if (*keyptr == ':' || isspace(*keyptr & 255))
1508 break;
1509
1510 *keyptr++ = '\0';
1511 while (isspace(*keyptr & 255))
1512 keyptr ++;
1513
1514 if (!strcmp(keyword, "PageRegion") ||
1515 !strcmp(keyword, "PageSize") ||
1516 !strcmp(keyword, "PaperDimension") ||
1517 !strcmp(keyword, "ImageableArea"))
1518 {
1519 if ((choice = ppdFindMarkedChoice(ppd, "PageSize")) == NULL)
1520 choice = ppdFindMarkedChoice(ppd, "PageRegion");
1521 }
1522 else
1523 choice = ppdFindMarkedChoice(ppd, keyword);
1524
1525 if (choice && strcmp(choice->choice, keyptr))
1526 {
1527 if (strcmp(choice->choice, "Custom"))
1528 {
1529 cupsFilePrintf(out, "*Default%s: %s\n", keyword, choice->choice);
1530 ppdchanged = 1;
1531 }
1532 else if ((customval = cupsGetOption(keyword, num_options,
1533 options)) != NULL)
1534 {
1535 cupsFilePrintf(out, "*Default%s: %s\n", keyword, customval);
1536 ppdchanged = 1;
1537 }
1538 else
1539 cupsFilePrintf(out, "%s\n", line);
1540 }
1541 else
1542 cupsFilePrintf(out, "%s\n", line);
1543 }
1544 }
1545
1546 if (!wrote_ipp_supplies &&
1547 (boolval = cupsGetOption("cupsIPPSupplies", num_options,
1548 options)) != NULL)
1549 {
1550 cupsFilePrintf(out, "*cupsIPPSupplies: %s\n",
1551 (!_cups_strcasecmp(boolval, "true") ||
1552 !_cups_strcasecmp(boolval, "yes") ||
1553 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1554 }
1555
1556 if (!wrote_snmp_supplies &&
1557 (boolval = cupsGetOption("cupsSNMPSupplies", num_options,
1558 options)) != NULL)
1559 {
1560 cupsFilePrintf(out, "*cupsSNMPSupplies: %s\n",
1561 (!_cups_strcasecmp(boolval, "true") ||
1562 !_cups_strcasecmp(boolval, "yes") ||
1563 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1564 }
1565
1566 cupsFileClose(in);
1567 cupsFileClose(out);
1568 ppdClose(ppd);
1569
1570 /*
1571 * Do the request...
1572 */
1573
1574 ippDelete(cupsDoFileRequest(http, request, "/admin/",
1575 ppdchanged ? tempfile : file));
1576
1577 /*
1578 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1579 * lpadmin)
1580 */
1581
1582 if (ppdfile != file)
1583 unlink(ppdfile);
1584 unlink(tempfile);
1585 }
1586 else
1587 {
1588 /*
1589 * No PPD file - just set the options...
1590 */
1591
1592 ippDelete(cupsDoRequest(http, request, "/admin/"));
1593 }
1594
1595 if (copied_options)
1596 cupsFreeOptions(num_options, options);
1597
1598 /*
1599 * Check the response...
1600 */
1601
1602 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
1603 {
1604 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1605
1606 return (1);
1607 }
1608 else
1609 return (0);
1610 }
1611
1612
1613 /*
1614 * 'validate_name()' - Make sure the printer name only contains valid chars.
1615 */
1616
1617 static int /* O - 0 if name is no good, 1 if name is good */
1618 validate_name(const char *name) /* I - Name to check */
1619 {
1620 const char *ptr; /* Pointer into name */
1621
1622
1623 /*
1624 * Scan the whole name...
1625 */
1626
1627 for (ptr = name; *ptr; ptr ++)
1628 if (*ptr == '@')
1629 break;
1630 else if ((*ptr >= 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '\\' || *ptr == '?' || *ptr == '\'' || *ptr == '\"' || *ptr == '#')
1631 return (0);
1632
1633 /*
1634 * All the characters are good; validate the length, too...
1635 */
1636
1637 return ((ptr - name) < 128);
1638 }