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