]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/adminutil.c
Merge changes from 1.4svn-r7568.
[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 *
080811b1 7 * Copyright 2007-2008 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
0a682745 294 if (sscanf(line, "*%40s%*[ \t]%40[^:/]", option, choice) != 2)
757d2cad 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);
839a51c8
MS
811
812 unlink(authfile);
813
757d2cad 814 return (0);
bc44d920 815 }
757d2cad 816
817 /*
818 * Finally, associate the drivers we just added with the queue...
819 */
820
821 snprintf(subcmd, sizeof(subcmd), "setdriver %s %s", dest, dest);
822
823 if ((status = do_samba_command("rpcclient", samba_server, subcmd,
824 authfile, logfile)) != 0)
825 {
826 snprintf(message, sizeof(message),
827 _cupsLangString(language,
b86bc4cf 828 _("Unable to set Windows printer driver (%d)!")),
757d2cad 829 status);
830
831 _cupsSetError(IPP_INTERNAL_ERROR, message);
832
833 if (logfile)
834 _cupsLangPrintf(logfile, "%s\n", message);
835
836 unlink(authfile);
837
838 return (0);
839 }
840
841 unlink(authfile);
842
843 return (1);
844}
845
846
7594b224 847/*
848 * 'cupsAdminGetServerSettings()' - Get settings from the server.
849 *
850 * The returned settings should be freed with cupsFreeOptions() when
851 * you are done with them.
852 *
853 * @since CUPS 1.3@
854 */
855
856int /* O - 1 on success, 0 on failure */
857cupsAdminGetServerSettings(
858 http_t *http, /* I - Connection to server */
859 int *num_settings, /* O - Number of settings */
860 cups_option_t **settings) /* O - Settings */
861{
862 return (_cupsAdminGetServerSettings(http, num_settings, settings));
863}
864
865
757d2cad 866/*
867 * '_cupsAdminGetServerSettings()' - Get settings from the server.
868 *
869 * The returned settings should be freed with cupsFreeOptions() when
870 * you are done with them.
871 *
872 * @since CUPS 1.2@
873 */
874
875int /* O - 1 on success, 0 on failure */
876_cupsAdminGetServerSettings(
877 http_t *http, /* I - Connection to server */
878 int *num_settings, /* O - Number of settings */
879 cups_option_t **settings) /* O - Settings */
880{
881 int i; /* Looping var */
882 cups_file_t *cupsd; /* cupsd.conf file */
883 char cupsdconf[1024]; /* cupsd.conf filename */
884 int remote; /* Remote cupsd.conf file? */
885 http_status_t status; /* Status of getting cupsd.conf */
886 char line[1024], /* Line from cupsd.conf file */
887 *value; /* Value on line */
888 cups_option_t *setting; /* Current setting */
889 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
890
891
892 /*
893 * Range check input...
894 */
895
896 if (!http || !num_settings || !settings)
897 {
898 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
899
900 if (num_settings)
901 *num_settings = 0;
902
903 if (settings)
904 *settings = NULL;
905
906 return (0);
907 }
908
909 *num_settings = 0;
910 *settings = NULL;
911
912 /*
913 * Get the cupsd.conf file...
914 */
915
916 if ((status = get_cupsd_conf(http, cg, cg->cupsd_update, cupsdconf,
917 sizeof(cupsdconf), &remote)) == HTTP_OK)
918 {
919 if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL)
480ef0fe 920 {
921 char message[1024]; /* Message string */
922
923
924 snprintf(message, sizeof(message),
925 _cupsLangString(cupsLangDefault(), _("open of %s failed: %s")),
926 cupsdconf, strerror(errno));
927 _cupsSetError(IPP_INTERNAL_ERROR, message);
928 }
757d2cad 929 }
930 else
931 cupsd = NULL;
932
933 if (cupsd)
934 {
935 /*
936 * Read the file, keeping track of what settings are enabled...
937 */
938
939 int remote_access = 0, /* Remote access allowed? */
940 remote_admin = 0, /* Remote administration allowed? */
f7deaa1a 941 remote_any = 0, /* Remote access from anywhere allowed? */
757d2cad 942 browsing = 1, /* Browsing enabled? */
943 browse_allow = 1, /* Browse address set? */
944 browse_address = 0, /* Browse address set? */
945 cancel_policy = 1, /* Cancel-job policy set? */
946 debug_logging = 0; /* LogLevel debug set? */
947 int linenum = 0, /* Line number in file */
948 in_location = 0, /* In a location section? */
949 in_policy = 0, /* In a policy section? */
950 in_cancel_job = 0, /* In a cancel-job section? */
951 in_admin_location = 0; /* In the /admin location? */
952
953
954 invalidate_cupsd_cache(cg);
955
956 cg->cupsd_update = time(NULL);
957 httpGetHostname(http, cg->cupsd_hostname, sizeof(cg->cupsd_hostname));
958
959 while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum))
960 {
c24d2134 961 if (!value && strncmp(line, "</", 2))
0a682745 962 value = line + strlen(line);
757d2cad 963
d09495fa 964 if (!strcasecmp(line, "Port") || !strcasecmp(line, "Listen"))
757d2cad 965 {
966 char *port; /* Pointer to port number, if any */
967
968
969 if ((port = strrchr(value, ':')) != NULL)
970 *port = '\0';
d09495fa 971 else if (isdigit(*value & 255))
972 {
973 /*
974 * Listen on a port number implies remote access...
975 */
976
977 remote_access = 1;
978 continue;
979 }
757d2cad 980
d09495fa 981 if (strcasecmp(value, "localhost") && strcmp(value, "127.0.0.1")
982#ifdef AF_LOCAL
983 && *value != '/'
984#endif /* AF_LOCAL */
985#ifdef AF_INET6
986 && strcmp(value, "::1")
987#endif /* AF_INET6 */
988 )
757d2cad 989 remote_access = 1;
990 }
991 else if (!strcasecmp(line, "Browsing"))
992 {
993 browsing = !strcasecmp(value, "yes") || !strcasecmp(value, "on") ||
994 !strcasecmp(value, "true");
995 }
996 else if (!strcasecmp(line, "BrowseAddress"))
997 {
998 browse_address = 1;
999 }
1000 else if (!strcasecmp(line, "BrowseAllow"))
1001 {
1002 browse_allow = 1;
1003 }
1004 else if (!strcasecmp(line, "BrowseOrder"))
1005 {
1006 browse_allow = !strncasecmp(value, "deny,", 5);
1007 }
1008 else if (!strcasecmp(line, "LogLevel"))
1009 {
1010 debug_logging = !strncasecmp(value, "debug", 5);
1011 }
1012 else if (!strcasecmp(line, "<Policy") && !strcasecmp(value, "default"))
1013 {
1014 in_policy = 1;
1015 }
1016 else if (!strcasecmp(line, "</Policy>"))
1017 {
1018 in_policy = 0;
1019 }
1020 else if (!strcasecmp(line, "<Limit") && in_policy)
1021 {
1022 /*
1023 * See if the policy limit is for the Cancel-Job operation...
1024 */
1025
1026 char *valptr; /* Pointer into value */
1027
1028
1029 while (*value)
1030 {
1031 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
1032
1033 if (*valptr)
1034 *valptr++ = '\0';
1035
1036 if (!strcasecmp(value, "cancel-job") || !strcasecmp(value, "all"))
1037 {
1038 in_cancel_job = 1;
1039 break;
1040 }
1041
1042 for (value = valptr; isspace(*value & 255); value ++);
1043 }
1044 }
1045 else if (!strcasecmp(line, "</Limit>"))
1046 {
1047 in_cancel_job = 0;
1048 }
1049 else if (!strcasecmp(line, "Require") && in_cancel_job)
1050 {
1051 cancel_policy = 0;
1052 }
1053 else if (!strcasecmp(line, "<Location"))
1054 {
1055 in_admin_location = !strcasecmp(value, "/admin");
1056 in_location = 1;
1057 }
1058 else if (!strcasecmp(line, "</Location>"))
1059 {
1060 in_admin_location = 0;
1061 in_location = 0;
1062 }
080811b1 1063 else if (!strcasecmp(line, "Allow") &&
d09495fa 1064 strcasecmp(value, "localhost") && strcasecmp(value, "127.0.0.1")
1065#ifdef AF_LOCAL
1066 && *value != '/'
1067#endif /* AF_LOCAL */
1068#ifdef AF_INET6
1069 && strcmp(value, "::1")
1070#endif /* AF_INET6 */
1071 )
757d2cad 1072 {
080811b1
MS
1073 if (in_admin_location)
1074 remote_admin = 1;
1075 else if (!strcasecmp(value, "all"))
f7deaa1a 1076 remote_any = 1;
757d2cad 1077 }
1078 else if (line[0] != '<' && !in_location && !in_policy)
1079 cg->cupsd_num_settings = cupsAddOption(line, value,
1080 cg->cupsd_num_settings,
1081 &(cg->cupsd_settings));
1082 }
1083
1084 cupsFileClose(cupsd);
1085
1086 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
1087 debug_logging ? "1" : "0",
1088 cg->cupsd_num_settings,
1089 &(cg->cupsd_settings));
1090
1091 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
1092 (remote_access && remote_admin) ?
1093 "1" : "0",
1094 cg->cupsd_num_settings,
1095 &(cg->cupsd_settings));
1096
f7deaa1a 1097 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY,
1098 remote_any ? "1" : "0",
1099 cg->cupsd_num_settings,
1100 &(cg->cupsd_settings));
1101
757d2cad 1102 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS,
1103 (browsing && browse_allow) ?
1104 "1" : "0",
1105 cg->cupsd_num_settings,
1106 &(cg->cupsd_settings));
1107
1108 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
1109 (remote_access && browsing &&
1110 browse_address) ? "1" : "0",
1111 cg->cupsd_num_settings,
1112 &(cg->cupsd_settings));
1113
1114 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
1115 cancel_policy ? "1" : "0",
1116 cg->cupsd_num_settings,
1117 &(cg->cupsd_settings));
1118 }
1119 else if (status != HTTP_NOT_MODIFIED)
1120 invalidate_cupsd_cache(cg);
1121
1122 /*
1123 * Remove any temporary files and copy the settings array...
1124 */
1125
1126 if (remote)
1127 unlink(cupsdconf);
1128
1129 for (i = cg->cupsd_num_settings, setting = cg->cupsd_settings;
1130 i > 0;
1131 i --, setting ++)
1132 *num_settings = cupsAddOption(setting->name, setting->value,
1133 *num_settings, settings);
1134
1135 return (cg->cupsd_num_settings > 0);
1136}
1137
1138
7594b224 1139/*
1140 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1141 *
1142 * @since CUPS 1.3@
1143 */
1144
1145int /* O - 1 on success, 0 on failure */
1146cupsAdminSetServerSettings(
1147 http_t *http, /* I - Connection to server */
1148 int num_settings, /* I - Number of settings */
1149 cups_option_t *settings) /* I - Settings */
1150{
1151 return (_cupsAdminSetServerSettings(http, num_settings, settings));
1152}
1153
1154
757d2cad 1155/*
1156 * '_cupsAdminSetServerSettings()' - Set settings on the server.
1157 *
1158 * @since CUPS 1.2@
1159 */
1160
1161int /* O - 1 on success, 0 on failure */
1162_cupsAdminSetServerSettings(
1163 http_t *http, /* I - Connection to server */
1164 int num_settings, /* I - Number of settings */
1165 cups_option_t *settings) /* I - Settings */
1166{
1167 int i; /* Looping var */
1168 http_status_t status; /* GET/PUT status */
07725fee 1169 const char *server_port_env; /* SERVER_PORT env var */
1170 int server_port; /* IPP port for server */
757d2cad 1171 cups_file_t *cupsd; /* cupsd.conf file */
1172 char cupsdconf[1024]; /* cupsd.conf filename */
1173 int remote; /* Remote cupsd.conf file? */
1174 char tempfile[1024]; /* Temporary new cupsd.conf */
1175 cups_file_t *temp; /* Temporary file */
1176 char line[1024], /* Line from cupsd.conf file */
1177 *value; /* Value on line */
1178 int linenum, /* Line number in file */
1179 in_location, /* In a location section? */
1180 in_policy, /* In a policy section? */
1181 in_default_policy, /* In the default policy section? */
1182 in_cancel_job, /* In a cancel-job section? */
1183 in_admin_location, /* In the /admin location? */
1184 in_conf_location, /* In the /admin/conf location? */
1185 in_root_location; /* In the / location? */
1186 const char *val; /* Setting value */
1187 int remote_printers, /* Show remote printers */
1188 share_printers, /* Share local printers */
1189 remote_admin, /* Remote administration allowed? */
f7deaa1a 1190 remote_any, /* Remote access from anywhere? */
757d2cad 1191 user_cancel_any, /* Cancel-job policy set? */
1192 debug_logging; /* LogLevel debug set? */
1193 int wrote_port_listen, /* Wrote the port/listen lines? */
1194 wrote_browsing, /* Wrote the browsing lines? */
1195 wrote_policy, /* Wrote the policy? */
1196 wrote_loglevel, /* Wrote the LogLevel line? */
1197 wrote_admin_location, /* Wrote the /admin location? */
1198 wrote_conf_location, /* Wrote the /admin/conf location? */
1199 wrote_root_location; /* Wrote the / location? */
1200 int indent; /* Indentation */
1201 int cupsd_num_settings; /* New number of settings */
b86bc4cf 1202 int old_remote_printers, /* Show remote printers */
1203 old_share_printers, /* Share local printers */
1204 old_remote_admin, /* Remote administration allowed? */
1205 old_user_cancel_any, /* Cancel-job policy set? */
1206 old_debug_logging; /* LogLevel debug set? */
757d2cad 1207 cups_option_t *cupsd_settings, /* New settings */
1208 *setting; /* Current setting */
1209 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
1210
1211
1212 /*
1213 * Range check input...
1214 */
1215
1216 if (!http || !num_settings || !settings)
1217 {
1218 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
1219
1220 return (0);
1221 }
1222
1223 /*
1224 * Get the cupsd.conf file...
1225 */
1226
1227 if ((status = get_cupsd_conf(http, cg, 0, cupsdconf, sizeof(cupsdconf),
1228 &remote)) == HTTP_OK)
1229 {
1230 if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL)
1231 {
1232 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
1233 return (0);
1234 }
1235 }
1236 else
1237 return (0);
1238
b86bc4cf 1239 /*
1240 * Get current settings...
1241 */
1242
1243 if (!_cupsAdminGetServerSettings(http, &cupsd_num_settings,
1244 &cupsd_settings))
1245 return (0);
1246
1247 if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, cupsd_num_settings,
1248 cupsd_settings)) != NULL)
1249 old_debug_logging = atoi(val);
1250 else
1251 old_debug_logging = 0;
1252
1253 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, cupsd_num_settings,
1254 cupsd_settings)) != NULL)
1255 old_remote_admin = atoi(val);
1256 else
1257 old_remote_admin = 0;
1258
f7deaa1a 1259 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, cupsd_num_settings,
1260 cupsd_settings)) != NULL)
1261 remote_any = atoi(val);
1262 else
1263 remote_any = 0;
1264
b86bc4cf 1265 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS, cupsd_num_settings,
1266 cupsd_settings)) != NULL)
1267 old_remote_printers = atoi(val);
1268 else
1269 old_remote_printers = 1;
1270
1271 if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, cupsd_num_settings,
1272 cupsd_settings)) != NULL)
1273 old_share_printers = atoi(val);
1274 else
1275 old_share_printers = 0;
1276
1277 if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, cupsd_num_settings,
1278 cupsd_settings)) != NULL)
1279 old_user_cancel_any = atoi(val);
1280 else
1281 old_user_cancel_any = 0;
1282
1283 cupsFreeOptions(cupsd_num_settings, cupsd_settings);
1284
757d2cad 1285 /*
1286 * Get basic settings...
1287 */
1288
1289 if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, num_settings,
1290 settings)) != NULL)
b86bc4cf 1291 {
757d2cad 1292 debug_logging = atoi(val);
b86bc4cf 1293
1294 if (debug_logging == old_debug_logging)
1295 {
1296 /*
1297 * No change to this setting...
1298 */
1299
1300 debug_logging = -1;
1301 }
1302 }
757d2cad 1303 else
b86bc4cf 1304 debug_logging = -1;
757d2cad 1305
f7deaa1a 1306 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, num_settings,
1307 settings)) != NULL)
1308 remote_any = atoi(val);
1309
757d2cad 1310 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, num_settings,
1311 settings)) != NULL)
b86bc4cf 1312 {
757d2cad 1313 remote_admin = atoi(val);
b86bc4cf 1314
f7deaa1a 1315 if (remote_admin == old_remote_admin && remote_any < 0)
b86bc4cf 1316 {
1317 /*
1318 * No change to this setting...
1319 */
1320
1321 remote_admin = -1;
1322 }
1323 }
757d2cad 1324 else
b86bc4cf 1325 remote_admin = -1;
757d2cad 1326
1327 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS, num_settings,
1328 settings)) != NULL)
b86bc4cf 1329 {
757d2cad 1330 remote_printers = atoi(val);
b86bc4cf 1331
1332 if (remote_printers == old_remote_printers)
1333 {
1334 /*
1335 * No change to this setting...
1336 */
1337
1338 remote_printers = -1;
1339 }
1340 }
757d2cad 1341 else
b86bc4cf 1342 remote_printers = -1;
757d2cad 1343
1344 if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, num_settings,
1345 settings)) != NULL)
b86bc4cf 1346 {
757d2cad 1347 share_printers = atoi(val);
b86bc4cf 1348
f7deaa1a 1349 if (share_printers == old_share_printers && remote_any < 0)
b86bc4cf 1350 {
1351 /*
1352 * No change to this setting...
1353 */
1354
1355 share_printers = -1;
1356 }
1357 }
757d2cad 1358 else
b86bc4cf 1359 share_printers = -1;
757d2cad 1360
1361 if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, num_settings,
1362 settings)) != NULL)
b86bc4cf 1363 {
757d2cad 1364 user_cancel_any = atoi(val);
b86bc4cf 1365
1366 if (user_cancel_any == old_user_cancel_any)
1367 {
1368 /*
1369 * No change to this setting...
1370 */
1371
1372 user_cancel_any = -1;
1373 }
1374 }
757d2cad 1375 else
b86bc4cf 1376 user_cancel_any = -1;
757d2cad 1377
1378 /*
1379 * Create a temporary file for the new cupsd.conf file...
1380 */
1381
1382 if ((temp = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
1383 {
1384 cupsFileClose(cupsd);
1385
1386 if (remote)
1387 unlink(cupsdconf);
1388
1389 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
1390 return (0);
1391 }
1392
1393 /*
1394 * Copy the old file to the new, making changes along the way...
1395 */
1396
1397 cupsd_num_settings = 0;
1398 in_admin_location = 0;
1399 in_cancel_job = 0;
1400 in_conf_location = 0;
1401 in_default_policy = 0;
1402 in_location = 0;
1403 in_policy = 0;
1404 in_root_location = 0;
1405 linenum = 0;
1406 wrote_admin_location = 0;
1407 wrote_browsing = 0;
1408 wrote_conf_location = 0;
1409 wrote_loglevel = 0;
1410 wrote_policy = 0;
1411 wrote_port_listen = 0;
1412 wrote_root_location = 0;
1413 indent = 0;
1414
07725fee 1415 if ((server_port_env = getenv("SERVER_PORT")) != NULL)
1416 {
1417 if ((server_port = atoi(server_port_env)) <= 0)
1418 server_port = ippPort();
1419 }
1420 else
1421 server_port = ippPort();
1422
1423 if (server_port <= 0)
1424 server_port = IPP_PORT;
1425
757d2cad 1426 while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum))
1427 {
b86bc4cf 1428 if ((!strcasecmp(line, "Port") || !strcasecmp(line, "Listen")) &&
1429 (share_printers >= 0 || remote_admin >= 0))
757d2cad 1430 {
1431 if (!wrote_port_listen)
1432 {
1433 wrote_port_listen = 1;
1434
b86bc4cf 1435 if (share_printers > 0 || remote_admin > 0)
757d2cad 1436 {
1437 cupsFilePuts(temp, "# Allow remote access\n");
07725fee 1438 cupsFilePrintf(temp, "Port %d\n", server_port);
757d2cad 1439 }
1440 else
1441 {
480ef0fe 1442 cupsFilePuts(temp, "# Only listen for connections from the local "
1443 "machine.\n");
07725fee 1444 cupsFilePrintf(temp, "Listen localhost:%d\n", server_port);
757d2cad 1445 }
1446
1447#ifdef CUPS_DEFAULT_DOMAINSOCKET
07725fee 1448 if ((!value || strcmp(CUPS_DEFAULT_DOMAINSOCKET, value)) &&
1449 !access(CUPS_DEFAULT_DOMAINSOCKET, 0))
757d2cad 1450 cupsFilePuts(temp, "Listen " CUPS_DEFAULT_DOMAINSOCKET "\n");
1451#endif /* CUPS_DEFAULT_DOMAINSOCKET */
1452 }
b86bc4cf 1453 else if (value && value[0] == '/'
1454#ifdef CUPS_DEFAULT_DOMAINSOCKET
1455 && strcmp(CUPS_DEFAULT_DOMAINSOCKET, value)
1456#endif /* CUPS_DEFAULT_DOMAINSOCKET */
1457 )
07725fee 1458 cupsFilePrintf(temp, "Listen %s\n", value);
757d2cad 1459 }
b86bc4cf 1460 else if ((!strcasecmp(line, "Browsing") ||
1461 !strcasecmp(line, "BrowseAddress") ||
1462 !strcasecmp(line, "BrowseAllow") ||
1463 !strcasecmp(line, "BrowseDeny") ||
2e4ff8af
MS
1464 !strcasecmp(line, "BrowseLocalProtocols") ||
1465 !strcasecmp(line, "BrowseRemoteProtocols") ||
b86bc4cf 1466 !strcasecmp(line, "BrowseOrder")) &&
1467 (remote_printers >= 0 || share_printers >= 0))
757d2cad 1468 {
1469 if (!wrote_browsing)
1470 {
09a101d6 1471 int new_remote_printers = (remote_printers > 0 ||
1472 (remote_printers == -1 &&
1473 old_remote_printers > 0));
1474 int new_share_printers = (share_printers > 0 ||
1475 (share_printers == -1 &&
1476 old_share_printers > 0));
1477
757d2cad 1478 wrote_browsing = 1;
1479
09a101d6 1480 if (new_remote_printers || new_share_printers)
757d2cad 1481 {
2e4ff8af
MS
1482 const char *localp = cupsGetOption("BrowseLocalProtocols",
1483 num_settings, settings);
1484 const char *remotep = cupsGetOption("BrowseRemoteProtocols",
1485 num_settings, settings);
1486
0a682745
MS
1487 if (!localp)
1488 localp = cupsGetOption("BrowseLocalProtocols", cupsd_num_settings,
1489 cupsd_settings);
1490
1491 if (!remotep)
1492 remotep = cupsGetOption("BrowseRemoteProtocols", cupsd_num_settings,
1493 cupsd_settings);
1494
09a101d6 1495 if (new_remote_printers && new_share_printers)
480ef0fe 1496 cupsFilePuts(temp,
1497 "# Enable printer sharing and shared printers.\n");
09a101d6 1498 else if (new_remote_printers)
480ef0fe 1499 cupsFilePuts(temp,
1500 "# Show shared printers on the local network.\n");
757d2cad 1501 else
480ef0fe 1502 cupsFilePuts(temp,
1503 "# Share local printers on the local network.\n");
757d2cad 1504
1505 cupsFilePuts(temp, "Browsing On\n");
1506 cupsFilePuts(temp, "BrowseOrder allow,deny\n");
1507
09a101d6 1508 if (new_remote_printers)
2e4ff8af 1509 {
f7deaa1a 1510 cupsFilePuts(temp, "BrowseAllow all\n");
757d2cad 1511
2e4ff8af
MS
1512 if (!remotep || !*remotep)
1513 cupsFilePuts(temp, "BrowseRemoteProtocols "
1514 CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS "\n");
1515 else if (remotep)
1516 cupsFilePrintf(temp, "BrowseRemoteProtocols %s\n", remotep);
1517 }
1518 else
1519 cupsFilePuts(temp, "BrowseRemoteProtocols\n");
1520
09a101d6 1521 if (new_share_printers)
2e4ff8af 1522 {
757d2cad 1523 cupsFilePuts(temp, "BrowseAddress @LOCAL\n");
2e4ff8af
MS
1524
1525 if (!localp || !*localp)
1526 cupsFilePuts(temp, "BrowseLocalProtocols "
1527 CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS "\n");
1528 else if (localp)
1529 cupsFilePrintf(temp, "BrowseLocalProtocols %s\n", localp);
1530 }
1531 else
1532 cupsFilePuts(temp, "BrowseLocalProtocols\n");
757d2cad 1533 }
1534 else
1535 {
480ef0fe 1536 cupsFilePuts(temp,
1537 "# Disable printer sharing and shared printers.\n");
757d2cad 1538 cupsFilePuts(temp, "Browsing Off\n");
1539 }
1540 }
1541 }
b86bc4cf 1542 else if (!strcasecmp(line, "LogLevel") && debug_logging >= 0)
757d2cad 1543 {
1544 wrote_loglevel = 1;
1545
1546 if (debug_logging)
1547 {
480ef0fe 1548 cupsFilePuts(temp,
1549 "# Show troubleshooting information in error_log.\n");
757d2cad 1550 cupsFilePuts(temp, "LogLevel debug\n");
1551 }
1552 else
1553 {
1554 cupsFilePuts(temp, "# Show general information in error_log.\n");
1555 cupsFilePuts(temp, "LogLevel info\n");
1556 }
1557 }
1558 else if (!strcasecmp(line, "<Policy"))
1559 {
1560 in_default_policy = !strcasecmp(value, "default");
1561 in_policy = 1;
1562
1563 cupsFilePrintf(temp, "%s %s>\n", line, value);
1564 indent += 2;
1565 }
1566 else if (!strcasecmp(line, "</Policy>"))
1567 {
1568 indent -= 2;
1569 if (!wrote_policy && in_default_policy)
1570 {
1571 wrote_policy = 1;
1572
1573 if (!user_cancel_any)
480ef0fe 1574 cupsFilePuts(temp, " # Only the owner or an administrator can "
1575 "cancel a job...\n"
757d2cad 1576 " <Limit Cancel-Job>\n"
1577 " Order deny,allow\n"
355e94dc
MS
1578 " Require user @OWNER "
1579 CUPS_DEFAULT_PRINTADMIN_AUTH "\n"
757d2cad 1580 " </Limit>\n");
1581 }
1582
1583 in_policy = 0;
1584 in_default_policy = 0;
1585
1586 cupsFilePuts(temp, "</Policy>\n");
1587 }
1588 else if (!strcasecmp(line, "<Location"))
1589 {
1590 in_location = 1;
1591 indent += 2;
1592 if (!strcmp(value, "/admin"))
1593 in_admin_location = 1;
1594 if (!strcmp(value, "/admin/conf"))
1595 in_conf_location = 1;
1596 else if (!strcmp(value, "/"))
1597 in_root_location = 1;
1598
1599 cupsFilePrintf(temp, "%s %s>\n", line, value);
1600 }
1601 else if (!strcasecmp(line, "</Location>"))
1602 {
1603 in_location = 0;
1604 indent -= 2;
b86bc4cf 1605 if (in_admin_location && remote_admin >= 0)
757d2cad 1606 {
1607 wrote_admin_location = 1;
1608
1609 if (remote_admin)
1610 cupsFilePuts(temp, " # Allow remote administration...\n");
b86bc4cf 1611 else if (remote_admin == 0)
757d2cad 1612 cupsFilePuts(temp, " # Restrict access to the admin pages...\n");
1613
1614 cupsFilePuts(temp, " Order allow,deny\n");
1615
1616 if (remote_admin)
f7deaa1a 1617 cupsFilePrintf(temp, " Allow %s\n",
1618 remote_any > 0 ? "all" : "@LOCAL");
757d2cad 1619 }
b86bc4cf 1620 else if (in_conf_location && remote_admin >= 0)
757d2cad 1621 {
1622 wrote_conf_location = 1;
1623
1624 if (remote_admin)
480ef0fe 1625 cupsFilePuts(temp, " # Allow remote access to the configuration "
1626 "files...\n");
757d2cad 1627 else
480ef0fe 1628 cupsFilePuts(temp, " # Restrict access to the configuration "
1629 "files...\n");
757d2cad 1630
1631 cupsFilePuts(temp, " Order allow,deny\n");
1632
1633 if (remote_admin)
f7deaa1a 1634 cupsFilePrintf(temp, " Allow %s\n",
1635 remote_any > 0 ? "all" : "@LOCAL");
757d2cad 1636 }
b86bc4cf 1637 else if (in_root_location && (remote_admin >= 0 || share_printers >= 0))
757d2cad 1638 {
1639 wrote_root_location = 1;
1640
b86bc4cf 1641 if (remote_admin > 0 && share_printers > 0)
480ef0fe 1642 cupsFilePuts(temp, " # Allow shared printing and remote "
1643 "administration...\n");
b86bc4cf 1644 else if (remote_admin > 0)
757d2cad 1645 cupsFilePuts(temp, " # Allow remote administration...\n");
b86bc4cf 1646 else if (share_printers > 0)
757d2cad 1647 cupsFilePuts(temp, " # Allow shared printing...\n");
1648 else
1649 cupsFilePuts(temp, " # Restrict access to the server...\n");
1650
1651 cupsFilePuts(temp, " Order allow,deny\n");
1652
b86bc4cf 1653 if (remote_admin > 0 || share_printers > 0)
f7deaa1a 1654 cupsFilePrintf(temp, " Allow %s\n",
1655 remote_any > 0 ? "all" : "@LOCAL");
757d2cad 1656 }
1657
1658 in_admin_location = 0;
1659 in_conf_location = 0;
1660 in_root_location = 0;
1661
1662 cupsFilePuts(temp, "</Location>\n");
1663 }
1664 else if (!strcasecmp(line, "<Limit") && in_default_policy)
1665 {
1666 /*
1667 * See if the policy limit is for the Cancel-Job operation...
1668 */
1669
1670 char *valptr; /* Pointer into value */
1671
1672
1673 indent += 2;
1674
b86bc4cf 1675 if (!strcasecmp(value, "cancel-job") && user_cancel_any >= 0)
757d2cad 1676 {
1677 /*
1678 * Don't write anything for this limit section...
1679 */
1680
1681 in_cancel_job = 2;
1682 }
1683 else
1684 {
1685 cupsFilePrintf(temp, " %s", line);
1686
1687 while (*value)
1688 {
1689 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
1690
1691 if (*valptr)
1692 *valptr++ = '\0';
1693
b86bc4cf 1694 if (!strcasecmp(value, "cancel-job") && user_cancel_any >= 0)
757d2cad 1695 {
1696 /*
1697 * Write everything except for this definition...
1698 */
1699
1700 in_cancel_job = 1;
1701 }
1702 else
1703 cupsFilePrintf(temp, " %s", value);
1704
1705 for (value = valptr; isspace(*value & 255); value ++);
1706 }
1707
1708 cupsFilePuts(temp, ">\n");
1709 }
1710 }
1711 else if (!strcasecmp(line, "</Limit>") && in_cancel_job)
1712 {
1713 indent -= 2;
1714
1715 if (in_cancel_job == 1)
1716 cupsFilePuts(temp, " </Limit>\n");
1717
1718 wrote_policy = 1;
1719
1720 if (!user_cancel_any)
480ef0fe 1721 cupsFilePuts(temp, " # Only the owner or an administrator can cancel "
1722 "a job...\n"
757d2cad 1723 " <Limit Cancel-Job>\n"
1724 " Order deny,allow\n"
355e94dc
MS
1725 " Require user @OWNER "
1726 CUPS_DEFAULT_PRINTADMIN_AUTH "\n"
757d2cad 1727 " </Limit>\n");
1728
1729 in_cancel_job = 0;
1730 }
b86bc4cf 1731 else if ((((in_admin_location || in_conf_location || in_root_location) &&
1732 remote_admin >= 0) ||
1733 (in_root_location && share_printers >= 0)) &&
0a682745
MS
1734 (((!strcasecmp(line, "Allow") || !strcasecmp(line, "Deny")) &&
1735 !strcasecmp(value, "@LOCAL")) ||
757d2cad 1736 !strcasecmp(line, "Order")))
1737 continue;
1738 else if (in_cancel_job == 2)
1739 continue;
1740 else if (!strcasecmp(line, "<Limit") && value)
1741 cupsFilePrintf(temp, " %s %s>\n", line, value);
1742 else if (line[0] == '<')
1743 {
1744 if (value)
1745 {
1746 cupsFilePrintf(temp, "%*s%s %s>\n", indent, "", line, value);
1747 indent += 2;
1748 }
1749 else
1750 {
1751 if (line[1] == '/')
1752 indent -= 2;
1753
1754 cupsFilePrintf(temp, "%*s%s\n", indent, "", line);
1755 }
1756 }
1757 else if (!in_policy && !in_location &&
7594b224 1758 (val = cupsGetOption(line, num_settings, settings)) != NULL)
757d2cad 1759 {
1760 /*
7594b224 1761 * Replace this directive's value with the new one...
757d2cad 1762 */
1763
7594b224 1764 cupsd_num_settings = cupsAddOption(line, val, cupsd_num_settings,
757d2cad 1765 &cupsd_settings);
1766
1767 /*
1768 * Write the new value in its place, without indentation since we
1769 * only support setting root directives, not in sections...
1770 */
1771
7594b224 1772 cupsFilePrintf(temp, "%s %s\n", line, val);
757d2cad 1773 }
1774 else if (value)
1775 {
1776 if (!in_policy && !in_location)
1777 {
1778 /*
1779 * Record the non-policy, non-location directives that we find
1780 * in the server settings, since we cache this info and record it
1781 * in _cupsAdminGetServerSettings()...
1782 */
1783
1784 cupsd_num_settings = cupsAddOption(line, value, cupsd_num_settings,
1785 &cupsd_settings);
1786 }
1787
1788 cupsFilePrintf(temp, "%*s%s %s\n", indent, "", line, value);
1789 }
1790 else
1791 cupsFilePrintf(temp, "%*s%s\n", indent, "", line);
1792 }
1793
1794 /*
1795 * Write any missing info...
1796 */
1797
b86bc4cf 1798 if (!wrote_browsing && (remote_printers >= 0 || share_printers >= 0))
757d2cad 1799 {
b86bc4cf 1800 if (remote_printers > 0 || share_printers > 0)
757d2cad 1801 {
b86bc4cf 1802 if (remote_printers > 0 && share_printers > 0)
757d2cad 1803 cupsFilePuts(temp, "# Enable printer sharing and shared printers.\n");
b86bc4cf 1804 else if (remote_printers > 0)
757d2cad 1805 cupsFilePuts(temp, "# Show shared printers on the local network.\n");
1806 else
1807 cupsFilePuts(temp, "# Share local printers on the local network.\n");
1808
1809 cupsFilePuts(temp, "Browsing On\n");
1810 cupsFilePuts(temp, "BrowseOrder allow,deny\n");
1811
b86bc4cf 1812 if (remote_printers > 0)
f7deaa1a 1813 cupsFilePuts(temp, "BrowseAllow all\n");
757d2cad 1814
b86bc4cf 1815 if (share_printers > 0)
757d2cad 1816 cupsFilePuts(temp, "BrowseAddress @LOCAL\n");
1817 }
1818 else
1819 {
1820 cupsFilePuts(temp, "# Disable printer sharing and shared printers.\n");
1821 cupsFilePuts(temp, "Browsing Off\n");
1822 }
1823 }
1824
b86bc4cf 1825 if (!wrote_loglevel && debug_logging >= 0)
757d2cad 1826 {
1827 if (debug_logging)
1828 {
1829 cupsFilePuts(temp, "# Show troubleshooting information in error_log.\n");
1830 cupsFilePuts(temp, "LogLevel debug\n");
1831 }
1832 else
1833 {
1834 cupsFilePuts(temp, "# Show general information in error_log.\n");
1835 cupsFilePuts(temp, "LogLevel info\n");
1836 }
1837 }
1838
b86bc4cf 1839 if (!wrote_port_listen && (share_printers >= 0 || remote_admin >= 0))
757d2cad 1840 {
b86bc4cf 1841 if (share_printers > 0 || remote_admin > 0)
757d2cad 1842 {
1843 cupsFilePuts(temp, "# Allow remote access\n");
1844 cupsFilePrintf(temp, "Port %d\n", ippPort());
1845 }
1846 else
1847 {
480ef0fe 1848 cupsFilePuts(temp,
1849 "# Only listen for connections from the local machine.\n");
d09495fa 1850 cupsFilePrintf(temp, "Listen localhost:%d\n", ippPort());
757d2cad 1851 }
1852
1853#ifdef CUPS_DEFAULT_DOMAINSOCKET
1854 if (!access(CUPS_DEFAULT_DOMAINSOCKET, 0))
1855 cupsFilePuts(temp, "Listen " CUPS_DEFAULT_DOMAINSOCKET "\n");
1856#endif /* CUPS_DEFAULT_DOMAINSOCKET */
1857 }
1858
b86bc4cf 1859 if (!wrote_root_location && (remote_admin >= 0 || share_printers >= 0))
757d2cad 1860 {
b86bc4cf 1861 if (remote_admin > 0 && share_printers > 0)
480ef0fe 1862 cupsFilePuts(temp,
1863 "# Allow shared printing and remote administration...\n");
b86bc4cf 1864 else if (remote_admin > 0)
757d2cad 1865 cupsFilePuts(temp, "# Allow remote administration...\n");
b86bc4cf 1866 else if (share_printers > 0)
757d2cad 1867 cupsFilePuts(temp, "# Allow shared printing...\n");
1868 else
1869 cupsFilePuts(temp, "# Restrict access to the server...\n");
1870
1871 cupsFilePuts(temp, "<Location />\n"
1872 " Order allow,deny\n");
1873
b86bc4cf 1874 if (remote_admin > 0 || share_printers > 0)
f7deaa1a 1875 cupsFilePrintf(temp, " Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
757d2cad 1876
1877 cupsFilePuts(temp, "</Location>\n");
1878 }
1879
b86bc4cf 1880 if (!wrote_admin_location && remote_admin >= 0)
757d2cad 1881 {
1882 if (remote_admin)
1883 cupsFilePuts(temp, "# Allow remote administration...\n");
1884 else
1885 cupsFilePuts(temp, "# Restrict access to the admin pages...\n");
1886
1887 cupsFilePuts(temp, "<Location /admin>\n"
1888 " Order allow,deny\n");
1889
1890 if (remote_admin)
f7deaa1a 1891 cupsFilePrintf(temp, " Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
757d2cad 1892
1893 cupsFilePuts(temp, "</Location>\n");
1894 }
1895
b86bc4cf 1896 if (!wrote_conf_location && remote_admin >= 0)
757d2cad 1897 {
1898 if (remote_admin)
480ef0fe 1899 cupsFilePuts(temp,
1900 "# Allow remote access to the configuration files...\n");
757d2cad 1901 else
1902 cupsFilePuts(temp, "# Restrict access to the configuration files...\n");
1903
1904 cupsFilePuts(temp, "<Location /admin/conf>\n"
0a682745 1905 " AuthType Default\n"
757d2cad 1906 " Require user @SYSTEM\n"
1907 " Order allow,deny\n");
1908
1909 if (remote_admin)
f7deaa1a 1910 cupsFilePrintf(temp, " Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
757d2cad 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"
0a682745 1941 " AuthType Default\n"
757d2cad 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 */