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