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