]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpadmin.c
Fix systemd restart policy (Issue #5297)
[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 * 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);
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 num_options; /* Number of options */
60 cups_option_t *options; /* Options */
61 char *file, /* New PPD file */
62 evefile[1024] = ""; /* IPP Everywhere PPD */
63 const char *ppd_name, /* ppd-name value */
64 *device_uri; /* device-uri value */
65
66
67 _cupsSetLocale(argv);
68
69 http = NULL;
70 printer = NULL;
71 num_options = 0;
72 options = NULL;
73 file = NULL;
74
75 for (i = 1; i < argc; i ++)
76 {
77 if (argv[i][0] == '-')
78 {
79 for (opt = argv[i] + 1; *opt; opt ++)
80 {
81 switch (*opt)
82 {
83 case 'c' : /* Add printer to class */
84 if (!http)
85 {
86 http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
87
88 if (http == NULL)
89 {
90 _cupsLangPrintf(stderr, _("lpadmin: Unable to connect to server: %s"), strerror(errno));
91 return (1);
92 }
93 }
94
95 if (printer == NULL)
96 {
97 _cupsLangPuts(stderr,
98 _("lpadmin: Unable to add a printer to the class:\n"
99 " You must specify a printer name first."));
100 return (1);
101 }
102
103 if (opt[1] != '\0')
104 {
105 pclass = opt + 1;
106 opt += strlen(opt) - 1;
107 }
108 else
109 {
110 i ++;
111
112 if (i >= argc)
113 {
114 _cupsLangPuts(stderr, _("lpadmin: Expected class name after \"-c\" option."));
115 return (1);
116 }
117
118 pclass = argv[i];
119 }
120
121 if (!validate_name(pclass))
122 {
123 _cupsLangPuts(stderr,
124 _("lpadmin: Class name can only contain printable "
125 "characters."));
126 return (1);
127 }
128
129 if (add_printer_to_class(http, printer, pclass))
130 return (1);
131 break;
132
133 case 'd' : /* Set as default destination */
134 if (!http)
135 {
136 http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
137
138 if (http == NULL)
139 {
140 _cupsLangPrintf(stderr, _("lpadmin: Unable to connect to server: %s"), strerror(errno));
141 return (1);
142 }
143 }
144
145 if (opt[1] != '\0')
146 {
147 printer = opt + 1;
148 opt += strlen(opt) - 1;
149 }
150 else
151 {
152 i ++;
153
154 if (i >= argc)
155 {
156 _cupsLangPuts(stderr, _("lpadmin: Expected printer name after \"-d\" option."));
157 return (1);
158 }
159
160 printer = argv[i];
161 }
162
163 if (!validate_name(printer))
164 {
165 _cupsLangPuts(stderr, _("lpadmin: Printer name can only contain printable characters."));
166 return (1);
167 }
168
169 if (default_printer(http, printer))
170 return (1);
171
172 i = argc;
173 break;
174
175 case 'h' : /* Connect to host */
176 if (http)
177 {
178 httpClose(http);
179 http = NULL;
180 }
181
182 if (opt[1] != '\0')
183 {
184 cupsSetServer(opt + 1);
185 opt += strlen(opt) - 1;
186 }
187 else
188 {
189 i ++;
190
191 if (i >= argc)
192 {
193 _cupsLangPuts(stderr, _("lpadmin: Expected hostname after \"-h\" option."));
194 return (1);
195 }
196
197 cupsSetServer(argv[i]);
198 }
199 break;
200
201 case 'P' : /* Use the specified PPD file */
202 case 'i' : /* Use the specified PPD file */
203 if (opt[1] != '\0')
204 {
205 file = opt + 1;
206 opt += strlen(opt) - 1;
207 }
208 else
209 {
210 i ++;
211
212 if (i >= argc)
213 {
214 _cupsLangPrintf(stderr, _("lpadmin: Expected PPD after \"-%c\" option."), argv[i - 1][1]);
215 return (1);
216 }
217
218 file = argv[i];
219 }
220
221 if (*opt == 'i')
222 {
223 /*
224 * Check to see that the specified file is, in fact, a PPD...
225 */
226
227 cups_file_t *fp = cupsFileOpen(file, "r");
228 char line[256];
229
230 if (!cupsFileGets(fp, line, sizeof(line)) || strncmp(line, "*PPD-Adobe", 10))
231 {
232 _cupsLangPuts(stderr, _("lpadmin: System V interface scripts are no longer supported for security reasons."));
233 cupsFileClose(fp);
234 return (1);
235 }
236
237 cupsFileClose(fp);
238 }
239 break;
240
241 case 'E' : /* Enable the printer/enable encryption */
242 if (printer == NULL)
243 {
244 #ifdef HAVE_SSL
245 cupsSetEncryption(HTTP_ENCRYPTION_REQUIRED);
246
247 if (http)
248 httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
249 #else
250 _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."), argv[0]);
251 #endif /* HAVE_SSL */
252 break;
253 }
254
255 if (!http)
256 {
257 http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
258
259 if (http == NULL)
260 {
261 _cupsLangPrintf(stderr,
262 _("lpadmin: Unable to connect to server: %s"),
263 strerror(errno));
264 return (1);
265 }
266 }
267
268 if (enable_printer(http, printer))
269 return (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))
653 return (1);
654 }
655
656 if (evefile[0])
657 unlink(evefile);
658
659 if (printer == NULL)
660 {
661 _cupsLangPuts(stdout,
662 _("Usage:\n"
663 "\n"
664 " lpadmin [-h server] -d destination\n"
665 " lpadmin [-h server] -x destination\n"
666 " lpadmin [-h server] -p printer [-c add-class] "
667 "[-i interface] [-m model]\n"
668 " [-r remove-class] [-v device] "
669 "[-D description]\n"
670 " [-P ppd-file] [-o name=value]\n"
671 " [-u allow:user,user] "
672 "[-u deny:user,user]"));
673 }
674
675 if (http)
676 httpClose(http);
677
678 return (0);
679 }
680
681
682 /*
683 * 'add_printer_to_class()' - Add a printer to a class.
684 */
685
686 static int /* O - 0 on success, 1 on fail */
687 add_printer_to_class(http_t *http, /* I - Server connection */
688 char *printer, /* I - Printer to add */
689 char *pclass) /* I - Class to add to */
690 {
691 int i; /* Looping var */
692 ipp_t *request, /* IPP Request */
693 *response; /* IPP Response */
694 ipp_attribute_t *attr, /* Current attribute */
695 *members; /* Members in class */
696 char uri[HTTP_MAX_URI]; /* URI for printer/class */
697
698
699 DEBUG_printf(("add_printer_to_class(%p, \"%s\", \"%s\")\n", http,
700 printer, pclass));
701
702 /*
703 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
704 * attributes:
705 *
706 * attributes-charset
707 * attributes-natural-language
708 * printer-uri
709 * requesting-user-name
710 */
711
712 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
713
714 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
715 "localhost", 0, "/classes/%s", pclass);
716 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
717 "printer-uri", NULL, uri);
718 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
719 NULL, cupsUser());
720
721 /*
722 * Do the request and get back a response...
723 */
724
725 response = cupsDoRequest(http, request, "/");
726
727 /*
728 * Build a CUPS-Add-Modify-Class request, which requires the following
729 * attributes:
730 *
731 * attributes-charset
732 * attributes-natural-language
733 * printer-uri
734 * requesting-user-name
735 * member-uris
736 */
737
738 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
739
740 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
741 "printer-uri", NULL, uri);
742 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
743 NULL, cupsUser());
744
745 /*
746 * See if the printer is already in the class...
747 */
748
749 if (response != NULL &&
750 (members = ippFindAttribute(response, "member-names",
751 IPP_TAG_NAME)) != NULL)
752 for (i = 0; i < members->num_values; i ++)
753 if (_cups_strcasecmp(printer, members->values[i].string.text) == 0)
754 {
755 _cupsLangPrintf(stderr,
756 _("lpadmin: Printer %s is already a member of class "
757 "%s."), printer, pclass);
758 ippDelete(request);
759 ippDelete(response);
760 return (0);
761 }
762
763 /*
764 * OK, the printer isn't part of the class, so add it...
765 */
766
767 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
768 "localhost", 0, "/printers/%s", printer);
769
770 if (response != NULL &&
771 (members = ippFindAttribute(response, "member-uris",
772 IPP_TAG_URI)) != NULL)
773 {
774 /*
775 * Add the printer to the existing list...
776 */
777
778 attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_URI,
779 "member-uris", members->num_values + 1, NULL, NULL);
780 for (i = 0; i < members->num_values; i ++)
781 attr->values[i].string.text =
782 _cupsStrAlloc(members->values[i].string.text);
783
784 attr->values[i].string.text = _cupsStrAlloc(uri);
785 }
786 else
787 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI, "member-uris", NULL,
788 uri);
789
790 /*
791 * Then send the request...
792 */
793
794 ippDelete(response);
795
796 ippDelete(cupsDoRequest(http, request, "/admin/"));
797 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
798 {
799 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
800
801 return (1);
802 }
803 else
804 return (0);
805 }
806
807
808 /*
809 * 'default_printer()' - Set the default printing destination.
810 */
811
812 static int /* O - 0 on success, 1 on fail */
813 default_printer(http_t *http, /* I - Server connection */
814 char *printer) /* I - Printer name */
815 {
816 ipp_t *request; /* IPP Request */
817 char uri[HTTP_MAX_URI]; /* URI for printer/class */
818
819
820 DEBUG_printf(("default_printer(%p, \"%s\")\n", http, printer));
821
822 /*
823 * Build a CUPS-Set-Default request, which requires the following
824 * attributes:
825 *
826 * attributes-charset
827 * attributes-natural-language
828 * printer-uri
829 * requesting-user-name
830 */
831
832 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
833 "localhost", 0, "/printers/%s", printer);
834
835 request = ippNewRequest(IPP_OP_CUPS_SET_DEFAULT);
836
837 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
838 "printer-uri", NULL, uri);
839 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
840 NULL, cupsUser());
841
842 /*
843 * Do the request and get back a response...
844 */
845
846 ippDelete(cupsDoRequest(http, request, "/admin/"));
847
848 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
849 {
850 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
851
852 return (1);
853 }
854 else
855 return (0);
856 }
857
858
859 /*
860 * 'delete_printer()' - Delete a printer from the system...
861 */
862
863 static int /* O - 0 on success, 1 on fail */
864 delete_printer(http_t *http, /* I - Server connection */
865 char *printer) /* I - Printer to delete */
866 {
867 ipp_t *request; /* IPP Request */
868 char uri[HTTP_MAX_URI]; /* URI for printer/class */
869
870
871 DEBUG_printf(("delete_printer(%p, \"%s\")\n", http, printer));
872
873 /*
874 * Build a CUPS-Delete-Printer request, which requires the following
875 * attributes:
876 *
877 * attributes-charset
878 * attributes-natural-language
879 * printer-uri
880 * requesting-user-name
881 */
882
883 request = ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER);
884
885 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
886 "localhost", 0, "/printers/%s", printer);
887 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
888 "printer-uri", NULL, uri);
889 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
890 NULL, cupsUser());
891
892 /*
893 * Do the request and get back a response...
894 */
895
896 ippDelete(cupsDoRequest(http, request, "/admin/"));
897
898 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
899 {
900 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
901
902 return (1);
903 }
904 else
905 return (0);
906 }
907
908
909 /*
910 * 'delete_printer_from_class()' - Delete a printer from a class.
911 */
912
913 static int /* O - 0 on success, 1 on fail */
914 delete_printer_from_class(
915 http_t *http, /* I - Server connection */
916 char *printer, /* I - Printer to remove */
917 char *pclass) /* I - Class to remove from */
918 {
919 int i, j, k; /* Looping vars */
920 ipp_t *request, /* IPP Request */
921 *response; /* IPP Response */
922 ipp_attribute_t *attr, /* Current attribute */
923 *members; /* Members in class */
924 char uri[HTTP_MAX_URI]; /* URI for printer/class */
925
926
927 DEBUG_printf(("delete_printer_from_class(%p, \"%s\", \"%s\")\n", http,
928 printer, pclass));
929
930 /*
931 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
932 * attributes:
933 *
934 * attributes-charset
935 * attributes-natural-language
936 * printer-uri
937 * requesting-user-name
938 */
939
940 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
941
942 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
943 "localhost", 0, "/classes/%s", pclass);
944 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
945 "printer-uri", NULL, uri);
946 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
947 NULL, cupsUser());
948
949 /*
950 * Do the request and get back a response...
951 */
952
953 if ((response = cupsDoRequest(http, request, "/classes/")) == NULL ||
954 response->request.status.status_code == IPP_STATUS_ERROR_NOT_FOUND)
955 {
956 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
957
958 ippDelete(response);
959
960 return (1);
961 }
962
963 /*
964 * See if the printer is already in the class...
965 */
966
967 if ((members = ippFindAttribute(response, "member-names", IPP_TAG_NAME)) == NULL)
968 {
969 _cupsLangPuts(stderr, _("lpadmin: No member names were seen."));
970
971 ippDelete(response);
972
973 return (1);
974 }
975
976 for (i = 0; i < members->num_values; i ++)
977 if (!_cups_strcasecmp(printer, members->values[i].string.text))
978 break;
979
980 if (i >= members->num_values)
981 {
982 _cupsLangPrintf(stderr,
983 _("lpadmin: Printer %s is not a member of class %s."),
984 printer, pclass);
985
986 ippDelete(response);
987
988 return (1);
989 }
990
991 if (members->num_values == 1)
992 {
993 /*
994 * Build a CUPS-Delete-Class request, which requires the following
995 * attributes:
996 *
997 * attributes-charset
998 * attributes-natural-language
999 * printer-uri
1000 * requesting-user-name
1001 */
1002
1003 request = ippNewRequest(IPP_OP_CUPS_DELETE_CLASS);
1004
1005 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1006 "printer-uri", NULL, uri);
1007 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1008 "requesting-user-name", NULL, cupsUser());
1009 }
1010 else
1011 {
1012 /*
1013 * Build a IPP_OP_CUPS_ADD_MODIFY_CLASS request, which requires the following
1014 * attributes:
1015 *
1016 * attributes-charset
1017 * attributes-natural-language
1018 * printer-uri
1019 * requesting-user-name
1020 * member-uris
1021 */
1022
1023 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
1024
1025 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1026 "printer-uri", NULL, uri);
1027 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1028 "requesting-user-name", NULL, cupsUser());
1029
1030 /*
1031 * Delete the printer from the class...
1032 */
1033
1034 members = ippFindAttribute(response, "member-uris", IPP_TAG_URI);
1035 attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_URI,
1036 "member-uris", members->num_values - 1, NULL, NULL);
1037
1038 for (j = 0, k = 0; j < members->num_values; j ++)
1039 if (j != i)
1040 attr->values[k ++].string.text =
1041 _cupsStrAlloc(members->values[j].string.text);
1042 }
1043
1044 /*
1045 * Then send the request...
1046 */
1047
1048 ippDelete(response);
1049
1050 ippDelete(cupsDoRequest(http, request, "/admin/"));
1051
1052 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
1053 {
1054 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1055
1056 return (1);
1057 }
1058 else
1059 return (0);
1060 }
1061
1062
1063 /*
1064 * 'delete_printer_option()' - Delete a printer option.
1065 */
1066
1067 static int /* O - 0 on success, 1 on fail */
1068 delete_printer_option(http_t *http, /* I - Server connection */
1069 char *printer, /* I - Printer */
1070 char *option) /* I - Option to delete */
1071 {
1072 ipp_t *request; /* IPP request */
1073 char uri[HTTP_MAX_URI]; /* URI for printer/class */
1074
1075
1076 /*
1077 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1078 * requires the following attributes:
1079 *
1080 * attributes-charset
1081 * attributes-natural-language
1082 * printer-uri
1083 * requesting-user-name
1084 * option with deleteAttr tag
1085 */
1086
1087 if (get_printer_type(http, printer, uri, sizeof(uri)) & CUPS_PRINTER_CLASS)
1088 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
1089 else
1090 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER);
1091
1092 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1093 "printer-uri", NULL, uri);
1094 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1095 "requesting-user-name", NULL, cupsUser());
1096 ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_DELETEATTR, option, 0);
1097
1098 /*
1099 * Do the request and get back a response...
1100 */
1101
1102 ippDelete(cupsDoRequest(http, request, "/admin/"));
1103
1104 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
1105 {
1106 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1107
1108 return (1);
1109 }
1110 else
1111 return (0);
1112 }
1113
1114
1115 /*
1116 * 'enable_printer()' - Enable a printer...
1117 */
1118
1119 static int /* O - 0 on success, 1 on fail */
1120 enable_printer(http_t *http, /* I - Server connection */
1121 char *printer) /* I - Printer to enable */
1122 {
1123 ipp_t *request; /* IPP Request */
1124 char uri[HTTP_MAX_URI]; /* URI for printer/class */
1125
1126
1127 DEBUG_printf(("enable_printer(%p, \"%s\")\n", http, printer));
1128
1129 /*
1130 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1131 * require the following attributes:
1132 *
1133 * attributes-charset
1134 * attributes-natural-language
1135 * printer-uri
1136 * requesting-user-name
1137 * printer-state
1138 * printer-is-accepting-jobs
1139 */
1140
1141 if (get_printer_type(http, printer, uri, sizeof(uri)) & CUPS_PRINTER_CLASS)
1142 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
1143 else
1144 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER);
1145
1146 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1147 "printer-uri", NULL, uri);
1148 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1149 "requesting-user-name", NULL, cupsUser());
1150 ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
1151 IPP_PSTATE_IDLE);
1152 ippAddBoolean(request, IPP_TAG_PRINTER, "printer-is-accepting-jobs", 1);
1153
1154 /*
1155 * Do the request and get back a response...
1156 */
1157
1158 ippDelete(cupsDoRequest(http, request, "/admin/"));
1159
1160 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
1161 {
1162 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1163
1164 return (1);
1165 }
1166 else
1167 return (0);
1168 }
1169
1170
1171 /*
1172 * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
1173 */
1174
1175 static char * /* O - Filename or NULL */
1176 get_printer_ppd(
1177 const char *uri, /* I - Printer URI */
1178 char *buffer, /* I - Filename buffer */
1179 size_t bufsize, /* I - Size of filename buffer */
1180 int *num_options, /* IO - Number of options */
1181 cups_option_t **options) /* IO - Options */
1182 {
1183 http_t *http; /* Connection to printer */
1184 ipp_t *request, /* Get-Printer-Attributes request */
1185 *response; /* Get-Printer-Attributes response */
1186 ipp_attribute_t *attr; /* Attribute from response */
1187 char resolved[1024], /* Resolved URI */
1188 scheme[32], /* URI scheme */
1189 userpass[256], /* Username:password */
1190 host[256], /* Hostname */
1191 resource[256]; /* Resource path */
1192 int port; /* Port number */
1193 static const char * const pattrs[] = /* Attributes to use */
1194 {
1195 "job-template",
1196 "printer-defaults",
1197 "printer-description",
1198 "media-col-database"
1199 };
1200
1201
1202 /*
1203 * Connect to the printer...
1204 */
1205
1206 if (strstr(uri, "._tcp"))
1207 {
1208 /*
1209 * Resolve URI...
1210 */
1211
1212 if (!_httpResolveURI(uri, resolved, sizeof(resolved), _HTTP_RESOLVE_DEFAULT, NULL, NULL))
1213 {
1214 _cupsLangPrintf(stderr, _("%s: Unable to resolve \"%s\"."), "lpadmin", uri);
1215 return (NULL);
1216 }
1217
1218 uri = resolved;
1219 }
1220
1221 if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), host, sizeof(host), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
1222 {
1223 _cupsLangPrintf(stderr, _("%s: Bad printer URI \"%s\"."), "lpadmin", uri);
1224 return (NULL);
1225 }
1226
1227 http = httpConnect2(host, port, NULL, AF_UNSPEC, !strcmp(scheme, "ipps") ? HTTP_ENCRYPTION_ALWAYS : HTTP_ENCRYPTION_IF_REQUESTED, 1, 30000, NULL);
1228 if (!http)
1229 {
1230 _cupsLangPrintf(stderr, _("%s: Unable to connect to \"%s:%d\": %s"), "lpadmin", host, port, cupsLastErrorString());
1231 return (NULL);
1232 }
1233
1234 /*
1235 * Send a Get-Printer-Attributes request...
1236 */
1237
1238 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
1239 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1240 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]), NULL, pattrs);
1241 response = cupsDoRequest(http, request, resource);
1242
1243 if (_ppdCreateFromIPP(buffer, bufsize, response))
1244 {
1245 if (!cupsGetOption("printer-geo-location", *num_options, *options) && (attr = ippFindAttribute(response, "printer-geo-location", IPP_TAG_URI)) != NULL)
1246 *num_options = cupsAddOption("printer-geo-location", ippGetString(attr, 0, NULL), *num_options, options);
1247
1248 if (!cupsGetOption("printer-info", *num_options, *options) && (attr = ippFindAttribute(response, "printer-info", IPP_TAG_TEXT)) != NULL)
1249 *num_options = cupsAddOption("printer-info", ippGetString(attr, 0, NULL), *num_options, options);
1250
1251 if (!cupsGetOption("printer-location", *num_options, *options) && (attr = ippFindAttribute(response, "printer-location", IPP_TAG_TEXT)) != NULL)
1252 *num_options = cupsAddOption("printer-location", ippGetString(attr, 0, NULL), *num_options, options);
1253 }
1254 else
1255 _cupsLangPrintf(stderr, _("%s: Unable to create PPD file: %s"), "lpadmin", strerror(errno));
1256
1257 ippDelete(response);
1258 httpClose(http);
1259
1260 if (buffer[0])
1261 return (buffer);
1262 else
1263 return (NULL);
1264 }
1265
1266
1267 /*
1268 * 'get_printer_type()' - Determine the printer type and URI.
1269 */
1270
1271 static cups_ptype_t /* O - printer-type value */
1272 get_printer_type(http_t *http, /* I - Server connection */
1273 char *printer, /* I - Printer name */
1274 char *uri, /* I - URI buffer */
1275 size_t urisize) /* I - Size of URI buffer */
1276 {
1277 ipp_t *request, /* IPP request */
1278 *response; /* IPP response */
1279 ipp_attribute_t *attr; /* printer-type attribute */
1280 cups_ptype_t type; /* printer-type value */
1281
1282
1283 /*
1284 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1285 * attributes:
1286 *
1287 * attributes-charset
1288 * attributes-natural-language
1289 * printer-uri
1290 * requested-attributes
1291 * requesting-user-name
1292 */
1293
1294 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, (int)urisize, "ipp", NULL, "localhost", ippPort(), "/printers/%s", printer);
1295
1296 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
1297 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1298 "printer-uri", NULL, uri);
1299 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1300 "requested-attributes", NULL, "printer-type");
1301 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1302 "requesting-user-name", NULL, cupsUser());
1303
1304 /*
1305 * Do the request...
1306 */
1307
1308 response = cupsDoRequest(http, request, "/");
1309 if ((attr = ippFindAttribute(response, "printer-type",
1310 IPP_TAG_ENUM)) != NULL)
1311 {
1312 type = (cups_ptype_t)attr->values[0].integer;
1313
1314 if (type & CUPS_PRINTER_CLASS)
1315 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, (int)urisize, "ipp", NULL, "localhost", ippPort(), "/classes/%s", printer);
1316 }
1317 else
1318 type = CUPS_PRINTER_LOCAL;
1319
1320 ippDelete(response);
1321
1322 return (type);
1323 }
1324
1325
1326 /*
1327 * 'set_printer_options()' - Set the printer options.
1328 */
1329
1330 static int /* O - 0 on success, 1 on fail */
1331 set_printer_options(
1332 http_t *http, /* I - Server connection */
1333 char *printer, /* I - Printer */
1334 int num_options, /* I - Number of options */
1335 cups_option_t *options, /* I - Options */
1336 char *file) /* I - PPD file/interface script */
1337 {
1338 ipp_t *request; /* IPP Request */
1339 const char *ppdfile; /* PPD filename */
1340 int ppdchanged = 0; /* PPD changed? */
1341 ppd_file_t *ppd; /* PPD file */
1342 ppd_choice_t *choice; /* Marked choice */
1343 char uri[HTTP_MAX_URI], /* URI for printer/class */
1344 line[1024], /* Line from PPD file */
1345 keyword[1024], /* Keyword from Default line */
1346 *keyptr, /* Pointer into keyword... */
1347 tempfile[1024]; /* Temporary filename */
1348 cups_file_t *in, /* PPD file */
1349 *out; /* Temporary file */
1350 const char *ppdname, /* ppd-name value */
1351 *protocol, /* Old protocol option */
1352 *customval, /* Custom option value */
1353 *boolval; /* Boolean value */
1354 int wrote_ipp_supplies = 0, /* Wrote cupsIPPSupplies keyword? */
1355 wrote_snmp_supplies = 0,/* Wrote cupsSNMPSupplies keyword? */
1356 copied_options = 0; /* Copied options? */
1357
1358
1359 DEBUG_printf(("set_printer_options(http=%p, printer=\"%s\", num_options=%d, "
1360 "options=%p, file=\"%s\")\n", http, printer, num_options,
1361 options, file));
1362
1363 /*
1364 * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
1365 * which requires the following attributes:
1366 *
1367 * attributes-charset
1368 * attributes-natural-language
1369 * printer-uri
1370 * requesting-user-name
1371 * other options
1372 */
1373
1374 if (get_printer_type(http, printer, uri, sizeof(uri)) & CUPS_PRINTER_CLASS)
1375 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
1376 else
1377 request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER);
1378
1379 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1380 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
1381
1382 /*
1383 * Add the options...
1384 */
1385
1386 if (file)
1387 ppdfile = file;
1388 else if ((ppdname = cupsGetOption("ppd-name", num_options, options)) != NULL && strcmp(ppdname, "raw") && num_options > 1)
1389 {
1390 if ((ppdfile = cupsGetServerPPD(http, ppdname)) != NULL)
1391 {
1392 /*
1393 * Copy options array and remove ppd-name from it...
1394 */
1395
1396 cups_option_t *temp = NULL, *optr;
1397 int i, num_temp = 0;
1398 for (i = num_options, optr = options; i > 0; i --, optr ++)
1399 if (strcmp(optr->name, "ppd-name"))
1400 num_temp = cupsAddOption(optr->name, optr->value, num_temp, &temp);
1401
1402 copied_options = 1;
1403 ppdchanged = 1;
1404 num_options = num_temp;
1405 options = temp;
1406 }
1407 }
1408 else if (request->request.op.operation_id == IPP_OP_CUPS_ADD_MODIFY_PRINTER)
1409 ppdfile = cupsGetPPD(printer);
1410 else
1411 ppdfile = NULL;
1412
1413 cupsEncodeOptions2(request, num_options, options, IPP_TAG_OPERATION);
1414 cupsEncodeOptions2(request, num_options, options, IPP_TAG_PRINTER);
1415
1416 if ((protocol = cupsGetOption("protocol", num_options, options)) != NULL)
1417 {
1418 if (!_cups_strcasecmp(protocol, "bcp"))
1419 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
1420 NULL, "bcp");
1421 else if (!_cups_strcasecmp(protocol, "tbcp"))
1422 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
1423 NULL, "tbcp");
1424 }
1425
1426 if (ppdfile)
1427 {
1428 /*
1429 * Set default options in the PPD file...
1430 */
1431
1432 if ((ppd = ppdOpenFile(ppdfile)) == NULL)
1433 {
1434 int linenum; /* Line number of error */
1435 ppd_status_t status = ppdLastError(&linenum);
1436 /* Status code */
1437
1438 _cupsLangPrintf(stderr, _("lpadmin: Unable to open PPD \"%s\": %s on line %d."), ppdfile, ppdErrorString(status), linenum);
1439 }
1440
1441 ppdMarkDefaults(ppd);
1442 cupsMarkOptions(ppd, num_options, options);
1443
1444 if ((out = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
1445 {
1446 _cupsLangPrintError(NULL, _("lpadmin: Unable to create temporary file"));
1447 ippDelete(request);
1448 if (ppdfile != file)
1449 unlink(ppdfile);
1450 if (copied_options)
1451 cupsFreeOptions(num_options, options);
1452 return (1);
1453 }
1454
1455 if ((in = cupsFileOpen(ppdfile, "r")) == NULL)
1456 {
1457 _cupsLangPrintf(stderr,
1458 _("lpadmin: Unable to open PPD file \"%s\" - %s"),
1459 ppdfile, strerror(errno));
1460 ippDelete(request);
1461 if (ppdfile != file)
1462 unlink(ppdfile);
1463 if (copied_options)
1464 cupsFreeOptions(num_options, options);
1465 cupsFileClose(out);
1466 unlink(tempfile);
1467 return (1);
1468 }
1469
1470 while (cupsFileGets(in, line, sizeof(line)))
1471 {
1472 if (!strncmp(line, "*cupsIPPSupplies:", 17) &&
1473 (boolval = cupsGetOption("cupsIPPSupplies", num_options,
1474 options)) != NULL)
1475 {
1476 wrote_ipp_supplies = 1;
1477 cupsFilePrintf(out, "*cupsIPPSupplies: %s\n",
1478 (!_cups_strcasecmp(boolval, "true") ||
1479 !_cups_strcasecmp(boolval, "yes") ||
1480 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1481 }
1482 else if (!strncmp(line, "*cupsSNMPSupplies:", 18) &&
1483 (boolval = cupsGetOption("cupsSNMPSupplies", num_options,
1484 options)) != NULL)
1485 {
1486 wrote_snmp_supplies = 1;
1487 cupsFilePrintf(out, "*cupsSNMPSupplies: %s\n",
1488 (!_cups_strcasecmp(boolval, "true") ||
1489 !_cups_strcasecmp(boolval, "yes") ||
1490 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1491 }
1492 else if (strncmp(line, "*Default", 8))
1493 cupsFilePrintf(out, "%s\n", line);
1494 else
1495 {
1496 /*
1497 * Get default option name...
1498 */
1499
1500 strlcpy(keyword, line + 8, sizeof(keyword));
1501
1502 for (keyptr = keyword; *keyptr; keyptr ++)
1503 if (*keyptr == ':' || isspace(*keyptr & 255))
1504 break;
1505
1506 *keyptr++ = '\0';
1507 while (isspace(*keyptr & 255))
1508 keyptr ++;
1509
1510 if (!strcmp(keyword, "PageRegion") ||
1511 !strcmp(keyword, "PageSize") ||
1512 !strcmp(keyword, "PaperDimension") ||
1513 !strcmp(keyword, "ImageableArea"))
1514 {
1515 if ((choice = ppdFindMarkedChoice(ppd, "PageSize")) == NULL)
1516 choice = ppdFindMarkedChoice(ppd, "PageRegion");
1517 }
1518 else
1519 choice = ppdFindMarkedChoice(ppd, keyword);
1520
1521 if (choice && strcmp(choice->choice, keyptr))
1522 {
1523 if (strcmp(choice->choice, "Custom"))
1524 {
1525 cupsFilePrintf(out, "*Default%s: %s\n", keyword, choice->choice);
1526 ppdchanged = 1;
1527 }
1528 else if ((customval = cupsGetOption(keyword, num_options,
1529 options)) != NULL)
1530 {
1531 cupsFilePrintf(out, "*Default%s: %s\n", keyword, customval);
1532 ppdchanged = 1;
1533 }
1534 else
1535 cupsFilePrintf(out, "%s\n", line);
1536 }
1537 else
1538 cupsFilePrintf(out, "%s\n", line);
1539 }
1540 }
1541
1542 if (!wrote_ipp_supplies &&
1543 (boolval = cupsGetOption("cupsIPPSupplies", num_options,
1544 options)) != NULL)
1545 {
1546 cupsFilePrintf(out, "*cupsIPPSupplies: %s\n",
1547 (!_cups_strcasecmp(boolval, "true") ||
1548 !_cups_strcasecmp(boolval, "yes") ||
1549 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1550 }
1551
1552 if (!wrote_snmp_supplies &&
1553 (boolval = cupsGetOption("cupsSNMPSupplies", num_options,
1554 options)) != NULL)
1555 {
1556 cupsFilePrintf(out, "*cupsSNMPSupplies: %s\n",
1557 (!_cups_strcasecmp(boolval, "true") ||
1558 !_cups_strcasecmp(boolval, "yes") ||
1559 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1560 }
1561
1562 cupsFileClose(in);
1563 cupsFileClose(out);
1564 ppdClose(ppd);
1565
1566 /*
1567 * Do the request...
1568 */
1569
1570 ippDelete(cupsDoFileRequest(http, request, "/admin/",
1571 ppdchanged ? tempfile : file));
1572
1573 /*
1574 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1575 * lpadmin)
1576 */
1577
1578 if (ppdfile != file)
1579 unlink(ppdfile);
1580 unlink(tempfile);
1581 }
1582 else
1583 {
1584 /*
1585 * No PPD file - just set the options...
1586 */
1587
1588 ippDelete(cupsDoRequest(http, request, "/admin/"));
1589 }
1590
1591 if (copied_options)
1592 cupsFreeOptions(num_options, options);
1593
1594 /*
1595 * Check the response...
1596 */
1597
1598 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
1599 {
1600 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1601
1602 return (1);
1603 }
1604 else
1605 return (0);
1606 }
1607
1608
1609 /*
1610 * 'validate_name()' - Make sure the printer name only contains valid chars.
1611 */
1612
1613 static int /* O - 0 if name is no good, 1 if name is good */
1614 validate_name(const char *name) /* I - Name to check */
1615 {
1616 const char *ptr; /* Pointer into name */
1617
1618
1619 /*
1620 * Scan the whole name...
1621 */
1622
1623 for (ptr = name; *ptr; ptr ++)
1624 if (*ptr == '@')
1625 break;
1626 else if ((*ptr >= 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '\\' || *ptr == '?' || *ptr == '\'' || *ptr == '\"' || *ptr == '#')
1627 return (0);
1628
1629 /*
1630 * All the characters are good; validate the length, too...
1631 */
1632
1633 return ((ptr - name) < 128);
1634 }