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