]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
c07d5b2d | 2 | * "$Id: classes.c 177 2006-06-21 00:20:03Z jlovell $" |
ef416fc2 | 3 | * |
4 | * Printer class routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
bd7854cb | 6 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. |
ef416fc2 | 7 | * |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * cupsdAddClass() - Add a class to the system. | |
27 | * cupsdAddPrinterToClass() - Add a printer to a class... | |
28 | * cupsdDeletePrinterFromClass() - Delete a printer from a class. | |
29 | * cupsdDeletePrinterFromClasses() - Delete a printer from all classes. | |
30 | * cupsdDeleteAllClasses() - Remove all classes from the system. | |
31 | * cupsdFindAvailablePrinter() - Find an available printer in a class. | |
32 | * cupsdFindClass() - Find the named class. | |
33 | * cupsdLoadAllClasses() - Load classes from the classes.conf file. | |
34 | * cupsdSaveAllClasses() - Save classes to the classes.conf file. | |
35 | * cupsdUpdateImplicitClasses() - Update the accepting state of implicit | |
36 | * classes. | |
37 | */ | |
38 | ||
39 | /* | |
40 | * Include necessary headers... | |
41 | */ | |
42 | ||
43 | #include "cupsd.h" | |
44 | ||
45 | ||
46 | /* | |
47 | * 'cupsdAddClass()' - Add a class to the system. | |
48 | */ | |
49 | ||
50 | cupsd_printer_t * /* O - New class */ | |
51 | cupsdAddClass(const char *name) /* I - Name of class */ | |
52 | { | |
53 | cupsd_printer_t *c; /* New class */ | |
54 | ||
55 | ||
56 | /* | |
57 | * Add the printer and set the type to "class"... | |
58 | */ | |
59 | ||
60 | if ((c = cupsdAddPrinter(name)) != NULL) | |
61 | { | |
62 | /* | |
63 | * Change from a printer to a class... | |
64 | */ | |
65 | ||
66 | c->type = CUPS_PRINTER_CLASS; | |
67 | ||
68 | cupsdSetStringf(&c->uri, "ipp://%s:%d/classes/%s", ServerName, LocalPort, | |
69 | name); | |
70 | cupsdSetString(&c->error_policy, "retry-job"); | |
71 | } | |
72 | ||
73 | return (c); | |
74 | } | |
75 | ||
76 | ||
77 | /* | |
78 | * 'cupsdAddPrinterToClass()' - Add a printer to a class... | |
79 | */ | |
80 | ||
81 | void | |
82 | cupsdAddPrinterToClass( | |
83 | cupsd_printer_t *c, /* I - Class to add to */ | |
84 | cupsd_printer_t *p) /* I - Printer to add */ | |
85 | { | |
86 | int i; /* Looping var */ | |
87 | cupsd_printer_t **temp; /* Pointer to printer array */ | |
88 | ||
89 | ||
90 | /* | |
91 | * See if this printer is already a member of the class... | |
92 | */ | |
93 | ||
94 | for (i = 0; i < c->num_printers; i ++) | |
95 | if (c->printers[i] == p) | |
96 | return; | |
97 | ||
98 | /* | |
99 | * Allocate memory as needed... | |
100 | */ | |
101 | ||
102 | if (c->num_printers == 0) | |
103 | temp = malloc(sizeof(cupsd_printer_t *)); | |
104 | else | |
105 | temp = realloc(c->printers, sizeof(cupsd_printer_t *) * (c->num_printers + 1)); | |
106 | ||
107 | if (temp == NULL) | |
108 | { | |
109 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to add printer %s to class %s!", | |
110 | p->name, c->name); | |
111 | return; | |
112 | } | |
113 | ||
114 | /* | |
115 | * Add the printer to the end of the array and update the number of printers. | |
116 | */ | |
117 | ||
118 | c->printers = temp; | |
119 | temp += c->num_printers; | |
120 | c->num_printers ++; | |
121 | ||
122 | *temp = p; | |
123 | } | |
124 | ||
125 | ||
126 | /* | |
127 | * 'cupsdDeletePrinterFromClass()' - Delete a printer from a class. | |
128 | */ | |
129 | ||
130 | void | |
131 | cupsdDeletePrinterFromClass( | |
132 | cupsd_printer_t *c, /* I - Class to delete from */ | |
133 | cupsd_printer_t *p) /* I - Printer to delete */ | |
134 | { | |
135 | int i; /* Looping var */ | |
136 | cups_ptype_t type, /* Class type */ | |
137 | oldtype; /* Old class type */ | |
138 | ||
139 | ||
140 | /* | |
141 | * See if the printer is in the class... | |
142 | */ | |
143 | ||
144 | for (i = 0; i < c->num_printers; i ++) | |
145 | if (p == c->printers[i]) | |
146 | break; | |
147 | ||
148 | /* | |
149 | * If it is, remove it from the list... | |
150 | */ | |
151 | ||
152 | if (i < c->num_printers) | |
153 | { | |
154 | /* | |
155 | * Yes, remove the printer... | |
156 | */ | |
157 | ||
158 | c->num_printers --; | |
159 | if (i < c->num_printers) | |
160 | memmove(c->printers + i, c->printers + i + 1, | |
161 | (c->num_printers - i) * sizeof(cupsd_printer_t *)); | |
162 | } | |
163 | else | |
164 | return; | |
165 | ||
166 | /* | |
167 | * Recompute the printer type mask as needed... | |
168 | */ | |
169 | ||
170 | if (c->num_printers > 0) | |
171 | { | |
172 | oldtype = c->type; | |
173 | type = c->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT); | |
174 | c->type = ~CUPS_PRINTER_REMOTE; | |
175 | ||
176 | for (i = 0; i < c->num_printers; i ++) | |
177 | c->type &= c->printers[i]->type; | |
178 | ||
179 | c->type |= type; | |
180 | ||
181 | /* | |
182 | * Update the IPP attributes... | |
183 | */ | |
184 | ||
185 | if (c->type != oldtype) | |
186 | cupsdSetPrinterAttrs(c); | |
187 | } | |
188 | } | |
189 | ||
190 | ||
191 | /* | |
192 | * 'cupsdDeletePrinterFromClasses()' - Delete a printer from all classes. | |
193 | */ | |
194 | ||
195 | void | |
196 | cupsdDeletePrinterFromClasses( | |
197 | cupsd_printer_t *p) /* I - Printer to delete */ | |
198 | { | |
199 | cupsd_printer_t *c; /* Pointer to current class */ | |
200 | ||
201 | ||
202 | /* | |
203 | * Loop through the printer/class list and remove the printer | |
204 | * from each class listed... | |
205 | */ | |
206 | ||
207 | for (c = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
208 | c; | |
209 | c = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
210 | if (c->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT)) | |
211 | cupsdDeletePrinterFromClass(c, p); | |
212 | ||
213 | /* | |
214 | * Then clean out any empty implicit classes... | |
215 | */ | |
216 | ||
217 | for (c = (cupsd_printer_t *)cupsArrayFirst(ImplicitPrinters); | |
218 | c; | |
219 | c = (cupsd_printer_t *)cupsArrayNext(ImplicitPrinters)) | |
220 | if (c->num_printers == 0) | |
221 | { | |
222 | cupsArrayRemove(ImplicitPrinters, c); | |
223 | cupsdDeletePrinter(c, 0); | |
224 | } | |
225 | } | |
226 | ||
227 | ||
228 | /* | |
229 | * 'cupsdDeleteAllClasses()' - Remove all classes from the system. | |
230 | */ | |
231 | ||
232 | void | |
233 | cupsdDeleteAllClasses(void) | |
234 | { | |
235 | cupsd_printer_t *c; /* Pointer to current printer/class */ | |
236 | ||
237 | ||
238 | for (c = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
239 | c; | |
240 | c = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
241 | if (c->type & CUPS_PRINTER_CLASS) | |
242 | cupsdDeletePrinter(c, 0); | |
243 | } | |
244 | ||
245 | ||
246 | /* | |
247 | * 'cupsdFindAvailablePrinter()' - Find an available printer in a class. | |
248 | */ | |
249 | ||
250 | cupsd_printer_t * /* O - Available printer or NULL */ | |
251 | cupsdFindAvailablePrinter( | |
252 | const char *name) /* I - Class to check */ | |
253 | { | |
254 | int i; /* Looping var */ | |
255 | cupsd_printer_t *c; /* Printer class */ | |
256 | ||
257 | ||
258 | /* | |
259 | * Find the class... | |
260 | */ | |
261 | ||
262 | if ((c = cupsdFindClass(name)) == NULL) | |
263 | { | |
264 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to find class \"%s\"!", name); | |
265 | return (NULL); | |
266 | } | |
267 | ||
268 | /* | |
269 | * Loop through the printers in the class and return the first idle | |
270 | * printer... We keep track of the last printer that we used so that | |
271 | * a "round robin" type of scheduling is realized (otherwise the first | |
272 | * server might be saturated with print jobs...) | |
273 | * | |
274 | * Thanks to Joel Fredrikson for helping us get this right! | |
275 | */ | |
276 | ||
277 | for (i = c->last_printer + 1; ; i ++) | |
278 | { | |
279 | if (i >= c->num_printers) | |
280 | i = 0; | |
281 | ||
282 | if (c->printers[i]->accepting && | |
283 | (c->printers[i]->state == IPP_PRINTER_IDLE || | |
284 | ((c->printers[i]->type & CUPS_PRINTER_REMOTE) && !c->printers[i]->job))) | |
285 | { | |
286 | c->last_printer = i; | |
287 | return (c->printers[i]); | |
288 | } | |
289 | ||
290 | if (i == c->last_printer) | |
291 | break; | |
292 | } | |
293 | ||
294 | return (NULL); | |
295 | } | |
296 | ||
297 | ||
298 | /* | |
299 | * 'cupsdFindClass()' - Find the named class. | |
300 | */ | |
301 | ||
302 | cupsd_printer_t * /* O - Matching class or NULL */ | |
303 | cupsdFindClass(const char *name) /* I - Name of class */ | |
304 | { | |
305 | cupsd_printer_t *c; /* Current class/printer */ | |
306 | ||
307 | ||
308 | if ((c = cupsdFindDest(name)) != NULL && | |
309 | (c->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))) | |
310 | return (c); | |
311 | else | |
312 | return (NULL); | |
313 | } | |
314 | ||
315 | ||
316 | /* | |
317 | * 'cupsdLoadAllClasses()' - Load classes from the classes.conf file. | |
318 | */ | |
319 | ||
320 | void | |
321 | cupsdLoadAllClasses(void) | |
322 | { | |
323 | cups_file_t *fp; /* classes.conf file */ | |
324 | int linenum; /* Current line number */ | |
325 | char line[1024], /* Line from file */ | |
326 | *value, /* Pointer to value */ | |
327 | *valueptr; /* Pointer into value */ | |
328 | cupsd_printer_t *p, /* Current printer class */ | |
329 | *temp; /* Temporary pointer to printer */ | |
330 | ||
331 | ||
332 | /* | |
333 | * Open the classes.conf file... | |
334 | */ | |
335 | ||
336 | snprintf(line, sizeof(line), "%s/classes.conf", ServerRoot); | |
337 | if ((fp = cupsFileOpen(line, "r")) == NULL) | |
338 | { | |
fa73b229 | 339 | if (errno != ENOENT) |
bd7854cb | 340 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open %s - %s", line, |
fa73b229 | 341 | strerror(errno)); |
ef416fc2 | 342 | return; |
343 | } | |
344 | ||
345 | /* | |
346 | * Read class configurations until we hit EOF... | |
347 | */ | |
348 | ||
349 | linenum = 0; | |
350 | p = NULL; | |
351 | ||
352 | while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) | |
353 | { | |
354 | /* | |
355 | * Decode the directive... | |
356 | */ | |
357 | ||
358 | if (!strcasecmp(line, "<Class") || | |
359 | !strcasecmp(line, "<DefaultClass")) | |
360 | { | |
361 | /* | |
362 | * <Class name> or <DefaultClass name> | |
363 | */ | |
364 | ||
365 | if (p == NULL && value) | |
366 | { | |
bd7854cb | 367 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Loading class %s...", value); |
ef416fc2 | 368 | |
369 | p = cupsdAddClass(value); | |
370 | p->accepting = 1; | |
371 | p->state = IPP_PRINTER_IDLE; | |
372 | ||
373 | if (!strcasecmp(line, "<DefaultClass")) | |
374 | DefaultPrinter = p; | |
375 | } | |
376 | else | |
377 | { | |
378 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
379 | "Syntax error on line %d of classes.conf.", linenum); | |
380 | return; | |
381 | } | |
382 | } | |
383 | else if (!strcasecmp(line, "</Class>")) | |
384 | { | |
385 | if (p != NULL) | |
386 | { | |
387 | cupsdSetPrinterAttrs(p); | |
388 | p = NULL; | |
389 | } | |
390 | else | |
391 | { | |
392 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
393 | "Syntax error on line %d of classes.conf.", linenum); | |
394 | return; | |
395 | } | |
396 | } | |
397 | else if (!p) | |
398 | { | |
399 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
400 | "Syntax error on line %d of classes.conf.", linenum); | |
401 | return; | |
402 | } | |
403 | else if (!strcasecmp(line, "Info")) | |
404 | { | |
405 | if (value) | |
406 | cupsdSetString(&p->info, value); | |
407 | } | |
408 | else if (!strcasecmp(line, "Location")) | |
409 | { | |
410 | if (value) | |
411 | cupsdSetString(&p->location, value); | |
412 | } | |
b423cd4c | 413 | else if (!strcasecmp(line, "Option") && value) |
414 | { | |
415 | /* | |
416 | * Option name value | |
417 | */ | |
418 | ||
419 | for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++); | |
420 | ||
421 | if (!*valueptr) | |
422 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
423 | "Syntax error on line %d of classes.conf.", linenum); | |
424 | else | |
425 | { | |
426 | for (; *valueptr && isspace(*valueptr & 255); *valueptr++ = '\0'); | |
427 | ||
428 | p->num_options = cupsAddOption(value, valueptr, p->num_options, | |
429 | &(p->options)); | |
430 | } | |
431 | } | |
ef416fc2 | 432 | else if (!strcasecmp(line, "Printer")) |
433 | { | |
434 | if (!value) | |
435 | { | |
436 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
437 | "Syntax error on line %d of classes.conf.", linenum); | |
438 | return; | |
439 | } | |
440 | else if ((temp = cupsdFindPrinter(value)) == NULL) | |
441 | { | |
442 | cupsdLogMessage(CUPSD_LOG_WARN, | |
443 | "Unknown printer %s on line %d of classes.conf.", | |
444 | value, linenum); | |
445 | ||
446 | /* | |
447 | * Add the missing remote printer... | |
448 | */ | |
449 | ||
450 | if ((temp = cupsdAddPrinter(value)) != NULL) | |
451 | { | |
452 | cupsdSetString(&temp->make_model, "Remote Printer on unknown"); | |
453 | ||
454 | temp->state = IPP_PRINTER_STOPPED; | |
455 | temp->type |= CUPS_PRINTER_REMOTE; | |
456 | temp->browse_time = 2147483647; | |
457 | ||
458 | cupsdSetString(&temp->location, "Location Unknown"); | |
459 | cupsdSetString(&temp->info, "No Information Available"); | |
460 | temp->hostname[0] = '\0'; | |
461 | ||
462 | cupsdSetPrinterAttrs(temp); | |
463 | } | |
464 | } | |
465 | ||
466 | if (temp) | |
467 | cupsdAddPrinterToClass(p, temp); | |
468 | } | |
469 | else if (!strcasecmp(line, "State")) | |
470 | { | |
471 | /* | |
472 | * Set the initial queue state... | |
473 | */ | |
474 | ||
475 | if (!strcasecmp(value, "idle")) | |
476 | p->state = IPP_PRINTER_IDLE; | |
477 | else if (!strcasecmp(value, "stopped")) | |
478 | p->state = IPP_PRINTER_STOPPED; | |
479 | else | |
480 | { | |
481 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
482 | "Syntax error on line %d of classes.conf.", | |
483 | linenum); | |
484 | return; | |
485 | } | |
486 | } | |
487 | else if (!strcasecmp(line, "StateMessage")) | |
488 | { | |
489 | /* | |
490 | * Set the initial queue state message... | |
491 | */ | |
492 | ||
493 | if (value) | |
494 | strlcpy(p->state_message, value, sizeof(p->state_message)); | |
495 | } | |
496 | else if (!strcasecmp(line, "StateTime")) | |
497 | { | |
498 | /* | |
499 | * Set the state time... | |
500 | */ | |
501 | ||
502 | if (value) | |
503 | p->state_time = atoi(value); | |
504 | } | |
505 | else if (!strcasecmp(line, "Accepting")) | |
506 | { | |
507 | /* | |
508 | * Set the initial accepting state... | |
509 | */ | |
510 | ||
511 | if (value && | |
512 | (!strcasecmp(value, "yes") || | |
513 | !strcasecmp(value, "on") || | |
514 | !strcasecmp(value, "true"))) | |
515 | p->accepting = 1; | |
516 | else if (value && | |
517 | (!strcasecmp(value, "no") || | |
518 | !strcasecmp(value, "off") || | |
519 | !strcasecmp(value, "false"))) | |
520 | p->accepting = 0; | |
521 | else | |
522 | { | |
523 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
524 | "Syntax error on line %d of classes.conf.", | |
525 | linenum); | |
526 | return; | |
527 | } | |
528 | } | |
529 | else if (!strcasecmp(line, "Shared")) | |
530 | { | |
531 | /* | |
532 | * Set the initial shared state... | |
533 | */ | |
534 | ||
535 | if (value && | |
536 | (!strcasecmp(value, "yes") || | |
537 | !strcasecmp(value, "on") || | |
538 | !strcasecmp(value, "true"))) | |
539 | p->shared = 1; | |
540 | else if (value && | |
541 | (!strcasecmp(value, "no") || | |
542 | !strcasecmp(value, "off") || | |
543 | !strcasecmp(value, "false"))) | |
544 | p->shared = 0; | |
545 | else | |
546 | { | |
547 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
548 | "Syntax error on line %d of printers.conf.", | |
549 | linenum); | |
550 | return; | |
551 | } | |
552 | } | |
553 | else if (!strcasecmp(line, "JobSheets")) | |
554 | { | |
555 | /* | |
556 | * Set the initial job sheets... | |
557 | */ | |
558 | ||
559 | if (value) | |
560 | { | |
561 | for (valueptr = value; | |
562 | *valueptr && !isspace(*valueptr & 255); | |
563 | valueptr ++); | |
564 | ||
565 | if (*valueptr) | |
566 | *valueptr++ = '\0'; | |
567 | ||
568 | cupsdSetString(&p->job_sheets[0], value); | |
569 | ||
570 | while (isspace(*valueptr & 255)) | |
571 | valueptr ++; | |
572 | ||
573 | if (*valueptr) | |
574 | { | |
575 | for (value = valueptr; | |
576 | *valueptr && !isspace(*valueptr & 255); | |
577 | valueptr ++); | |
578 | ||
579 | if (*valueptr) | |
580 | *valueptr++ = '\0'; | |
581 | ||
582 | cupsdSetString(&p->job_sheets[1], value); | |
583 | } | |
584 | } | |
585 | else | |
586 | { | |
587 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
588 | "Syntax error on line %d of classes.conf.", linenum); | |
589 | return; | |
590 | } | |
591 | } | |
592 | else if (!strcasecmp(line, "AllowUser")) | |
593 | { | |
594 | if (value) | |
595 | { | |
596 | p->deny_users = 0; | |
597 | cupsdAddPrinterUser(p, value); | |
598 | } | |
599 | else | |
600 | { | |
601 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
602 | "Syntax error on line %d of classes.conf.", linenum); | |
603 | return; | |
604 | } | |
605 | } | |
606 | else if (!strcasecmp(line, "DenyUser")) | |
607 | { | |
608 | if (value) | |
609 | { | |
610 | p->deny_users = 1; | |
611 | cupsdAddPrinterUser(p, value); | |
612 | } | |
613 | else | |
614 | { | |
615 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
616 | "Syntax error on line %d of classes.conf.", linenum); | |
617 | return; | |
618 | } | |
619 | } | |
620 | else if (!strcasecmp(line, "QuotaPeriod")) | |
621 | { | |
622 | if (value) | |
623 | p->quota_period = atoi(value); | |
624 | else | |
625 | { | |
626 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
627 | "Syntax error on line %d of classes.conf.", linenum); | |
628 | return; | |
629 | } | |
630 | } | |
631 | else if (!strcasecmp(line, "PageLimit")) | |
632 | { | |
633 | if (value) | |
634 | p->page_limit = atoi(value); | |
635 | else | |
636 | { | |
637 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
638 | "Syntax error on line %d of classes.conf.", linenum); | |
639 | return; | |
640 | } | |
641 | } | |
642 | else if (!strcasecmp(line, "KLimit")) | |
643 | { | |
644 | if (value) | |
645 | p->k_limit = atoi(value); | |
646 | else | |
647 | { | |
648 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
649 | "Syntax error on line %d of classes.conf.", linenum); | |
650 | return; | |
651 | } | |
652 | } | |
653 | else if (!strcasecmp(line, "OpPolicy")) | |
654 | { | |
655 | if (value) | |
656 | cupsdSetString(&p->op_policy, value); | |
657 | else | |
658 | { | |
659 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
660 | "Syntax error on line %d of classes.conf.", linenum); | |
661 | return; | |
662 | } | |
663 | } | |
664 | else if (!strcasecmp(line, "ErrorPolicy")) | |
665 | { | |
666 | if (value) | |
667 | cupsdSetString(&p->error_policy, value); | |
668 | else | |
669 | { | |
670 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
671 | "Syntax error on line %d of classes.conf.", linenum); | |
672 | return; | |
673 | } | |
674 | } | |
675 | else | |
676 | { | |
677 | /* | |
678 | * Something else we don't understand... | |
679 | */ | |
680 | ||
681 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
682 | "Unknown configuration directive %s on line %d of classes.conf.", | |
683 | line, linenum); | |
684 | } | |
685 | } | |
686 | ||
687 | cupsFileClose(fp); | |
688 | } | |
689 | ||
690 | ||
691 | /* | |
692 | * 'cupsdSaveAllClasses()' - Save classes to the classes.conf file. | |
693 | */ | |
694 | ||
695 | void | |
696 | cupsdSaveAllClasses(void) | |
697 | { | |
698 | cups_file_t *fp; /* classes.conf file */ | |
699 | char temp[1024]; /* Temporary string */ | |
700 | char backup[1024]; /* classes.conf.O file */ | |
701 | cupsd_printer_t *pclass; /* Current printer class */ | |
702 | int i; /* Looping var */ | |
703 | time_t curtime; /* Current time */ | |
704 | struct tm *curdate; /* Current date */ | |
b423cd4c | 705 | cups_option_t *option; /* Current option */ |
ef416fc2 | 706 | |
707 | ||
708 | /* | |
709 | * Create the classes.conf file... | |
710 | */ | |
711 | ||
712 | snprintf(temp, sizeof(temp), "%s/classes.conf", ServerRoot); | |
713 | snprintf(backup, sizeof(backup), "%s/classes.conf.O", ServerRoot); | |
714 | ||
715 | if (rename(temp, backup)) | |
716 | { | |
717 | if (errno != ENOENT) | |
718 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to backup classes.conf - %s", | |
719 | strerror(errno)); | |
720 | } | |
721 | ||
722 | if ((fp = cupsFileOpen(temp, "w")) == NULL) | |
723 | { | |
724 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to save classes.conf - %s", | |
725 | strerror(errno)); | |
726 | ||
727 | if (rename(backup, temp)) | |
728 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to restore classes.conf - %s", | |
729 | strerror(errno)); | |
730 | return; | |
731 | } | |
732 | else | |
733 | cupsdLogMessage(CUPSD_LOG_INFO, "Saving classes.conf..."); | |
734 | ||
735 | /* | |
736 | * Restrict access to the file... | |
737 | */ | |
738 | ||
739 | fchown(cupsFileNumber(fp), RunUser, Group); | |
fa73b229 | 740 | fchmod(cupsFileNumber(fp), 0600); |
ef416fc2 | 741 | |
742 | /* | |
743 | * Write a small header to the file... | |
744 | */ | |
745 | ||
746 | curtime = time(NULL); | |
747 | curdate = localtime(&curtime); | |
748 | strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate); | |
749 | ||
750 | cupsFilePuts(fp, "# Class configuration file for " CUPS_SVERSION "\n"); | |
751 | cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp); | |
752 | ||
753 | /* | |
754 | * Write each local class known to the system... | |
755 | */ | |
756 | ||
757 | for (pclass = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
758 | pclass; | |
759 | pclass = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
760 | { | |
761 | /* | |
762 | * Skip remote destinations and regular printers... | |
763 | */ | |
764 | ||
765 | if ((pclass->type & CUPS_PRINTER_REMOTE) || | |
766 | (pclass->type & CUPS_PRINTER_IMPLICIT) || | |
767 | !(pclass->type & CUPS_PRINTER_CLASS)) | |
768 | continue; | |
769 | ||
770 | /* | |
771 | * Write printers as needed... | |
772 | */ | |
773 | ||
774 | if (pclass == DefaultPrinter) | |
775 | cupsFilePrintf(fp, "<DefaultClass %s>\n", pclass->name); | |
776 | else | |
777 | cupsFilePrintf(fp, "<Class %s>\n", pclass->name); | |
778 | ||
779 | if (pclass->info) | |
780 | cupsFilePrintf(fp, "Info %s\n", pclass->info); | |
781 | ||
782 | if (pclass->location) | |
783 | cupsFilePrintf(fp, "Location %s\n", pclass->location); | |
784 | ||
785 | if (pclass->state == IPP_PRINTER_STOPPED) | |
786 | { | |
787 | cupsFilePuts(fp, "State Stopped\n"); | |
788 | cupsFilePrintf(fp, "StateMessage %s\n", pclass->state_message); | |
789 | } | |
790 | else | |
791 | cupsFilePuts(fp, "State Idle\n"); | |
792 | ||
793 | cupsFilePrintf(fp, "StateTime %d\n", (int)pclass->state_time); | |
794 | ||
795 | if (pclass->accepting) | |
796 | cupsFilePuts(fp, "Accepting Yes\n"); | |
797 | else | |
798 | cupsFilePuts(fp, "Accepting No\n"); | |
799 | ||
800 | if (pclass->shared) | |
801 | cupsFilePuts(fp, "Shared Yes\n"); | |
802 | else | |
803 | cupsFilePuts(fp, "Shared No\n"); | |
804 | ||
805 | cupsFilePrintf(fp, "JobSheets %s %s\n", pclass->job_sheets[0], | |
806 | pclass->job_sheets[1]); | |
807 | ||
808 | for (i = 0; i < pclass->num_printers; i ++) | |
809 | cupsFilePrintf(fp, "Printer %s\n", pclass->printers[i]->name); | |
810 | ||
811 | cupsFilePrintf(fp, "QuotaPeriod %d\n", pclass->quota_period); | |
812 | cupsFilePrintf(fp, "PageLimit %d\n", pclass->page_limit); | |
813 | cupsFilePrintf(fp, "KLimit %d\n", pclass->k_limit); | |
814 | ||
815 | for (i = 0; i < pclass->num_users; i ++) | |
816 | cupsFilePrintf(fp, "%sUser %s\n", pclass->deny_users ? "Deny" : "Allow", | |
817 | pclass->users[i]); | |
818 | ||
819 | if (pclass->op_policy) | |
820 | cupsFilePrintf(fp, "OpPolicy %s\n", pclass->op_policy); | |
821 | if (pclass->error_policy) | |
822 | cupsFilePrintf(fp, "ErrorPolicy %s\n", pclass->error_policy); | |
823 | ||
b423cd4c | 824 | for (i = pclass->num_options, option = pclass->options; |
825 | i > 0; | |
826 | i --, option ++) | |
827 | cupsFilePrintf(fp, "Option %s %s\n", option->name, option->value); | |
828 | ||
ef416fc2 | 829 | cupsFilePuts(fp, "</Class>\n"); |
830 | } | |
831 | ||
832 | cupsFileClose(fp); | |
833 | } | |
834 | ||
835 | ||
836 | /* | |
837 | * 'cupsdUpdateImplicitClasses()' - Update the accepting state of implicit | |
838 | * classes. | |
839 | */ | |
840 | ||
841 | void | |
842 | cupsdUpdateImplicitClasses(void) | |
843 | { | |
844 | int i; /* Looping var */ | |
845 | cupsd_printer_t *pclass; /* Current class */ | |
846 | int accepting; /* printer-is-accepting-jobs value */ | |
847 | ||
848 | ||
849 | for (pclass = (cupsd_printer_t *)cupsArrayFirst(ImplicitPrinters); | |
850 | pclass; | |
851 | pclass = (cupsd_printer_t *)cupsArrayNext(ImplicitPrinters)) | |
852 | { | |
853 | /* | |
854 | * Loop through the printers to come up with a composite state... | |
855 | */ | |
856 | ||
857 | for (i = 0, accepting = 0; i < pclass->num_printers; i ++) | |
858 | if ((accepting = pclass->printers[i]->accepting) != 0) | |
859 | break; | |
860 | ||
861 | pclass->accepting = accepting; | |
862 | } | |
863 | } | |
864 | ||
865 | ||
866 | /* | |
c07d5b2d | 867 | * End of "$Id: classes.c 177 2006-06-21 00:20:03Z jlovell $". |
ef416fc2 | 868 | */ |