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