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