]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/adminutil.c
Move debug printfs to internal usage only.
[thirdparty/cups.git] / cups / adminutil.c
CommitLineData
757d2cad 1/*
7e86f2f6 2 * Administration utility API definitions for CUPS.
757d2cad 3 *
6720d4f4 4 * Copyright 2007-2016 by Apple Inc.
7e86f2f6 5 * Copyright 2001-2007 by Easy Software Products.
757d2cad 6 *
e3101897 7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
757d2cad 8 */
9
10/*
11 * Include necessary headers...
12 */
13
71e16022 14#include "cups-private.h"
fb863569 15#include "debug-internal.h"
f787e1e3 16#include "ppd.h"
757d2cad 17#include "adminutil.h"
757d2cad 18#include <fcntl.h>
757d2cad 19#include <sys/stat.h>
19dc16f7 20#ifndef _WIN32
b86bc4cf 21# include <unistd.h>
22# include <sys/wait.h>
19dc16f7 23#endif /* !_WIN32 */
757d2cad 24
25
26/*
27 * Local functions...
28 */
29
30static int do_samba_command(const char *command,
31 const char *address,
32 const char *subcommand,
33 const char *authfile,
34 FILE *logfile);
35static http_status_t get_cupsd_conf(http_t *http, _cups_globals_t *cg,
36 time_t last_update, char *name,
07623986 37 size_t namelen, int *remote);
757d2cad 38static void invalidate_cupsd_cache(_cups_globals_t *cg);
39static 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.
426c6a59 49 *
5a9febac 50 * @deprecated@
757d2cad 51 */
52
53char * /* O - PPD file or NULL */
54cupsAdminCreateWindowsPPD(
01ce6322 55 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
757d2cad 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? */
323c5de1 75 jclorder, /* Next JCL order dependency */
757d2cad 76 linenum; /* Current line number */
77 time_t curtime; /* Current time */
78 struct tm *curdate; /* Current date */
d09495fa 79 static const char * const pattrs[] = /* Printer attributes we want */
757d2cad 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
01ce6322
MS
97 if (!http)
98 http = _cupsConnect();
99
757d2cad 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
cb7f98ee 114 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
757d2cad 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, "/");
cb7f98ee 130 if (!response || cupsLastError() > IPP_STATUS_OK_CONFLICTING)
757d2cad 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
e1d6a774 147 if ((dstfp = cupsTempFile2(buffer, bufsize)) == NULL)
757d2cad 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;
323c5de1 176 jclorder = 0;
757d2cad 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"
ed486911 202 "*%%%s\n", line + 1);
757d2cad 203 continue;
204 }
205 else if (!strncmp(line, "*JCLOpenUI", 10))
206 {
207 jcloption = 1;
ed486911 208 cupsFilePrintf(dstfp, "%s\n", line);
757d2cad 209 }
210 else if (!strncmp(line, "*JCLCloseUI", 11))
211 {
212 jcloption = 0;
ed486911 213 cupsFilePrintf(dstfp, "%s\n", line);
757d2cad 214 }
323c5de1 215 else if (jcloption && !strncmp(line, "*OrderDependency:", 17))
216 {
7cf5915e 217 for (ptr = line + 17; _cups_isspace(*ptr); ptr ++);
323c5de1 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 }
757d2cad 229 else if (jcloption &&
230 strncmp(line, "*End", 4) &&
323c5de1 231 strncmp(line, "*Default", 8))
757d2cad 232 {
233 if ((ptr = strchr(line, ':')) == NULL)
234 {
235 snprintf(line, sizeof(line),
84315f46 236 _cupsLangString(language, _("Missing value on line %d.")),
749b1e90 237 linenum);
cb7f98ee 238 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR, line, 0);
757d2cad 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,
84315f46 255 _("Missing double quote on line %d.")),
757d2cad 256 linenum);
cb7f98ee 257 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR, line, 0);
757d2cad 258
259 cupsFileClose(srcfp);
260 cupsFileClose(dstfp);
261
262 unlink(src);
263 unlink(buffer);
264
265 *buffer = '\0';
266
267 return (NULL);
268 }
269
0a682745 270 if (sscanf(line, "*%40s%*[ \t]%40[^:/]", option, choice) != 2)
757d2cad 271 {
272 snprintf(line, sizeof(line),
273 _cupsLangString(language,
84315f46 274 _("Bad option + choice on line %d.")),
757d2cad 275 linenum);
cb7f98ee 276 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR, line, 0);
757d2cad 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
7e86f2f6 304 snprintf(ptr + 1, sizeof(line) - (size_t)(ptr - line + 1),
ed486911 305 "%%cupsJobTicket: %s=%s\n\"\n*End", option, choice);
757d2cad 306
ed486911 307 cupsFilePrintf(dstfp, "*%% Changed for CUPS Windows Driver...\n%s\n",
308 line);
757d2cad 309 }
310 else
ed486911 311 cupsFilePrintf(dstfp, "%s\n", line);
757d2cad 312 }
313
314 cupsFileClose(srcfp);
315 unlink(src);
316
b86bc4cf 317 if (linenum == 0)
318 {
cb7f98ee 319 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR, _("Empty PPD file."), 1);
b86bc4cf 320
321 cupsFileClose(dstfp);
322 unlink(buffer);
323
324 *buffer = '\0';
325
326 return (NULL);
327 }
328
757d2cad 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)
323c5de1 345 write_option(dstfp, jclorder ++, "cupsJobHoldUntil", "Hold Until",
346 "job-hold-until", suppattr, defattr, 0, 1);
757d2cad 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)
323c5de1 352 write_option(dstfp, jclorder ++, "cupsJobPriority", "Priority",
353 "job-priority", suppattr, defattr, 0, 1);
757d2cad 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 {
323c5de1 360 write_option(dstfp, jclorder ++, "cupsJobSheetsStart", "Start Banner",
757d2cad 361 "job-sheets", suppattr, defattr, 0, 2);
1f0275e3 362 write_option(dstfp, jclorder, "cupsJobSheetsEnd", "End Banner",
757d2cad 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.
426c6a59 377 *
5a9febac 378 * @deprecated@
757d2cad 379 */
380
381int /* O - 1 on success, 0 on failure */
382cupsAdminExportSamba(
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{
480ef0fe 390 int status; /* Status of Samba commands */
757d2cad 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 {
cb7f98ee 409 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
757d2cad 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 {
cb7f98ee 419 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
757d2cad 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 "
84315f46 470 "driver files (%d).")), status);
757d2cad 471
cb7f98ee 472 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
757d2cad 473
474 if (logfile)
0837b7e8 475 _cupsLangPuts(logfile, message);
757d2cad 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 "
84315f46 505 "files (%d).")), status);
757d2cad 506
cb7f98ee 507 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
757d2cad 508
509 if (logfile)
0837b7e8 510 _cupsLangPuts(logfile, message);
757d2cad 511
512 unlink(authfile);
513
514 return (0);
515 }
88f9aafc 516
757d2cad 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 "
84315f46 548 "driver files (%d).")), status);
757d2cad 549
cb7f98ee 550 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
757d2cad 551
552 if (logfile)
0837b7e8 553 _cupsLangPuts(logfile, message);
757d2cad 554
555 unlink(authfile);
556
557 return (0);
558 }
559 }
560
355e94dc
MS
561 /*
562 * See if we have the Win9x PS driver...
563 */
564
757d2cad 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 "
84315f46 593 "driver files (%d).")), status);
757d2cad 594
cb7f98ee 595 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
757d2cad 596
597 if (logfile)
0837b7e8 598 _cupsLangPuts(logfile, message);
757d2cad 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 "
84315f46 622 "driver files (%d).")), status);
757d2cad 623
cb7f98ee 624 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
757d2cad 625
626 if (logfile)
0837b7e8 627 _cupsLangPuts(logfile, message);
757d2cad 628
629 unlink(authfile);
630
631 return (0);
632 }
633 }
634
355e94dc
MS
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 "
84315f46 674 "driver files (%d).")), status);
355e94dc 675
cb7f98ee 676 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
355e94dc
MS
677
678 if (logfile)
0837b7e8 679 _cupsLangPuts(logfile, message);
355e94dc
MS
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 "
84315f46 709 "files (%d).")), status);
355e94dc 710
cb7f98ee 711 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
355e94dc
MS
712
713 if (logfile)
0837b7e8 714 _cupsLangPuts(logfile, message);
355e94dc
MS
715
716 unlink(authfile);
717
718 return (0);
719 }
88f9aafc 720
355e94dc
MS
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 "
84315f46 752 "driver files (%d).")), status);
355e94dc 753
cb7f98ee 754 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
355e94dc
MS
755
756 if (logfile)
0837b7e8 757 _cupsLangPuts(logfile, message);
355e94dc
MS
758
759 unlink(authfile);
760
761 return (0);
762 }
763 }
764
757d2cad 765 if (logfile && !(have_drivers & 1))
766 {
767 if (!have_drivers)
768 strlcpy(message,
769 _cupsLangString(language,
84315f46 770 _("No Windows printer drivers are installed.")),
757d2cad 771 sizeof(message));
772 else
773 strlcpy(message,
774 _cupsLangString(language,
775 _("Warning, no Windows 2000 printer drivers "
84315f46 776 "are installed.")),
757d2cad 777 sizeof(message));
778
cb7f98ee 779 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND, message, 0);
0837b7e8 780 _cupsLangPuts(logfile, message);
757d2cad 781 }
782
783 if (have_drivers == 0)
bc44d920 784 {
cb7f98ee 785 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND, message, 0);
839a51c8
MS
786
787 unlink(authfile);
788
757d2cad 789 return (0);
bc44d920 790 }
757d2cad 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,
84315f46 803 _("Unable to set Windows printer driver (%d).")),
757d2cad 804 status);
805
cb7f98ee 806 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
757d2cad 807
808 if (logfile)
0837b7e8 809 _cupsLangPuts(logfile, message);
757d2cad 810
811 unlink(authfile);
812
813 return (0);
814 }
815
816 unlink(authfile);
817
818 return (1);
819}
820
821
7594b224 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 *
8072030b 828 * @since CUPS 1.3/macOS 10.5@
7594b224 829 */
830
831int /* O - 1 on success, 0 on failure */
832cupsAdminGetServerSettings(
01ce6322 833 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
7594b224 834 int *num_settings, /* O - Number of settings */
835 cups_option_t **settings) /* O - Settings */
757d2cad 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
01ce6322 852 if (!http)
1ff0402e
MS
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) ||
a469f8a5 867 cg->ipp_port != httpAddrPort(cg->http->hostaddr) ||
1ff0402e 868 (cg->http->encryption != cg->encryption &&
cb7f98ee 869 cg->http->encryption == HTTP_ENCRYPTION_NEVER))
1ff0402e
MS
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 {
db8b865d
MS
886 if ((cg->http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC,
887 cupsEncryption(), 1, 0, NULL)) == NULL)
1ff0402e
MS
888 {
889 if (errno)
cb7f98ee 890 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, NULL, 0);
1ff0402e 891 else
cb7f98ee 892 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE,
1ff0402e
MS
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 }
f11a948a
MS
904
905 http = cg->http;
1ff0402e 906 }
01ce6322 907
757d2cad 908 if (!http || !num_settings || !settings)
909 {
cb7f98ee 910 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
757d2cad 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,
cb7f98ee 929 sizeof(cupsdconf), &remote)) == HTTP_STATUS_OK)
757d2cad 930 {
931 if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL)
480ef0fe 932 {
933 char message[1024]; /* Message string */
934
935
936 snprintf(message, sizeof(message),
749b1e90 937 _cupsLangString(cupsLangDefault(), _("Open of %s failed: %s")),
480ef0fe 938 cupsdconf, strerror(errno));
cb7f98ee 939 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
480ef0fe 940 }
757d2cad 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? */
f7deaa1a 953 remote_any = 0, /* Remote access from anywhere allowed? */
757d2cad 954 browsing = 1, /* Browsing enabled? */
757d2cad 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 {
c24d2134 971 if (!value && strncmp(line, "</", 2))
0a682745 972 value = line + strlen(line);
757d2cad 973
88f9aafc 974 if ((!_cups_strcasecmp(line, "Port") || !_cups_strcasecmp(line, "Listen")) && value)
757d2cad 975 {
976 char *port; /* Pointer to port number, if any */
977
978
979 if ((port = strrchr(value, ':')) != NULL)
980 *port = '\0';
d09495fa 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 }
757d2cad 990
88f9aafc 991 if (_cups_strcasecmp(value, "localhost") && strcmp(value, "127.0.0.1")
d09495fa 992#ifdef AF_LOCAL
993 && *value != '/'
994#endif /* AF_LOCAL */
995#ifdef AF_INET6
1106b00e 996 && strcmp(value, "[::1]")
d09495fa 997#endif /* AF_INET6 */
998 )
757d2cad 999 remote_access = 1;
1000 }
88f9aafc 1001 else if (!_cups_strcasecmp(line, "Browsing"))
757d2cad 1002 {
a2326b5b
MS
1003 browsing = !_cups_strcasecmp(value, "yes") ||
1004 !_cups_strcasecmp(value, "on") ||
88f9aafc 1005 !_cups_strcasecmp(value, "true");
757d2cad 1006 }
88f9aafc 1007 else if (!_cups_strcasecmp(line, "LogLevel"))
757d2cad 1008 {
88f9aafc 1009 debug_logging = !_cups_strncasecmp(value, "debug", 5);
757d2cad 1010 }
a2326b5b
MS
1011 else if (!_cups_strcasecmp(line, "<Policy") &&
1012 !_cups_strcasecmp(value, "default"))
757d2cad 1013 {
1014 in_policy = 1;
1015 }
88f9aafc 1016 else if (!_cups_strcasecmp(line, "</Policy>"))
757d2cad 1017 {
1018 in_policy = 0;
1019 }
88f9aafc 1020 else if (!_cups_strcasecmp(line, "<Limit") && in_policy && value)
757d2cad 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 {
7cf5915e 1031 for (valptr = value; *valptr && !_cups_isspace(*valptr); valptr ++);
757d2cad 1032
1033 if (*valptr)
1034 *valptr++ = '\0';
1035
a2326b5b
MS
1036 if (!_cups_strcasecmp(value, "cancel-job") ||
1037 !_cups_strcasecmp(value, "all"))
757d2cad 1038 {
1039 in_cancel_job = 1;
1040 break;
1041 }
1042
7cf5915e 1043 for (value = valptr; _cups_isspace(*value); value ++);
757d2cad 1044 }
1045 }
88f9aafc 1046 else if (!_cups_strcasecmp(line, "</Limit>"))
757d2cad 1047 {
1048 in_cancel_job = 0;
1049 }
88f9aafc 1050 else if (!_cups_strcasecmp(line, "Require") && in_cancel_job)
757d2cad 1051 {
1052 cancel_policy = 0;
1053 }
88f9aafc 1054 else if (!_cups_strcasecmp(line, "<Location") && value)
757d2cad 1055 {
88f9aafc 1056 in_admin_location = !_cups_strcasecmp(value, "/admin");
757d2cad 1057 in_location = 1;
1058 }
88f9aafc 1059 else if (!_cups_strcasecmp(line, "</Location>"))
757d2cad 1060 {
1061 in_admin_location = 0;
1062 in_location = 0;
1063 }
88f9aafc 1064 else if (!_cups_strcasecmp(line, "Allow") && value &&
a2326b5b
MS
1065 _cups_strcasecmp(value, "localhost") &&
1066 _cups_strcasecmp(value, "127.0.0.1")
d09495fa 1067#ifdef AF_LOCAL
1068 && *value != '/'
1069#endif /* AF_LOCAL */
1070#ifdef AF_INET6
1071 && strcmp(value, "::1")
1072#endif /* AF_INET6 */
1073 )
757d2cad 1074 {
080811b1
MS
1075 if (in_admin_location)
1076 remote_admin = 1;
88f9aafc 1077 else if (!_cups_strcasecmp(value, "all"))
f7deaa1a 1078 remote_any = 1;
757d2cad 1079 }
b9faaae1 1080 else if (line[0] != '<' && !in_location && !in_policy &&
88f9aafc
MS
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"))
757d2cad 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
f7deaa1a 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
757d2cad 1110 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
a2326b5b
MS
1111 (remote_access && browsing) ? "1" :
1112 "0",
757d2cad 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 }
cb7f98ee 1121 else if (status != HTTP_STATUS_NOT_MODIFIED)
757d2cad 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
7594b224 1141/*
1142 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1143 *
8072030b 1144 * @since CUPS 1.3/macOS 10.5@
7594b224 1145 */
1146
1147int /* O - 1 on success, 0 on failure */
1148cupsAdminSetServerSettings(
01ce6322 1149 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
7594b224 1150 int num_settings, /* I - Number of settings */
1151 cups_option_t *settings) /* I - Settings */
757d2cad 1152{
1153 int i; /* Looping var */
1154 http_status_t status; /* GET/PUT status */
07725fee 1155 const char *server_port_env; /* SERVER_PORT env var */
1156 int server_port; /* IPP port for server */
757d2cad 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? */
fcad6032 1171 in_log_location, /* In the /admin/log location? */
757d2cad 1172 in_root_location; /* In the / location? */
1173 const char *val; /* Setting value */
a2326b5b 1174 int share_printers, /* Share local printers */
757d2cad 1175 remote_admin, /* Remote administration allowed? */
f7deaa1a 1176 remote_any, /* Remote access from anywhere? */
757d2cad 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? */
fcad6032 1185 wrote_log_location, /* Wrote the /admin/log location? */
757d2cad 1186 wrote_root_location; /* Wrote the / location? */
1187 int indent; /* Indentation */
1188 int cupsd_num_settings; /* New number of settings */
a2326b5b 1189 int old_share_printers, /* Share local printers */
b86bc4cf 1190 old_remote_admin, /* Remote administration allowed? */
6720d4f4 1191 old_remote_any, /* Remote access from anywhere? */
b86bc4cf 1192 old_user_cancel_any, /* Cancel-job policy set? */
1193 old_debug_logging; /* LogLevel debug set? */
757d2cad 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
01ce6322
MS
1203 if (!http)
1204 http = _cupsConnect();
1205
757d2cad 1206 if (!http || !num_settings || !settings)
1207 {
cb7f98ee 1208 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
757d2cad 1209
1210 return (0);
1211 }
1212
1213 /*
1214 * Get the cupsd.conf file...
1215 */
1216
1f0275e3 1217 if (get_cupsd_conf(http, cg, 0, cupsdconf, sizeof(cupsdconf),
cb7f98ee 1218 &remote) == HTTP_STATUS_OK)
757d2cad 1219 {
1220 if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL)
1221 {
cb7f98ee 1222 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
757d2cad 1223 return (0);
1224 }
1225 }
1226 else
1227 return (0);
1228
b86bc4cf 1229 /*
1230 * Get current settings...
1231 */
1232
ba55dc12
MS
1233 if (!cupsAdminGetServerSettings(http, &cupsd_num_settings,
1234 &cupsd_settings))
b86bc4cf 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
1106b00e
MS
1243 DEBUG_printf(("1cupsAdminSetServerSettings: old debug_logging=%d",
1244 old_debug_logging));
1245
b86bc4cf 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
1106b00e
MS
1252 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_admin=%d",
1253 old_remote_admin));
1254
f7deaa1a 1255 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, cupsd_num_settings,
1256 cupsd_settings)) != NULL)
6720d4f4 1257 old_remote_any = atoi(val);
f7deaa1a 1258 else
6720d4f4 1259 old_remote_any = 0;
f7deaa1a 1260
ba55dc12 1261 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_any=%d",
6720d4f4 1262 old_remote_any));
ba55dc12 1263
b86bc4cf 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
1106b00e
MS
1270 DEBUG_printf(("1cupsAdminSetServerSettings: old share_printers=%d",
1271 old_share_printers));
1272
b86bc4cf 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
1106b00e
MS
1279 DEBUG_printf(("1cupsAdminSetServerSettings: old user_cancel_any=%d",
1280 old_user_cancel_any));
1281
b86bc4cf 1282 cupsFreeOptions(cupsd_num_settings, cupsd_settings);
1283
757d2cad 1284 /*
1285 * Get basic settings...
1286 */
1287
1288 if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, num_settings,
1289 settings)) != NULL)
b86bc4cf 1290 {
757d2cad 1291 debug_logging = atoi(val);
b86bc4cf 1292
1293 if (debug_logging == old_debug_logging)
1294 {
1295 /*
1296 * No change to this setting...
1297 */
1298
1299 debug_logging = -1;
1300 }
1301 }
757d2cad 1302 else
b86bc4cf 1303 debug_logging = -1;
757d2cad 1304
1106b00e
MS
1305 DEBUG_printf(("1cupsAdminSetServerSettings: debug_logging=%d",
1306 debug_logging));
1307
6720d4f4
MS
1308 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, num_settings, settings)) != NULL)
1309 {
f7deaa1a 1310 remote_any = atoi(val);
1311
6720d4f4
MS
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));
1106b00e 1325
757d2cad 1326 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, num_settings,
1327 settings)) != NULL)
b86bc4cf 1328 {
757d2cad 1329 remote_admin = atoi(val);
b86bc4cf 1330
1106b00e 1331 if (remote_admin == old_remote_admin)
b86bc4cf 1332 {
1333 /*
1334 * No change to this setting...
1335 */
1336
1337 remote_admin = -1;
1338 }
1339 }
757d2cad 1340 else
b86bc4cf 1341 remote_admin = -1;
757d2cad 1342
1106b00e
MS
1343 DEBUG_printf(("1cupsAdminSetServerSettings: remote_admin=%d",
1344 remote_admin));
1345
757d2cad 1346 if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, num_settings,
1347 settings)) != NULL)
b86bc4cf 1348 {
757d2cad 1349 share_printers = atoi(val);
b86bc4cf 1350
1106b00e 1351 if (share_printers == old_share_printers)
b86bc4cf 1352 {
1353 /*
1354 * No change to this setting...
1355 */
1356
1357 share_printers = -1;
1358 }
1359 }
757d2cad 1360 else
b86bc4cf 1361 share_printers = -1;
757d2cad 1362
1106b00e
MS
1363 DEBUG_printf(("1cupsAdminSetServerSettings: share_printers=%d",
1364 share_printers));
1365
757d2cad 1366 if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, num_settings,
1367 settings)) != NULL)
b86bc4cf 1368 {
757d2cad 1369 user_cancel_any = atoi(val);
b86bc4cf 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 }
757d2cad 1380 else
b86bc4cf 1381 user_cancel_any = -1;
757d2cad 1382
1106b00e
MS
1383 DEBUG_printf(("1cupsAdminSetServerSettings: user_cancel_any=%d",
1384 user_cancel_any));
1385
757d2cad 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
cb7f98ee 1397 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
757d2cad 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;
fcad6032 1411 in_log_location = 0;
757d2cad 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;
fcad6032 1418 wrote_log_location = 0;
757d2cad 1419 wrote_loglevel = 0;
1420 wrote_policy = 0;
1421 wrote_port_listen = 0;
1422 wrote_root_location = 0;
1423 indent = 0;
1424
07725fee 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
757d2cad 1436 while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum))
1437 {
88f9aafc 1438 if ((!_cups_strcasecmp(line, "Port") || !_cups_strcasecmp(line, "Listen")) &&
6720d4f4 1439 (remote_admin >= 0 || remote_any >= 0 || share_printers >= 0))
757d2cad 1440 {
1441 if (!wrote_port_listen)
1442 {
1443 wrote_port_listen = 1;
1444
ba55dc12 1445 if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
757d2cad 1446 {
1447 cupsFilePuts(temp, "# Allow remote access\n");
07725fee 1448 cupsFilePrintf(temp, "Port %d\n", server_port);
757d2cad 1449 }
1450 else
1451 {
480ef0fe 1452 cupsFilePuts(temp, "# Only listen for connections from the local "
1453 "machine.\n");
07725fee 1454 cupsFilePrintf(temp, "Listen localhost:%d\n", server_port);
757d2cad 1455 }
1456
1457#ifdef CUPS_DEFAULT_DOMAINSOCKET
07725fee 1458 if ((!value || strcmp(CUPS_DEFAULT_DOMAINSOCKET, value)) &&
1459 !access(CUPS_DEFAULT_DOMAINSOCKET, 0))
757d2cad 1460 cupsFilePuts(temp, "Listen " CUPS_DEFAULT_DOMAINSOCKET "\n");
1461#endif /* CUPS_DEFAULT_DOMAINSOCKET */
1462 }
b86bc4cf 1463 else if (value && value[0] == '/'
1464#ifdef CUPS_DEFAULT_DOMAINSOCKET
1465 && strcmp(CUPS_DEFAULT_DOMAINSOCKET, value)
1466#endif /* CUPS_DEFAULT_DOMAINSOCKET */
1467 )
07725fee 1468 cupsFilePrintf(temp, "Listen %s\n", value);
757d2cad 1469 }
88f9aafc 1470 else if ((!_cups_strcasecmp(line, "Browsing") ||
a2326b5b
MS
1471 !_cups_strcasecmp(line, "BrowseLocalProtocols")) &&
1472 share_printers >= 0)
757d2cad 1473 {
1474 if (!wrote_browsing)
1475 {
09a101d6 1476 int new_share_printers = (share_printers > 0 ||
1477 (share_printers == -1 &&
1478 old_share_printers > 0));
1479
757d2cad 1480 wrote_browsing = 1;
1481
a2326b5b 1482 if (new_share_printers)
757d2cad 1483 {
2e4ff8af
MS
1484 const char *localp = cupsGetOption("BrowseLocalProtocols",
1485 num_settings, settings);
2e4ff8af 1486
7a0cbd5e 1487 if (!localp || !localp[0])
0a682745
MS
1488 localp = cupsGetOption("BrowseLocalProtocols", cupsd_num_settings,
1489 cupsd_settings);
1490
a2326b5b 1491 cupsFilePuts(temp, "# Share local printers on the local network.\n");
757d2cad 1492 cupsFilePuts(temp, "Browsing On\n");
2e4ff8af 1493
a2326b5b
MS
1494 if (!localp)
1495 localp = CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS;
0af14961 1496
a2326b5b 1497 cupsFilePrintf(temp, "BrowseLocalProtocols %s\n", localp);
0af14961
MS
1498
1499 cupsd_num_settings = cupsAddOption("BrowseLocalProtocols", localp,
1500 cupsd_num_settings,
1501 &cupsd_settings);
757d2cad 1502 }
1503 else
1504 {
a2326b5b 1505 cupsFilePuts(temp, "# Disable printer sharing.\n");
757d2cad 1506 cupsFilePuts(temp, "Browsing Off\n");
1507 }
1508 }
1509 }
88f9aafc 1510 else if (!_cups_strcasecmp(line, "LogLevel") && debug_logging >= 0)
757d2cad 1511 {
1512 wrote_loglevel = 1;
1513
1514 if (debug_logging)
1515 {
480ef0fe 1516 cupsFilePuts(temp,
1517 "# Show troubleshooting information in error_log.\n");
757d2cad 1518 cupsFilePuts(temp, "LogLevel debug\n");
1519 }
1520 else
1521 {
1522 cupsFilePuts(temp, "# Show general information in error_log.\n");
1f0275e3 1523 cupsFilePuts(temp, "LogLevel " CUPS_DEFAULT_LOG_LEVEL "\n");
757d2cad 1524 }
1525 }
88f9aafc 1526 else if (!_cups_strcasecmp(line, "<Policy"))
757d2cad 1527 {
88f9aafc 1528 in_default_policy = !_cups_strcasecmp(value, "default");
757d2cad 1529 in_policy = 1;
1530
1531 cupsFilePrintf(temp, "%s %s>\n", line, value);
1532 indent += 2;
1533 }
88f9aafc 1534 else if (!_cups_strcasecmp(line, "</Policy>"))
757d2cad 1535 {
1536 indent -= 2;
1537 if (!wrote_policy && in_default_policy)
1538 {
1539 wrote_policy = 1;
1540
1541 if (!user_cancel_any)
480ef0fe 1542 cupsFilePuts(temp, " # Only the owner or an administrator can "
1543 "cancel a job...\n"
757d2cad 1544 " <Limit Cancel-Job>\n"
1545 " Order deny,allow\n"
355e94dc 1546 " Require user @OWNER "
b9faaae1 1547 CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n"
757d2cad 1548 " </Limit>\n");
1549 }
1550
1551 in_policy = 0;
1552 in_default_policy = 0;
1553
1554 cupsFilePuts(temp, "</Policy>\n");
1555 }
88f9aafc 1556 else if (!_cups_strcasecmp(line, "<Location"))
757d2cad 1557 {
1558 in_location = 1;
1559 indent += 2;
1560 if (!strcmp(value, "/admin"))
1561 in_admin_location = 1;
fcad6032 1562 else if (!strcmp(value, "/admin/conf"))
757d2cad 1563 in_conf_location = 1;
fcad6032
MS
1564 else if (!strcmp(value, "/admin/log"))
1565 in_log_location = 1;
757d2cad 1566 else if (!strcmp(value, "/"))
1567 in_root_location = 1;
1568
1569 cupsFilePrintf(temp, "%s %s>\n", line, value);
1570 }
88f9aafc 1571 else if (!_cups_strcasecmp(line, "</Location>"))
757d2cad 1572 {
1573 in_location = 0;
1574 indent -= 2;
b86bc4cf 1575 if (in_admin_location && remote_admin >= 0)
757d2cad 1576 {
1577 wrote_admin_location = 1;
1578
1579 if (remote_admin)
1580 cupsFilePuts(temp, " # Allow remote administration...\n");
b86bc4cf 1581 else if (remote_admin == 0)
757d2cad 1582 cupsFilePuts(temp, " # Restrict access to the admin pages...\n");
1583
1584 cupsFilePuts(temp, " Order allow,deny\n");
1585
1586 if (remote_admin)
f7deaa1a 1587 cupsFilePrintf(temp, " Allow %s\n",
1588 remote_any > 0 ? "all" : "@LOCAL");
757d2cad 1589 }
b86bc4cf 1590 else if (in_conf_location && remote_admin >= 0)
757d2cad 1591 {
1592 wrote_conf_location = 1;
1593
1594 if (remote_admin)
480ef0fe 1595 cupsFilePuts(temp, " # Allow remote access to the configuration "
1596 "files...\n");
757d2cad 1597 else
480ef0fe 1598 cupsFilePuts(temp, " # Restrict access to the configuration "
1599 "files...\n");
757d2cad 1600
1601 cupsFilePuts(temp, " Order allow,deny\n");
1602
1603 if (remote_admin)
f7deaa1a 1604 cupsFilePrintf(temp, " Allow %s\n",
1605 remote_any > 0 ? "all" : "@LOCAL");
757d2cad 1606 }
fcad6032
MS
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 }
ba55dc12 1624 else if (in_root_location &&
6720d4f4 1625 (remote_admin >= 0 || remote_any >= 0 || share_printers >= 0))
757d2cad 1626 {
1627 wrote_root_location = 1;
1628
b86bc4cf 1629 if (remote_admin > 0 && share_printers > 0)
480ef0fe 1630 cupsFilePuts(temp, " # Allow shared printing and remote "
1631 "administration...\n");
b86bc4cf 1632 else if (remote_admin > 0)
757d2cad 1633 cupsFilePuts(temp, " # Allow remote administration...\n");
b86bc4cf 1634 else if (share_printers > 0)
757d2cad 1635 cupsFilePuts(temp, " # Allow shared printing...\n");
ba55dc12
MS
1636 else if (remote_any > 0)
1637 cupsFilePuts(temp, " # Allow remote access...\n");
757d2cad 1638 else
1639 cupsFilePuts(temp, " # Restrict access to the server...\n");
1640
1641 cupsFilePuts(temp, " Order allow,deny\n");
1642
ba55dc12 1643 if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
f7deaa1a 1644 cupsFilePrintf(temp, " Allow %s\n",
1645 remote_any > 0 ? "all" : "@LOCAL");
757d2cad 1646 }
1647
1648 in_admin_location = 0;
1649 in_conf_location = 0;
fcad6032 1650 in_log_location = 0;
757d2cad 1651 in_root_location = 0;
1652
1653 cupsFilePuts(temp, "</Location>\n");
1654 }
88f9aafc 1655 else if (!_cups_strcasecmp(line, "<Limit"))
757d2cad 1656 {
7cf5915e 1657 if (in_default_policy)
757d2cad 1658 {
1659 /*
7cf5915e 1660 * See if the policy limit is for the Cancel-Job operation...
757d2cad 1661 */
88f9aafc 1662
7cf5915e 1663 char *valptr; /* Pointer into value */
88f9aafc
MS
1664
1665
1666 if (!_cups_strcasecmp(value, "cancel-job") && user_cancel_any >= 0)
757d2cad 1667 {
7cf5915e
MS
1668 /*
1669 * Don't write anything for this limit section...
1670 */
88f9aafc 1671
7cf5915e
MS
1672 in_cancel_job = 2;
1673 }
1674 else
1675 {
1676 cupsFilePrintf(temp, "%*s%s", indent, "", line);
88f9aafc 1677
7cf5915e 1678 while (*value)
757d2cad 1679 {
7cf5915e 1680 for (valptr = value; *valptr && !_cups_isspace(*valptr); valptr ++);
88f9aafc 1681
7cf5915e
MS
1682 if (*valptr)
1683 *valptr++ = '\0';
88f9aafc
MS
1684
1685 if (!_cups_strcasecmp(value, "cancel-job") && user_cancel_any >= 0)
7cf5915e
MS
1686 {
1687 /*
1688 * Write everything except for this definition...
1689 */
88f9aafc 1690
7cf5915e
MS
1691 in_cancel_job = 1;
1692 }
1693 else
1694 cupsFilePrintf(temp, " %s", value);
88f9aafc 1695
7cf5915e 1696 for (value = valptr; _cups_isspace(*value); value ++);
757d2cad 1697 }
88f9aafc 1698
7cf5915e 1699 cupsFilePuts(temp, ">\n");
757d2cad 1700 }
757d2cad 1701 }
7cf5915e
MS
1702 else
1703 cupsFilePrintf(temp, "%*s%s %s>\n", indent, "", line, value);
1704
1705 indent += 2;
757d2cad 1706 }
88f9aafc 1707 else if (!_cups_strcasecmp(line, "</Limit>") && in_cancel_job)
757d2cad 1708 {
1709 indent -= 2;
1710
1711 if (in_cancel_job == 1)
7cf5915e 1712 cupsFilePuts(temp, " </Limit>\n");
757d2cad 1713
1714 wrote_policy = 1;
1715
1716 if (!user_cancel_any)
480ef0fe 1717 cupsFilePuts(temp, " # Only the owner or an administrator can cancel "
7cf5915e
MS
1718 "a job...\n"
1719 " <Limit Cancel-Job>\n"
1720 " Order deny,allow\n"
1721 " Require user @OWNER "
b9faaae1 1722 CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n"
757d2cad 1723 " </Limit>\n");
1724
1725 in_cancel_job = 0;
1726 }
b86bc4cf 1727 else if ((((in_admin_location || in_conf_location || in_root_location) &&
6720d4f4 1728 (remote_admin >= 0 || remote_any >= 0)) ||
b86bc4cf 1729 (in_root_location && share_printers >= 0)) &&
88f9aafc
MS
1730 (!_cups_strcasecmp(line, "Allow") || !_cups_strcasecmp(line, "Deny") ||
1731 !_cups_strcasecmp(line, "Order")))
757d2cad 1732 continue;
1733 else if (in_cancel_job == 2)
1734 continue;
757d2cad 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 &&
7594b224 1751 (val = cupsGetOption(line, num_settings, settings)) != NULL)
757d2cad 1752 {
1753 /*
7594b224 1754 * Replace this directive's value with the new one...
757d2cad 1755 */
1756
7594b224 1757 cupsd_num_settings = cupsAddOption(line, val, cupsd_num_settings,
757d2cad 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
7594b224 1765 cupsFilePrintf(temp, "%s %s\n", line, val);
757d2cad 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
1106b00e 1774 * in cupsAdminGetServerSettings()...
757d2cad 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
a2326b5b 1791 if (!wrote_browsing && share_printers >= 0)
757d2cad 1792 {
a2326b5b 1793 if (share_printers > 0)
757d2cad 1794 {
a2326b5b 1795 cupsFilePuts(temp, "# Share local printers on the local network.\n");
757d2cad 1796 cupsFilePuts(temp, "Browsing On\n");
757d2cad 1797 }
1798 else
1799 {
1800 cupsFilePuts(temp, "# Disable printer sharing and shared printers.\n");
1801 cupsFilePuts(temp, "Browsing Off\n");
1802 }
1803 }
1804
b86bc4cf 1805 if (!wrote_loglevel && debug_logging >= 0)
757d2cad 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");
1f0275e3 1815 cupsFilePuts(temp, "LogLevel " CUPS_DEFAULT_LOG_LEVEL "\n");
757d2cad 1816 }
1817 }
1818
ba55dc12 1819 if (!wrote_port_listen &&
6720d4f4 1820 (remote_admin >= 0 || remote_any >= 0 || share_printers >= 0))
757d2cad 1821 {
ba55dc12 1822 if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
757d2cad 1823 {
1824 cupsFilePuts(temp, "# Allow remote access\n");
1825 cupsFilePrintf(temp, "Port %d\n", ippPort());
1826 }
1827 else
1828 {
480ef0fe 1829 cupsFilePuts(temp,
1830 "# Only listen for connections from the local machine.\n");
d09495fa 1831 cupsFilePrintf(temp, "Listen localhost:%d\n", ippPort());
757d2cad 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
ba55dc12 1840 if (!wrote_root_location &&
6720d4f4 1841 (remote_admin >= 0 || remote_any >= 0 || share_printers >= 0))
757d2cad 1842 {
b86bc4cf 1843 if (remote_admin > 0 && share_printers > 0)
480ef0fe 1844 cupsFilePuts(temp,
1845 "# Allow shared printing and remote administration...\n");
b86bc4cf 1846 else if (remote_admin > 0)
757d2cad 1847 cupsFilePuts(temp, "# Allow remote administration...\n");
b86bc4cf 1848 else if (share_printers > 0)
757d2cad 1849 cupsFilePuts(temp, "# Allow shared printing...\n");
ba55dc12
MS
1850 else if (remote_any > 0)
1851 cupsFilePuts(temp, "# Allow remote access...\n");
757d2cad 1852 else
1853 cupsFilePuts(temp, "# Restrict access to the server...\n");
1854
1855 cupsFilePuts(temp, "<Location />\n"
1856 " Order allow,deny\n");
1857
ba55dc12 1858 if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
f7deaa1a 1859 cupsFilePrintf(temp, " Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
757d2cad 1860
1861 cupsFilePuts(temp, "</Location>\n");
1862 }
1863
b86bc4cf 1864 if (!wrote_admin_location && remote_admin >= 0)
757d2cad 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)
f7deaa1a 1875 cupsFilePrintf(temp, " Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
757d2cad 1876
1877 cupsFilePuts(temp, "</Location>\n");
1878 }
1879
b86bc4cf 1880 if (!wrote_conf_location && remote_admin >= 0)
757d2cad 1881 {
1882 if (remote_admin)
480ef0fe 1883 cupsFilePuts(temp,
1884 "# Allow remote access to the configuration files...\n");
757d2cad 1885 else
1886 cupsFilePuts(temp, "# Restrict access to the configuration files...\n");
1887
1888 cupsFilePuts(temp, "<Location /admin/conf>\n"
0a682745 1889 " AuthType Default\n"
fcad6032
MS
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"
757d2cad 1909 " Require user @SYSTEM\n"
1910 " Order allow,deny\n");
1911
1912 if (remote_admin)
f7deaa1a 1913 cupsFilePrintf(temp, " Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
757d2cad 1914
1915 cupsFilePuts(temp, "</Location>\n");
1916 }
1917
b86bc4cf 1918 if (!wrote_policy && user_cancel_any >= 0)
757d2cad 1919 {
1920 cupsFilePuts(temp, "<Policy default>\n"
480ef0fe 1921 " # Job-related operations must be done by the owner "
355e94dc 1922 "or an administrator...\n"
757d2cad 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"
480ef0fe 1932 " # All administration operations require an "
355e94dc 1933 "administrator to authenticate...\n"
757d2cad 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"
0a682745 1944 " AuthType Default\n"
757d2cad 1945 " Require user @SYSTEM\n"
1946 " Order deny,allow\n"
1947 "</Limit>\n");
1948
1949 if (!user_cancel_any)
480ef0fe 1950 cupsFilePuts(temp, " # Only the owner or an administrator can cancel "
1951 "a job...\n"
757d2cad 1952 " <Limit Cancel-Job>\n"
757d2cad 1953 " Order deny,allow\n"
355e94dc 1954 " Require user @OWNER "
b9faaae1 1955 CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n"
757d2cad 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] != '_' &&
88f9aafc
MS
1966 _cups_strcasecmp(setting->name, "Listen") &&
1967 _cups_strcasecmp(setting->name, "Port") &&
757d2cad 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 /*
7594b224 1978 * Write the new value, without indentation since we only support
1979 * setting root directives, not in sections...
757d2cad 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
cb7f98ee 1994 if (status == HTTP_STATUS_CREATED)
757d2cad 1995 {
1996 /*
1997 * Updated OK, add the basic settings...
1998 */
1999
b86bc4cf 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
6720d4f4
MS
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);
f7deaa1a 2026
b86bc4cf 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);
757d2cad 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
cb7f98ee 2069 return (status == HTTP_STATUS_CREATED);
757d2cad 2070}
2071
2072
2073/*
2074 * 'do_samba_command()' - Do a SAMBA command.
2075 */
2076
2077static int /* O - Status of command */
2078do_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{
19dc16f7 2084#ifdef _WIN32
b86bc4cf 2085 return (1); /* Always fail on Windows... */
2086
2087#else
757d2cad 2088 int status; /* Status of command */
2089 int pid; /* Process ID of child */
2090
2091
2092 if (logfile)
2093 _cupsLangPrintf(logfile,
0837b7e8 2094 _("Running command: %s %s -N -A %s -c \'%s\'"),
757d2cad 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
97c9a8d7 2103 int fd = open("/dev/null", O_RDONLY);
757d2cad 2104
97c9a8d7
MS
2105 if (fd > 0)
2106 {
2107 dup2(fd, 0);
2108 close(fd);
2109 }
757d2cad 2110
2111 if (logfile)
97c9a8d7
MS
2112 dup2(fileno(logfile), 1);
2113 else if ((fd = open("/dev/null", O_WRONLY)) > 1)
2114 {
2115 dup2(fd, 1);
2116 close(fd);
2117 }
757d2cad 2118
97c9a8d7 2119 dup2(1, 2);
757d2cad 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
8ca02f3c 2129 if (logfile)
0837b7e8 2130 _cupsLangPrintf(logfile, _("Unable to run \"%s\": %s"),
8ca02f3c 2131 command, strerror(errno));
757d2cad 2132 }
2133 else
2134 {
2135 /*
2136 * Wait for the process to complete...
2137 */
2138
2139 while (wait(&status) != pid);
2140 }
2141
2142 if (logfile)
0837b7e8 2143 _cupsLangPuts(logfile, "");
757d2cad 2144
e07d4801 2145 DEBUG_printf(("9do_samba_command: status=%d", status));
757d2cad 2146
2147 if (WIFEXITED(status))
2148 return (WEXITSTATUS(status));
2149 else
2150 return (-WTERMSIG(status));
19dc16f7 2151#endif /* _WIN32 */
757d2cad 2152}
2153
2154
2155/*
2156 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
2157 */
2158
2159static http_status_t /* O - Status of request */
2160get_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 */
07623986 2165 size_t namesize, /* I - Size of filename buffer */
757d2cad 2166 int *remote) /* O - Remote file? */
2167{
2168 int fd; /* Temporary file descriptor */
19dc16f7 2169#ifndef _WIN32
757d2cad 2170 struct stat info; /* cupsd.conf file information */
19dc16f7 2171#endif /* _WIN32 */
757d2cad 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
88f9aafc 2182 if (_cups_strcasecmp(cg->cupsd_hostname, host))
757d2cad 2183 invalidate_cupsd_cache(cg);
2184
2185 snprintf(name, namesize, "%s/cupsd.conf", cg->cups_serverroot);
2186 *remote = 0;
2187
19dc16f7 2188#ifndef _WIN32
88f9aafc 2189 if (!_cups_strcasecmp(host, "localhost") && !access(name, R_OK))
757d2cad 2190 {
2191 /*
2192 * Read the local file rather than using HTTP...
2193 */
2194
2195 if (stat(name, &info))
2196 {
480ef0fe 2197 char message[1024]; /* Message string */
757d2cad 2198
480ef0fe 2199
2200 snprintf(message, sizeof(message),
2201 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2202 name, strerror(errno));
cb7f98ee 2203 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
480ef0fe 2204
2205 *name = '\0';
757d2cad 2206
cb7f98ee 2207 return (HTTP_STATUS_SERVER_ERROR);
757d2cad 2208 }
2209 else if (last_update && info.st_mtime <= last_update)
cb7f98ee 2210 status = HTTP_STATUS_NOT_MODIFIED;
757d2cad 2211 else
cb7f98ee 2212 status = HTTP_STATUS_OK;
757d2cad 2213 }
2214 else
19dc16f7 2215#endif /* !_WIN32 */
757d2cad 2216 {
2217 /*
2218 * Read cupsd.conf via a HTTP GET request...
2219 */
2220
df0f06ae 2221 if ((fd = cupsTempFd(name, (int)namesize)) < 0)
757d2cad 2222 {
2223 *name = '\0';
2224
cb7f98ee 2225 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
757d2cad 2226
2227 invalidate_cupsd_cache(cg);
2228
cb7f98ee 2229 return (HTTP_STATUS_SERVER_ERROR);
757d2cad 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
cb7f98ee 2244 if (status != HTTP_STATUS_OK)
757d2cad 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
2259static void
2260invalidate_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
2276static void
2277write_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}