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