]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpadmin.c
2f406425c59b5cf22338e2a4200d3c435db735f2
[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 DEBUG_printf(("add_printer_to_class(%p, \"%s\", \"%s\")\n", http,
708 printer, pclass));
709
710 /*
711 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
712 * attributes:
713 *
714 * attributes-charset
715 * attributes-natural-language
716 * printer-uri
717 * requesting-user-name
718 */
719
720 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
721
722 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
723 "localhost", 0, "/classes/%s", pclass);
724 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
725 "printer-uri", NULL, uri);
726 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
727 NULL, cupsUser());
728
729 /*
730 * Do the request and get back a response...
731 */
732
733 response = cupsDoRequest(http, request, "/");
734
735 /*
736 * Build a CUPS-Add-Modify-Class request, which requires the following
737 * attributes:
738 *
739 * attributes-charset
740 * attributes-natural-language
741 * printer-uri
742 * requesting-user-name
743 * member-uris
744 */
745
746 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
747
748 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
749 "printer-uri", NULL, uri);
750 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
751 NULL, cupsUser());
752
753 /*
754 * See if the printer is already in the class...
755 */
756
757 if (response != NULL &&
758 (members = ippFindAttribute(response, "member-names",
759 IPP_TAG_NAME)) != NULL)
760 for (i = 0; i < members->num_values; i ++)
761 if (_cups_strcasecmp(printer, members->values[i].string.text) == 0)
762 {
763 _cupsLangPrintf(stderr,
764 _("lpadmin: Printer %s is already a member of class "
765 "%s."), printer, pclass);
766 ippDelete(request);
767 ippDelete(response);
768 return (0);
769 }
770
771 /*
772 * OK, the printer isn't part of the class, so add it...
773 */
774
775 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
776 "localhost", 0, "/printers/%s", printer);
777
778 if (response != NULL &&
779 (members = ippFindAttribute(response, "member-uris",
780 IPP_TAG_URI)) != NULL)
781 {
782 /*
783 * Add the printer to the existing list...
784 */
785
786 attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_URI,
787 "member-uris", members->num_values + 1, NULL, NULL);
788 for (i = 0; i < members->num_values; i ++)
789 attr->values[i].string.text =
790 _cupsStrAlloc(members->values[i].string.text);
791
792 attr->values[i].string.text = _cupsStrAlloc(uri);
793 }
794 else
795 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI, "member-uris", NULL,
796 uri);
797
798 /*
799 * Then send the request...
800 */
801
802 ippDelete(response);
803
804 ippDelete(cupsDoRequest(http, request, "/admin/"));
805 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
806 {
807 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
808
809 return (1);
810 }
811 else
812 return (0);
813 }
814
815
816 /*
817 * 'default_printer()' - Set the default printing destination.
818 */
819
820 static int /* O - 0 on success, 1 on fail */
821 default_printer(http_t *http, /* I - Server connection */
822 char *printer) /* I - Printer name */
823 {
824 ipp_t *request; /* IPP Request */
825 char uri[HTTP_MAX_URI]; /* URI for printer/class */
826
827
828 DEBUG_printf(("default_printer(%p, \"%s\")\n", http, printer));
829
830 /*
831 * Build a CUPS-Set-Default request, which requires the following
832 * attributes:
833 *
834 * attributes-charset
835 * attributes-natural-language
836 * printer-uri
837 * requesting-user-name
838 */
839
840 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
841 "localhost", 0, "/printers/%s", printer);
842
843 request = ippNewRequest(IPP_OP_CUPS_SET_DEFAULT);
844
845 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
846 "printer-uri", NULL, uri);
847 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
848 NULL, cupsUser());
849
850 /*
851 * Do the request and get back a response...
852 */
853
854 ippDelete(cupsDoRequest(http, request, "/admin/"));
855
856 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
857 {
858 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
859
860 return (1);
861 }
862 else
863 return (0);
864 }
865
866
867 /*
868 * 'delete_printer()' - Delete a printer from the system...
869 */
870
871 static int /* O - 0 on success, 1 on fail */
872 delete_printer(http_t *http, /* I - Server connection */
873 char *printer) /* I - Printer to delete */
874 {
875 ipp_t *request; /* IPP Request */
876 char uri[HTTP_MAX_URI]; /* URI for printer/class */
877
878
879 DEBUG_printf(("delete_printer(%p, \"%s\")\n", http, printer));
880
881 /*
882 * Build a CUPS-Delete-Printer request, which requires the following
883 * attributes:
884 *
885 * attributes-charset
886 * attributes-natural-language
887 * printer-uri
888 * requesting-user-name
889 */
890
891 request = ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER);
892
893 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
894 "localhost", 0, "/printers/%s", printer);
895 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
896 "printer-uri", NULL, uri);
897 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
898 NULL, cupsUser());
899
900 /*
901 * Do the request and get back a response...
902 */
903
904 ippDelete(cupsDoRequest(http, request, "/admin/"));
905
906 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
907 {
908 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
909
910 return (1);
911 }
912 else
913 return (0);
914 }
915
916
917 /*
918 * 'delete_printer_from_class()' - Delete a printer from a class.
919 */
920
921 static int /* O - 0 on success, 1 on fail */
922 delete_printer_from_class(
923 http_t *http, /* I - Server connection */
924 char *printer, /* I - Printer to remove */
925 char *pclass) /* I - Class to remove from */
926 {
927 int i, j, k; /* Looping vars */
928 ipp_t *request, /* IPP Request */
929 *response; /* IPP Response */
930 ipp_attribute_t *attr, /* Current attribute */
931 *members; /* Members in class */
932 char uri[HTTP_MAX_URI]; /* URI for printer/class */
933
934
935 DEBUG_printf(("delete_printer_from_class(%p, \"%s\", \"%s\")\n", http,
936 printer, pclass));
937
938 /*
939 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
940 * attributes:
941 *
942 * attributes-charset
943 * attributes-natural-language
944 * printer-uri
945 * requesting-user-name
946 */
947
948 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
949
950 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
951 "localhost", 0, "/classes/%s", pclass);
952 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
953 "printer-uri", NULL, uri);
954 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
955 NULL, cupsUser());
956
957 /*
958 * Do the request and get back a response...
959 */
960
961 if ((response = cupsDoRequest(http, request, "/classes/")) == NULL ||
962 response->request.status.status_code == IPP_STATUS_ERROR_NOT_FOUND)
963 {
964 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
965
966 ippDelete(response);
967
968 return (1);
969 }
970
971 /*
972 * See if the printer is already in the class...
973 */
974
975 if ((members = ippFindAttribute(response, "member-names", IPP_TAG_NAME)) == NULL)
976 {
977 _cupsLangPuts(stderr, _("lpadmin: No member names were seen."));
978
979 ippDelete(response);
980
981 return (1);
982 }
983
984 for (i = 0; i < members->num_values; i ++)
985 if (!_cups_strcasecmp(printer, members->values[i].string.text))
986 break;
987
988 if (i >= members->num_values)
989 {
990 _cupsLangPrintf(stderr,
991 _("lpadmin: Printer %s is not a member of class %s."),
992 printer, pclass);
993
994 ippDelete(response);
995
996 return (1);
997 }
998
999 if (members->num_values == 1)
1000 {
1001 /*
1002 * Build a CUPS-Delete-Class request, which requires the following
1003 * attributes:
1004 *
1005 * attributes-charset
1006 * attributes-natural-language
1007 * printer-uri
1008 * requesting-user-name
1009 */
1010
1011 request = ippNewRequest(IPP_OP_CUPS_DELETE_CLASS);
1012
1013 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1014 "printer-uri", NULL, uri);
1015 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1016 "requesting-user-name", NULL, cupsUser());
1017 }
1018 else
1019 {
1020 /*
1021 * Build a IPP_OP_CUPS_ADD_MODIFY_CLASS request, which requires the following
1022 * attributes:
1023 *
1024 * attributes-charset
1025 * attributes-natural-language
1026 * printer-uri
1027 * requesting-user-name
1028 * member-uris
1029 */
1030
1031 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
1032
1033 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1034 "printer-uri", NULL, uri);
1035 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1036 "requesting-user-name", NULL, cupsUser());
1037
1038 /*
1039 * Delete the printer from the class...
1040 */
1041
1042 members = ippFindAttribute(response, "member-uris", IPP_TAG_URI);
1043 attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_URI,
1044 "member-uris", members->num_values - 1, NULL, NULL);
1045
1046 for (j = 0, k = 0; j < members->num_values; j ++)
1047 if (j != i)
1048 attr->values[k ++].string.text =
1049 _cupsStrAlloc(members->values[j].string.text);
1050 }
1051
1052 /*
1053 * Then send the request...
1054 */
1055
1056 ippDelete(response);
1057
1058 ippDelete(cupsDoRequest(http, request, "/admin/"));
1059
1060 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
1061 {
1062 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1063
1064 return (1);
1065 }
1066 else
1067 return (0);
1068 }
1069
1070
1071 /*
1072 * 'delete_printer_option()' - Delete a printer option.
1073 */
1074
1075 static int /* O - 0 on success, 1 on fail */
1076 delete_printer_option(http_t *http, /* I - Server connection */
1077 char *printer, /* I - Printer */
1078 char *option) /* I - Option to delete */
1079 {
1080 ipp_t *request; /* IPP request */
1081 char uri[HTTP_MAX_URI]; /* URI for printer/class */
1082
1083
1084 /*
1085 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1086 * requires the following attributes:
1087 *
1088 * attributes-charset
1089 * attributes-natural-language
1090 * printer-uri
1091 * requesting-user-name
1092 * option with deleteAttr tag
1093 */
1094
1095 if (get_printer_type(http, printer, uri, sizeof(uri)) & CUPS_PRINTER_CLASS)
1096 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
1097 else
1098 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER);
1099
1100 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1101 "printer-uri", NULL, uri);
1102 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1103 "requesting-user-name", NULL, cupsUser());
1104 ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_DELETEATTR, option, 0);
1105
1106 /*
1107 * Do the request and get back a response...
1108 */
1109
1110 ippDelete(cupsDoRequest(http, request, "/admin/"));
1111
1112 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
1113 {
1114 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1115
1116 return (1);
1117 }
1118 else
1119 return (0);
1120 }
1121
1122
1123 /*
1124 * 'enable_printer()' - Enable a printer...
1125 */
1126
1127 static int /* O - 0 on success, 1 on fail */
1128 enable_printer(http_t *http, /* I - Server connection */
1129 char *printer) /* I - Printer to enable */
1130 {
1131 ipp_t *request; /* IPP Request */
1132 char uri[HTTP_MAX_URI]; /* URI for printer/class */
1133
1134
1135 DEBUG_printf(("enable_printer(%p, \"%s\")\n", http, printer));
1136
1137 /*
1138 * Send IPP_OP_ENABLE_PRINTER and IPP_OP_RESUME_PRINTER requests, which
1139 * require the following attributes:
1140 *
1141 * attributes-charset
1142 * attributes-natural-language
1143 * printer-uri
1144 * requesting-user-name
1145 */
1146
1147 request = ippNewRequest(IPP_OP_ENABLE_PRINTER);
1148
1149 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1150 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
1151
1152 ippDelete(cupsDoRequest(http, request, "/admin/"));
1153
1154 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
1155 {
1156 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1157
1158 return (1);
1159 }
1160
1161 request = ippNewRequest(IPP_OP_RESUME_PRINTER);
1162
1163 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1164 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
1165
1166 ippDelete(cupsDoRequest(http, request, "/admin/"));
1167
1168 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
1169 {
1170 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1171
1172 return (1);
1173 }
1174
1175 return (0);
1176 }
1177
1178
1179 /*
1180 * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
1181 */
1182
1183 static char * /* O - Filename or NULL */
1184 get_printer_ppd(
1185 const char *uri, /* I - Printer URI */
1186 char *buffer, /* I - Filename buffer */
1187 size_t bufsize, /* I - Size of filename buffer */
1188 int *num_options, /* IO - Number of options */
1189 cups_option_t **options) /* IO - Options */
1190 {
1191 http_t *http; /* Connection to printer */
1192 ipp_t *request, /* Get-Printer-Attributes request */
1193 *response; /* Get-Printer-Attributes response */
1194 ipp_attribute_t *attr; /* Attribute from response */
1195 char resolved[1024], /* Resolved URI */
1196 scheme[32], /* URI scheme */
1197 userpass[256], /* Username:password */
1198 host[256], /* Hostname */
1199 resource[256]; /* Resource path */
1200 int port; /* Port number */
1201 static const char * const pattrs[] = /* Attributes to use */
1202 {
1203 "job-template",
1204 "printer-defaults",
1205 "printer-description",
1206 "media-col-database"
1207 };
1208
1209
1210 /*
1211 * Connect to the printer...
1212 */
1213
1214 if (strstr(uri, "._tcp"))
1215 {
1216 /*
1217 * Resolve URI...
1218 */
1219
1220 if (!_httpResolveURI(uri, resolved, sizeof(resolved), _HTTP_RESOLVE_DEFAULT, NULL, NULL))
1221 {
1222 _cupsLangPrintf(stderr, _("%s: Unable to resolve \"%s\"."), "lpadmin", uri);
1223 return (NULL);
1224 }
1225
1226 uri = resolved;
1227 }
1228
1229 if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), host, sizeof(host), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
1230 {
1231 _cupsLangPrintf(stderr, _("%s: Bad printer URI \"%s\"."), "lpadmin", uri);
1232 return (NULL);
1233 }
1234
1235 http = httpConnect2(host, port, NULL, AF_UNSPEC, !strcmp(scheme, "ipps") ? HTTP_ENCRYPTION_ALWAYS : HTTP_ENCRYPTION_IF_REQUESTED, 1, 30000, NULL);
1236 if (!http)
1237 {
1238 _cupsLangPrintf(stderr, _("%s: Unable to connect to \"%s:%d\": %s"), "lpadmin", host, port, cupsLastErrorString());
1239 return (NULL);
1240 }
1241
1242 /*
1243 * Send a Get-Printer-Attributes request...
1244 */
1245
1246 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
1247 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1248 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]), NULL, pattrs);
1249 response = cupsDoRequest(http, request, resource);
1250
1251 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE)
1252 {
1253 _cupsLangPrintf(stderr, _("%s: Unable to query printer: %s"), "lpadmin", cupsLastErrorString());
1254 buffer[0] = '\0';
1255 }
1256 else if (_ppdCreateFromIPP(buffer, bufsize, response))
1257 {
1258 if (!cupsGetOption("printer-geo-location", *num_options, *options) && (attr = ippFindAttribute(response, "printer-geo-location", IPP_TAG_URI)) != NULL)
1259 *num_options = cupsAddOption("printer-geo-location", ippGetString(attr, 0, NULL), *num_options, options);
1260
1261 if (!cupsGetOption("printer-info", *num_options, *options) && (attr = ippFindAttribute(response, "printer-info", IPP_TAG_TEXT)) != NULL)
1262 *num_options = cupsAddOption("printer-info", ippGetString(attr, 0, NULL), *num_options, options);
1263
1264 if (!cupsGetOption("printer-location", *num_options, *options) && (attr = ippFindAttribute(response, "printer-location", IPP_TAG_TEXT)) != NULL)
1265 *num_options = cupsAddOption("printer-location", ippGetString(attr, 0, NULL), *num_options, options);
1266 }
1267 else
1268 _cupsLangPrintf(stderr, _("%s: Unable to create PPD file: %s"), "lpadmin", strerror(errno));
1269
1270 ippDelete(response);
1271 httpClose(http);
1272
1273 if (buffer[0])
1274 return (buffer);
1275 else
1276 return (NULL);
1277 }
1278
1279
1280 /*
1281 * 'get_printer_type()' - Determine the printer type and URI.
1282 */
1283
1284 static cups_ptype_t /* O - printer-type value */
1285 get_printer_type(http_t *http, /* I - Server connection */
1286 char *printer, /* I - Printer name */
1287 char *uri, /* I - URI buffer */
1288 size_t urisize) /* I - Size of URI buffer */
1289 {
1290 ipp_t *request, /* IPP request */
1291 *response; /* IPP response */
1292 ipp_attribute_t *attr; /* printer-type attribute */
1293 cups_ptype_t type; /* printer-type value */
1294
1295
1296 /*
1297 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1298 * attributes:
1299 *
1300 * attributes-charset
1301 * attributes-natural-language
1302 * printer-uri
1303 * requested-attributes
1304 * requesting-user-name
1305 */
1306
1307 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, (int)urisize, "ipp", NULL, "localhost", ippPort(), "/printers/%s", printer);
1308
1309 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
1310 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1311 "printer-uri", NULL, uri);
1312 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1313 "requested-attributes", NULL, "printer-type");
1314 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1315 "requesting-user-name", NULL, cupsUser());
1316
1317 /*
1318 * Do the request...
1319 */
1320
1321 response = cupsDoRequest(http, request, "/");
1322 if ((attr = ippFindAttribute(response, "printer-type",
1323 IPP_TAG_ENUM)) != NULL)
1324 {
1325 type = (cups_ptype_t)attr->values[0].integer;
1326
1327 if (type & CUPS_PRINTER_CLASS)
1328 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, (int)urisize, "ipp", NULL, "localhost", ippPort(), "/classes/%s", printer);
1329 }
1330 else
1331 type = CUPS_PRINTER_LOCAL;
1332
1333 ippDelete(response);
1334
1335 return (type);
1336 }
1337
1338
1339 /*
1340 * 'set_printer_options()' - Set the printer options.
1341 */
1342
1343 static int /* O - 0 on success, 1 on fail */
1344 set_printer_options(
1345 http_t *http, /* I - Server connection */
1346 char *printer, /* I - Printer */
1347 int num_options, /* I - Number of options */
1348 cups_option_t *options, /* I - Options */
1349 char *file, /* I - PPD file */
1350 int enable) /* I - Enable printer? */
1351 {
1352 ipp_t *request; /* IPP Request */
1353 const char *ppdfile; /* PPD filename */
1354 int ppdchanged = 0; /* PPD changed? */
1355 ppd_file_t *ppd; /* PPD file */
1356 ppd_choice_t *choice; /* Marked choice */
1357 char uri[HTTP_MAX_URI], /* URI for printer/class */
1358 line[1024], /* Line from PPD file */
1359 keyword[1024], /* Keyword from Default line */
1360 *keyptr, /* Pointer into keyword... */
1361 tempfile[1024]; /* Temporary filename */
1362 cups_file_t *in, /* PPD file */
1363 *out; /* Temporary file */
1364 const char *ppdname, /* ppd-name value */
1365 *protocol, /* Old protocol option */
1366 *customval, /* Custom option value */
1367 *boolval; /* Boolean value */
1368 int wrote_ipp_supplies = 0, /* Wrote cupsIPPSupplies keyword? */
1369 wrote_snmp_supplies = 0,/* Wrote cupsSNMPSupplies keyword? */
1370 copied_options = 0; /* Copied options? */
1371
1372
1373 DEBUG_printf(("set_printer_options(http=%p, printer=\"%s\", num_options=%d, "
1374 "options=%p, file=\"%s\")\n", http, printer, num_options,
1375 options, file));
1376
1377 /*
1378 * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
1379 * which requires the following attributes:
1380 *
1381 * attributes-charset
1382 * attributes-natural-language
1383 * printer-uri
1384 * requesting-user-name
1385 * other options
1386 */
1387
1388 if (get_printer_type(http, printer, uri, sizeof(uri)) & CUPS_PRINTER_CLASS)
1389 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
1390 else
1391 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER);
1392
1393 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1394 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
1395
1396 if (enable)
1397 {
1398 ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", IPP_PSTATE_IDLE);
1399 ippAddBoolean(request, IPP_TAG_PRINTER, "printer-is-accepting-jobs", 1);
1400 }
1401
1402 /*
1403 * Add the options...
1404 */
1405
1406 if (file)
1407 ppdfile = file;
1408 else if ((ppdname = cupsGetOption("ppd-name", num_options, options)) != NULL && strcmp(ppdname, "raw") && num_options > 1)
1409 {
1410 if ((ppdfile = cupsGetServerPPD(http, ppdname)) != NULL)
1411 {
1412 /*
1413 * Copy options array and remove ppd-name from it...
1414 */
1415
1416 cups_option_t *temp = NULL, *optr;
1417 int i, num_temp = 0;
1418 for (i = num_options, optr = options; i > 0; i --, optr ++)
1419 if (strcmp(optr->name, "ppd-name"))
1420 num_temp = cupsAddOption(optr->name, optr->value, num_temp, &temp);
1421
1422 copied_options = 1;
1423 ppdchanged = 1;
1424 num_options = num_temp;
1425 options = temp;
1426 }
1427 }
1428 else if (request->request.op.operation_id == IPP_OP_CUPS_ADD_MODIFY_PRINTER)
1429 ppdfile = cupsGetPPD(printer);
1430 else
1431 ppdfile = NULL;
1432
1433 cupsEncodeOptions2(request, num_options, options, IPP_TAG_OPERATION);
1434 cupsEncodeOptions2(request, num_options, options, IPP_TAG_PRINTER);
1435
1436 if ((protocol = cupsGetOption("protocol", num_options, options)) != NULL)
1437 {
1438 if (!_cups_strcasecmp(protocol, "bcp"))
1439 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
1440 NULL, "bcp");
1441 else if (!_cups_strcasecmp(protocol, "tbcp"))
1442 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
1443 NULL, "tbcp");
1444 }
1445
1446 if (ppdfile)
1447 {
1448 /*
1449 * Set default options in the PPD file...
1450 */
1451
1452 if ((ppd = ppdOpenFile(ppdfile)) == NULL)
1453 {
1454 int linenum; /* Line number of error */
1455 ppd_status_t status = ppdLastError(&linenum);
1456 /* Status code */
1457
1458 _cupsLangPrintf(stderr, _("lpadmin: Unable to open PPD \"%s\": %s on line %d."), ppdfile, ppdErrorString(status), linenum);
1459 }
1460
1461 ppdMarkDefaults(ppd);
1462 cupsMarkOptions(ppd, num_options, options);
1463
1464 if ((out = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
1465 {
1466 _cupsLangPrintError(NULL, _("lpadmin: Unable to create temporary file"));
1467 ippDelete(request);
1468 if (ppdfile != file)
1469 unlink(ppdfile);
1470 if (copied_options)
1471 cupsFreeOptions(num_options, options);
1472 return (1);
1473 }
1474
1475 if ((in = cupsFileOpen(ppdfile, "r")) == NULL)
1476 {
1477 _cupsLangPrintf(stderr,
1478 _("lpadmin: Unable to open PPD file \"%s\" - %s"),
1479 ppdfile, strerror(errno));
1480 ippDelete(request);
1481 if (ppdfile != file)
1482 unlink(ppdfile);
1483 if (copied_options)
1484 cupsFreeOptions(num_options, options);
1485 cupsFileClose(out);
1486 unlink(tempfile);
1487 return (1);
1488 }
1489
1490 while (cupsFileGets(in, line, sizeof(line)))
1491 {
1492 if (!strncmp(line, "*cupsIPPSupplies:", 17) &&
1493 (boolval = cupsGetOption("cupsIPPSupplies", num_options,
1494 options)) != NULL)
1495 {
1496 wrote_ipp_supplies = 1;
1497 cupsFilePrintf(out, "*cupsIPPSupplies: %s\n",
1498 (!_cups_strcasecmp(boolval, "true") ||
1499 !_cups_strcasecmp(boolval, "yes") ||
1500 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1501 }
1502 else if (!strncmp(line, "*cupsSNMPSupplies:", 18) &&
1503 (boolval = cupsGetOption("cupsSNMPSupplies", num_options,
1504 options)) != NULL)
1505 {
1506 wrote_snmp_supplies = 1;
1507 cupsFilePrintf(out, "*cupsSNMPSupplies: %s\n",
1508 (!_cups_strcasecmp(boolval, "true") ||
1509 !_cups_strcasecmp(boolval, "yes") ||
1510 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1511 }
1512 else if (strncmp(line, "*Default", 8))
1513 cupsFilePrintf(out, "%s\n", line);
1514 else
1515 {
1516 /*
1517 * Get default option name...
1518 */
1519
1520 strlcpy(keyword, line + 8, sizeof(keyword));
1521
1522 for (keyptr = keyword; *keyptr; keyptr ++)
1523 if (*keyptr == ':' || isspace(*keyptr & 255))
1524 break;
1525
1526 *keyptr++ = '\0';
1527 while (isspace(*keyptr & 255))
1528 keyptr ++;
1529
1530 if (!strcmp(keyword, "PageRegion") ||
1531 !strcmp(keyword, "PageSize") ||
1532 !strcmp(keyword, "PaperDimension") ||
1533 !strcmp(keyword, "ImageableArea"))
1534 {
1535 if ((choice = ppdFindMarkedChoice(ppd, "PageSize")) == NULL)
1536 choice = ppdFindMarkedChoice(ppd, "PageRegion");
1537 }
1538 else
1539 choice = ppdFindMarkedChoice(ppd, keyword);
1540
1541 if (choice && strcmp(choice->choice, keyptr))
1542 {
1543 if (strcmp(choice->choice, "Custom"))
1544 {
1545 cupsFilePrintf(out, "*Default%s: %s\n", keyword, choice->choice);
1546 ppdchanged = 1;
1547 }
1548 else if ((customval = cupsGetOption(keyword, num_options,
1549 options)) != NULL)
1550 {
1551 cupsFilePrintf(out, "*Default%s: %s\n", keyword, customval);
1552 ppdchanged = 1;
1553 }
1554 else
1555 cupsFilePrintf(out, "%s\n", line);
1556 }
1557 else
1558 cupsFilePrintf(out, "%s\n", line);
1559 }
1560 }
1561
1562 if (!wrote_ipp_supplies &&
1563 (boolval = cupsGetOption("cupsIPPSupplies", num_options,
1564 options)) != NULL)
1565 {
1566 cupsFilePrintf(out, "*cupsIPPSupplies: %s\n",
1567 (!_cups_strcasecmp(boolval, "true") ||
1568 !_cups_strcasecmp(boolval, "yes") ||
1569 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1570 }
1571
1572 if (!wrote_snmp_supplies &&
1573 (boolval = cupsGetOption("cupsSNMPSupplies", num_options,
1574 options)) != NULL)
1575 {
1576 cupsFilePrintf(out, "*cupsSNMPSupplies: %s\n",
1577 (!_cups_strcasecmp(boolval, "true") ||
1578 !_cups_strcasecmp(boolval, "yes") ||
1579 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1580 }
1581
1582 cupsFileClose(in);
1583 cupsFileClose(out);
1584 ppdClose(ppd);
1585
1586 /*
1587 * Do the request...
1588 */
1589
1590 ippDelete(cupsDoFileRequest(http, request, "/admin/",
1591 ppdchanged ? tempfile : file));
1592
1593 /*
1594 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1595 * lpadmin)
1596 */
1597
1598 if (ppdfile != file)
1599 unlink(ppdfile);
1600 unlink(tempfile);
1601 }
1602 else
1603 {
1604 /*
1605 * No PPD file - just set the options...
1606 */
1607
1608 ippDelete(cupsDoRequest(http, request, "/admin/"));
1609 }
1610
1611 if (copied_options)
1612 cupsFreeOptions(num_options, options);
1613
1614 /*
1615 * Check the response...
1616 */
1617
1618 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
1619 {
1620 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1621
1622 return (1);
1623 }
1624 else
1625 return (0);
1626 }
1627
1628
1629 /*
1630 * 'validate_name()' - Make sure the printer name only contains valid chars.
1631 */
1632
1633 static int /* O - 0 if name is no good, 1 if name is good */
1634 validate_name(const char *name) /* I - Name to check */
1635 {
1636 const char *ptr; /* Pointer into name */
1637
1638
1639 /*
1640 * Scan the whole name...
1641 */
1642
1643 for (ptr = name; *ptr; ptr ++)
1644 if (*ptr == '@')
1645 break;
1646 else if ((*ptr >= 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '\\' || *ptr == '?' || *ptr == '\'' || *ptr == '\"' || *ptr == '#')
1647 return (0);
1648
1649 /*
1650 * All the characters are good; validate the length, too...
1651 */
1652
1653 return ((ptr - name) < 128);
1654 }