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