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