]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2 | * "$Id: classes.c 4848 2005-11-22 04:14:43Z mike $" | |
3 | * | |
4 | * Printer class routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2005 by Easy Software Products, all rights reserved. | |
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 | { | |
339 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
340 | "cupsdLoadAllClasses: Unable to open %s - %s", line, | |
341 | strerror(errno)); | |
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 | { | |
367 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
368 | "cupsdLoadAllClasses: Loading class %s...", value); | |
369 | ||
370 | p = cupsdAddClass(value); | |
371 | p->accepting = 1; | |
372 | p->state = IPP_PRINTER_IDLE; | |
373 | ||
374 | if (!strcasecmp(line, "<DefaultClass")) | |
375 | DefaultPrinter = p; | |
376 | } | |
377 | else | |
378 | { | |
379 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
380 | "Syntax error on line %d of classes.conf.", linenum); | |
381 | return; | |
382 | } | |
383 | } | |
384 | else if (!strcasecmp(line, "</Class>")) | |
385 | { | |
386 | if (p != NULL) | |
387 | { | |
388 | cupsdSetPrinterAttrs(p); | |
389 | p = NULL; | |
390 | } | |
391 | else | |
392 | { | |
393 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
394 | "Syntax error on line %d of classes.conf.", linenum); | |
395 | return; | |
396 | } | |
397 | } | |
398 | else if (!p) | |
399 | { | |
400 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
401 | "Syntax error on line %d of classes.conf.", linenum); | |
402 | return; | |
403 | } | |
404 | else if (!strcasecmp(line, "Info")) | |
405 | { | |
406 | if (value) | |
407 | cupsdSetString(&p->info, value); | |
408 | } | |
409 | else if (!strcasecmp(line, "Location")) | |
410 | { | |
411 | if (value) | |
412 | cupsdSetString(&p->location, value); | |
413 | } | |
414 | else if (!strcasecmp(line, "Printer")) | |
415 | { | |
416 | if (!value) | |
417 | { | |
418 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
419 | "Syntax error on line %d of classes.conf.", linenum); | |
420 | return; | |
421 | } | |
422 | else if ((temp = cupsdFindPrinter(value)) == NULL) | |
423 | { | |
424 | cupsdLogMessage(CUPSD_LOG_WARN, | |
425 | "Unknown printer %s on line %d of classes.conf.", | |
426 | value, linenum); | |
427 | ||
428 | /* | |
429 | * Add the missing remote printer... | |
430 | */ | |
431 | ||
432 | if ((temp = cupsdAddPrinter(value)) != NULL) | |
433 | { | |
434 | cupsdSetString(&temp->make_model, "Remote Printer on unknown"); | |
435 | ||
436 | temp->state = IPP_PRINTER_STOPPED; | |
437 | temp->type |= CUPS_PRINTER_REMOTE; | |
438 | temp->browse_time = 2147483647; | |
439 | ||
440 | cupsdSetString(&temp->location, "Location Unknown"); | |
441 | cupsdSetString(&temp->info, "No Information Available"); | |
442 | temp->hostname[0] = '\0'; | |
443 | ||
444 | cupsdSetPrinterAttrs(temp); | |
445 | } | |
446 | } | |
447 | ||
448 | if (temp) | |
449 | cupsdAddPrinterToClass(p, temp); | |
450 | } | |
451 | else if (!strcasecmp(line, "State")) | |
452 | { | |
453 | /* | |
454 | * Set the initial queue state... | |
455 | */ | |
456 | ||
457 | if (!strcasecmp(value, "idle")) | |
458 | p->state = IPP_PRINTER_IDLE; | |
459 | else if (!strcasecmp(value, "stopped")) | |
460 | p->state = IPP_PRINTER_STOPPED; | |
461 | else | |
462 | { | |
463 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
464 | "Syntax error on line %d of classes.conf.", | |
465 | linenum); | |
466 | return; | |
467 | } | |
468 | } | |
469 | else if (!strcasecmp(line, "StateMessage")) | |
470 | { | |
471 | /* | |
472 | * Set the initial queue state message... | |
473 | */ | |
474 | ||
475 | if (value) | |
476 | strlcpy(p->state_message, value, sizeof(p->state_message)); | |
477 | } | |
478 | else if (!strcasecmp(line, "StateTime")) | |
479 | { | |
480 | /* | |
481 | * Set the state time... | |
482 | */ | |
483 | ||
484 | if (value) | |
485 | p->state_time = atoi(value); | |
486 | } | |
487 | else if (!strcasecmp(line, "Accepting")) | |
488 | { | |
489 | /* | |
490 | * Set the initial accepting state... | |
491 | */ | |
492 | ||
493 | if (value && | |
494 | (!strcasecmp(value, "yes") || | |
495 | !strcasecmp(value, "on") || | |
496 | !strcasecmp(value, "true"))) | |
497 | p->accepting = 1; | |
498 | else if (value && | |
499 | (!strcasecmp(value, "no") || | |
500 | !strcasecmp(value, "off") || | |
501 | !strcasecmp(value, "false"))) | |
502 | p->accepting = 0; | |
503 | else | |
504 | { | |
505 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
506 | "Syntax error on line %d of classes.conf.", | |
507 | linenum); | |
508 | return; | |
509 | } | |
510 | } | |
511 | else if (!strcasecmp(line, "Shared")) | |
512 | { | |
513 | /* | |
514 | * Set the initial shared state... | |
515 | */ | |
516 | ||
517 | if (value && | |
518 | (!strcasecmp(value, "yes") || | |
519 | !strcasecmp(value, "on") || | |
520 | !strcasecmp(value, "true"))) | |
521 | p->shared = 1; | |
522 | else if (value && | |
523 | (!strcasecmp(value, "no") || | |
524 | !strcasecmp(value, "off") || | |
525 | !strcasecmp(value, "false"))) | |
526 | p->shared = 0; | |
527 | else | |
528 | { | |
529 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
530 | "Syntax error on line %d of printers.conf.", | |
531 | linenum); | |
532 | return; | |
533 | } | |
534 | } | |
535 | else if (!strcasecmp(line, "JobSheets")) | |
536 | { | |
537 | /* | |
538 | * Set the initial job sheets... | |
539 | */ | |
540 | ||
541 | if (value) | |
542 | { | |
543 | for (valueptr = value; | |
544 | *valueptr && !isspace(*valueptr & 255); | |
545 | valueptr ++); | |
546 | ||
547 | if (*valueptr) | |
548 | *valueptr++ = '\0'; | |
549 | ||
550 | cupsdSetString(&p->job_sheets[0], value); | |
551 | ||
552 | while (isspace(*valueptr & 255)) | |
553 | valueptr ++; | |
554 | ||
555 | if (*valueptr) | |
556 | { | |
557 | for (value = valueptr; | |
558 | *valueptr && !isspace(*valueptr & 255); | |
559 | valueptr ++); | |
560 | ||
561 | if (*valueptr) | |
562 | *valueptr++ = '\0'; | |
563 | ||
564 | cupsdSetString(&p->job_sheets[1], value); | |
565 | } | |
566 | } | |
567 | else | |
568 | { | |
569 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
570 | "Syntax error on line %d of classes.conf.", linenum); | |
571 | return; | |
572 | } | |
573 | } | |
574 | else if (!strcasecmp(line, "AllowUser")) | |
575 | { | |
576 | if (value) | |
577 | { | |
578 | p->deny_users = 0; | |
579 | cupsdAddPrinterUser(p, value); | |
580 | } | |
581 | else | |
582 | { | |
583 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
584 | "Syntax error on line %d of classes.conf.", linenum); | |
585 | return; | |
586 | } | |
587 | } | |
588 | else if (!strcasecmp(line, "DenyUser")) | |
589 | { | |
590 | if (value) | |
591 | { | |
592 | p->deny_users = 1; | |
593 | cupsdAddPrinterUser(p, value); | |
594 | } | |
595 | else | |
596 | { | |
597 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
598 | "Syntax error on line %d of classes.conf.", linenum); | |
599 | return; | |
600 | } | |
601 | } | |
602 | else if (!strcasecmp(line, "QuotaPeriod")) | |
603 | { | |
604 | if (value) | |
605 | p->quota_period = atoi(value); | |
606 | else | |
607 | { | |
608 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
609 | "Syntax error on line %d of classes.conf.", linenum); | |
610 | return; | |
611 | } | |
612 | } | |
613 | else if (!strcasecmp(line, "PageLimit")) | |
614 | { | |
615 | if (value) | |
616 | p->page_limit = atoi(value); | |
617 | else | |
618 | { | |
619 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
620 | "Syntax error on line %d of classes.conf.", linenum); | |
621 | return; | |
622 | } | |
623 | } | |
624 | else if (!strcasecmp(line, "KLimit")) | |
625 | { | |
626 | if (value) | |
627 | p->k_limit = atoi(value); | |
628 | else | |
629 | { | |
630 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
631 | "Syntax error on line %d of classes.conf.", linenum); | |
632 | return; | |
633 | } | |
634 | } | |
635 | else if (!strcasecmp(line, "OpPolicy")) | |
636 | { | |
637 | if (value) | |
638 | cupsdSetString(&p->op_policy, value); | |
639 | else | |
640 | { | |
641 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
642 | "Syntax error on line %d of classes.conf.", linenum); | |
643 | return; | |
644 | } | |
645 | } | |
646 | else if (!strcasecmp(line, "ErrorPolicy")) | |
647 | { | |
648 | if (value) | |
649 | cupsdSetString(&p->error_policy, value); | |
650 | else | |
651 | { | |
652 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
653 | "Syntax error on line %d of classes.conf.", linenum); | |
654 | return; | |
655 | } | |
656 | } | |
657 | else | |
658 | { | |
659 | /* | |
660 | * Something else we don't understand... | |
661 | */ | |
662 | ||
663 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
664 | "Unknown configuration directive %s on line %d of classes.conf.", | |
665 | line, linenum); | |
666 | } | |
667 | } | |
668 | ||
669 | cupsFileClose(fp); | |
670 | } | |
671 | ||
672 | ||
673 | /* | |
674 | * 'cupsdSaveAllClasses()' - Save classes to the classes.conf file. | |
675 | */ | |
676 | ||
677 | void | |
678 | cupsdSaveAllClasses(void) | |
679 | { | |
680 | cups_file_t *fp; /* classes.conf file */ | |
681 | char temp[1024]; /* Temporary string */ | |
682 | char backup[1024]; /* classes.conf.O file */ | |
683 | cupsd_printer_t *pclass; /* Current printer class */ | |
684 | int i; /* Looping var */ | |
685 | time_t curtime; /* Current time */ | |
686 | struct tm *curdate; /* Current date */ | |
687 | ||
688 | ||
689 | /* | |
690 | * Create the classes.conf file... | |
691 | */ | |
692 | ||
693 | snprintf(temp, sizeof(temp), "%s/classes.conf", ServerRoot); | |
694 | snprintf(backup, sizeof(backup), "%s/classes.conf.O", ServerRoot); | |
695 | ||
696 | if (rename(temp, backup)) | |
697 | { | |
698 | if (errno != ENOENT) | |
699 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to backup classes.conf - %s", | |
700 | strerror(errno)); | |
701 | } | |
702 | ||
703 | if ((fp = cupsFileOpen(temp, "w")) == NULL) | |
704 | { | |
705 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to save classes.conf - %s", | |
706 | strerror(errno)); | |
707 | ||
708 | if (rename(backup, temp)) | |
709 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to restore classes.conf - %s", | |
710 | strerror(errno)); | |
711 | return; | |
712 | } | |
713 | else | |
714 | cupsdLogMessage(CUPSD_LOG_INFO, "Saving classes.conf..."); | |
715 | ||
716 | /* | |
717 | * Restrict access to the file... | |
718 | */ | |
719 | ||
720 | fchown(cupsFileNumber(fp), RunUser, Group); | |
721 | fchmod(cupsFileNumber(fp), ConfigFilePerm); | |
722 | ||
723 | /* | |
724 | * Write a small header to the file... | |
725 | */ | |
726 | ||
727 | curtime = time(NULL); | |
728 | curdate = localtime(&curtime); | |
729 | strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate); | |
730 | ||
731 | cupsFilePuts(fp, "# Class configuration file for " CUPS_SVERSION "\n"); | |
732 | cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp); | |
733 | ||
734 | /* | |
735 | * Write each local class known to the system... | |
736 | */ | |
737 | ||
738 | for (pclass = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
739 | pclass; | |
740 | pclass = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
741 | { | |
742 | /* | |
743 | * Skip remote destinations and regular printers... | |
744 | */ | |
745 | ||
746 | if ((pclass->type & CUPS_PRINTER_REMOTE) || | |
747 | (pclass->type & CUPS_PRINTER_IMPLICIT) || | |
748 | !(pclass->type & CUPS_PRINTER_CLASS)) | |
749 | continue; | |
750 | ||
751 | /* | |
752 | * Write printers as needed... | |
753 | */ | |
754 | ||
755 | if (pclass == DefaultPrinter) | |
756 | cupsFilePrintf(fp, "<DefaultClass %s>\n", pclass->name); | |
757 | else | |
758 | cupsFilePrintf(fp, "<Class %s>\n", pclass->name); | |
759 | ||
760 | if (pclass->info) | |
761 | cupsFilePrintf(fp, "Info %s\n", pclass->info); | |
762 | ||
763 | if (pclass->location) | |
764 | cupsFilePrintf(fp, "Location %s\n", pclass->location); | |
765 | ||
766 | if (pclass->state == IPP_PRINTER_STOPPED) | |
767 | { | |
768 | cupsFilePuts(fp, "State Stopped\n"); | |
769 | cupsFilePrintf(fp, "StateMessage %s\n", pclass->state_message); | |
770 | } | |
771 | else | |
772 | cupsFilePuts(fp, "State Idle\n"); | |
773 | ||
774 | cupsFilePrintf(fp, "StateTime %d\n", (int)pclass->state_time); | |
775 | ||
776 | if (pclass->accepting) | |
777 | cupsFilePuts(fp, "Accepting Yes\n"); | |
778 | else | |
779 | cupsFilePuts(fp, "Accepting No\n"); | |
780 | ||
781 | if (pclass->shared) | |
782 | cupsFilePuts(fp, "Shared Yes\n"); | |
783 | else | |
784 | cupsFilePuts(fp, "Shared No\n"); | |
785 | ||
786 | cupsFilePrintf(fp, "JobSheets %s %s\n", pclass->job_sheets[0], | |
787 | pclass->job_sheets[1]); | |
788 | ||
789 | for (i = 0; i < pclass->num_printers; i ++) | |
790 | cupsFilePrintf(fp, "Printer %s\n", pclass->printers[i]->name); | |
791 | ||
792 | cupsFilePrintf(fp, "QuotaPeriod %d\n", pclass->quota_period); | |
793 | cupsFilePrintf(fp, "PageLimit %d\n", pclass->page_limit); | |
794 | cupsFilePrintf(fp, "KLimit %d\n", pclass->k_limit); | |
795 | ||
796 | for (i = 0; i < pclass->num_users; i ++) | |
797 | cupsFilePrintf(fp, "%sUser %s\n", pclass->deny_users ? "Deny" : "Allow", | |
798 | pclass->users[i]); | |
799 | ||
800 | if (pclass->op_policy) | |
801 | cupsFilePrintf(fp, "OpPolicy %s\n", pclass->op_policy); | |
802 | if (pclass->error_policy) | |
803 | cupsFilePrintf(fp, "ErrorPolicy %s\n", pclass->error_policy); | |
804 | ||
805 | cupsFilePuts(fp, "</Class>\n"); | |
806 | } | |
807 | ||
808 | cupsFileClose(fp); | |
809 | } | |
810 | ||
811 | ||
812 | /* | |
813 | * 'cupsdUpdateImplicitClasses()' - Update the accepting state of implicit | |
814 | * classes. | |
815 | */ | |
816 | ||
817 | void | |
818 | cupsdUpdateImplicitClasses(void) | |
819 | { | |
820 | int i; /* Looping var */ | |
821 | cupsd_printer_t *pclass; /* Current class */ | |
822 | int accepting; /* printer-is-accepting-jobs value */ | |
823 | ||
824 | ||
825 | for (pclass = (cupsd_printer_t *)cupsArrayFirst(ImplicitPrinters); | |
826 | pclass; | |
827 | pclass = (cupsd_printer_t *)cupsArrayNext(ImplicitPrinters)) | |
828 | { | |
829 | /* | |
830 | * Loop through the printers to come up with a composite state... | |
831 | */ | |
832 | ||
833 | for (i = 0, accepting = 0; i < pclass->num_printers; i ++) | |
834 | if ((accepting = pclass->printers[i]->accepting) != 0) | |
835 | break; | |
836 | ||
837 | pclass->accepting = accepting; | |
838 | } | |
839 | } | |
840 | ||
841 | ||
842 | /* | |
843 | * End of "$Id: classes.c 4848 2005-11-22 04:14:43Z mike $". | |
844 | */ |