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