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