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