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