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