]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/adminutil.c
Load cups into easysw/current.
[thirdparty/cups.git] / cups / adminutil.c
1 /*
2 * "$Id: adminutil.c 6549 2007-06-04 15:32:58Z mike $"
3 *
4 * Administration utility API definitions for the Common UNIX Printing
5 * System (CUPS).
6 *
7 * Copyright 2001-2007 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Easy Software Products and are protected by Federal
11 * copyright law. Distribution and use rights are outlined in the file
12 * "LICENSE.txt" which should have been included with this file. If this
13 * file is missing or damaged please contact Easy Software Products
14 * at:
15 *
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636 USA
20 *
21 * Voice: (301) 373-9600
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
24 *
25 * This file is subject to the Apple OS-Developed Software exception.
26 *
27 * Contents:
28 *
29 * cupsAdminCreateWindowsPPD() - Create the Windows PPD file for a printer.
30 * cupsAdminExportSamba() - Export a printer to Samba.
31 * cupsAdminGetServerSettings() - Get settings from the server.
32 * _cupsAdminGetServerSettings() - Get settings from the server (private).
33 * cupsAdminSetServerSettings() - Set settings on the server.
34 * _cupsAdminSetServerSettings() - Set settings on the server (private).
35 * do_samba_command() - Do a SAMBA command.
36 * get_cupsd_conf() - Get the current cupsd.conf file.
37 * invalidate_cupsd_cache() - Invalidate the cached cupsd.conf settings.
38 * write_option() - Write a CUPS option to a PPD file.
39 */
40
41 /*
42 * Include necessary headers...
43 */
44
45 #include "adminutil.h"
46 #include "globals.h"
47 #include "debug.h"
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <sys/stat.h>
51 #ifdef WIN32
52 #else
53 # include <unistd.h>
54 # include <sys/wait.h>
55 #endif /* WIN32 */
56
57
58 /*
59 * Local functions...
60 */
61
62 extern int _cupsAdminGetServerSettings(http_t *http,
63 int *num_settings,
64 cups_option_t **settings);
65 extern int _cupsAdminSetServerSettings(http_t *http,
66 int num_settings,
67 cups_option_t *settings);
68 static int do_samba_command(const char *command,
69 const char *address,
70 const char *subcommand,
71 const char *authfile,
72 FILE *logfile);
73 static http_status_t get_cupsd_conf(http_t *http, _cups_globals_t *cg,
74 time_t last_update, char *name,
75 int namelen, int *remote);
76 static void invalidate_cupsd_cache(_cups_globals_t *cg);
77 static void write_option(cups_file_t *dstfp, int order,
78 const char *name, const char *text,
79 const char *attrname,
80 ipp_attribute_t *suppattr,
81 ipp_attribute_t *defattr, int defval,
82 int valcount);
83
84
85 /*
86 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
87 */
88
89 char * /* O - PPD file or NULL */
90 cupsAdminCreateWindowsPPD(
91 http_t *http, /* I - Connection to server */
92 const char *dest, /* I - Printer or class */
93 char *buffer, /* I - Filename buffer */
94 int bufsize) /* I - Size of filename buffer */
95 {
96 const char *src; /* Source PPD filename */
97 cups_file_t *srcfp, /* Source PPD file */
98 *dstfp; /* Destination PPD file */
99 ipp_t *request, /* IPP request */
100 *response; /* IPP response */
101 ipp_attribute_t *suppattr, /* IPP -supported attribute */
102 *defattr; /* IPP -default attribute */
103 cups_lang_t *language; /* Current language */
104 char line[256], /* Line from PPD file */
105 junk[256], /* Extra junk to throw away */
106 *ptr, /* Pointer into line */
107 uri[1024], /* Printer URI */
108 option[41], /* Option */
109 choice[41]; /* Choice */
110 int jcloption, /* In a JCL option? */
111 jclorder, /* Next JCL order dependency */
112 linenum; /* Current line number */
113 time_t curtime; /* Current time */
114 struct tm *curdate; /* Current date */
115 static const char * const pattrs[] = /* Printer attributes we want */
116 {
117 "job-hold-until-supported",
118 "job-hold-until-default",
119 "job-sheets-supported",
120 "job-sheets-default",
121 "job-priority-supported",
122 "job-priority-default"
123 };
124
125
126 /*
127 * Range check the input...
128 */
129
130 if (buffer)
131 *buffer = '\0';
132
133 if (!http || !dest || !buffer || bufsize < 2)
134 return (NULL);
135
136 /*
137 * Get the PPD file...
138 */
139
140 if ((src = cupsGetPPD2(http, dest)) == NULL)
141 return (NULL);
142
143 /*
144 * Get the supported banner pages, etc. for the printer...
145 */
146
147 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
148
149 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
150 "localhost", 0, "/printers/%s", dest);
151 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
152 "printer-uri", NULL, uri);
153
154 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
155 "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
156 NULL, pattrs);
157
158 /*
159 * Do the request and get back a response...
160 */
161
162 response = cupsDoRequest(http, request, "/");
163 if (!response || cupsLastError() > IPP_OK_CONFLICT)
164 {
165 unlink(src);
166 return (NULL);
167 }
168
169 /*
170 * Open the original PPD file...
171 */
172
173 if ((srcfp = cupsFileOpen(src, "rb")) == NULL)
174 return (NULL);
175
176 /*
177 * Create a temporary output file using the destination buffer...
178 */
179
180 if ((dstfp = cupsTempFile2(buffer, bufsize)) == NULL)
181 {
182 cupsFileClose(srcfp);
183
184 unlink(src);
185
186 return (NULL);
187 }
188
189 /*
190 * Write a new header explaining that this isn't the original PPD...
191 */
192
193 cupsFilePuts(dstfp, "*PPD-Adobe: \"4.3\"\n");
194
195 curtime = time(NULL);
196 curdate = gmtime(&curtime);
197
198 cupsFilePrintf(dstfp, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
199 "for CUPS Windows Driver\n",
200 curdate->tm_year + 1900, curdate->tm_mon + 1, curdate->tm_mday,
201 curdate->tm_hour, curdate->tm_min, curdate->tm_sec);
202
203 /*
204 * Read the existing PPD file, converting all PJL commands to CUPS
205 * job ticket comments...
206 */
207
208 jcloption = 0;
209 jclorder = 0;
210 linenum = 0;
211 language = cupsLangDefault();
212
213 while (cupsFileGets(srcfp, line, sizeof(line)))
214 {
215 linenum ++;
216
217 if (!strncmp(line, "*PPD-Adobe:", 11))
218 {
219 /*
220 * Already wrote the PPD header...
221 */
222
223 continue;
224 }
225 else if (!strncmp(line, "*JCLBegin:", 10) ||
226 !strncmp(line, "*JCLToPSInterpreter:", 20) ||
227 !strncmp(line, "*JCLEnd:", 8) ||
228 !strncmp(line, "*Protocols:", 11))
229 {
230 /*
231 * Don't use existing JCL keywords; we'll create our own, below...
232 */
233
234 cupsFilePrintf(dstfp, "*%% Commented out for CUPS Windows Driver...\n"
235 "*%%%s\n", line + 1);
236 continue;
237 }
238 else if (!strncmp(line, "*JCLOpenUI", 10))
239 {
240 jcloption = 1;
241 cupsFilePrintf(dstfp, "%s\n", line);
242 }
243 else if (!strncmp(line, "*JCLCloseUI", 11))
244 {
245 jcloption = 0;
246 cupsFilePrintf(dstfp, "%s\n", line);
247 }
248 else if (jcloption && !strncmp(line, "*OrderDependency:", 17))
249 {
250 for (ptr = line + 17; *ptr && isspace(*ptr & 255); ptr ++);
251
252 ptr = strchr(ptr, ' ');
253
254 if (ptr)
255 {
256 cupsFilePrintf(dstfp, "*OrderDependency: %d%s\n", jclorder, ptr);
257 jclorder ++;
258 }
259 else
260 cupsFilePrintf(dstfp, "%s\n", line);
261 }
262 else if (jcloption &&
263 strncmp(line, "*End", 4) &&
264 strncmp(line, "*Default", 8))
265 {
266 if ((ptr = strchr(line, ':')) == NULL)
267 {
268 snprintf(line, sizeof(line),
269 _cupsLangString(language, _("Missing value on line %d!")),
270 linenum);
271 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
272
273 cupsFileClose(srcfp);
274 cupsFileClose(dstfp);
275
276 unlink(src);
277 unlink(buffer);
278
279 *buffer = '\0';
280
281 return (NULL);
282 }
283
284 if ((ptr = strchr(ptr, '\"')) == NULL)
285 {
286 snprintf(line, sizeof(line),
287 _cupsLangString(language,
288 _("Missing double quote on line %d!")),
289 linenum);
290 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
291
292 cupsFileClose(srcfp);
293 cupsFileClose(dstfp);
294
295 unlink(src);
296 unlink(buffer);
297
298 *buffer = '\0';
299
300 return (NULL);
301 }
302
303 if (sscanf(line, "*%40s%*[ \t]%40[^/]", option, choice) != 2)
304 {
305 snprintf(line, sizeof(line),
306 _cupsLangString(language,
307 _("Bad option + choice on line %d!")),
308 linenum);
309 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
310
311 cupsFileClose(srcfp);
312 cupsFileClose(dstfp);
313
314 unlink(src);
315 unlink(buffer);
316
317 *buffer = '\0';
318
319 return (NULL);
320 }
321
322 if (strchr(ptr + 1, '\"') == NULL)
323 {
324 /*
325 * Skip remaining...
326 */
327
328 while (cupsFileGets(srcfp, junk, sizeof(junk)) != NULL)
329 {
330 linenum ++;
331
332 if (!strncmp(junk, "*End", 4))
333 break;
334 }
335 }
336
337 snprintf(ptr + 1, sizeof(line) - (ptr - line + 1),
338 "%%cupsJobTicket: %s=%s\n\"\n*End", option, choice);
339
340 cupsFilePrintf(dstfp, "*%% Changed for CUPS Windows Driver...\n%s\n",
341 line);
342 }
343 else
344 cupsFilePrintf(dstfp, "%s\n", line);
345 }
346
347 cupsFileClose(srcfp);
348 unlink(src);
349
350 if (linenum == 0)
351 {
352 snprintf(line, sizeof(line),
353 _cupsLangString(language, _("Empty PPD file!")),
354 linenum);
355 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
356
357 cupsFileClose(dstfp);
358 unlink(buffer);
359
360 *buffer = '\0';
361
362 return (NULL);
363 }
364
365 /*
366 * Now add the CUPS-specific attributes and options...
367 */
368
369 cupsFilePuts(dstfp, "\n*% CUPS Job Ticket support and options...\n");
370 cupsFilePuts(dstfp, "*Protocols: PJL\n");
371 cupsFilePuts(dstfp, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
372 cupsFilePuts(dstfp, "*JCLToPSInterpreter: \"\"\n");
373 cupsFilePuts(dstfp, "*JCLEnd: \"\"\n");
374
375 cupsFilePuts(dstfp, "\n*OpenGroup: CUPS/CUPS Options\n\n");
376
377 if ((defattr = ippFindAttribute(response, "job-hold-until-default",
378 IPP_TAG_ZERO)) != NULL &&
379 (suppattr = ippFindAttribute(response, "job-hold-until-supported",
380 IPP_TAG_ZERO)) != NULL)
381 write_option(dstfp, jclorder ++, "cupsJobHoldUntil", "Hold Until",
382 "job-hold-until", suppattr, defattr, 0, 1);
383
384 if ((defattr = ippFindAttribute(response, "job-priority-default",
385 IPP_TAG_INTEGER)) != NULL &&
386 (suppattr = ippFindAttribute(response, "job-priority-supported",
387 IPP_TAG_RANGE)) != NULL)
388 write_option(dstfp, jclorder ++, "cupsJobPriority", "Priority",
389 "job-priority", suppattr, defattr, 0, 1);
390
391 if ((defattr = ippFindAttribute(response, "job-sheets-default",
392 IPP_TAG_ZERO)) != NULL &&
393 (suppattr = ippFindAttribute(response, "job-sheets-supported",
394 IPP_TAG_ZERO)) != NULL)
395 {
396 write_option(dstfp, jclorder ++, "cupsJobSheetsStart", "Start Banner",
397 "job-sheets", suppattr, defattr, 0, 2);
398 write_option(dstfp, jclorder ++, "cupsJobSheetsEnd", "End Banner",
399 "job-sheets", suppattr, defattr, 1, 2);
400 }
401
402 cupsFilePuts(dstfp, "*CloseGroup: CUPS\n");
403 cupsFileClose(dstfp);
404
405 ippDelete(response);
406
407 return (buffer);
408 }
409
410
411 /*
412 * 'cupsAdminExportSamba()' - Export a printer to Samba.
413 */
414
415 int /* O - 1 on success, 0 on failure */
416 cupsAdminExportSamba(
417 const char *dest, /* I - Destination to export */
418 const char *ppd, /* I - PPD file */
419 const char *samba_server, /* I - Samba server */
420 const char *samba_user, /* I - Samba username */
421 const char *samba_password, /* I - Samba password */
422 FILE *logfile) /* I - Log file, if any */
423 {
424 int status; /* Status of Samba commands */
425 int have_drivers; /* Have drivers? */
426 char file[1024], /* File to test for */
427 authfile[1024], /* Temporary authentication file */
428 address[1024], /* Address for command */
429 subcmd[1024], /* Sub-command */
430 message[1024]; /* Error message */
431 cups_file_t *fp; /* Authentication file */
432 cups_lang_t *language; /* Current language */
433 _cups_globals_t *cg = _cupsGlobals();
434 /* Global data */
435
436
437 /*
438 * Range check input...
439 */
440
441 if (!dest || !ppd || !samba_server || !samba_user || !samba_password)
442 {
443 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
444 return (0);
445 }
446
447 /*
448 * Create a temporary authentication file for Samba...
449 */
450
451 if ((fp = cupsTempFile2(authfile, sizeof(authfile))) == NULL)
452 {
453 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
454 return (0);
455 }
456
457 cupsFilePrintf(fp, "username = %s\n", samba_user);
458 cupsFilePrintf(fp, "password = %s\n", samba_password);
459 cupsFileClose(fp);
460
461 /*
462 * See which drivers are available; the new CUPS v6 and Adobe drivers
463 * depend on the Windows 2k PS driver, so copy that driver first:
464 *
465 * Files:
466 *
467 * ps5ui.dll
468 * pscript.hlp
469 * pscript.ntf
470 * pscript5.dll
471 */
472
473 have_drivers = 0;
474 language = cupsLangDefault();
475
476 snprintf(file, sizeof(file), "%s/drivers/pscript5.dll", cg->cups_datadir);
477 if (!access(file, 0))
478 {
479 have_drivers |= 1;
480
481 /*
482 * Windows 2k driver is installed; do the smbclient commands needed
483 * to copy the Win2k drivers over...
484 */
485
486 snprintf(address, sizeof(address), "//%s/print$", samba_server);
487
488 snprintf(subcmd, sizeof(subcmd),
489 "mkdir W32X86;"
490 "put %s W32X86/%s.ppd;"
491 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
492 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
493 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
494 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
495 ppd, dest, cg->cups_datadir, cg->cups_datadir,
496 cg->cups_datadir, cg->cups_datadir);
497
498 if ((status = do_samba_command("smbclient", address, subcmd,
499 authfile, logfile)) != 0)
500 {
501 snprintf(message, sizeof(message),
502 _cupsLangString(language,
503 _("Unable to copy Windows 2000 printer "
504 "driver files (%d)!")), status);
505
506 _cupsSetError(IPP_INTERNAL_ERROR, message);
507
508 if (logfile)
509 _cupsLangPrintf(logfile, "%s\n", message);
510
511 unlink(authfile);
512
513 return (0);
514 }
515
516 /*
517 * See if we also have the CUPS driver files; if so, use them!
518 */
519
520 snprintf(file, sizeof(file), "%s/drivers/cupsps6.dll", cg->cups_datadir);
521 if (!access(file, 0))
522 {
523 /*
524 * Copy the CUPS driver files over...
525 */
526
527 snprintf(subcmd, sizeof(subcmd),
528 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
529 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
530 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
531 cg->cups_datadir, cg->cups_datadir, cg->cups_datadir);
532
533 if ((status = do_samba_command("smbclient", address, subcmd,
534 authfile, logfile)) != 0)
535 {
536 snprintf(message, sizeof(message),
537 _cupsLangString(language,
538 _("Unable to copy CUPS printer driver "
539 "files (%d)!")), status);
540
541 _cupsSetError(IPP_INTERNAL_ERROR, message);
542
543 if (logfile)
544 _cupsLangPrintf(logfile, "%s\n", message);
545
546 unlink(authfile);
547
548 return (0);
549 }
550
551 /*
552 * Do the rpcclient command needed for the CUPS drivers...
553 */
554
555 snprintf(subcmd, sizeof(subcmd),
556 "adddriver \"Windows NT x86\" \"%s:"
557 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
558 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
559 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
560 dest, dest, dest);
561 }
562 else
563 {
564 /*
565 * Don't have the CUPS drivers, so just use the standard Windows
566 * drivers...
567 */
568
569 snprintf(subcmd, sizeof(subcmd),
570 "adddriver \"Windows NT x86\" \"%s:"
571 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
572 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
573 dest, dest, dest);
574 }
575
576 if ((status = do_samba_command("rpcclient", samba_server, subcmd,
577 authfile, logfile)) != 0)
578 {
579 snprintf(message, sizeof(message),
580 _cupsLangString(language,
581 _("Unable to install Windows 2000 printer "
582 "driver files (%d)!")), status);
583
584 _cupsSetError(IPP_INTERNAL_ERROR, message);
585
586 if (logfile)
587 _cupsLangPrintf(logfile, "%s\n", message);
588
589 unlink(authfile);
590
591 return (0);
592 }
593 }
594
595 snprintf(file, sizeof(file), "%s/drivers/ADOBEPS4.DRV", cg->cups_datadir);
596 if (!access(file, 0))
597 {
598 have_drivers |= 2;
599
600 /*
601 * Do the smbclient commands needed for the Adobe Win9x drivers...
602 */
603
604 snprintf(address, sizeof(address), "//%s/print$", samba_server);
605
606 snprintf(subcmd, sizeof(subcmd),
607 "mkdir WIN40;"
608 "put %s WIN40/%s.PPD;"
609 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
610 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
611 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
612 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
613 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
614 ppd, dest, cg->cups_datadir, cg->cups_datadir,
615 cg->cups_datadir, cg->cups_datadir, cg->cups_datadir);
616
617 if ((status = do_samba_command("smbclient", address, subcmd,
618 authfile, logfile)) != 0)
619 {
620 snprintf(message, sizeof(message),
621 _cupsLangString(language,
622 _("Unable to copy Windows 9x printer "
623 "driver files (%d)!")), status);
624
625 _cupsSetError(IPP_INTERNAL_ERROR, message);
626
627 if (logfile)
628 _cupsLangPrintf(logfile, "%s\n", message);
629
630 unlink(authfile);
631
632 return (0);
633 }
634
635 /*
636 * Do the rpcclient commands needed for the Adobe Win9x drivers...
637 */
638
639 snprintf(subcmd, sizeof(subcmd),
640 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
641 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
642 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
643 "ICONLIB.DLL\"",
644 dest, dest, dest);
645
646 if ((status = do_samba_command("rpcclient", samba_server, subcmd,
647 authfile, logfile)) != 0)
648 {
649 snprintf(message, sizeof(message),
650 _cupsLangString(language,
651 _("Unable to install Windows 9x printer "
652 "driver files (%d)!")), status);
653
654 _cupsSetError(IPP_INTERNAL_ERROR, message);
655
656 if (logfile)
657 _cupsLangPrintf(logfile, "%s\n", message);
658
659 unlink(authfile);
660
661 return (0);
662 }
663 }
664
665 if (logfile && !(have_drivers & 1))
666 {
667 if (!have_drivers)
668 strlcpy(message,
669 _cupsLangString(language,
670 _("No Windows printer drivers are installed!")),
671 sizeof(message));
672 else
673 strlcpy(message,
674 _cupsLangString(language,
675 _("Warning, no Windows 2000 printer drivers "
676 "are installed!")),
677 sizeof(message));
678
679 _cupsSetError(IPP_INTERNAL_ERROR, message);
680 _cupsLangPrintf(logfile, "%s\n", message);
681 }
682
683 if (have_drivers == 0)
684 return (0);
685
686 /*
687 * Finally, associate the drivers we just added with the queue...
688 */
689
690 snprintf(subcmd, sizeof(subcmd), "setdriver %s %s", dest, dest);
691
692 if ((status = do_samba_command("rpcclient", samba_server, subcmd,
693 authfile, logfile)) != 0)
694 {
695 snprintf(message, sizeof(message),
696 _cupsLangString(language,
697 _("Unable to set Windows printer driver (%d)!")),
698 status);
699
700 _cupsSetError(IPP_INTERNAL_ERROR, message);
701
702 if (logfile)
703 _cupsLangPrintf(logfile, "%s\n", message);
704
705 unlink(authfile);
706
707 return (0);
708 }
709
710 unlink(authfile);
711
712 return (1);
713 }
714
715
716 /*
717 * 'cupsAdminGetServerSettings()' - Get settings from the server.
718 *
719 * The returned settings should be freed with cupsFreeOptions() when
720 * you are done with them.
721 *
722 * @since CUPS 1.3@
723 */
724
725 int /* O - 1 on success, 0 on failure */
726 cupsAdminGetServerSettings(
727 http_t *http, /* I - Connection to server */
728 int *num_settings, /* O - Number of settings */
729 cups_option_t **settings) /* O - Settings */
730 {
731 return (_cupsAdminGetServerSettings(http, num_settings, settings));
732 }
733
734
735 /*
736 * '_cupsAdminGetServerSettings()' - Get settings from the server.
737 *
738 * The returned settings should be freed with cupsFreeOptions() when
739 * you are done with them.
740 *
741 * @since CUPS 1.2@
742 */
743
744 int /* O - 1 on success, 0 on failure */
745 _cupsAdminGetServerSettings(
746 http_t *http, /* I - Connection to server */
747 int *num_settings, /* O - Number of settings */
748 cups_option_t **settings) /* O - Settings */
749 {
750 int i; /* Looping var */
751 cups_file_t *cupsd; /* cupsd.conf file */
752 char cupsdconf[1024]; /* cupsd.conf filename */
753 int remote; /* Remote cupsd.conf file? */
754 http_status_t status; /* Status of getting cupsd.conf */
755 char line[1024], /* Line from cupsd.conf file */
756 *value; /* Value on line */
757 cups_option_t *setting; /* Current setting */
758 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
759
760
761 /*
762 * Range check input...
763 */
764
765 if (!http || !num_settings || !settings)
766 {
767 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
768
769 if (num_settings)
770 *num_settings = 0;
771
772 if (settings)
773 *settings = NULL;
774
775 return (0);
776 }
777
778 *num_settings = 0;
779 *settings = NULL;
780
781 /*
782 * Get the cupsd.conf file...
783 */
784
785 if ((status = get_cupsd_conf(http, cg, cg->cupsd_update, cupsdconf,
786 sizeof(cupsdconf), &remote)) == HTTP_OK)
787 {
788 if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL)
789 {
790 char message[1024]; /* Message string */
791
792
793 snprintf(message, sizeof(message),
794 _cupsLangString(cupsLangDefault(), _("open of %s failed: %s")),
795 cupsdconf, strerror(errno));
796 _cupsSetError(IPP_INTERNAL_ERROR, message);
797 }
798 }
799 else
800 cupsd = NULL;
801
802 if (cupsd)
803 {
804 /*
805 * Read the file, keeping track of what settings are enabled...
806 */
807
808 int remote_access = 0, /* Remote access allowed? */
809 remote_admin = 0, /* Remote administration allowed? */
810 remote_any = 0, /* Remote access from anywhere allowed? */
811 browsing = 1, /* Browsing enabled? */
812 browse_allow = 1, /* Browse address set? */
813 browse_address = 0, /* Browse address set? */
814 cancel_policy = 1, /* Cancel-job policy set? */
815 debug_logging = 0; /* LogLevel debug set? */
816 int linenum = 0, /* Line number in file */
817 in_location = 0, /* In a location section? */
818 in_policy = 0, /* In a policy section? */
819 in_cancel_job = 0, /* In a cancel-job section? */
820 in_admin_location = 0; /* In the /admin location? */
821
822
823 invalidate_cupsd_cache(cg);
824
825 cg->cupsd_update = time(NULL);
826 httpGetHostname(http, cg->cupsd_hostname, sizeof(cg->cupsd_hostname));
827
828 while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum))
829 {
830 if (!value)
831 continue;
832
833 if (!strcasecmp(line, "Port") || !strcasecmp(line, "Listen"))
834 {
835 char *port; /* Pointer to port number, if any */
836
837
838 if ((port = strrchr(value, ':')) != NULL)
839 *port = '\0';
840 else if (isdigit(*value & 255))
841 {
842 /*
843 * Listen on a port number implies remote access...
844 */
845
846 remote_access = 1;
847 continue;
848 }
849
850 if (strcasecmp(value, "localhost") && strcmp(value, "127.0.0.1")
851 #ifdef AF_LOCAL
852 && *value != '/'
853 #endif /* AF_LOCAL */
854 #ifdef AF_INET6
855 && strcmp(value, "::1")
856 #endif /* AF_INET6 */
857 )
858 remote_access = 1;
859 }
860 else if (!strcasecmp(line, "Browsing"))
861 {
862 browsing = !strcasecmp(value, "yes") || !strcasecmp(value, "on") ||
863 !strcasecmp(value, "true");
864 }
865 else if (!strcasecmp(line, "BrowseAddress"))
866 {
867 browse_address = 1;
868 }
869 else if (!strcasecmp(line, "BrowseAllow"))
870 {
871 browse_allow = 1;
872 }
873 else if (!strcasecmp(line, "BrowseOrder"))
874 {
875 browse_allow = !strncasecmp(value, "deny,", 5);
876 }
877 else if (!strcasecmp(line, "LogLevel"))
878 {
879 debug_logging = !strncasecmp(value, "debug", 5);
880 }
881 else if (!strcasecmp(line, "<Policy") && !strcasecmp(value, "default"))
882 {
883 in_policy = 1;
884 }
885 else if (!strcasecmp(line, "</Policy>"))
886 {
887 in_policy = 0;
888 }
889 else if (!strcasecmp(line, "<Limit") && in_policy)
890 {
891 /*
892 * See if the policy limit is for the Cancel-Job operation...
893 */
894
895 char *valptr; /* Pointer into value */
896
897
898 while (*value)
899 {
900 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
901
902 if (*valptr)
903 *valptr++ = '\0';
904
905 if (!strcasecmp(value, "cancel-job") || !strcasecmp(value, "all"))
906 {
907 in_cancel_job = 1;
908 break;
909 }
910
911 for (value = valptr; isspace(*value & 255); value ++);
912 }
913 }
914 else if (!strcasecmp(line, "</Limit>"))
915 {
916 in_cancel_job = 0;
917 }
918 else if (!strcasecmp(line, "Require") && in_cancel_job)
919 {
920 cancel_policy = 0;
921 }
922 else if (!strcasecmp(line, "<Location"))
923 {
924 in_admin_location = !strcasecmp(value, "/admin");
925 in_location = 1;
926 }
927 else if (!strcasecmp(line, "</Location>"))
928 {
929 in_admin_location = 0;
930 in_location = 0;
931 }
932 else if (!strcasecmp(line, "Allow") && in_admin_location &&
933 strcasecmp(value, "localhost") && strcasecmp(value, "127.0.0.1")
934 #ifdef AF_LOCAL
935 && *value != '/'
936 #endif /* AF_LOCAL */
937 #ifdef AF_INET6
938 && strcmp(value, "::1")
939 #endif /* AF_INET6 */
940 )
941 {
942 remote_admin = 1;
943
944 if (!strcasecmp(value, "all"))
945 remote_any = 1;
946 }
947 else if (line[0] != '<' && !in_location && !in_policy)
948 cg->cupsd_num_settings = cupsAddOption(line, value,
949 cg->cupsd_num_settings,
950 &(cg->cupsd_settings));
951 }
952
953 cupsFileClose(cupsd);
954
955 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
956 debug_logging ? "1" : "0",
957 cg->cupsd_num_settings,
958 &(cg->cupsd_settings));
959
960 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
961 (remote_access && remote_admin) ?
962 "1" : "0",
963 cg->cupsd_num_settings,
964 &(cg->cupsd_settings));
965
966 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY,
967 remote_any ? "1" : "0",
968 cg->cupsd_num_settings,
969 &(cg->cupsd_settings));
970
971 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS,
972 (browsing && browse_allow) ?
973 "1" : "0",
974 cg->cupsd_num_settings,
975 &(cg->cupsd_settings));
976
977 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
978 (remote_access && browsing &&
979 browse_address) ? "1" : "0",
980 cg->cupsd_num_settings,
981 &(cg->cupsd_settings));
982
983 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
984 cancel_policy ? "1" : "0",
985 cg->cupsd_num_settings,
986 &(cg->cupsd_settings));
987 }
988 else if (status != HTTP_NOT_MODIFIED)
989 invalidate_cupsd_cache(cg);
990
991 /*
992 * Remove any temporary files and copy the settings array...
993 */
994
995 if (remote)
996 unlink(cupsdconf);
997
998 for (i = cg->cupsd_num_settings, setting = cg->cupsd_settings;
999 i > 0;
1000 i --, setting ++)
1001 *num_settings = cupsAddOption(setting->name, setting->value,
1002 *num_settings, settings);
1003
1004 return (cg->cupsd_num_settings > 0);
1005 }
1006
1007
1008 /*
1009 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1010 *
1011 * @since CUPS 1.3@
1012 */
1013
1014 int /* O - 1 on success, 0 on failure */
1015 cupsAdminSetServerSettings(
1016 http_t *http, /* I - Connection to server */
1017 int num_settings, /* I - Number of settings */
1018 cups_option_t *settings) /* I - Settings */
1019 {
1020 return (_cupsAdminSetServerSettings(http, num_settings, settings));
1021 }
1022
1023
1024 /*
1025 * '_cupsAdminSetServerSettings()' - Set settings on the server.
1026 *
1027 * @since CUPS 1.2@
1028 */
1029
1030 int /* O - 1 on success, 0 on failure */
1031 _cupsAdminSetServerSettings(
1032 http_t *http, /* I - Connection to server */
1033 int num_settings, /* I - Number of settings */
1034 cups_option_t *settings) /* I - Settings */
1035 {
1036 int i; /* Looping var */
1037 http_status_t status; /* GET/PUT status */
1038 const char *server_port_env; /* SERVER_PORT env var */
1039 int server_port; /* IPP port for server */
1040 cups_file_t *cupsd; /* cupsd.conf file */
1041 char cupsdconf[1024]; /* cupsd.conf filename */
1042 int remote; /* Remote cupsd.conf file? */
1043 char tempfile[1024]; /* Temporary new cupsd.conf */
1044 cups_file_t *temp; /* Temporary file */
1045 char line[1024], /* Line from cupsd.conf file */
1046 *value; /* Value on line */
1047 int linenum, /* Line number in file */
1048 in_location, /* In a location section? */
1049 in_policy, /* In a policy section? */
1050 in_default_policy, /* In the default policy section? */
1051 in_cancel_job, /* In a cancel-job section? */
1052 in_admin_location, /* In the /admin location? */
1053 in_conf_location, /* In the /admin/conf location? */
1054 in_root_location; /* In the / location? */
1055 const char *val; /* Setting value */
1056 int remote_printers, /* Show remote printers */
1057 share_printers, /* Share local printers */
1058 remote_admin, /* Remote administration allowed? */
1059 remote_any, /* Remote access from anywhere? */
1060 user_cancel_any, /* Cancel-job policy set? */
1061 debug_logging; /* LogLevel debug set? */
1062 int wrote_port_listen, /* Wrote the port/listen lines? */
1063 wrote_browsing, /* Wrote the browsing lines? */
1064 wrote_policy, /* Wrote the policy? */
1065 wrote_loglevel, /* Wrote the LogLevel line? */
1066 wrote_admin_location, /* Wrote the /admin location? */
1067 wrote_conf_location, /* Wrote the /admin/conf location? */
1068 wrote_root_location; /* Wrote the / location? */
1069 int indent; /* Indentation */
1070 int cupsd_num_settings; /* New number of settings */
1071 int old_remote_printers, /* Show remote printers */
1072 old_share_printers, /* Share local printers */
1073 old_remote_admin, /* Remote administration allowed? */
1074 old_user_cancel_any, /* Cancel-job policy set? */
1075 old_debug_logging; /* LogLevel debug set? */
1076 cups_option_t *cupsd_settings, /* New settings */
1077 *setting; /* Current setting */
1078 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
1079
1080
1081 /*
1082 * Range check input...
1083 */
1084
1085 if (!http || !num_settings || !settings)
1086 {
1087 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
1088
1089 return (0);
1090 }
1091
1092 /*
1093 * Get the cupsd.conf file...
1094 */
1095
1096 if ((status = get_cupsd_conf(http, cg, 0, cupsdconf, sizeof(cupsdconf),
1097 &remote)) == HTTP_OK)
1098 {
1099 if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL)
1100 {
1101 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
1102 return (0);
1103 }
1104 }
1105 else
1106 return (0);
1107
1108 /*
1109 * Get current settings...
1110 */
1111
1112 if (!_cupsAdminGetServerSettings(http, &cupsd_num_settings,
1113 &cupsd_settings))
1114 return (0);
1115
1116 if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, cupsd_num_settings,
1117 cupsd_settings)) != NULL)
1118 old_debug_logging = atoi(val);
1119 else
1120 old_debug_logging = 0;
1121
1122 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, cupsd_num_settings,
1123 cupsd_settings)) != NULL)
1124 old_remote_admin = atoi(val);
1125 else
1126 old_remote_admin = 0;
1127
1128 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, cupsd_num_settings,
1129 cupsd_settings)) != NULL)
1130 remote_any = atoi(val);
1131 else
1132 remote_any = 0;
1133
1134 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS, cupsd_num_settings,
1135 cupsd_settings)) != NULL)
1136 old_remote_printers = atoi(val);
1137 else
1138 old_remote_printers = 1;
1139
1140 if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, cupsd_num_settings,
1141 cupsd_settings)) != NULL)
1142 old_share_printers = atoi(val);
1143 else
1144 old_share_printers = 0;
1145
1146 if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, cupsd_num_settings,
1147 cupsd_settings)) != NULL)
1148 old_user_cancel_any = atoi(val);
1149 else
1150 old_user_cancel_any = 0;
1151
1152 cupsFreeOptions(cupsd_num_settings, cupsd_settings);
1153
1154 /*
1155 * Get basic settings...
1156 */
1157
1158 if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, num_settings,
1159 settings)) != NULL)
1160 {
1161 debug_logging = atoi(val);
1162
1163 if (debug_logging == old_debug_logging)
1164 {
1165 /*
1166 * No change to this setting...
1167 */
1168
1169 debug_logging = -1;
1170 }
1171 }
1172 else
1173 debug_logging = -1;
1174
1175 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, num_settings,
1176 settings)) != NULL)
1177 remote_any = atoi(val);
1178
1179 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, num_settings,
1180 settings)) != NULL)
1181 {
1182 remote_admin = atoi(val);
1183
1184 if (remote_admin == old_remote_admin && remote_any < 0)
1185 {
1186 /*
1187 * No change to this setting...
1188 */
1189
1190 remote_admin = -1;
1191 }
1192 }
1193 else
1194 remote_admin = -1;
1195
1196 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS, num_settings,
1197 settings)) != NULL)
1198 {
1199 remote_printers = atoi(val);
1200
1201 if (remote_printers == old_remote_printers)
1202 {
1203 /*
1204 * No change to this setting...
1205 */
1206
1207 remote_printers = -1;
1208 }
1209 }
1210 else
1211 remote_printers = -1;
1212
1213 if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, num_settings,
1214 settings)) != NULL)
1215 {
1216 share_printers = atoi(val);
1217
1218 if (share_printers == old_share_printers && remote_any < 0)
1219 {
1220 /*
1221 * No change to this setting...
1222 */
1223
1224 share_printers = -1;
1225 }
1226 }
1227 else
1228 share_printers = -1;
1229
1230 if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, num_settings,
1231 settings)) != NULL)
1232 {
1233 user_cancel_any = atoi(val);
1234
1235 if (user_cancel_any == old_user_cancel_any)
1236 {
1237 /*
1238 * No change to this setting...
1239 */
1240
1241 user_cancel_any = -1;
1242 }
1243 }
1244 else
1245 user_cancel_any = -1;
1246
1247 /*
1248 * Create a temporary file for the new cupsd.conf file...
1249 */
1250
1251 if ((temp = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
1252 {
1253 cupsFileClose(cupsd);
1254
1255 if (remote)
1256 unlink(cupsdconf);
1257
1258 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
1259 return (0);
1260 }
1261
1262 /*
1263 * Copy the old file to the new, making changes along the way...
1264 */
1265
1266 cupsd_num_settings = 0;
1267 in_admin_location = 0;
1268 in_cancel_job = 0;
1269 in_conf_location = 0;
1270 in_default_policy = 0;
1271 in_location = 0;
1272 in_policy = 0;
1273 in_root_location = 0;
1274 linenum = 0;
1275 wrote_admin_location = 0;
1276 wrote_browsing = 0;
1277 wrote_conf_location = 0;
1278 wrote_loglevel = 0;
1279 wrote_policy = 0;
1280 wrote_port_listen = 0;
1281 wrote_root_location = 0;
1282 indent = 0;
1283
1284 if ((server_port_env = getenv("SERVER_PORT")) != NULL)
1285 {
1286 if ((server_port = atoi(server_port_env)) <= 0)
1287 server_port = ippPort();
1288 }
1289 else
1290 server_port = ippPort();
1291
1292 if (server_port <= 0)
1293 server_port = IPP_PORT;
1294
1295 while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum))
1296 {
1297 if ((!strcasecmp(line, "Port") || !strcasecmp(line, "Listen")) &&
1298 (share_printers >= 0 || remote_admin >= 0))
1299 {
1300 if (!wrote_port_listen)
1301 {
1302 wrote_port_listen = 1;
1303
1304 if (share_printers > 0 || remote_admin > 0)
1305 {
1306 cupsFilePuts(temp, "# Allow remote access\n");
1307 cupsFilePrintf(temp, "Port %d\n", server_port);
1308 }
1309 else
1310 {
1311 cupsFilePuts(temp, "# Only listen for connections from the local "
1312 "machine.\n");
1313 cupsFilePrintf(temp, "Listen localhost:%d\n", server_port);
1314 }
1315
1316 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1317 if ((!value || strcmp(CUPS_DEFAULT_DOMAINSOCKET, value)) &&
1318 !access(CUPS_DEFAULT_DOMAINSOCKET, 0))
1319 cupsFilePuts(temp, "Listen " CUPS_DEFAULT_DOMAINSOCKET "\n");
1320 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1321 }
1322 else if (value && value[0] == '/'
1323 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1324 && strcmp(CUPS_DEFAULT_DOMAINSOCKET, value)
1325 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1326 )
1327 cupsFilePrintf(temp, "Listen %s\n", value);
1328 }
1329 else if ((!strcasecmp(line, "Browsing") ||
1330 !strcasecmp(line, "BrowseAddress") ||
1331 !strcasecmp(line, "BrowseAllow") ||
1332 !strcasecmp(line, "BrowseDeny") ||
1333 !strcasecmp(line, "BrowseOrder")) &&
1334 (remote_printers >= 0 || share_printers >= 0))
1335 {
1336 if (!wrote_browsing)
1337 {
1338 int new_remote_printers = (remote_printers > 0 ||
1339 (remote_printers == -1 &&
1340 old_remote_printers > 0));
1341 int new_share_printers = (share_printers > 0 ||
1342 (share_printers == -1 &&
1343 old_share_printers > 0));
1344
1345 wrote_browsing = 1;
1346
1347 if (new_remote_printers || new_share_printers)
1348 {
1349 if (new_remote_printers && new_share_printers)
1350 cupsFilePuts(temp,
1351 "# Enable printer sharing and shared printers.\n");
1352 else if (new_remote_printers)
1353 cupsFilePuts(temp,
1354 "# Show shared printers on the local network.\n");
1355 else
1356 cupsFilePuts(temp,
1357 "# Share local printers on the local network.\n");
1358
1359 cupsFilePuts(temp, "Browsing On\n");
1360 cupsFilePuts(temp, "BrowseOrder allow,deny\n");
1361
1362 if (new_remote_printers)
1363 cupsFilePuts(temp, "BrowseAllow all\n");
1364
1365 if (new_share_printers)
1366 cupsFilePuts(temp, "BrowseAddress @LOCAL\n");
1367 }
1368 else
1369 {
1370 cupsFilePuts(temp,
1371 "# Disable printer sharing and shared printers.\n");
1372 cupsFilePuts(temp, "Browsing Off\n");
1373 }
1374 }
1375 }
1376 else if (!strcasecmp(line, "LogLevel") && debug_logging >= 0)
1377 {
1378 wrote_loglevel = 1;
1379
1380 if (debug_logging)
1381 {
1382 cupsFilePuts(temp,
1383 "# Show troubleshooting information in error_log.\n");
1384 cupsFilePuts(temp, "LogLevel debug\n");
1385 }
1386 else
1387 {
1388 cupsFilePuts(temp, "# Show general information in error_log.\n");
1389 cupsFilePuts(temp, "LogLevel info\n");
1390 }
1391 }
1392 else if (!strcasecmp(line, "<Policy"))
1393 {
1394 in_default_policy = !strcasecmp(value, "default");
1395 in_policy = 1;
1396
1397 cupsFilePrintf(temp, "%s %s>\n", line, value);
1398 indent += 2;
1399 }
1400 else if (!strcasecmp(line, "</Policy>"))
1401 {
1402 indent -= 2;
1403 if (!wrote_policy && in_default_policy)
1404 {
1405 wrote_policy = 1;
1406
1407 if (!user_cancel_any)
1408 cupsFilePuts(temp, " # Only the owner or an administrator can "
1409 "cancel a job...\n"
1410 " <Limit Cancel-Job>\n"
1411 " Order deny,allow\n"
1412 " Allow @SYSTEM\n"
1413 " Allow @OWNER\n"
1414 " </Limit>\n");
1415 }
1416
1417 in_policy = 0;
1418 in_default_policy = 0;
1419
1420 cupsFilePuts(temp, "</Policy>\n");
1421 }
1422 else if (!strcasecmp(line, "<Location"))
1423 {
1424 in_location = 1;
1425 indent += 2;
1426 if (!strcmp(value, "/admin"))
1427 in_admin_location = 1;
1428 if (!strcmp(value, "/admin/conf"))
1429 in_conf_location = 1;
1430 else if (!strcmp(value, "/"))
1431 in_root_location = 1;
1432
1433 cupsFilePrintf(temp, "%s %s>\n", line, value);
1434 }
1435 else if (!strcasecmp(line, "</Location>"))
1436 {
1437 in_location = 0;
1438 indent -= 2;
1439 if (in_admin_location && remote_admin >= 0)
1440 {
1441 wrote_admin_location = 1;
1442
1443 if (remote_admin)
1444 cupsFilePuts(temp, " # Allow remote administration...\n");
1445 else if (remote_admin == 0)
1446 cupsFilePuts(temp, " # Restrict access to the admin pages...\n");
1447
1448 cupsFilePuts(temp, " Order allow,deny\n");
1449
1450 if (remote_admin)
1451 cupsFilePrintf(temp, " Allow %s\n",
1452 remote_any > 0 ? "all" : "@LOCAL");
1453 else
1454 cupsFilePuts(temp, " Allow localhost\n");
1455 }
1456 else if (in_conf_location && remote_admin >= 0)
1457 {
1458 wrote_conf_location = 1;
1459
1460 if (remote_admin)
1461 cupsFilePuts(temp, " # Allow remote access to the configuration "
1462 "files...\n");
1463 else
1464 cupsFilePuts(temp, " # Restrict access to the configuration "
1465 "files...\n");
1466
1467 cupsFilePuts(temp, " Order allow,deny\n");
1468
1469 if (remote_admin)
1470 cupsFilePrintf(temp, " Allow %s\n",
1471 remote_any > 0 ? "all" : "@LOCAL");
1472 else
1473 cupsFilePuts(temp, " Allow localhost\n");
1474 }
1475 else if (in_root_location && (remote_admin >= 0 || share_printers >= 0))
1476 {
1477 wrote_root_location = 1;
1478
1479 if (remote_admin > 0 && share_printers > 0)
1480 cupsFilePuts(temp, " # Allow shared printing and remote "
1481 "administration...\n");
1482 else if (remote_admin > 0)
1483 cupsFilePuts(temp, " # Allow remote administration...\n");
1484 else if (share_printers > 0)
1485 cupsFilePuts(temp, " # Allow shared printing...\n");
1486 else
1487 cupsFilePuts(temp, " # Restrict access to the server...\n");
1488
1489 cupsFilePuts(temp, " Order allow,deny\n");
1490
1491 if (remote_admin > 0 || share_printers > 0)
1492 cupsFilePrintf(temp, " Allow %s\n",
1493 remote_any > 0 ? "all" : "@LOCAL");
1494 else
1495 cupsFilePuts(temp, " Allow localhost\n");
1496 }
1497
1498 in_admin_location = 0;
1499 in_conf_location = 0;
1500 in_root_location = 0;
1501
1502 cupsFilePuts(temp, "</Location>\n");
1503 }
1504 else if (!strcasecmp(line, "<Limit") && in_default_policy)
1505 {
1506 /*
1507 * See if the policy limit is for the Cancel-Job operation...
1508 */
1509
1510 char *valptr; /* Pointer into value */
1511
1512
1513 indent += 2;
1514
1515 if (!strcasecmp(value, "cancel-job") && user_cancel_any >= 0)
1516 {
1517 /*
1518 * Don't write anything for this limit section...
1519 */
1520
1521 in_cancel_job = 2;
1522 }
1523 else
1524 {
1525 cupsFilePrintf(temp, " %s", line);
1526
1527 while (*value)
1528 {
1529 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
1530
1531 if (*valptr)
1532 *valptr++ = '\0';
1533
1534 if (!strcasecmp(value, "cancel-job") && user_cancel_any >= 0)
1535 {
1536 /*
1537 * Write everything except for this definition...
1538 */
1539
1540 in_cancel_job = 1;
1541 }
1542 else
1543 cupsFilePrintf(temp, " %s", value);
1544
1545 for (value = valptr; isspace(*value & 255); value ++);
1546 }
1547
1548 cupsFilePuts(temp, ">\n");
1549 }
1550 }
1551 else if (!strcasecmp(line, "</Limit>") && in_cancel_job)
1552 {
1553 indent -= 2;
1554
1555 if (in_cancel_job == 1)
1556 cupsFilePuts(temp, " </Limit>\n");
1557
1558 wrote_policy = 1;
1559
1560 if (!user_cancel_any)
1561 cupsFilePuts(temp, " # Only the owner or an administrator can cancel "
1562 "a job...\n"
1563 " <Limit Cancel-Job>\n"
1564 " Order deny,allow\n"
1565 " Require user @OWNER @SYSTEM\n"
1566 " </Limit>\n");
1567
1568 in_cancel_job = 0;
1569 }
1570 else if ((((in_admin_location || in_conf_location || in_root_location) &&
1571 remote_admin >= 0) ||
1572 (in_root_location && share_printers >= 0)) &&
1573 (!strcasecmp(line, "Allow") || !strcasecmp(line, "Deny") ||
1574 !strcasecmp(line, "Order")))
1575 continue;
1576 else if (in_cancel_job == 2)
1577 continue;
1578 else if (!strcasecmp(line, "<Limit") && value)
1579 cupsFilePrintf(temp, " %s %s>\n", line, value);
1580 else if (line[0] == '<')
1581 {
1582 if (value)
1583 {
1584 cupsFilePrintf(temp, "%*s%s %s>\n", indent, "", line, value);
1585 indent += 2;
1586 }
1587 else
1588 {
1589 if (line[1] == '/')
1590 indent -= 2;
1591
1592 cupsFilePrintf(temp, "%*s%s\n", indent, "", line);
1593 }
1594 }
1595 else if (!in_policy && !in_location &&
1596 (val = cupsGetOption(line, num_settings, settings)) != NULL)
1597 {
1598 /*
1599 * Replace this directive's value with the new one...
1600 */
1601
1602 cupsd_num_settings = cupsAddOption(line, val, cupsd_num_settings,
1603 &cupsd_settings);
1604
1605 /*
1606 * Write the new value in its place, without indentation since we
1607 * only support setting root directives, not in sections...
1608 */
1609
1610 cupsFilePrintf(temp, "%s %s\n", line, val);
1611 }
1612 else if (value)
1613 {
1614 if (!in_policy && !in_location)
1615 {
1616 /*
1617 * Record the non-policy, non-location directives that we find
1618 * in the server settings, since we cache this info and record it
1619 * in _cupsAdminGetServerSettings()...
1620 */
1621
1622 cupsd_num_settings = cupsAddOption(line, value, cupsd_num_settings,
1623 &cupsd_settings);
1624 }
1625
1626 cupsFilePrintf(temp, "%*s%s %s\n", indent, "", line, value);
1627 }
1628 else
1629 cupsFilePrintf(temp, "%*s%s\n", indent, "", line);
1630 }
1631
1632 /*
1633 * Write any missing info...
1634 */
1635
1636 if (!wrote_browsing && (remote_printers >= 0 || share_printers >= 0))
1637 {
1638 if (remote_printers > 0 || share_printers > 0)
1639 {
1640 if (remote_printers > 0 && share_printers > 0)
1641 cupsFilePuts(temp, "# Enable printer sharing and shared printers.\n");
1642 else if (remote_printers > 0)
1643 cupsFilePuts(temp, "# Show shared printers on the local network.\n");
1644 else
1645 cupsFilePuts(temp, "# Share local printers on the local network.\n");
1646
1647 cupsFilePuts(temp, "Browsing On\n");
1648 cupsFilePuts(temp, "BrowseOrder allow,deny\n");
1649
1650 if (remote_printers > 0)
1651 cupsFilePuts(temp, "BrowseAllow all\n");
1652
1653 if (share_printers > 0)
1654 cupsFilePuts(temp, "BrowseAddress @LOCAL\n");
1655 }
1656 else
1657 {
1658 cupsFilePuts(temp, "# Disable printer sharing and shared printers.\n");
1659 cupsFilePuts(temp, "Browsing Off\n");
1660 }
1661 }
1662
1663 if (!wrote_loglevel && debug_logging >= 0)
1664 {
1665 if (debug_logging)
1666 {
1667 cupsFilePuts(temp, "# Show troubleshooting information in error_log.\n");
1668 cupsFilePuts(temp, "LogLevel debug\n");
1669 }
1670 else
1671 {
1672 cupsFilePuts(temp, "# Show general information in error_log.\n");
1673 cupsFilePuts(temp, "LogLevel info\n");
1674 }
1675 }
1676
1677 if (!wrote_port_listen && (share_printers >= 0 || remote_admin >= 0))
1678 {
1679 if (share_printers > 0 || remote_admin > 0)
1680 {
1681 cupsFilePuts(temp, "# Allow remote access\n");
1682 cupsFilePrintf(temp, "Port %d\n", ippPort());
1683 }
1684 else
1685 {
1686 cupsFilePuts(temp,
1687 "# Only listen for connections from the local machine.\n");
1688 cupsFilePrintf(temp, "Listen localhost:%d\n", ippPort());
1689 }
1690
1691 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1692 if (!access(CUPS_DEFAULT_DOMAINSOCKET, 0))
1693 cupsFilePuts(temp, "Listen " CUPS_DEFAULT_DOMAINSOCKET "\n");
1694 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1695 }
1696
1697 if (!wrote_root_location && (remote_admin >= 0 || share_printers >= 0))
1698 {
1699 if (remote_admin > 0 && share_printers > 0)
1700 cupsFilePuts(temp,
1701 "# Allow shared printing and remote administration...\n");
1702 else if (remote_admin > 0)
1703 cupsFilePuts(temp, "# Allow remote administration...\n");
1704 else if (share_printers > 0)
1705 cupsFilePuts(temp, "# Allow shared printing...\n");
1706 else
1707 cupsFilePuts(temp, "# Restrict access to the server...\n");
1708
1709 cupsFilePuts(temp, "<Location />\n"
1710 " Order allow,deny\n");
1711
1712 if (remote_admin > 0 || share_printers > 0)
1713 cupsFilePrintf(temp, " Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
1714 else
1715 cupsFilePuts(temp, " Allow localhost\n");
1716
1717 cupsFilePuts(temp, "</Location>\n");
1718 }
1719
1720 if (!wrote_admin_location && remote_admin >= 0)
1721 {
1722 if (remote_admin)
1723 cupsFilePuts(temp, "# Allow remote administration...\n");
1724 else
1725 cupsFilePuts(temp, "# Restrict access to the admin pages...\n");
1726
1727 cupsFilePuts(temp, "<Location /admin>\n"
1728 " Order allow,deny\n");
1729
1730 if (remote_admin)
1731 cupsFilePrintf(temp, " Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
1732 else
1733 cupsFilePuts(temp, " Allow localhost\n");
1734
1735 cupsFilePuts(temp, "</Location>\n");
1736 }
1737
1738 if (!wrote_conf_location && remote_admin >= 0)
1739 {
1740 if (remote_admin)
1741 cupsFilePuts(temp,
1742 "# Allow remote access to the configuration files...\n");
1743 else
1744 cupsFilePuts(temp, "# Restrict access to the configuration files...\n");
1745
1746 cupsFilePuts(temp, "<Location /admin/conf>\n"
1747 " AuthType Basic\n"
1748 " Require user @SYSTEM\n"
1749 " Order allow,deny\n");
1750
1751 if (remote_admin)
1752 cupsFilePrintf(temp, " Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
1753 else
1754 cupsFilePuts(temp, " Allow localhost\n");
1755
1756 cupsFilePuts(temp, "</Location>\n");
1757 }
1758
1759 if (!wrote_policy && user_cancel_any >= 0)
1760 {
1761 cupsFilePuts(temp, "<Policy default>\n"
1762 " # Job-related operations must be done by the owner "
1763 "or an adminstrator...\n"
1764 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1765 "Restart-Job Purge-Jobs Set-Job-Attributes "
1766 "Create-Job-Subscription Renew-Subscription "
1767 "Cancel-Subscription Get-Notifications Reprocess-Job "
1768 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1769 "CUPS-Move-Job>\n"
1770 " Require user @OWNER @SYSTEM\n"
1771 " Order deny,allow\n"
1772 " </Limit>\n"
1773 " # All administration operations require an "
1774 "adminstrator to authenticate...\n"
1775 " <Limit Pause-Printer Resume-Printer "
1776 "Set-Printer-Attributes Enable-Printer "
1777 "Disable-Printer Pause-Printer-After-Current-Job "
1778 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1779 "Activate-Printer Restart-Printer Shutdown-Printer "
1780 "Startup-Printer Promote-Job Schedule-Job-After "
1781 "CUPS-Add-Printer CUPS-Delete-Printer "
1782 "CUPS-Add-Class CUPS-Delete-Class "
1783 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1784 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1785 " AuthType Basic\n"
1786 " Require user @SYSTEM\n"
1787 " Order deny,allow\n"
1788 "</Limit>\n");
1789
1790 if (!user_cancel_any)
1791 cupsFilePuts(temp, " # Only the owner or an administrator can cancel "
1792 "a job...\n"
1793 " <Limit Cancel-Job>\n"
1794 " Require user @OWNER @SYSTEM\n"
1795 " Order deny,allow\n"
1796 " </Limit>\n");
1797
1798 cupsFilePuts(temp, " <Limit All>\n"
1799 " Order deny,allow\n"
1800 " </Limit>\n"
1801 "</Policy>\n");
1802 }
1803
1804 for (i = num_settings, setting = settings; i > 0; i --, setting ++)
1805 if (setting->name[0] != '_' &&
1806 !cupsGetOption(setting->name, cupsd_num_settings, cupsd_settings))
1807 {
1808 /*
1809 * Add this directive to the list of directives we have written...
1810 */
1811
1812 cupsd_num_settings = cupsAddOption(setting->name, setting->value,
1813 cupsd_num_settings, &cupsd_settings);
1814
1815 /*
1816 * Write the new value, without indentation since we only support
1817 * setting root directives, not in sections...
1818 */
1819
1820 cupsFilePrintf(temp, "%s %s\n", setting->name, setting->value);
1821 }
1822
1823 cupsFileClose(cupsd);
1824 cupsFileClose(temp);
1825
1826 /*
1827 * Upload the configuration file to the server...
1828 */
1829
1830 status = cupsPutFile(http, "/admin/conf/cupsd.conf", tempfile);
1831
1832 if (status == HTTP_CREATED)
1833 {
1834 /*
1835 * Updated OK, add the basic settings...
1836 */
1837
1838 if (debug_logging >= 0)
1839 cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
1840 debug_logging ? "1" : "0",
1841 cupsd_num_settings, &cupsd_settings);
1842 else
1843 cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
1844 old_debug_logging ? "1" : "0",
1845 cupsd_num_settings, &cupsd_settings);
1846
1847 if (remote_admin >= 0)
1848 cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
1849 remote_admin ? "1" : "0",
1850 cupsd_num_settings, &cupsd_settings);
1851 else
1852 cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
1853 old_remote_admin ? "1" : "0",
1854 cupsd_num_settings, &cupsd_settings);
1855
1856 cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY,
1857 remote_any ? "1" : "0",
1858 cupsd_num_settings, &cupsd_settings);
1859
1860 if (remote_printers >= 0)
1861 cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS,
1862 remote_printers ? "1" : "0",
1863 cupsd_num_settings, &cupsd_settings);
1864 else
1865 cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS,
1866 old_remote_printers ? "1" : "0",
1867 cupsd_num_settings, &cupsd_settings);
1868
1869 if (share_printers >= 0)
1870 cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
1871 share_printers ? "1" : "0",
1872 cupsd_num_settings, &cupsd_settings);
1873 else
1874 cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
1875 old_share_printers ? "1" : "0",
1876 cupsd_num_settings, &cupsd_settings);
1877
1878 if (user_cancel_any >= 0)
1879 cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
1880 user_cancel_any ? "1" : "0",
1881 cupsd_num_settings, &cupsd_settings);
1882 else
1883 cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
1884 old_user_cancel_any ? "1" : "0",
1885 cupsd_num_settings, &cupsd_settings);
1886
1887 /*
1888 * Save the new values...
1889 */
1890
1891 invalidate_cupsd_cache(cg);
1892
1893 cg->cupsd_num_settings = cupsd_num_settings;
1894 cg->cupsd_settings = cupsd_settings;
1895 cg->cupsd_update = time(NULL);
1896
1897 httpGetHostname(http, cg->cupsd_hostname, sizeof(cg->cupsd_hostname));
1898 }
1899 else
1900 cupsFreeOptions(cupsd_num_settings, cupsd_settings);
1901
1902 /*
1903 * Remote our temp files and return...
1904 */
1905
1906 if (remote)
1907 unlink(cupsdconf);
1908
1909 unlink(tempfile);
1910
1911 return (status == HTTP_CREATED);
1912 }
1913
1914
1915 /*
1916 * 'do_samba_command()' - Do a SAMBA command.
1917 */
1918
1919 static int /* O - Status of command */
1920 do_samba_command(const char *command, /* I - Command to run */
1921 const char *address, /* I - Address for command */
1922 const char *subcmd, /* I - Sub-command */
1923 const char *authfile, /* I - Samba authentication file */
1924 FILE *logfile) /* I - Optional log file */
1925 {
1926 #ifdef WIN32
1927 return (1); /* Always fail on Windows... */
1928
1929 #else
1930 int status; /* Status of command */
1931 int pid; /* Process ID of child */
1932
1933
1934 if (logfile)
1935 _cupsLangPrintf(logfile,
1936 _("Running command: %s %s -N -A %s -c \'%s\'\n"),
1937 command, address, authfile, subcmd);
1938
1939 if ((pid = fork()) == 0)
1940 {
1941 /*
1942 * Child goes here, redirect stdin/out/err and execute the command...
1943 */
1944
1945 close(0);
1946 open("/dev/null", O_RDONLY);
1947
1948 close(1);
1949
1950 if (logfile)
1951 dup(fileno(logfile));
1952 else
1953 open("/dev/null", O_WRONLY);
1954
1955 close(2);
1956 dup(1);
1957
1958 execlp(command, command, address, "-N", "-A", authfile, "-c", subcmd,
1959 (char *)0);
1960 exit(errno);
1961 }
1962 else if (pid < 0)
1963 {
1964 status = -1;
1965
1966 if (logfile)
1967 _cupsLangPrintf(logfile, _("Unable to run \"%s\": %s\n"),
1968 command, strerror(errno));
1969 }
1970 else
1971 {
1972 /*
1973 * Wait for the process to complete...
1974 */
1975
1976 while (wait(&status) != pid);
1977 }
1978
1979 if (logfile)
1980 _cupsLangPuts(logfile, "\n");
1981
1982 DEBUG_printf(("status=%d\n", status));
1983
1984 if (WIFEXITED(status))
1985 return (WEXITSTATUS(status));
1986 else
1987 return (-WTERMSIG(status));
1988 #endif /* WIN32 */
1989 }
1990
1991
1992 /*
1993 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
1994 */
1995
1996 static http_status_t /* O - Status of request */
1997 get_cupsd_conf(
1998 http_t *http, /* I - Connection to server */
1999 _cups_globals_t *cg, /* I - Global data */
2000 time_t last_update, /* I - Last update time for file */
2001 char *name, /* I - Filename buffer */
2002 int namesize, /* I - Size of filename buffer */
2003 int *remote) /* O - Remote file? */
2004 {
2005 int fd; /* Temporary file descriptor */
2006 #ifndef WIN32
2007 struct stat info; /* cupsd.conf file information */
2008 #endif /* WIN32 */
2009 http_status_t status; /* Status of getting cupsd.conf */
2010 char host[HTTP_MAX_HOST]; /* Hostname for connection */
2011
2012
2013 /*
2014 * See if we already have the data we need...
2015 */
2016
2017 httpGetHostname(http, host, sizeof(host));
2018
2019 if (strcasecmp(cg->cupsd_hostname, host))
2020 invalidate_cupsd_cache(cg);
2021
2022 snprintf(name, namesize, "%s/cupsd.conf", cg->cups_serverroot);
2023 *remote = 0;
2024
2025 #ifndef WIN32
2026 if (!strcasecmp(host, "localhost") && !access(name, R_OK))
2027 {
2028 /*
2029 * Read the local file rather than using HTTP...
2030 */
2031
2032 if (stat(name, &info))
2033 {
2034 char message[1024]; /* Message string */
2035
2036
2037 snprintf(message, sizeof(message),
2038 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2039 name, strerror(errno));
2040 _cupsSetError(IPP_INTERNAL_ERROR, message);
2041
2042 *name = '\0';
2043
2044 return (HTTP_SERVER_ERROR);
2045 }
2046 else if (last_update && info.st_mtime <= last_update)
2047 status = HTTP_NOT_MODIFIED;
2048 else
2049 status = HTTP_OK;
2050 }
2051 else
2052 #endif /* !WIN32 */
2053 {
2054 /*
2055 * Read cupsd.conf via a HTTP GET request...
2056 */
2057
2058 if ((fd = cupsTempFd(name, namesize)) < 0)
2059 {
2060 *name = '\0';
2061
2062 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
2063
2064 invalidate_cupsd_cache(cg);
2065
2066 return (HTTP_SERVER_ERROR);
2067 }
2068
2069 *remote = 1;
2070
2071 httpClearFields(http);
2072
2073 if (last_update)
2074 httpSetField(http, HTTP_FIELD_IF_MODIFIED_SINCE,
2075 httpGetDateString(last_update));
2076
2077 status = cupsGetFd(http, "/admin/conf/cupsd.conf", fd);
2078
2079 close(fd);
2080
2081 if (status != HTTP_OK)
2082 {
2083 unlink(name);
2084 *name = '\0';
2085 }
2086 }
2087
2088 return (status);
2089 }
2090
2091
2092 /*
2093 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2094 */
2095
2096 static void
2097 invalidate_cupsd_cache(
2098 _cups_globals_t *cg) /* I - Global data */
2099 {
2100 cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings);
2101
2102 cg->cupsd_hostname[0] = '\0';
2103 cg->cupsd_update = 0;
2104 cg->cupsd_num_settings = 0;
2105 cg->cupsd_settings = NULL;
2106 }
2107
2108
2109 /*
2110 * 'write_option()' - Write a CUPS option to a PPD file.
2111 */
2112
2113 static void
2114 write_option(cups_file_t *dstfp, /* I - PPD file */
2115 int order, /* I - Order dependency */
2116 const char *name, /* I - Option name */
2117 const char *text, /* I - Option text */
2118 const char *attrname, /* I - Attribute name */
2119 ipp_attribute_t *suppattr, /* I - IPP -supported attribute */
2120 ipp_attribute_t *defattr, /* I - IPP -default attribute */
2121 int defval, /* I - Default value number */
2122 int valcount) /* I - Number of values */
2123 {
2124 int i; /* Looping var */
2125
2126
2127 cupsFilePrintf(dstfp, "*JCLOpenUI *%s/%s: PickOne\n"
2128 "*OrderDependency: %d JCLSetup *%s\n",
2129 name, text, order, name);
2130
2131 if (defattr->value_tag == IPP_TAG_INTEGER)
2132 {
2133 /*
2134 * Do numeric options with a range or list...
2135 */
2136
2137 cupsFilePrintf(dstfp, "*Default%s: %d\n", name,
2138 defattr->values[defval].integer);
2139
2140 if (suppattr->value_tag == IPP_TAG_RANGE)
2141 {
2142 /*
2143 * List each number in the range...
2144 */
2145
2146 for (i = suppattr->values[0].range.lower;
2147 i <= suppattr->values[0].range.upper;
2148 i ++)
2149 {
2150 cupsFilePrintf(dstfp, "*%s %d: \"", name, i);
2151
2152 if (valcount == 1)
2153 cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2154 attrname, i);
2155 else if (defval == 0)
2156 cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\"\n", attrname, i);
2157 else if (defval < (valcount - 1))
2158 cupsFilePrintf(dstfp, ",%d\"\n", i);
2159 else
2160 cupsFilePrintf(dstfp, ",%d\n\"\n*End\n", i);
2161 }
2162 }
2163 else
2164 {
2165 /*
2166 * List explicit numbers...
2167 */
2168
2169 for (i = 0; i < suppattr->num_values; i ++)
2170 {
2171 cupsFilePrintf(dstfp, "*%s %d: \"", name, suppattr->values[i].integer);
2172
2173 if (valcount == 1)
2174 cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname,
2175 suppattr->values[i].integer);
2176 else if (defval == 0)
2177 cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\"\n", attrname,
2178 suppattr->values[i].integer);
2179 else if (defval < (valcount - 1))
2180 cupsFilePrintf(dstfp, ",%d\"\n", suppattr->values[i].integer);
2181 else
2182 cupsFilePrintf(dstfp, ",%d\n\"\n*End\n", suppattr->values[i].integer);
2183 }
2184 }
2185 }
2186 else
2187 {
2188 /*
2189 * Do text options with a list...
2190 */
2191
2192 cupsFilePrintf(dstfp, "*Default%s: %s\n", name,
2193 defattr->values[defval].string.text);
2194
2195 for (i = 0; i < suppattr->num_values; i ++)
2196 {
2197 cupsFilePrintf(dstfp, "*%s %s: \"", name,
2198 suppattr->values[i].string.text);
2199
2200 if (valcount == 1)
2201 cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname,
2202 suppattr->values[i].string.text);
2203 else if (defval == 0)
2204 cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%s\"\n", attrname,
2205 suppattr->values[i].string.text);
2206 else if (defval < (valcount - 1))
2207 cupsFilePrintf(dstfp, ",%s\"\n", suppattr->values[i].string.text);
2208 else
2209 cupsFilePrintf(dstfp, ",%s\n\"\n*End\n",
2210 suppattr->values[i].string.text);
2211 }
2212 }
2213
2214 cupsFilePrintf(dstfp, "*JCLCloseUI: *%s\n\n", name);
2215 }
2216
2217
2218 /*
2219 * End of "$Id: adminutil.c 6549 2007-06-04 15:32:58Z mike $".
2220 */