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