• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Administration utility API definitions for CUPS.
3  *
4  * Copyright © 2020-2024 by OpenPrinting.
5  * Copyright © 2007-2019 by Apple Inc.
6  * Copyright © 2001-2007 by Easy Software Products.
7  *
8  * Licensed under Apache License v2.0.  See the file "LICENSE" for more
9  * information.
10  */
11 
12 /*
13  * Include necessary headers...
14  */
15 
16 #include "cups-private.h"
17 #include "debug-internal.h"
18 #include "ppd.h"
19 #include "adminutil.h"
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #ifndef _WIN32
23 #  include <unistd.h>
24 #  include <sys/wait.h>
25 #endif /* !_WIN32 */
26 
27 
28 /*
29  * Local functions...
30  */
31 
32 static http_status_t	get_cupsd_conf(http_t *http, _cups_globals_t *cg,
33 			               time_t last_update, char *name,
34 				       size_t namelen, int *remote);
35 static void		invalidate_cupsd_cache(_cups_globals_t *cg);
36 
37 
38 /*
39  * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
40  *
41  * @deprecated@
42  */
43 
44 char *					/* O - PPD file or NULL */
cupsAdminCreateWindowsPPD(http_t * http,const char * dest,char * buffer,int bufsize)45 cupsAdminCreateWindowsPPD(
46     http_t     *http,			/* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
47     const char *dest,			/* I - Printer or class */
48     char       *buffer,			/* I - Filename buffer */
49     int        bufsize)			/* I - Size of filename buffer */
50 {
51   (void)http;
52   (void)dest;
53   (void)bufsize;
54 
55   if (buffer)
56     *buffer = '\0';
57 
58   return (NULL);
59 }
60 
61 
62 /*
63  * 'cupsAdminExportSamba()' - Export a printer to Samba.
64  *
65  * @deprecated@
66  */
67 
68 int					/* O - 1 on success, 0 on failure */
cupsAdminExportSamba(const char * dest,const char * ppd,const char * samba_server,const char * samba_user,const char * samba_password,FILE * logfile)69 cupsAdminExportSamba(
70     const char *dest,			/* I - Destination to export */
71     const char *ppd,			/* I - PPD file */
72     const char *samba_server,		/* I - Samba server */
73     const char *samba_user,		/* I - Samba username */
74     const char *samba_password,		/* I - Samba password */
75     FILE       *logfile)		/* I - Log file, if any */
76 {
77   (void)dest;
78   (void)ppd;
79   (void)samba_server;
80   (void)samba_user;
81   (void)samba_password;
82   (void)logfile;
83 
84   return (0);
85 }
86 
87 
88 /*
89  * 'cupsAdminGetServerSettings()' - Get settings from the server.
90  *
91  * The returned settings should be freed with cupsFreeOptions() when
92  * you are done with them.
93  *
94  * @since CUPS 1.3/macOS 10.5@
95  */
96 
97 int					/* O - 1 on success, 0 on failure */
cupsAdminGetServerSettings(http_t * http,int * num_settings,cups_option_t ** settings)98 cupsAdminGetServerSettings(
99     http_t        *http,		/* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
100     int           *num_settings,	/* O - Number of settings */
101     cups_option_t **settings)		/* O - Settings */
102 {
103   int		i;			/* Looping var */
104   cups_file_t	*cupsd;			/* cupsd.conf file */
105   char		cupsdconf[1024];	/* cupsd.conf filename */
106   int		remote;			/* Remote cupsd.conf file? */
107   http_status_t	status;			/* Status of getting cupsd.conf */
108   char		line[1024],		/* Line from cupsd.conf file */
109 		*value;			/* Value on line */
110   cups_option_t	*setting;		/* Current setting */
111   _cups_globals_t *cg = _cupsGlobals();	/* Global data */
112 
113 
114  /*
115   * Range check input...
116   */
117 
118   if (!http)
119   {
120    /*
121     * See if we are connected to the same server...
122     */
123 
124     if (cg->http)
125     {
126      /*
127       * Compare the connection hostname, port, and encryption settings to
128       * the cached defaults; these were initialized the first time we
129       * connected...
130       */
131 
132       if (strcmp(cg->http->hostname, cg->server) ||
133           cg->ipp_port != httpAddrPort(cg->http->hostaddr) ||
134 	  (cg->http->encryption != cg->encryption &&
135 	   cg->http->encryption == HTTP_ENCRYPTION_NEVER))
136       {
137        /*
138 	* Need to close the current connection because something has changed...
139 	*/
140 
141 	httpClose(cg->http);
142 	cg->http = NULL;
143       }
144     }
145 
146    /*
147     * (Re)connect as needed...
148     */
149 
150     if (!cg->http)
151     {
152       if ((cg->http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC,
153                                    cupsEncryption(), 1, 0, NULL)) == NULL)
154       {
155 	if (errno)
156 	  _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, NULL, 0);
157 	else
158 	  _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE,
159 			_("Unable to connect to host."), 1);
160 
161 	if (num_settings)
162 	  *num_settings = 0;
163 
164 	if (settings)
165 	  *settings = NULL;
166 
167 	return (0);
168       }
169     }
170 
171     http = cg->http;
172   }
173 
174   if (!http || !num_settings || !settings)
175   {
176     _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
177 
178     if (num_settings)
179       *num_settings = 0;
180 
181     if (settings)
182       *settings = NULL;
183 
184     return (0);
185   }
186 
187   *num_settings = 0;
188   *settings     = NULL;
189 
190  /*
191   * Get the cupsd.conf file...
192   */
193 
194   if ((status = get_cupsd_conf(http, cg, cg->cupsd_update, cupsdconf,
195                                sizeof(cupsdconf), &remote)) == HTTP_STATUS_OK)
196   {
197     if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL)
198     {
199       char	message[1024];		/* Message string */
200 
201 
202       snprintf(message, sizeof(message),
203                _cupsLangString(cupsLangDefault(), _("Open of %s failed: %s")),
204                cupsdconf, strerror(errno));
205       _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
206     }
207   }
208   else
209     cupsd = NULL;
210 
211   if (cupsd)
212   {
213    /*
214     * Read the file, keeping track of what settings are enabled...
215     */
216 
217     int		remote_access = 0,	/* Remote access allowed? */
218 		remote_admin = 0,	/* Remote administration allowed? */
219 		remote_any = 0,		/* Remote access from anywhere allowed? */
220 		browsing = 1,		/* Browsing enabled? */
221 		cancel_policy = 1,	/* Cancel-job policy set? */
222 		debug_logging = 0;	/* LogLevel debug set? */
223     int		linenum = 0,		/* Line number in file */
224 		in_location = 0,	/* In a location section? */
225 		in_policy = 0,		/* In a policy section? */
226 		in_cancel_job = 0,	/* In a cancel-job section? */
227 		in_admin_location = 0;	/* In the /admin location? */
228 
229 
230     invalidate_cupsd_cache(cg);
231 
232     cg->cupsd_update = time(NULL);
233     httpGetHostname(http, cg->cupsd_hostname, sizeof(cg->cupsd_hostname));
234 
235     while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum))
236     {
237       if (!value && strncmp(line, "</", 2))
238         value = line + strlen(line);
239 
240       if ((!_cups_strcasecmp(line, "Port") || !_cups_strcasecmp(line, "Listen")) && value)
241       {
242 	char	*port;			/* Pointer to port number, if any */
243 
244 
245 	if ((port = strrchr(value, ':')) != NULL)
246 	  *port = '\0';
247 	else if (isdigit(*value & 255))
248 	{
249 	 /*
250 	  * Listen on a port number implies remote access...
251 	  */
252 
253 	  remote_access = 1;
254 	  continue;
255 	}
256 
257 	if (_cups_strcasecmp(value, "localhost") && strcmp(value, "127.0.0.1")
258 #ifdef AF_LOCAL
259             && *value != '/'
260 #endif /* AF_LOCAL */
261 #ifdef AF_INET6
262             && strcmp(value, "[::1]")
263 #endif /* AF_INET6 */
264 	    )
265 	  remote_access = 1;
266       }
267       else if (!_cups_strcasecmp(line, "Browsing") && value)
268       {
269 	browsing = !_cups_strcasecmp(value, "yes") ||
270 	           !_cups_strcasecmp(value, "on") ||
271 	           !_cups_strcasecmp(value, "true");
272       }
273       else if (!_cups_strcasecmp(line, "LogLevel") && value)
274       {
275 	debug_logging = !_cups_strncasecmp(value, "debug", 5);
276       }
277       else if (!_cups_strcasecmp(line, "<Policy") && value && !_cups_strcasecmp(value, "default"))
278       {
279 	in_policy = 1;
280       }
281       else if (!_cups_strcasecmp(line, "</Policy>"))
282       {
283 	in_policy = 0;
284       }
285       else if (!_cups_strcasecmp(line, "<Limit") && in_policy && value)
286       {
287        /*
288 	* See if the policy limit is for the Cancel-Job operation...
289 	*/
290 
291 	char	*valptr;		/* Pointer into value */
292 
293 
294 	while (*value)
295 	{
296 	  for (valptr = value; *valptr && !_cups_isspace(*valptr); valptr ++);
297 
298 	  if (*valptr)
299 	    *valptr++ = '\0';
300 
301           if (!_cups_strcasecmp(value, "cancel-job") ||
302               !_cups_strcasecmp(value, "all"))
303 	  {
304 	    in_cancel_job = 1;
305 	    break;
306 	  }
307 
308           for (value = valptr; _cups_isspace(*value); value ++);
309 	}
310       }
311       else if (!_cups_strcasecmp(line, "</Limit>"))
312       {
313 	in_cancel_job = 0;
314       }
315       else if (!_cups_strcasecmp(line, "Require") && in_cancel_job)
316       {
317 	cancel_policy = 0;
318       }
319       else if (!_cups_strcasecmp(line, "<Location") && value)
320       {
321         in_admin_location = !_cups_strcasecmp(value, "/admin");
322 	in_location       = 1;
323       }
324       else if (!_cups_strcasecmp(line, "</Location>"))
325       {
326 	in_admin_location = 0;
327 	in_location       = 0;
328       }
329       else if (!_cups_strcasecmp(line, "Allow") && value &&
330                _cups_strcasecmp(value, "localhost") &&
331                _cups_strcasecmp(value, "127.0.0.1")
332 #ifdef AF_LOCAL
333 	       && *value != '/'
334 #endif /* AF_LOCAL */
335 #ifdef AF_INET6
336 	       && strcmp(value, "::1")
337 #endif /* AF_INET6 */
338 	       )
339       {
340         if (in_admin_location)
341 	  remote_admin = 1;
342         else if (!_cups_strcasecmp(value, "all"))
343 	  remote_any = 1;
344       }
345       else if (line[0] != '<' && !in_location && !in_policy &&
346 	       _cups_strcasecmp(line, "Allow") &&
347                _cups_strcasecmp(line, "AuthType") &&
348 	       _cups_strcasecmp(line, "Deny") &&
349 	       _cups_strcasecmp(line, "Order") &&
350 	       _cups_strcasecmp(line, "Require") &&
351 	       _cups_strcasecmp(line, "Satisfy"))
352         cg->cupsd_num_settings = cupsAddOption(line, value,
353 	                                       cg->cupsd_num_settings,
354 					       &(cg->cupsd_settings));
355     }
356 
357     cupsFileClose(cupsd);
358 
359     cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
360                                            debug_logging ? "1" : "0",
361 					   cg->cupsd_num_settings,
362 					   &(cg->cupsd_settings));
363 
364     cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
365                                            (remote_access && remote_admin) ?
366 					       "1" : "0",
367 					   cg->cupsd_num_settings,
368 					   &(cg->cupsd_settings));
369 
370     cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY,
371                                            remote_any ? "1" : "0",
372 					   cg->cupsd_num_settings,
373 					   &(cg->cupsd_settings));
374 
375     cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
376                                            (remote_access && browsing) ? "1" :
377                                                                          "0",
378 					   cg->cupsd_num_settings,
379 					   &(cg->cupsd_settings));
380 
381     cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
382                                            cancel_policy ? "1" : "0",
383 					   cg->cupsd_num_settings,
384 					   &(cg->cupsd_settings));
385   }
386   else if (status != HTTP_STATUS_NOT_MODIFIED)
387     invalidate_cupsd_cache(cg);
388 
389  /*
390   * Remove any temporary files and copy the settings array...
391   */
392 
393   if (remote)
394     unlink(cupsdconf);
395 
396   for (i = cg->cupsd_num_settings, setting = cg->cupsd_settings;
397        i > 0;
398        i --, setting ++)
399     *num_settings = cupsAddOption(setting->name, setting->value,
400                                   *num_settings, settings);
401 
402   return (cg->cupsd_num_settings > 0);
403 }
404 
405 
406 /*
407  * 'cupsAdminSetServerSettings()' - Set settings on the server.
408  *
409  * @since CUPS 1.3/macOS 10.5@
410  */
411 
412 int					/* O - 1 on success, 0 on failure */
cupsAdminSetServerSettings(http_t * http,int num_settings,cups_option_t * settings)413 cupsAdminSetServerSettings(
414     http_t        *http,		/* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
415     int           num_settings,		/* I - Number of settings */
416     cups_option_t *settings)		/* I - Settings */
417 {
418   int		i;			/* Looping var */
419   http_status_t status;			/* GET/PUT status */
420   const char	*server_port_env;	/* SERVER_PORT env var */
421   int		server_port;		/* IPP port for server */
422   cups_file_t	*cupsd;			/* cupsd.conf file */
423   char		cupsdconf[1024];	/* cupsd.conf filename */
424   int		remote;			/* Remote cupsd.conf file? */
425   char		tempfile[1024];		/* Temporary new cupsd.conf */
426   cups_file_t	*temp;			/* Temporary file */
427   char		line[1024],		/* Line from cupsd.conf file */
428 		*value;			/* Value on line */
429   int		linenum,		/* Line number in file */
430 		in_location,		/* In a location section? */
431 		in_policy,		/* In a policy section? */
432 		in_default_policy,	/* In the default policy section? */
433 		in_cancel_job,		/* In a cancel-job section? */
434 		in_admin_location,	/* In the /admin location? */
435 		in_conf_location,	/* In the /admin/conf location? */
436 		in_log_location,	/* In the /admin/log location? */
437 		in_root_location;	/* In the / location? */
438   const char	*val;			/* Setting value */
439   int		share_printers,		/* Share local printers */
440 		remote_admin,		/* Remote administration allowed? */
441 		remote_any,		/* Remote access from anywhere? */
442 		user_cancel_any,	/* Cancel-job policy set? */
443 		debug_logging;		/* LogLevel debug set? */
444   int		wrote_port_listen,	/* Wrote the port/listen lines? */
445 		wrote_browsing,		/* Wrote the browsing lines? */
446 		wrote_policy,		/* Wrote the policy? */
447 		wrote_loglevel,		/* Wrote the LogLevel line? */
448 		wrote_admin_location,	/* Wrote the /admin location? */
449 		wrote_conf_location,	/* Wrote the /admin/conf location? */
450 		wrote_log_location,	/* Wrote the /admin/log location? */
451 		wrote_root_location;	/* Wrote the / location? */
452   int		indent;			/* Indentation */
453   int		cupsd_num_settings;	/* New number of settings */
454   int		old_share_printers,	/* Share local printers */
455 		old_remote_admin,	/* Remote administration allowed? */
456 		old_remote_any,		/* Remote access from anywhere? */
457 		old_user_cancel_any,	/* Cancel-job policy set? */
458 		old_debug_logging;	/* LogLevel debug set? */
459   cups_option_t	*cupsd_settings,	/* New settings */
460 		*setting;		/* Current setting */
461   _cups_globals_t *cg = _cupsGlobals();	/* Global data */
462 
463 
464  /*
465   * Range check input...
466   */
467 
468   if (!http)
469     http = _cupsConnect();
470 
471   if (!http || !num_settings || !settings)
472   {
473     _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
474 
475     return (0);
476   }
477 
478  /*
479   * Get the cupsd.conf file...
480   */
481 
482   if (get_cupsd_conf(http, cg, 0, cupsdconf, sizeof(cupsdconf),
483                      &remote) == HTTP_STATUS_OK)
484   {
485     if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL)
486     {
487       _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
488       return (0);
489     }
490   }
491   else
492     return (0);
493 
494  /*
495   * Get current settings...
496   */
497 
498   if (!cupsAdminGetServerSettings(http, &cupsd_num_settings,
499 				  &cupsd_settings))
500     return (0);
501 
502   if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, cupsd_num_settings,
503                            cupsd_settings)) != NULL)
504     old_debug_logging = atoi(val);
505   else
506     old_debug_logging = 0;
507 
508   DEBUG_printf(("1cupsAdminSetServerSettings: old debug_logging=%d",
509                 old_debug_logging));
510 
511   if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, cupsd_num_settings,
512                            cupsd_settings)) != NULL)
513     old_remote_admin = atoi(val);
514   else
515     old_remote_admin = 0;
516 
517   DEBUG_printf(("1cupsAdminSetServerSettings: old remote_admin=%d",
518                 old_remote_admin));
519 
520   if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, cupsd_num_settings,
521                            cupsd_settings)) != NULL)
522     old_remote_any = atoi(val);
523   else
524     old_remote_any = 0;
525 
526   DEBUG_printf(("1cupsAdminSetServerSettings: old remote_any=%d",
527                 old_remote_any));
528 
529   if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, cupsd_num_settings,
530                            cupsd_settings)) != NULL)
531     old_share_printers = atoi(val);
532   else
533     old_share_printers = 0;
534 
535   DEBUG_printf(("1cupsAdminSetServerSettings: old share_printers=%d",
536                 old_share_printers));
537 
538   if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, cupsd_num_settings,
539                            cupsd_settings)) != NULL)
540     old_user_cancel_any = atoi(val);
541   else
542     old_user_cancel_any = 0;
543 
544   DEBUG_printf(("1cupsAdminSetServerSettings: old user_cancel_any=%d",
545                 old_user_cancel_any));
546 
547   cupsFreeOptions(cupsd_num_settings, cupsd_settings);
548 
549  /*
550   * Get basic settings...
551   */
552 
553   if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, num_settings,
554                            settings)) != NULL)
555   {
556     debug_logging = atoi(val);
557 
558     if (debug_logging == old_debug_logging)
559     {
560      /*
561       * No change to this setting...
562       */
563 
564       debug_logging = -1;
565     }
566   }
567   else
568     debug_logging = -1;
569 
570   DEBUG_printf(("1cupsAdminSetServerSettings: debug_logging=%d",
571                 debug_logging));
572 
573   if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, num_settings, settings)) != NULL)
574   {
575     remote_any = atoi(val);
576 
577     if (remote_any == old_remote_any)
578     {
579      /*
580       * No change to this setting...
581       */
582 
583       remote_any = -1;
584     }
585   }
586   else
587     remote_any = -1;
588 
589   DEBUG_printf(("1cupsAdminSetServerSettings: remote_any=%d", remote_any));
590 
591   if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, num_settings,
592                            settings)) != NULL)
593   {
594     remote_admin = atoi(val);
595 
596     if (remote_admin == old_remote_admin)
597     {
598      /*
599       * No change to this setting...
600       */
601 
602       remote_admin = -1;
603     }
604   }
605   else
606     remote_admin = -1;
607 
608   DEBUG_printf(("1cupsAdminSetServerSettings: remote_admin=%d",
609                 remote_admin));
610 
611   if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, num_settings,
612                            settings)) != NULL)
613   {
614     share_printers = atoi(val);
615 
616     if (share_printers == old_share_printers)
617     {
618      /*
619       * No change to this setting...
620       */
621 
622       share_printers = -1;
623     }
624   }
625   else
626     share_printers = -1;
627 
628   DEBUG_printf(("1cupsAdminSetServerSettings: share_printers=%d",
629                 share_printers));
630 
631   if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, num_settings,
632                            settings)) != NULL)
633   {
634     user_cancel_any = atoi(val);
635 
636     if (user_cancel_any == old_user_cancel_any)
637     {
638      /*
639       * No change to this setting...
640       */
641 
642       user_cancel_any = -1;
643     }
644   }
645   else
646     user_cancel_any = -1;
647 
648   DEBUG_printf(("1cupsAdminSetServerSettings: user_cancel_any=%d",
649                 user_cancel_any));
650 
651  /*
652   * Create a temporary file for the new cupsd.conf file...
653   */
654 
655   if ((temp = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
656   {
657     cupsFileClose(cupsd);
658 
659     if (remote)
660       unlink(cupsdconf);
661 
662     _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
663     return (0);
664   }
665 
666  /*
667   * Copy the old file to the new, making changes along the way...
668   */
669 
670   cupsd_num_settings   = 0;
671   in_admin_location    = 0;
672   in_cancel_job        = 0;
673   in_conf_location     = 0;
674   in_default_policy    = 0;
675   in_location          = 0;
676   in_log_location      = 0;
677   in_policy            = 0;
678   in_root_location     = 0;
679   linenum              = 0;
680   wrote_admin_location = 0;
681   wrote_browsing       = 0;
682   wrote_conf_location  = 0;
683   wrote_log_location   = 0;
684   wrote_loglevel       = 0;
685   wrote_policy         = 0;
686   wrote_port_listen    = 0;
687   wrote_root_location  = 0;
688   indent               = 0;
689 
690   if ((server_port_env = getenv("SERVER_PORT")) != NULL)
691   {
692     if ((server_port = atoi(server_port_env)) <= 0)
693       server_port = ippPort();
694   }
695   else
696     server_port = ippPort();
697 
698   if (server_port <= 0)
699     server_port = IPP_PORT;
700 
701   while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum))
702   {
703     if ((!_cups_strcasecmp(line, "Port") || !_cups_strcasecmp(line, "Listen")) &&
704         (remote_admin >= 0 || remote_any >= 0 || share_printers >= 0))
705     {
706       if (!wrote_port_listen)
707       {
708         wrote_port_listen = 1;
709 
710 	if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
711 	{
712 	  cupsFilePuts(temp, "# Allow remote access\n");
713 	  cupsFilePrintf(temp, "Port %d\n", server_port);
714 	}
715 	else
716 	{
717 	  cupsFilePuts(temp, "# Only listen for connections from the local "
718 	                     "machine.\n");
719 	  cupsFilePrintf(temp, "Listen localhost:%d\n", server_port);
720 	}
721 
722 #ifdef CUPS_DEFAULT_DOMAINSOCKET
723         if ((!value || strcmp(CUPS_DEFAULT_DOMAINSOCKET, value)) &&
724 	    !access(CUPS_DEFAULT_DOMAINSOCKET, 0))
725           cupsFilePuts(temp, "Listen " CUPS_DEFAULT_DOMAINSOCKET "\n");
726 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
727       }
728       else if (value && value[0] == '/'
729 #ifdef CUPS_DEFAULT_DOMAINSOCKET
730                && strcmp(CUPS_DEFAULT_DOMAINSOCKET, value)
731 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
732                )
733         cupsFilePrintf(temp, "Listen %s\n", value);
734     }
735     else if ((!_cups_strcasecmp(line, "Browsing") ||
736               !_cups_strcasecmp(line, "BrowseLocalProtocols")) &&
737 	     share_printers >= 0)
738     {
739       if (!wrote_browsing)
740       {
741         wrote_browsing = 1;
742 
743         if (share_printers)
744 	{
745 	  const char *localp = cupsGetOption("BrowseLocalProtocols",
746 					     num_settings, settings);
747 
748           if (!localp || !localp[0])
749 	    localp = cupsGetOption("BrowseLocalProtocols", cupsd_num_settings,
750 	                           cupsd_settings);
751 
752 	  cupsFilePuts(temp, "# Share local printers on the local network.\n");
753 	  cupsFilePuts(temp, "Browsing On\n");
754 
755 	  if (!localp)
756 	    localp = CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS;
757 
758 	  cupsFilePrintf(temp, "BrowseLocalProtocols %s\n", localp);
759 
760 	  cupsd_num_settings = cupsAddOption("BrowseLocalProtocols", localp,
761 					     cupsd_num_settings,
762 					     &cupsd_settings);
763         }
764 	else
765 	{
766 	  cupsFilePuts(temp, "# Disable printer sharing.\n");
767 	  cupsFilePuts(temp, "Browsing Off\n");
768 	}
769       }
770     }
771     else if (!_cups_strcasecmp(line, "LogLevel") && debug_logging >= 0)
772     {
773       wrote_loglevel = 1;
774 
775       if (debug_logging)
776       {
777         cupsFilePuts(temp,
778 	             "# Show troubleshooting information in error_log.\n");
779 	cupsFilePuts(temp, "LogLevel debug\n");
780       }
781       else
782       {
783         cupsFilePuts(temp, "# Show general information in error_log.\n");
784 	cupsFilePuts(temp, "LogLevel " CUPS_DEFAULT_LOG_LEVEL "\n");
785       }
786     }
787     else if (!_cups_strcasecmp(line, "<Policy"))
788     {
789       in_default_policy = !_cups_strcasecmp(value, "default");
790       in_policy         = 1;
791 
792       cupsFilePrintf(temp, "%s %s>\n", line, value);
793       indent += 2;
794     }
795     else if (!_cups_strcasecmp(line, "</Policy>"))
796     {
797       indent -= 2;
798       if (!wrote_policy && in_default_policy)
799       {
800 	wrote_policy = 1;
801 
802         if (!user_cancel_any)
803 	  cupsFilePuts(temp, "  # Only the owner or an administrator can "
804 	                     "cancel a job...\n"
805 	                     "  <Limit Cancel-Job>\n"
806 	                     "    Order deny,allow\n"
807 			     "    Require user @OWNER "
808 			     CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n"
809 			     "  </Limit>\n");
810       }
811 
812       in_policy         = 0;
813       in_default_policy = 0;
814 
815       cupsFilePuts(temp, "</Policy>\n");
816     }
817     else if (!_cups_strcasecmp(line, "<Location"))
818     {
819       in_location = 1;
820       indent += 2;
821       if (!strcmp(value, "/admin"))
822 	in_admin_location = 1;
823       else if (!strcmp(value, "/admin/conf"))
824 	in_conf_location = 1;
825       else if (!strcmp(value, "/admin/log"))
826 	in_log_location = 1;
827       else if (!strcmp(value, "/"))
828 	in_root_location = 1;
829 
830       cupsFilePrintf(temp, "%s %s>\n", line, value);
831     }
832     else if (!_cups_strcasecmp(line, "</Location>"))
833     {
834       in_location = 0;
835       indent -= 2;
836       if (in_admin_location && remote_admin >= 0)
837       {
838 	wrote_admin_location = 1;
839 
840 	if (remote_admin)
841           cupsFilePuts(temp, "  # Allow remote administration...\n");
842 	else
843           cupsFilePuts(temp, "  # Restrict access to the admin pages...\n");
844 
845         cupsFilePuts(temp, "  Order allow,deny\n");
846 
847 	if (remote_admin)
848 	{
849 	  if (remote_any >= 0)
850 	    cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
851 	  else
852 	    cupsFilePrintf(temp, "  Allow %s\n", old_remote_any > 0 ? "all" : "@LOCAL");
853 	}
854       }
855       else if (in_conf_location && remote_admin >= 0)
856       {
857 	wrote_conf_location = 1;
858 
859 	if (remote_admin)
860           cupsFilePuts(temp, "  # Allow remote access to the configuration "
861 	                     "files...\n");
862 	else
863           cupsFilePuts(temp, "  # Restrict access to the configuration "
864 	                     "files...\n");
865 
866         cupsFilePuts(temp, "  Order allow,deny\n");
867 
868 	if (remote_admin)
869 	{
870 	  if (remote_any >= 0)
871 	    cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
872 	  else
873 	    cupsFilePrintf(temp, "  Allow %s\n", old_remote_any > 0 ? "all" : "@LOCAL");
874 	}
875       }
876       else if (in_log_location && remote_admin >= 0)
877       {
878 	wrote_log_location = 1;
879 
880 	if (remote_admin)
881           cupsFilePuts(temp, "  # Allow remote access to the log "
882 	                     "files...\n");
883 	else
884           cupsFilePuts(temp, "  # Restrict access to the log "
885 	                     "files...\n");
886 
887         cupsFilePuts(temp, "  Order allow,deny\n");
888 
889 	if (remote_admin)
890 	{
891 	  if (remote_any >= 0)
892 	    cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
893 	  else
894 	    cupsFilePrintf(temp, "  Allow %s\n", old_remote_any > 0 ? "all" : "@LOCAL");
895 	}
896       }
897       else if (in_root_location &&
898                (remote_admin >= 0 || remote_any >= 0 || share_printers >= 0))
899       {
900 	wrote_root_location = 1;
901 
902 	if (remote_admin > 0 && share_printers > 0)
903           cupsFilePuts(temp, "  # Allow shared printing and remote "
904 	                     "administration...\n");
905 	else if (remote_admin > 0)
906           cupsFilePuts(temp, "  # Allow remote administration...\n");
907 	else if (share_printers > 0)
908           cupsFilePuts(temp, "  # Allow shared printing...\n");
909 	else if (remote_any > 0)
910           cupsFilePuts(temp, "  # Allow remote access...\n");
911 	else
912           cupsFilePuts(temp, "  # Restrict access to the server...\n");
913 
914         cupsFilePuts(temp, "  Order allow,deny\n");
915 
916 	if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
917 	{
918 	  if (remote_any >= 0)
919 	    cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
920 	  else
921 	    cupsFilePrintf(temp, "  Allow %s\n", old_remote_any > 0 ? "all" : "@LOCAL");
922 	}
923       }
924 
925       in_admin_location = 0;
926       in_conf_location  = 0;
927       in_log_location   = 0;
928       in_root_location  = 0;
929 
930       cupsFilePuts(temp, "</Location>\n");
931     }
932     else if (!_cups_strcasecmp(line, "<Limit"))
933     {
934       if (in_default_policy)
935       {
936        /*
937 	* See if the policy limit is for the Cancel-Job operation...
938 	*/
939 
940 	char	*valptr;		/* Pointer into value */
941 
942 
943 	if (!_cups_strcasecmp(value, "cancel-job") && user_cancel_any >= 0)
944 	{
945 	 /*
946 	  * Don't write anything for this limit section...
947 	  */
948 
949 	  in_cancel_job = 2;
950 	}
951 	else
952 	{
953 	  cupsFilePrintf(temp, "%*s%s", indent, "", line);
954 
955 	  while (*value)
956 	  {
957 	    for (valptr = value; *valptr && !_cups_isspace(*valptr); valptr ++);
958 
959 	    if (*valptr)
960 	      *valptr++ = '\0';
961 
962 	    if (!_cups_strcasecmp(value, "cancel-job") && user_cancel_any >= 0)
963 	    {
964 	     /*
965 	      * Write everything except for this definition...
966 	      */
967 
968 	      in_cancel_job = 1;
969 	    }
970 	    else
971 	      cupsFilePrintf(temp, " %s", value);
972 
973 	    for (value = valptr; _cups_isspace(*value); value ++);
974 	  }
975 
976 	  cupsFilePuts(temp, ">\n");
977 	}
978       }
979       else
980         cupsFilePrintf(temp, "%*s%s %s>\n", indent, "", line, value);
981 
982       indent += 2;
983     }
984     else if (!_cups_strcasecmp(line, "</Limit>") && in_cancel_job)
985     {
986       indent -= 2;
987 
988       if (in_cancel_job == 1)
989 	cupsFilePuts(temp, "  </Limit>\n");
990 
991       wrote_policy = 1;
992 
993       if (!user_cancel_any)
994 	cupsFilePuts(temp, "  # Only the owner or an administrator can cancel "
995 			   "a job...\n"
996 			   "  <Limit Cancel-Job>\n"
997 			   "    Order deny,allow\n"
998 			   "    Require user @OWNER "
999 			   CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n"
1000 			   "  </Limit>\n");
1001 
1002       in_cancel_job = 0;
1003     }
1004     else if ((((in_admin_location || in_conf_location || in_root_location || in_log_location) &&
1005                (remote_admin >= 0 || remote_any >= 0)) ||
1006               (in_root_location && share_printers >= 0)) &&
1007              (!_cups_strcasecmp(line, "Allow") || !_cups_strcasecmp(line, "Deny") ||
1008 	      !_cups_strcasecmp(line, "Order")))
1009       continue;
1010     else if (in_cancel_job == 2)
1011       continue;
1012     else if (line[0] == '<')
1013     {
1014       if (value)
1015       {
1016         cupsFilePrintf(temp, "%*s%s %s>\n", indent, "", line, value);
1017 	indent += 2;
1018       }
1019       else
1020       {
1021 	if (line[1] == '/')
1022 	  indent -= 2;
1023 
1024 	cupsFilePrintf(temp, "%*s%s\n", indent, "", line);
1025       }
1026     }
1027     else if (!in_policy && !in_location &&
1028              (val = cupsGetOption(line, num_settings, settings)) != NULL)
1029     {
1030      /*
1031       * Replace this directive's value with the new one...
1032       */
1033 
1034       cupsd_num_settings = cupsAddOption(line, val, cupsd_num_settings,
1035                                          &cupsd_settings);
1036 
1037      /*
1038       * Write the new value in its place, without indentation since we
1039       * only support setting root directives, not in sections...
1040       */
1041 
1042       cupsFilePrintf(temp, "%s %s\n", line, val);
1043     }
1044     else if (value)
1045     {
1046       if (!in_policy && !in_location)
1047       {
1048        /*
1049         * Record the non-policy, non-location directives that we find
1050 	* in the server settings, since we cache this info and record it
1051 	* in cupsAdminGetServerSettings()...
1052 	*/
1053 
1054 	cupsd_num_settings = cupsAddOption(line, value, cupsd_num_settings,
1055                                            &cupsd_settings);
1056       }
1057 
1058       cupsFilePrintf(temp, "%*s%s %s\n", indent, "", line, value);
1059     }
1060     else
1061       cupsFilePrintf(temp, "%*s%s\n", indent, "", line);
1062   }
1063 
1064  /*
1065   * Write any missing info...
1066   */
1067 
1068   if (!wrote_browsing && share_printers >= 0)
1069   {
1070     if (share_printers > 0)
1071     {
1072       cupsFilePuts(temp, "# Share local printers on the local network.\n");
1073       cupsFilePuts(temp, "Browsing On\n");
1074     }
1075     else
1076     {
1077       cupsFilePuts(temp, "# Disable printer sharing and shared printers.\n");
1078       cupsFilePuts(temp, "Browsing Off\n");
1079     }
1080   }
1081 
1082   if (!wrote_loglevel && debug_logging >= 0)
1083   {
1084     if (debug_logging)
1085     {
1086       cupsFilePuts(temp, "# Show troubleshooting information in error_log.\n");
1087       cupsFilePuts(temp, "LogLevel debug\n");
1088     }
1089     else
1090     {
1091       cupsFilePuts(temp, "# Show general information in error_log.\n");
1092       cupsFilePuts(temp, "LogLevel " CUPS_DEFAULT_LOG_LEVEL "\n");
1093     }
1094   }
1095 
1096   if (!wrote_port_listen &&
1097       (remote_admin >= 0 || remote_any >= 0 || share_printers >= 0))
1098   {
1099     if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
1100     {
1101       cupsFilePuts(temp, "# Allow remote access\n");
1102       cupsFilePrintf(temp, "Port %d\n", ippPort());
1103     }
1104     else
1105     {
1106       cupsFilePuts(temp,
1107                    "# Only listen for connections from the local machine.\n");
1108       cupsFilePrintf(temp, "Listen localhost:%d\n", ippPort());
1109     }
1110 
1111 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1112     if (!access(CUPS_DEFAULT_DOMAINSOCKET, 0))
1113       cupsFilePuts(temp, "Listen " CUPS_DEFAULT_DOMAINSOCKET "\n");
1114 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1115   }
1116 
1117   if (!wrote_root_location &&
1118       (remote_admin >= 0 || remote_any >= 0 || share_printers >= 0))
1119   {
1120     if (remote_admin > 0 && share_printers > 0)
1121       cupsFilePuts(temp,
1122                    "# Allow shared printing and remote administration...\n");
1123     else if (remote_admin > 0)
1124       cupsFilePuts(temp, "# Allow remote administration...\n");
1125     else if (share_printers > 0)
1126       cupsFilePuts(temp, "# Allow shared printing...\n");
1127     else if (remote_any > 0)
1128       cupsFilePuts(temp, "# Allow remote access...\n");
1129     else
1130       cupsFilePuts(temp, "# Restrict access to the server...\n");
1131 
1132     cupsFilePuts(temp, "<Location />\n"
1133                        "  Order allow,deny\n");
1134 
1135     if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
1136       cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
1137 
1138     cupsFilePuts(temp, "</Location>\n");
1139   }
1140 
1141   if (!wrote_admin_location && remote_admin >= 0)
1142   {
1143     if (remote_admin)
1144       cupsFilePuts(temp, "# Allow remote administration...\n");
1145     else
1146       cupsFilePuts(temp, "# Restrict access to the admin pages...\n");
1147 
1148     cupsFilePuts(temp, "<Location /admin>\n"
1149                        "  Order allow,deny\n");
1150 
1151     if (remote_admin)
1152       cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
1153 
1154     cupsFilePuts(temp, "</Location>\n");
1155   }
1156 
1157   if (!wrote_conf_location && remote_admin >= 0)
1158   {
1159     if (remote_admin)
1160       cupsFilePuts(temp,
1161                    "# Allow remote access to the configuration files...\n");
1162     else
1163       cupsFilePuts(temp, "# Restrict access to the configuration files...\n");
1164 
1165     cupsFilePuts(temp, "<Location /admin/conf>\n"
1166                        "  AuthType Default\n"
1167                        "  Require user @SYSTEM\n"
1168                        "  Order allow,deny\n");
1169 
1170     if (remote_admin)
1171       cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
1172 
1173     cupsFilePuts(temp, "</Location>\n");
1174   }
1175 
1176   if (!wrote_log_location && remote_admin >= 0)
1177   {
1178     if (remote_admin)
1179       cupsFilePuts(temp,
1180                    "# Allow remote access to the log files...\n");
1181     else
1182       cupsFilePuts(temp, "# Restrict access to the log files...\n");
1183 
1184     cupsFilePuts(temp, "<Location /admin/log>\n"
1185                        "  AuthType Default\n"
1186                        "  Require user @SYSTEM\n"
1187                        "  Order allow,deny\n");
1188 
1189     if (remote_admin)
1190       cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
1191 
1192     cupsFilePuts(temp, "</Location>\n");
1193   }
1194 
1195   if (!wrote_policy && user_cancel_any >= 0)
1196   {
1197     cupsFilePuts(temp, "<Policy default>\n"
1198                        "  # Job-related operations must be done by the owner "
1199 		       "or an administrator...\n"
1200                        "  <Limit Send-Document Send-URI Hold-Job Release-Job "
1201 		       "Restart-Job Purge-Jobs Set-Job-Attributes "
1202 		       "Create-Job-Subscription Renew-Subscription "
1203 		       "Cancel-Subscription Get-Notifications Reprocess-Job "
1204 		       "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1205 		       "CUPS-Move-Job>\n"
1206                        "    Require user @OWNER @SYSTEM\n"
1207                        "    Order deny,allow\n"
1208                        "  </Limit>\n"
1209                        "  # All administration operations require an "
1210 		       "administrator to authenticate...\n"
1211 		       "  <Limit Pause-Printer Resume-Printer "
1212                        "Set-Printer-Attributes Enable-Printer "
1213 		       "Disable-Printer Pause-Printer-After-Current-Job "
1214 		       "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1215 		       "Activate-Printer Restart-Printer Shutdown-Printer "
1216 		       "Startup-Printer Promote-Job Schedule-Job-After "
1217 		       "CUPS-Add-Printer CUPS-Delete-Printer "
1218 		       "CUPS-Add-Class CUPS-Delete-Class "
1219 		       "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1220 		       "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1221                        "    AuthType Default\n"
1222 		       "    Require user @SYSTEM\n"
1223                        "    Order deny,allow\n"
1224                        "</Limit>\n");
1225 
1226     if (!user_cancel_any)
1227       cupsFilePuts(temp, "  # Only the owner or an administrator can cancel "
1228                          "a job...\n"
1229 	                 "  <Limit Cancel-Job>\n"
1230 	                 "    Order deny,allow\n"
1231 	                 "    Require user @OWNER "
1232 			 CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n"
1233 			 "  </Limit>\n");
1234 
1235     cupsFilePuts(temp, "  <Limit All>\n"
1236                        "  Order deny,allow\n"
1237                        "  </Limit>\n"
1238 		       "</Policy>\n");
1239   }
1240 
1241   for (i = num_settings, setting = settings; i > 0; i --, setting ++)
1242     if (setting->name[0] != '_' &&
1243         _cups_strcasecmp(setting->name, "Listen") &&
1244 	_cups_strcasecmp(setting->name, "Port") &&
1245         !cupsGetOption(setting->name, cupsd_num_settings, cupsd_settings))
1246     {
1247      /*
1248       * Add this directive to the list of directives we have written...
1249       */
1250 
1251       cupsd_num_settings = cupsAddOption(setting->name, setting->value,
1252                                          cupsd_num_settings, &cupsd_settings);
1253 
1254      /*
1255       * Write the new value, without indentation since we only support
1256       * setting root directives, not in sections...
1257       */
1258 
1259       cupsFilePrintf(temp, "%s %s\n", setting->name, setting->value);
1260     }
1261 
1262   cupsFileClose(cupsd);
1263   cupsFileClose(temp);
1264 
1265  /*
1266   * Upload the configuration file to the server...
1267   */
1268 
1269   status = cupsPutFile(http, "/admin/conf/cupsd.conf", tempfile);
1270 
1271   if (status == HTTP_STATUS_CREATED)
1272   {
1273    /*
1274     * Updated OK, add the basic settings...
1275     */
1276 
1277     if (debug_logging >= 0)
1278       cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
1279                                 	 debug_logging ? "1" : "0",
1280 					 cupsd_num_settings, &cupsd_settings);
1281     else
1282       cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
1283                                 	 old_debug_logging ? "1" : "0",
1284 					 cupsd_num_settings, &cupsd_settings);
1285 
1286     if (remote_admin >= 0)
1287       cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
1288                                 	 remote_admin ? "1" : "0",
1289 					 cupsd_num_settings, &cupsd_settings);
1290     else
1291       cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
1292                                 	 old_remote_admin ? "1" : "0",
1293 					 cupsd_num_settings, &cupsd_settings);
1294 
1295     if (remote_any >= 0)
1296       cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY,
1297 					 remote_any ? "1" : "0",
1298 					 cupsd_num_settings, &cupsd_settings);
1299     else
1300       cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY,
1301 					 old_remote_any ? "1" : "0",
1302 					 cupsd_num_settings, &cupsd_settings);
1303 
1304     if (share_printers >= 0)
1305       cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
1306                                 	 share_printers ? "1" : "0",
1307 					 cupsd_num_settings, &cupsd_settings);
1308     else
1309       cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
1310                                 	 old_share_printers ? "1" : "0",
1311 					 cupsd_num_settings, &cupsd_settings);
1312 
1313     if (user_cancel_any >= 0)
1314       cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
1315                                 	 user_cancel_any ? "1" : "0",
1316 					 cupsd_num_settings, &cupsd_settings);
1317     else
1318       cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
1319                                 	 old_user_cancel_any ? "1" : "0",
1320 					 cupsd_num_settings, &cupsd_settings);
1321 
1322    /*
1323     * Save the new values...
1324     */
1325 
1326     invalidate_cupsd_cache(cg);
1327 
1328     cg->cupsd_num_settings = cupsd_num_settings;
1329     cg->cupsd_settings     = cupsd_settings;
1330     cg->cupsd_update       = time(NULL);
1331 
1332     httpGetHostname(http, cg->cupsd_hostname, sizeof(cg->cupsd_hostname));
1333   }
1334   else
1335     cupsFreeOptions(cupsd_num_settings, cupsd_settings);
1336 
1337  /*
1338   * Remote our temp files and return...
1339   */
1340 
1341   if (remote)
1342     unlink(cupsdconf);
1343 
1344   unlink(tempfile);
1345 
1346   return (status == HTTP_STATUS_CREATED);
1347 }
1348 
1349 
1350 /*
1351  * 'get_cupsd_conf()' - Get the current cupsd.conf file.
1352  */
1353 
1354 static http_status_t			/* O - Status of request */
get_cupsd_conf(http_t * http,_cups_globals_t * cg,time_t last_update,char * name,size_t namesize,int * remote)1355 get_cupsd_conf(
1356     http_t          *http,		/* I - Connection to server */
1357     _cups_globals_t *cg,		/* I - Global data */
1358     time_t          last_update,	/* I - Last update time for file */
1359     char            *name,		/* I - Filename buffer */
1360     size_t          namesize,		/* I - Size of filename buffer */
1361     int             *remote)		/* O - Remote file? */
1362 {
1363   int		fd;			/* Temporary file descriptor */
1364 #ifndef _WIN32
1365   struct stat	info;			/* cupsd.conf file information */
1366 #endif /* _WIN32 */
1367   http_status_t	status;			/* Status of getting cupsd.conf */
1368   char		host[HTTP_MAX_HOST];	/* Hostname for connection */
1369 
1370 
1371  /*
1372   * See if we already have the data we need...
1373   */
1374 
1375   httpGetHostname(http, host, sizeof(host));
1376 
1377   if (_cups_strcasecmp(cg->cupsd_hostname, host))
1378     invalidate_cupsd_cache(cg);
1379 
1380   snprintf(name, namesize, "%s/cupsd.conf", cg->cups_serverroot);
1381   *remote = 0;
1382 
1383 #ifndef _WIN32
1384   if (!_cups_strcasecmp(host, "localhost") && !access(name, R_OK))
1385   {
1386    /*
1387     * Read the local file rather than using HTTP...
1388     */
1389 
1390     if (stat(name, &info))
1391     {
1392       char	message[1024];		/* Message string */
1393 
1394 
1395       snprintf(message, sizeof(message),
1396                _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
1397                name, strerror(errno));
1398       _cupsSetError(IPP_STATUS_ERROR_INTERNAL, message, 0);
1399 
1400       *name = '\0';
1401 
1402       return (HTTP_STATUS_SERVER_ERROR);
1403     }
1404     else if (last_update && info.st_mtime <= last_update)
1405       status = HTTP_STATUS_NOT_MODIFIED;
1406     else
1407       status = HTTP_STATUS_OK;
1408   }
1409   else
1410 #endif /* !_WIN32 */
1411   {
1412    /*
1413     * Read cupsd.conf via a HTTP GET request...
1414     */
1415 
1416     if ((fd = cupsTempFd(name, (int)namesize)) < 0)
1417     {
1418       *name = '\0';
1419 
1420       _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
1421 
1422       invalidate_cupsd_cache(cg);
1423 
1424       return (HTTP_STATUS_SERVER_ERROR);
1425     }
1426 
1427     *remote = 1;
1428 
1429     httpClearFields(http);
1430 
1431     if (last_update)
1432       httpSetField(http, HTTP_FIELD_IF_MODIFIED_SINCE,
1433                    httpGetDateString(last_update));
1434 
1435     status = cupsGetFd(http, "/admin/conf/cupsd.conf", fd);
1436 
1437     close(fd);
1438 
1439     if (status != HTTP_STATUS_OK)
1440     {
1441       unlink(name);
1442       *name = '\0';
1443     }
1444   }
1445 
1446   return (status);
1447 }
1448 
1449 
1450 /*
1451  * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
1452  */
1453 
1454 static void
invalidate_cupsd_cache(_cups_globals_t * cg)1455 invalidate_cupsd_cache(
1456     _cups_globals_t *cg)		/* I - Global data */
1457 {
1458   cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings);
1459 
1460   cg->cupsd_hostname[0]  = '\0';
1461   cg->cupsd_update       = 0;
1462   cg->cupsd_num_settings = 0;
1463   cg->cupsd_settings     = NULL;
1464 }
1465