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