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