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