4 * Directory services routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdLoadRemoteCache() - Load the remote printer cache.
27 * cupsdProcessBrowseData() - Process new browse data.
28 * cupsdProcessImplicitClasses() - Create/update implicit classes as needed.
29 * cupsdSaveRemoteCache() - Save the remote printer cache.
30 * cupsdSendBrowseDelete() - Send a "browse delete" message for a
32 * cupsdSendBrowseList() - Send new browsing information as necessary.
33 * cupsdSendCUPSBrowse() - Send new browsing information using the
35 * cupsdSendSLPBrowse() - Register the specified printer with SLP.
36 * cupsdStartBrowsing() - Start sending and receiving broadcast
38 * cupsdStartPolling() - Start polling servers as needed.
39 * cupsdStopBrowsing() - Stop sending and receiving broadcast
41 * cupsdStopPolling() - Stop polling servers as needed.
42 * cupsdUpdateCUPSBrowse() - Update the browse lists using the CUPS
44 * cupsdUpdatePolling() - Read status messages from the poll daemons.
45 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
46 * slp_attr_callback() - SLP attribute callback
47 * slp_dereg_printer() - SLPDereg() the specified printer
48 * slp_get_attr() - Get an attribute from an SLP registration.
49 * slp_reg_callback() - Empty SLPRegReport.
50 * slp_url_callback() - SLP service url callback
54 * Include necessary headers...
67 * SLP service name for CUPS...
70 # define SLP_CUPS_SRVTYPE "service:printer"
71 # define SLP_CUPS_SRVLEN 15
75 * Printer service URL structure
78 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
80 struct _slpsrvurl_s
*next
; /* Next URL in list */
81 char url
[HTTP_MAX_URI
];
90 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
91 SLPError errcode
, void *cookie
);
92 static void slp_dereg_printer(cupsd_printer_t
*p
);
93 static int slp_get_attr(const char *attrlist
, const char *tag
,
95 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
97 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
98 unsigned short lifetime
,
99 SLPError errcode
, void *cookie
);
100 #endif /* HAVE_LIBSLP */
104 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
108 cupsdLoadRemoteCache(void)
110 cups_file_t
*fp
; /* remote.cache file */
111 int linenum
; /* Current line number */
112 char line
[1024], /* Line from file */
113 *value
, /* Pointer to value */
114 *valueptr
; /* Pointer into value */
115 cupsd_printer_t
*p
; /* Current printer */
116 time_t now
; /* Current time */
120 * Open the remote.cache file...
123 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
124 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
128 * Read printer configurations until we hit EOF...
135 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
138 * Decode the directive...
141 if (!strcasecmp(line
, "<Printer") ||
142 !strcasecmp(line
, "<DefaultPrinter"))
145 * <Printer name> or <DefaultPrinter name>
148 if (p
== NULL
&& value
)
151 * Add the printer and a base file type...
154 cupsdLogMessage(CUPSD_LOG_DEBUG
,
155 "cupsdLoadRemoteCache: Loading printer %s...", value
);
157 p
= cupsdAddPrinter(value
);
159 p
->state
= IPP_PRINTER_IDLE
;
160 p
->type
|= CUPS_PRINTER_REMOTE
;
161 p
->browse_time
= now
+ BrowseTimeout
;
164 * Set the default printer as needed...
167 if (!strcasecmp(line
, "<DefaultPrinter"))
172 cupsdLogMessage(CUPSD_LOG_ERROR
,
173 "Syntax error on line %d of remote.cache.", linenum
);
177 else if (!strcasecmp(line
, "<Class") ||
178 !strcasecmp(line
, "<DefaultClass"))
181 * <Class name> or <DefaultClass name>
184 if (p
== NULL
&& value
)
187 * Add the printer and a base file type...
190 cupsdLogMessage(CUPSD_LOG_DEBUG
,
191 "cupsdLoadRemoteCache: Loading class %s...", value
);
193 p
= cupsdAddClass(value
);
195 p
->state
= IPP_PRINTER_IDLE
;
196 p
->type
|= CUPS_PRINTER_REMOTE
;
197 p
->browse_time
= now
+ BrowseTimeout
;
200 * Set the default printer as needed...
203 if (!strcasecmp(line
, "<DefaultClass"))
208 cupsdLogMessage(CUPSD_LOG_ERROR
,
209 "Syntax error on line %d of remote.cache.", linenum
);
213 else if (!strcasecmp(line
, "</Printer>") ||
214 !strcasecmp(line
, "</Class>"))
219 * Close out the current printer...
222 cupsdSetPrinterAttrs(p
);
228 cupsdLogMessage(CUPSD_LOG_ERROR
,
229 "Syntax error on line %d of remote.cache.", linenum
);
235 cupsdLogMessage(CUPSD_LOG_ERROR
,
236 "Syntax error on line %d of remote.cache.", linenum
);
239 else if (!strcasecmp(line
, "Info"))
242 cupsdSetString(&p
->info
, value
);
244 else if (!strcasecmp(line
, "MakeModel"))
247 cupsdSetString(&p
->make_model
, value
);
249 else if (!strcasecmp(line
, "Location"))
252 cupsdSetString(&p
->location
, value
);
254 else if (!strcasecmp(line
, "DeviceURI"))
257 cupsdSetString(&p
->device_uri
, value
);
260 cupsdLogMessage(CUPSD_LOG_ERROR
,
261 "Syntax error on line %d of remote.cache.", linenum
);
265 else if (!strcasecmp(line
, "State"))
268 * Set the initial queue state...
271 if (value
&& !strcasecmp(value
, "idle"))
272 p
->state
= IPP_PRINTER_IDLE
;
273 else if (value
&& !strcasecmp(value
, "stopped"))
274 p
->state
= IPP_PRINTER_STOPPED
;
277 cupsdLogMessage(CUPSD_LOG_ERROR
,
278 "Syntax error on line %d of remote.cache.", linenum
);
282 else if (!strcasecmp(line
, "StateMessage"))
285 * Set the initial queue state message...
289 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
291 else if (!strcasecmp(line
, "Accepting"))
294 * Set the initial accepting state...
298 (!strcasecmp(value
, "yes") ||
299 !strcasecmp(value
, "on") ||
300 !strcasecmp(value
, "true")))
303 (!strcasecmp(value
, "no") ||
304 !strcasecmp(value
, "off") ||
305 !strcasecmp(value
, "false")))
309 cupsdLogMessage(CUPSD_LOG_ERROR
,
310 "Syntax error on line %d of remote.cache.", linenum
);
314 else if (!strcasecmp(line
, "Type"))
317 p
->type
= atoi(value
);
320 cupsdLogMessage(CUPSD_LOG_ERROR
,
321 "Syntax error on line %d of remote.cache.", linenum
);
325 else if (!strcasecmp(line
, "BrowseTime"))
329 time_t t
= atoi(value
);
331 if (t
> (now
+ BrowseInterval
))
336 cupsdLogMessage(CUPSD_LOG_ERROR
,
337 "Syntax error on line %d of remote.cache.", linenum
);
341 else if (!strcasecmp(line
, "JobSheets"))
344 * Set the initial job sheets...
349 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
354 cupsdSetString(&p
->job_sheets
[0], value
);
356 while (isspace(*valueptr
& 255))
361 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
366 cupsdSetString(&p
->job_sheets
[1], value
);
371 cupsdLogMessage(CUPSD_LOG_ERROR
,
372 "Syntax error on line %d of remote.cache.", linenum
);
376 else if (!strcasecmp(line
, "AllowUser"))
381 cupsdAddPrinterUser(p
, value
);
385 cupsdLogMessage(CUPSD_LOG_ERROR
,
386 "Syntax error on line %d of remote.cache.", linenum
);
390 else if (!strcasecmp(line
, "DenyUser"))
395 cupsdAddPrinterUser(p
, value
);
399 cupsdLogMessage(CUPSD_LOG_ERROR
,
400 "Syntax error on line %d of remote.cache.", linenum
);
407 * Something else we don't understand...
410 cupsdLogMessage(CUPSD_LOG_ERROR
,
411 "Unknown configuration directive %s on line %d of remote.cache.",
419 * Do auto-classing if needed...
422 cupsdProcessImplicitClasses();
427 * 'cupsdProcessBrowseData()' - Process new browse data.
431 cupsdProcessBrowseData(
432 const char *uri
, /* I - URI of printer/class */
433 cups_ptype_t type
, /* I - Printer type */
434 ipp_pstate_t state
, /* I - Printer state */
435 const char *location
, /* I - Printer location */
436 const char *info
, /* I - Printer information */
437 const char *make_model
, /* I - Printer make and model */
438 int num_attrs
, /* I - Number of attributes */
439 cups_option_t
*attrs
) /* I - Attributes */
441 int update
; /* Update printer attributes? */
442 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
443 method
[HTTP_MAX_URI
], /* Method portion of URI */
444 username
[HTTP_MAX_URI
], /* Username portion of URI */
445 host
[HTTP_MAX_URI
], /* Host portion of URI */
446 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
447 int port
; /* Port portion of URI */
448 char name
[IPP_MAX_NAME
], /* Name of printer */
449 *hptr
, /* Pointer into hostname */
450 *sptr
; /* Pointer into ServerName */
451 char local_make_model
[IPP_MAX_NAME
];
452 /* Local make and model */
453 cupsd_printer_t
*p
; /* Printer information */
454 const char *ipp_options
; /* ipp-options value */
458 * Pull the URI apart to see if this is a local or remote printer...
461 httpSeparateURI(uri
, method
, sizeof(method
), username
, sizeof(username
),
462 host
, sizeof(host
), &port
, resource
, sizeof(resource
));
465 * Determine if the URI contains any illegal characters in it...
468 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
469 (strncmp(resource
, "/printers/", 10) &&
470 strncmp(resource
, "/classes/", 9)))
472 cupsdLogMessage(CUPSD_LOG_ERROR
,
473 "cupsdProcessBrowseData: Bad printer URI in browse data: %s",
478 if (strchr(resource
, '?') ||
479 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
480 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
482 cupsdLogMessage(CUPSD_LOG_ERROR
,
483 "cupsdProcessBrowseData: Bad resource in browse data: %s",
489 * OK, this isn't a local printer; add any remote options...
492 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
494 if (BrowseRemoteOptions
)
496 if (BrowseRemoteOptions
[0] == '?')
499 * Override server-supplied options...
502 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
504 else if (ipp_options
)
507 * Combine the server and local options...
510 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
511 BrowseRemoteOptions
);
516 * Just use the local options...
519 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
524 else if (ipp_options
)
527 * Just use the server-supplied options...
530 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
535 * See if we already have it listed in the Printers list, and add it if not...
538 type
|= CUPS_PRINTER_REMOTE
;
539 type
&= ~CUPS_PRINTER_IMPLICIT
;
541 hptr
= strchr(host
, '.');
542 sptr
= strchr(ServerName
, '.');
544 if (sptr
!= NULL
&& hptr
!= NULL
)
547 * Strip the common domain name components...
552 if (!strcasecmp(hptr
, sptr
))
558 hptr
= strchr(hptr
+ 1, '.');
562 if (type
& CUPS_PRINTER_CLASS
)
565 * Remote destination is a class...
568 if (!strncmp(resource
, "/classes/", 9))
569 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
573 if ((p
= cupsdFindClass(name
)) == NULL
&& BrowseShortNames
)
575 if ((p
= cupsdFindClass(resource
+ 9)) != NULL
)
577 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
580 * Nope, this isn't the same host; if the hostname isn't the local host,
581 * add it to the other class and then find a class using the full host
585 if (p
->type
& CUPS_PRINTER_REMOTE
)
587 cupsdLogMessage(CUPSD_LOG_INFO
,
588 "Renamed remote class \"%s\" to \"%s@%s\"...",
589 p
->name
, p
->name
, p
->hostname
);
590 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
591 "Class \'%s\' deleted by directory services.",
594 cupsArrayRemove(Printers
, p
);
595 cupsdSetStringf(&p
->name
, "%s@%s", p
->name
, p
->hostname
);
596 cupsdSetPrinterAttrs(p
);
597 cupsArrayAdd(Printers
, p
);
599 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
600 "Class \'%s\' added by directory services.",
606 else if (!p
->hostname
)
609 * Hostname not set, so this must be a cached remote printer
610 * that was created for a pending print job...
613 cupsdSetString(&p
->hostname
, host
);
614 cupsdSetString(&p
->uri
, uri
);
615 cupsdSetString(&p
->device_uri
, uri
);
622 * Use the short name for this shared class.
625 strlcpy(name
, resource
+ 9, sizeof(name
));
628 else if (p
&& !p
->hostname
)
631 * Hostname not set, so this must be a cached remote printer
632 * that was created for a pending print job...
635 cupsdSetString(&p
->hostname
, host
);
636 cupsdSetString(&p
->uri
, uri
);
637 cupsdSetString(&p
->device_uri
, uri
);
644 * Class doesn't exist; add it...
647 p
= cupsdAddClass(name
);
649 cupsdLogMessage(CUPSD_LOG_INFO
, "Added remote class \"%s\"...", name
);
651 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
652 "Class \'%s\' added by directory services.", name
);
655 * Force the URI to point to the real server...
658 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
660 cupsdSetString(&p
->uri
, uri
);
661 cupsdSetString(&p
->device_uri
, uri
);
662 cupsdSetString(&p
->hostname
, host
);
670 * Remote destination is a printer...
673 if (strncmp(resource
, "/printers/", 10) == 0)
674 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
678 if ((p
= cupsdFindPrinter(name
)) == NULL
&& BrowseShortNames
)
680 if ((p
= cupsdFindPrinter(resource
+ 10)) != NULL
)
682 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
685 * Nope, this isn't the same host; if the hostname isn't the local host,
686 * add it to the other printer and then find a printer using the full host
690 if (p
->type
& CUPS_PRINTER_REMOTE
)
692 cupsdLogMessage(CUPSD_LOG_INFO
,
693 "Renamed remote printer \"%s\" to \"%s@%s\"...",
694 p
->name
, p
->name
, p
->hostname
);
695 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
696 "Printer \'%s\' deleted by directory services.",
699 cupsArrayRemove(Printers
, p
);
700 cupsdSetStringf(&p
->name
, "%s@%s", p
->name
, p
->hostname
);
701 cupsdSetPrinterAttrs(p
);
702 cupsArrayAdd(Printers
, p
);
704 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
705 "Printer \'%s\' added by directory services.",
711 else if (!p
->hostname
)
714 * Hostname not set, so this must be a cached remote printer
715 * that was created for a pending print job...
718 cupsdSetString(&p
->hostname
, host
);
719 cupsdSetString(&p
->uri
, uri
);
720 cupsdSetString(&p
->device_uri
, uri
);
727 * Use the short name for this shared printer.
730 strlcpy(name
, resource
+ 10, sizeof(name
));
733 else if (p
&& !p
->hostname
)
736 * Hostname not set, so this must be a cached remote printer
737 * that was created for a pending print job...
740 cupsdSetString(&p
->hostname
, host
);
741 cupsdSetString(&p
->uri
, uri
);
742 cupsdSetString(&p
->device_uri
, uri
);
749 * Printer doesn't exist; add it...
752 p
= cupsdAddPrinter(name
);
754 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
755 "Printer \'%s\' added by directory services.", name
);
757 cupsdLogMessage(CUPSD_LOG_INFO
, "Added remote printer \"%s\"...", name
);
760 * Force the URI to point to the real server...
763 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
765 cupsdSetString(&p
->hostname
, host
);
766 cupsdSetString(&p
->uri
, uri
);
767 cupsdSetString(&p
->device_uri
, uri
);
774 * Update the state...
778 p
->browse_time
= time(NULL
);
780 if (type
& CUPS_PRINTER_REJECTING
)
782 type
&= ~CUPS_PRINTER_REJECTING
;
790 else if (!p
->accepting
)
802 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
804 cupsdSetString(&p
->location
, location
);
808 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
810 cupsdSetString(&p
->info
, info
);
814 if (!make_model
|| !make_model
[0])
816 if (type
& CUPS_PRINTER_CLASS
)
817 snprintf(local_make_model
, sizeof(local_make_model
),
818 "Remote Class on %s", host
);
820 snprintf(local_make_model
, sizeof(local_make_model
),
821 "Remote Printer on %s", host
);
824 snprintf(local_make_model
, sizeof(local_make_model
),
825 "%s on %s", make_model
, host
);
827 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
829 cupsdSetString(&p
->make_model
, local_make_model
);
833 if (type
& CUPS_PRINTER_DELETE
)
835 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
836 "%s \'%s\' deleted by directory services.",
837 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
839 cupsdExpireSubscriptions(p
, NULL
);
841 cupsdDeletePrinter(p
, 1);
842 cupsdUpdateImplicitClasses();
846 cupsdSetPrinterAttrs(p
);
847 cupsdUpdateImplicitClasses();
851 * See if we have a default printer... If not, make the first printer the
855 if (DefaultPrinter
== NULL
&& Printers
!= NULL
)
856 DefaultPrinter
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
859 * Do auto-classing if needed...
862 cupsdProcessImplicitClasses();
865 * Update the printcap file...
868 cupsdWritePrintcap();
873 * 'cupsdProcessImplicitClasses()' - Create/update implicit classes as needed.
877 cupsdProcessImplicitClasses(void)
879 int i
; /* Looping var */
880 int update
; /* Update printer attributes? */
881 char name
[IPP_MAX_NAME
], /* Name of printer */
882 *hptr
; /* Pointer into hostname */
883 cupsd_printer_t
*p
, /* Printer information */
884 *pclass
, /* Printer class */
885 *first
; /* First printer in class */
886 int offset
, /* Offset of name */
887 len
; /* Length of name */
890 if (!ImplicitClasses
|| !Printers
)
894 * Loop through all available printers and create classes as needed...
897 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
898 update
= 0, pclass
= NULL
, first
= NULL
;
900 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
903 * Skip implicit classes...
906 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
913 * If len == 0, get the length of this printer name up to the "@"
917 cupsArraySave(Printers
);
920 strncasecmp(p
->name
, name
+ offset
, len
) == 0 &&
921 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
924 * We have more than one printer with the same name; see if
925 * we have a class, and if this printer is a member...
928 if (pclass
&& strcasecmp(pclass
->name
, name
))
931 cupsdSetPrinterAttrs(pclass
);
937 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
940 * Need to add the class...
943 pclass
= cupsdAddPrinter(name
);
944 cupsArrayAdd(ImplicitPrinters
, pclass
);
946 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
947 pclass
->accepting
= 1;
948 pclass
->state
= IPP_PRINTER_IDLE
;
950 cupsdSetString(&pclass
->location
, p
->location
);
951 cupsdSetString(&pclass
->info
, p
->info
);
955 cupsdLogMessage(CUPSD_LOG_INFO
, "Added implicit class \"%s\"...",
961 for (i
= 0; i
< pclass
->num_printers
; i
++)
962 if (pclass
->printers
[i
] == first
)
965 if (i
>= pclass
->num_printers
)
967 first
->in_implicit_class
= 1;
968 cupsdAddPrinterToClass(pclass
, first
);
974 for (i
= 0; i
< pclass
->num_printers
; i
++)
975 if (pclass
->printers
[i
] == p
)
978 if (i
>= pclass
->num_printers
)
980 p
->in_implicit_class
= 1;
981 cupsdAddPrinterToClass(pclass
, p
);
988 * First time around; just get name length and mark it as first
992 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
993 len
= hptr
- p
->name
;
995 len
= strlen(p
->name
);
997 strncpy(name
, p
->name
, len
);
1001 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
1002 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
1005 * Can't use same name as a local printer; add "Any" to the
1006 * front of the name, unless we have explicitly disabled
1007 * the "ImplicitAnyClasses"...
1010 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
1013 * Add "Any" to the class name...
1016 strcpy(name
, "Any");
1017 strncpy(name
+ 3, p
->name
, len
);
1018 name
[len
+ 3] = '\0';
1024 * Don't create an implicit class if we have a local printer
1025 * with the same name...
1029 cupsArrayRestore(Printers
);
1037 cupsArrayRestore(Printers
);
1041 * Update the last printer class as needed...
1044 if (pclass
&& update
)
1045 cupsdSetPrinterAttrs(pclass
);
1050 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
1054 cupsdSaveRemoteCache(void)
1056 int i
; /* Looping var */
1057 cups_file_t
*fp
; /* printers.conf file */
1058 char temp
[1024]; /* Temporary string */
1059 cupsd_printer_t
*printer
; /* Current printer class */
1060 time_t curtime
; /* Current time */
1061 struct tm
*curdate
; /* Current date */
1065 * Create the remote.cache file...
1068 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
1070 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1072 cupsdLogMessage(CUPSD_LOG_ERROR
,
1073 "Unable to save remote.cache - %s", strerror(errno
));
1077 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving remote.cache...");
1080 * Restrict access to the file...
1083 fchown(cupsFileNumber(fp
), getuid(), Group
);
1084 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1087 * Write a small header to the file...
1090 curtime
= time(NULL
);
1091 curdate
= localtime(&curtime
);
1092 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1094 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
1095 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1098 * Write each local printer known to the system...
1101 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1103 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1106 * Skip local destinations...
1109 if (!(printer
->type
& CUPS_PRINTER_REMOTE
))
1113 * Write printers as needed...
1116 if (printer
== DefaultPrinter
)
1117 cupsFilePuts(fp
, "<Default");
1119 cupsFilePutChar(fp
, '<');
1121 if (printer
->type
& CUPS_PRINTER_CLASS
)
1122 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
1124 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
1126 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
1128 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_time
);
1131 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
1133 if (printer
->make_model
)
1134 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
1136 if (printer
->location
)
1137 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
1139 if (printer
->device_uri
)
1140 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
1142 if (printer
->state
== IPP_PRINTER_STOPPED
)
1144 cupsFilePuts(fp
, "State Stopped\n");
1145 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
1148 cupsFilePuts(fp
, "State Idle\n");
1150 if (printer
->accepting
)
1151 cupsFilePuts(fp
, "Accepting Yes\n");
1153 cupsFilePuts(fp
, "Accepting No\n");
1155 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
1156 printer
->job_sheets
[1]);
1158 for (i
= 0; i
< printer
->num_users
; i
++)
1159 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
1162 if (printer
->type
& CUPS_PRINTER_CLASS
)
1163 cupsFilePuts(fp
, "</Class>\n");
1165 cupsFilePuts(fp
, "</Printer>\n");
1173 * 'cupsdSendBrowseDelete()' - Send a "browse delete" message for a printer.
1177 cupsdSendBrowseDelete(
1178 cupsd_printer_t
*p
) /* I - Printer to delete */
1181 * Only announce if browsing is enabled...
1184 if (!Browsing
|| !p
->shared
)
1188 * First mark the printer for deletion...
1191 p
->type
|= CUPS_PRINTER_DELETE
;
1194 * Announce the deletion...
1197 if (BrowseLocalProtocols
& BROWSE_CUPS
)
1198 cupsdSendCUPSBrowse(p
);
1200 if (BrowseLocalProtocols
& BROWSE_SLP
)
1201 slp_dereg_printer(p
);
1202 #endif /* HAVE_LIBSLP */
1207 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
1211 cupsdSendBrowseList(void)
1213 int count
; /* Number of dests to update */
1214 cupsd_printer_t
*p
; /* Current printer */
1215 time_t ut
, /* Minimum update time */
1216 to
; /* Timeout time */
1219 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
1223 * Compute the update and timeout times...
1226 ut
= time(NULL
) - BrowseInterval
;
1227 to
= time(NULL
) - BrowseTimeout
;
1230 * Figure out how many printers need an update...
1233 if (BrowseInterval
> 0)
1235 int max_count
; /* Maximum number to update */
1239 * Throttle the number of printers we'll be updating this time
1240 * around based on the number of queues that need updating and
1241 * the maximum number of queues to update each second...
1244 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
1246 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1247 count
< max_count
&& p
!= NULL
;
1248 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1249 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
1250 p
->shared
&& p
->browse_time
< ut
)
1254 * Loop through all of the printers and send local updates as needed...
1258 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
1260 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1264 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1267 * Check for wraparound...
1271 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1275 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1278 else if (p
->browse_time
< ut
)
1281 * Need to send an update...
1286 p
->browse_time
= time(NULL
);
1288 if (BrowseLocalProtocols
& BROWSE_CUPS
)
1289 cupsdSendCUPSBrowse(p
);
1292 if (BrowseLocalProtocols
& BROWSE_SLP
)
1293 cupsdSendSLPBrowse(p
);
1294 #endif /* HAVE_LIBSLP */
1299 * Save where we left off so that all printers get updated...
1306 * Loop through all of the printers and send local updates as needed...
1309 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1311 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1314 * If this is a remote queue, see if it needs to be timed out...
1317 if (p
->type
& CUPS_PRINTER_REMOTE
)
1319 if (p
->browse_time
< to
)
1321 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1322 "%s \'%s\' deleted by directory services (timeout).",
1323 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
1326 cupsdLogMessage(CUPSD_LOG_INFO
,
1327 "Remote destination \"%s\" has timed out; deleting it...",
1330 cupsArraySave(Printers
);
1331 cupsdDeletePrinter(p
, 1);
1332 cupsArrayRestore(Printers
);
1340 * 'cupsdSendCUPSBrowse()' - Send new browsing information using the CUPS protocol.
1344 cupsdSendCUPSBrowse(cupsd_printer_t
*p
) /* I - Printer to send */
1346 int i
; /* Looping var */
1347 cups_ptype_t type
; /* Printer type */
1348 cupsd_dirsvc_addr_t
*b
; /* Browse address */
1349 int bytes
; /* Length of packet */
1350 char packet
[1453]; /* Browse data packet */
1351 char uri
[1024]; /* Printer URI */
1352 char options
[1024]; /* Browse local options */
1353 cupsd_netif_t
*iface
; /* Network interface */
1357 * Figure out the printer type value...
1360 type
= p
->type
| CUPS_PRINTER_REMOTE
;
1363 type
|= CUPS_PRINTER_REJECTING
;
1366 * Initialize the browse options...
1369 if (BrowseLocalOptions
)
1370 snprintf(options
, sizeof(options
), " ipp-options=%s", BrowseLocalOptions
);
1375 * Send a packet to each browse address...
1378 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
1382 * Send the browse packet to one or more interfaces...
1385 if (strcmp(b
->iface
, "*") == 0)
1388 * Send to all local interfaces...
1393 for (iface
= NetIFList
; iface
!= NULL
; iface
= iface
->next
)
1396 * Only send to local, IPv4 interfaces...
1399 if (!iface
->is_local
|| !iface
->port
||
1400 iface
->address
.addr
.sa_family
!= AF_INET
)
1403 httpAssembleURIf(uri
, sizeof(uri
), "ipp", NULL
, iface
->hostname
,
1405 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s%s" :
1408 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
1409 type
, p
->state
, uri
, p
->location
? p
->location
: "",
1410 p
->info
? p
->info
: "",
1411 p
->make_model
? p
->make_model
: "Unknown", options
);
1413 bytes
= strlen(packet
);
1415 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1416 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
1417 iface
->name
, packet
);
1419 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
1421 sendto(BrowseSocket
, packet
, bytes
, 0,
1422 (struct sockaddr
*)&(iface
->broadcast
),
1423 sizeof(struct sockaddr_in
));
1426 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
1429 * Send to the named interface using the IPv4 address...
1433 if (strcasecmp(b
->iface
, iface
->name
))
1438 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
1441 iface
= iface
->next
;
1445 httpAssembleURIf(uri
, sizeof(uri
), "ipp", NULL
, iface
->hostname
,
1447 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s%s" :
1450 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
1451 type
, p
->state
, uri
, p
->location
? p
->location
: "",
1452 p
->info
? p
->info
: "",
1453 p
->make_model
? p
->make_model
: "Unknown", options
);
1455 bytes
= strlen(packet
);
1457 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1458 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
1459 iface
->name
, packet
);
1461 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
1463 sendto(BrowseSocket
, packet
, bytes
, 0,
1464 (struct sockaddr
*)&(iface
->broadcast
),
1465 sizeof(struct sockaddr_in
));
1472 * Send the browse packet to the indicated address using
1473 * the default server name...
1476 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
1477 type
, p
->state
, p
->uri
,
1478 p
->location
? p
->location
: "",
1479 p
->info
? p
->info
: "",
1480 p
->make_model
? p
->make_model
: "Unknown", options
);
1482 bytes
= strlen(packet
);
1483 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1484 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
1486 if (sendto(BrowseSocket
, packet
, bytes
, 0,
1487 (struct sockaddr
*)&(b
->to
),
1488 sizeof(struct sockaddr_in
)) <= 0)
1491 * Unable to send browse packet, so remove this address from the
1495 cupsdLogMessage(CUPSD_LOG_ERROR
,
1496 "cupsdSendBrowseList: sendto failed for browser %d - %s.",
1497 b
- Browsers
+ 1, strerror(errno
));
1500 memcpy(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
1511 * 'cupsdSendSLPBrowse()' - Register the specified printer with SLP.
1515 cupsdSendSLPBrowse(cupsd_printer_t
*p
) /* I - Printer to register */
1517 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
1518 attrs
[8192], /* Printer attributes */
1519 finishings
[1024], /* Finishings to support */
1520 make_model
[IPP_MAX_NAME
* 2],
1521 /* Make and model, quoted */
1522 location
[IPP_MAX_NAME
* 2],
1523 /* Location, quoted */
1524 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
1525 *src
, /* Pointer to original string */
1526 *dst
; /* Pointer to destination string */
1527 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
1528 SLPError error
; /* SLP error, if any */
1531 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendSLPBrowse(%p = \"%s\")", p
,
1535 * Make the SLP service URL that conforms to the IANA
1536 * 'printer:' template.
1539 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
1541 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
1544 * Figure out the finishings string...
1547 if (p
->type
& CUPS_PRINTER_STAPLE
)
1548 strcpy(finishings
, "staple");
1550 finishings
[0] = '\0';
1552 if (p
->type
& CUPS_PRINTER_BIND
)
1555 strlcat(finishings
, ",bind", sizeof(finishings
));
1557 strcpy(finishings
, "bind");
1560 if (p
->type
& CUPS_PRINTER_PUNCH
)
1563 strlcat(finishings
, ",punch", sizeof(finishings
));
1565 strcpy(finishings
, "punch");
1568 if (p
->type
& CUPS_PRINTER_COVER
)
1571 strlcat(finishings
, ",cover", sizeof(finishings
));
1573 strcpy(finishings
, "cover");
1576 if (p
->type
& CUPS_PRINTER_SORT
)
1579 strlcat(finishings
, ",sort", sizeof(finishings
));
1581 strcpy(finishings
, "sort");
1585 strcpy(finishings
, "none");
1588 * Quote any commas in the make and model, location, and info strings...
1591 for (src
= p
->make_model
, dst
= make_model
;
1592 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
1594 if (*src
== ',' || *src
== '\\' || *src
== ')')
1603 strcpy(make_model
, "Unknown");
1605 for (src
= p
->location
, dst
= location
;
1606 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
1608 if (*src
== ',' || *src
== '\\' || *src
== ')')
1617 strcpy(location
, "Unknown");
1619 for (src
= p
->info
, dst
= info
;
1620 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
1622 if (*src
== ',' || *src
== '\\' || *src
== ')')
1631 strcpy(info
, "Unknown");
1634 * Get the authentication value...
1637 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
1641 * Make the SLP attribute string list that conforms to
1642 * the IANA 'printer:' template.
1645 snprintf(attrs
, sizeof(attrs
),
1646 "(printer-uri-supported=%s),"
1647 "(uri-authentication-supported=%s>),"
1649 "(uri-security-supported=tls>),"
1651 "(uri-security-supported=none>),"
1652 #endif /* HAVE_SSL */
1653 "(printer-name=%s),"
1654 "(printer-location=%s),"
1655 "(printer-info=%s),"
1656 "(printer-more-info=%s),"
1657 "(printer-make-and-model=%s),"
1658 "(charset-supported=utf-8),"
1659 "(natural-language-configured=%s),"
1660 "(natural-language-supported=de,en,es,fr,it),"
1661 "(color-supported=%s),"
1662 "(finishings-supported=%s),"
1663 "(sides-supported=one-sided%s),"
1664 "(multiple-document-jobs-supported=true)"
1665 "(ipp-versions-supported=1.0,1.1)",
1666 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
1667 info
, p
->uri
, make_model
, DefaultLanguage
,
1668 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
1670 p
->type
& CUPS_PRINTER_DUPLEX
?
1671 ",two-sided-long-edge,two-sided-short-edge" : "");
1673 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
1676 * Register the printer with the SLP server...
1679 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
1680 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
1682 if (error
!= SLP_OK
)
1683 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
1686 #endif /* HAVE_LIBSLP */
1690 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1694 cupsdStartBrowsing(void)
1696 int val
; /* Socket option value */
1697 struct sockaddr_in addr
; /* Broadcast address */
1702 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1705 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1709 * Create the broadcast socket...
1712 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1714 cupsdLogMessage(CUPSD_LOG_ERROR
,
1715 "cupsdStartBrowsing: Unable to create broadcast socket - %s.",
1717 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1718 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1723 * Set the "broadcast" flag...
1727 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1729 cupsdLogMessage(CUPSD_LOG_ERROR
,
1730 "cupsdStartBrowsing: Unable to set broadcast mode - %s.",
1734 closesocket(BrowseSocket
);
1736 close(BrowseSocket
);
1740 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1741 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1746 * Bind the socket to browse port...
1749 memset(&addr
, 0, sizeof(addr
));
1750 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1751 addr
.sin_family
= AF_INET
;
1752 addr
.sin_port
= htons(BrowsePort
);
1754 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1756 cupsdLogMessage(CUPSD_LOG_ERROR
,
1757 "cupsdStartBrowsing: Unable to bind broadcast socket - %s.",
1761 closesocket(BrowseSocket
);
1763 close(BrowseSocket
);
1767 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1768 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1773 * Close the socket on exec...
1776 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1779 * Finally, add the socket to the input selection set...
1782 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1783 "cupsdStartBrowsing: Adding fd %d to InputSet...",
1786 FD_SET(BrowseSocket
, InputSet
);
1792 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1795 * Open SLP handle...
1798 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1800 cupsdLogMessage(CUPSD_LOG_ERROR
,
1801 "Unable to open an SLP handle; disabling SLP browsing!");
1802 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1803 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1806 BrowseSLPRefresh
= 0;
1808 #endif /* HAVE_LIBSLP */
1813 * 'cupsdStartPolling()' - Start polling servers as needed.
1817 cupsdStartPolling(void)
1819 int i
; /* Looping var */
1820 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1821 char polld
[1024]; /* Poll daemon path */
1822 char sport
[10]; /* Server port */
1823 char bport
[10]; /* Browser port */
1824 char interval
[10]; /* Poll interval */
1825 int statusfds
[2]; /* Status pipe */
1826 char *argv
[6]; /* Arguments */
1827 char *envp
[100]; /* Environment */
1831 * Don't do anything if we aren't polling...
1837 PollStatusBuffer
= NULL
;
1842 * Setup string arguments for polld, port and interval options.
1845 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1847 sprintf(bport
, "%d", BrowsePort
);
1850 sprintf(interval
, "%d", BrowseInterval
);
1852 strcpy(interval
, "30");
1854 argv
[0] = "cups-polld";
1860 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1863 * Create a pipe that receives the status messages from each
1867 if (cupsdOpenPipe(statusfds
))
1869 cupsdLogMessage(CUPSD_LOG_ERROR
,
1870 "Unable to create polling status pipes - %s.",
1873 PollStatusBuffer
= NULL
;
1877 PollPipe
= statusfds
[0];
1878 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1881 * Run each polling daemon, redirecting stderr to the polling pipe...
1884 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1886 sprintf(sport
, "%d", pollp
->port
);
1888 argv
[1] = pollp
->hostname
;
1890 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1,
1891 0, &(pollp
->pid
)) < 0)
1893 cupsdLogMessage(CUPSD_LOG_ERROR
,
1894 "cupsdStartPolling: Unable to fork polling daemon - %s",
1900 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1901 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1902 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1905 close(statusfds
[1]);
1908 * Finally, add the pipe to the input selection set...
1911 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1912 "cupsdStartPolling: Adding fd %d to InputSet...", PollPipe
);
1914 FD_SET(PollPipe
, InputSet
);
1919 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1923 cupsdStopBrowsing(void)
1925 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1928 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1932 * Close the socket and remove it from the input selection set.
1936 closesocket(BrowseSocket
);
1938 close(BrowseSocket
);
1941 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1942 "cupsdStopBrowsing: Removing fd %d from InputSet...",
1945 FD_CLR(BrowseSocket
, InputSet
);
1950 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1953 * Close SLP handle...
1956 SLPClose(BrowseSLPHandle
);
1958 #endif /* HAVE_LIBSLP */
1963 * 'cupsdStopPolling()' - Stop polling servers as needed.
1967 cupsdStopPolling(void)
1969 int i
; /* Looping var */
1970 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1975 cupsdStatBufDelete(PollStatusBuffer
);
1978 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1979 "cupsdStopPolling: removing fd %d from InputSet.", PollPipe
);
1980 FD_CLR(PollPipe
, InputSet
);
1983 PollStatusBuffer
= NULL
;
1986 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1988 cupsdEndProcess(pollp
->pid
, 0);
1993 * 'cupsdUpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
1997 cupsdUpdateCUPSBrowse(void)
1999 int i
; /* Looping var */
2000 int auth
; /* Authorization status */
2001 int len
; /* Length of name string */
2002 int bytes
; /* Number of bytes left */
2003 char packet
[1541], /* Broadcast packet */
2004 *pptr
; /* Pointer into packet */
2005 socklen_t srclen
; /* Length of source address */
2006 http_addr_t srcaddr
; /* Source address */
2007 char srcname
[1024]; /* Source hostname */
2008 unsigned address
[4]; /* Source address */
2009 unsigned type
; /* Printer type */
2010 unsigned state
; /* Printer state */
2011 char uri
[HTTP_MAX_URI
], /* Printer URI */
2012 method
[HTTP_MAX_URI
], /* Method portion of URI */
2013 username
[HTTP_MAX_URI
], /* Username portion of URI */
2014 host
[HTTP_MAX_URI
], /* Host portion of URI */
2015 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
2016 info
[IPP_MAX_NAME
], /* Information string */
2017 location
[IPP_MAX_NAME
], /* Location string */
2018 make_model
[IPP_MAX_NAME
];/* Make and model string */
2019 int port
; /* Port portion of URI */
2020 cupsd_netif_t
*iface
; /* Network interface */
2021 int num_attrs
; /* Number of attributes */
2022 cups_option_t
*attrs
; /* Attributes */
2026 * Read a packet from the browse socket...
2029 srclen
= sizeof(srcaddr
);
2030 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
2031 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
2034 * "Connection refused" is returned under Linux if the destination port
2035 * or address is unreachable from a previous sendto(); check for the
2036 * error here and ignore it for now...
2039 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
2041 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
2043 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browsing turned off.");
2045 cupsdStopBrowsing();
2052 packet
[bytes
] = '\0';
2055 * If we're about to sleep, ignore incoming browse packets.
2062 * Figure out where it came from...
2066 if (srcaddr
.addr
.sa_family
== AF_INET6
)
2068 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
2069 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
2070 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
2071 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
2074 #endif /* AF_INET6 */
2079 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
2082 if (HostNameLookups
)
2083 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
2085 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
2087 len
= strlen(srcname
);
2095 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
2098 * Access from localhost (127.0.0.1) is always allowed...
2106 * Do authorization checks on the domain/address...
2109 switch (BrowseACL
->order_type
)
2112 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
2115 case AUTH_ALLOW
: /* Order Deny,Allow */
2118 if (cupsdCheckAuth(address
, srcname
, len
,
2119 BrowseACL
->num_deny
, BrowseACL
->deny
))
2122 if (cupsdCheckAuth(address
, srcname
, len
,
2123 BrowseACL
->num_allow
, BrowseACL
->allow
))
2127 case AUTH_DENY
: /* Order Allow,Deny */
2130 if (cupsdCheckAuth(address
, srcname
, len
,
2131 BrowseACL
->num_allow
, BrowseACL
->allow
))
2134 if (cupsdCheckAuth(address
, srcname
, len
,
2135 BrowseACL
->num_deny
, BrowseACL
->deny
))
2144 if (auth
== AUTH_DENY
)
2146 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2147 "cupsdUpdateCUPSBrowse: Refused %d bytes from %s", bytes
,
2152 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2153 "cupsdUpdateCUPSBrowse: (%d bytes from %s) %s", bytes
,
2160 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
2162 cupsdLogMessage(CUPSD_LOG_WARN
,
2163 "cupsdUpdateCUPSBrowse: Garbled browse packet - %s", packet
);
2167 strcpy(location
, "Location Unknown");
2168 strcpy(info
, "No Information Available");
2169 make_model
[0] = '\0';
2173 if ((pptr
= strchr(packet
, '\"')) != NULL
)
2176 * Have extended information; can't use sscanf for it because not all
2177 * sscanf's allow empty strings with %[^\"]...
2180 for (i
= 0, pptr
++;
2181 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
2183 location
[i
] = *pptr
;
2191 while (*pptr
&& isspace(*pptr
& 255))
2196 for (i
= 0, pptr
++;
2197 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
2206 while (*pptr
&& isspace(*pptr
& 255))
2211 for (i
= 0, pptr
++;
2212 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
2214 make_model
[i
] = *pptr
;
2219 make_model
[i
] = '\0';
2222 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
2228 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
2229 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
2230 type
, state
, uri
, location
, info
, make_model
));
2233 * Pull the URI apart to see if this is a local or remote printer...
2236 httpSeparateURI(uri
, method
, sizeof(method
), username
, sizeof(username
),
2237 host
, sizeof(host
), &port
, resource
, sizeof(resource
));
2239 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
2242 * Check for packets from the local server...
2245 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
2247 cupsFreeOptions(num_attrs
, attrs
);
2253 for (iface
= NetIFList
; iface
!= NULL
; iface
= iface
->next
)
2254 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
2256 cupsFreeOptions(num_attrs
, attrs
);
2264 for (i
= 0; i
< NumRelays
; i
++)
2265 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
2266 if (sendto(BrowseSocket
, packet
, bytes
, 0,
2267 (struct sockaddr
*)&(Relays
[i
].to
),
2268 sizeof(http_addr_t
)) <= 0)
2270 cupsdLogMessage(CUPSD_LOG_ERROR
,
2271 "cupsdUpdateCUPSBrowse: sendto failed for relay %d - %s.",
2272 i
+ 1, strerror(errno
));
2273 cupsFreeOptions(num_attrs
, attrs
);
2278 * Process the browse data...
2281 cupsdProcessBrowseData(uri
, (cups_ptype_t
)type
, (ipp_pstate_t
)state
, location
,
2282 info
, make_model
, num_attrs
, attrs
);
2283 cupsFreeOptions(num_attrs
, attrs
);
2288 * 'cupsdUpdatePolling()' - Read status messages from the poll daemons.
2292 cupsdUpdatePolling(void)
2294 char *ptr
, /* Pointer to end of line in buffer */
2295 message
[1024]; /* Pointer to message text */
2296 int loglevel
; /* Log level for message */
2299 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
2300 message
, sizeof(message
))) != NULL
)
2301 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
2307 * All polling processes have died; stop polling...
2310 cupsdLogMessage(CUPSD_LOG_ERROR
,
2311 "cupsdUpdatePolling: all polling processes have exited!");
2319 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2323 cupsdUpdateSLPBrowse(void)
2325 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2326 *next
; /* Next service in list */
2327 cupsd_printer_t p
; /* Printer information */
2328 const char *uri
; /* Pointer to printer URI */
2329 char method
[HTTP_MAX_URI
],
2330 /* Method portion of URI */
2331 username
[HTTP_MAX_URI
],
2332 /* Username portion of URI */
2334 /* Host portion of URI */
2335 resource
[HTTP_MAX_URI
];
2336 /* Resource portion of URI */
2337 int port
; /* Port portion of URI */
2340 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdUpdateSLPBrowse() Start...");
2343 * Reset the refresh time...
2346 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2349 * Poll for remote printers using SLP...
2354 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2355 slp_url_callback
, &s
);
2358 * Loop through the list of available printers...
2364 * Save the "next" pointer...
2370 * Load a cupsd_printer_t structure with the SLP service attributes...
2373 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2376 * Process this printer entry...
2379 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2381 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2384 * Pull the URI apart to see if this is a local or remote printer...
2387 httpSeparateURI(uri
, method
, sizeof(method
), username
, sizeof(username
),
2388 host
, sizeof(host
), &port
, resource
, sizeof(resource
));
2390 if (strcasecmp(host
, ServerName
) == 0)
2394 * OK, at least an IPP printer, see if it is a CUPS printer or
2398 if (strstr(uri
, "/printers/") != NULL
)
2399 cupsdProcessBrowseData(uri
, p
.type
, IPP_PRINTER_IDLE
, p
.location
,
2400 p
.info
, p
.make_model
, 0, NULL
);
2401 else if (strstr(uri
, "/classes/") != NULL
)
2402 cupsdProcessBrowseData(uri
, p
.type
| CUPS_PRINTER_CLASS
, IPP_PRINTER_IDLE
,
2403 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2407 * Free this listing...
2413 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdUpdateSLPBrowse() End...");
2418 * 'slp_attr_callback()' - SLP attribute callback
2421 static SLPBoolean
/* O - SLP_TRUE for success */
2423 SLPHandle hslp
, /* I - SLP handle */
2424 const char *attrlist
, /* I - Attribute list */
2425 SLPError errcode
, /* I - Parsing status for this attr */
2426 void *cookie
) /* I - Current printer */
2429 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
2433 * Let the compiler know we won't be using these...
2439 * Bail if there was an error
2442 if (errcode
!= SLP_OK
)
2446 * Parse the attrlist to obtain things needed to build CUPS browse packet
2449 memset(p
, 0, sizeof(cupsd_printer_t
));
2451 p
->type
= CUPS_PRINTER_REMOTE
;
2453 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
2455 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
2457 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
2460 if (slp_get_attr(attrlist
, "(color-supported=", &tmp
))
2462 if (strcasecmp(tmp
, "true") == 0)
2463 p
->type
|= CUPS_PRINTER_COLOR
;
2465 if (slp_get_attr(attrlist
, "(finishings-supported=", &tmp
))
2467 if (strstr(tmp
, "staple"))
2468 p
->type
|= CUPS_PRINTER_STAPLE
;
2469 if (strstr(tmp
, "bind"))
2470 p
->type
|= CUPS_PRINTER_BIND
;
2471 if (strstr(tmp
, "punch"))
2472 p
->type
|= CUPS_PRINTER_PUNCH
;
2474 if (slp_get_attr(attrlist
, "(sides-supported=", &tmp
))
2476 if (strstr(tmp
,"two-sided"))
2477 p
->type
|= CUPS_PRINTER_DUPLEX
;
2479 cupsdClearString(&tmp
);
2486 * 'slp_dereg_printer()' - SLPDereg() the specified printer
2490 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
2492 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
2495 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
2497 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
2500 * Make the SLP service URL that conforms to the IANA
2501 * 'printer:' template.
2504 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
2507 * Deregister the printer...
2510 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
2516 * 'slp_get_attr()' - Get an attribute from an SLP registration.
2519 static int /* O - 0 on success */
2520 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
2521 const char *tag
, /* I - Name of attribute */
2522 char **valbuf
) /* O - Value */
2524 char *ptr1
, /* Pointer into string */
2528 cupsdClearString(valbuf
);
2530 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
2532 ptr1
+= strlen(tag
);
2534 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
2540 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
2541 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
2544 * Dequote the value...
2547 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
2548 if (*ptr1
== '\\' && ptr1
[1])
2549 _cups_strcpy(ptr1
, ptr1
+ 1);
2560 * 'slp_reg_callback()' - Empty SLPRegReport.
2564 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
2565 SLPError errcode
, /* I - Error code, if any */
2566 void *cookie
) /* I - App data */
2577 * 'slp_url_callback()' - SLP service url callback
2580 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
2582 SLPHandle hslp
, /* I - SLP handle */
2583 const char *srvurl
, /* I - URL of service */
2584 unsigned short lifetime
, /* I - Life of service */
2585 SLPError errcode
, /* I - Existing error code */
2586 void *cookie
) /* I - Pointer to service list */
2588 slpsrvurl_t
*s
, /* New service entry */
2589 **head
; /* Pointer to head of entry */
2593 * Let the compiler know we won't be using these vars...
2600 * Bail if there was an error
2603 if (errcode
!= SLP_OK
)
2607 * Grab the head of the list...
2610 head
= (slpsrvurl_t
**)cookie
;
2613 * Allocate a *temporary* slpsrvurl_t to hold this entry.
2616 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
2620 * Copy the SLP service URL...
2623 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
2626 * Link the SLP service URL into the head of the list
2636 #endif /* HAVE_LIBSLP */