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