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