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