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