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