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