2 * "$Id: dirsvc.c 5023 2006-01-29 14:39:44Z mike $"
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(HTTP_URI_CODING_ALL
, uri
, method
, sizeof(method
), username
,
462 sizeof(username
), host
, sizeof(host
), &port
, resource
,
466 * Determine if the URI contains any illegal characters in it...
469 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
470 (strncmp(resource
, "/printers/", 10) &&
471 strncmp(resource
, "/classes/", 9)))
473 cupsdLogMessage(CUPSD_LOG_ERROR
,
474 "cupsdProcessBrowseData: Bad printer URI in browse data: %s",
479 if (strchr(resource
, '?') ||
480 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
481 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
483 cupsdLogMessage(CUPSD_LOG_ERROR
,
484 "cupsdProcessBrowseData: Bad resource in browse data: %s",
490 * OK, this isn't a local printer; add any remote options...
493 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
495 if (BrowseRemoteOptions
)
497 if (BrowseRemoteOptions
[0] == '?')
500 * Override server-supplied options...
503 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
505 else if (ipp_options
)
508 * Combine the server and local options...
511 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
512 BrowseRemoteOptions
);
517 * Just use the local options...
520 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
525 else if (ipp_options
)
528 * Just use the server-supplied options...
531 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
536 * See if we already have it listed in the Printers list, and add it if not...
539 type
|= CUPS_PRINTER_REMOTE
;
540 type
&= ~CUPS_PRINTER_IMPLICIT
;
542 hptr
= strchr(host
, '.');
543 sptr
= strchr(ServerName
, '.');
545 if (sptr
!= NULL
&& hptr
!= NULL
)
548 * Strip the common domain name components...
553 if (!strcasecmp(hptr
, sptr
))
559 hptr
= strchr(hptr
+ 1, '.');
563 if (type
& CUPS_PRINTER_CLASS
)
566 * Remote destination is a class...
569 if (!strncmp(resource
, "/classes/", 9))
570 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
574 if ((p
= cupsdFindClass(name
)) == NULL
&& BrowseShortNames
)
576 if ((p
= cupsdFindClass(resource
+ 9)) != NULL
)
578 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
581 * Nope, this isn't the same host; if the hostname isn't the local host,
582 * add it to the other class and then find a class using the full host
586 if (p
->type
& CUPS_PRINTER_REMOTE
)
588 cupsdLogMessage(CUPSD_LOG_INFO
,
589 "Renamed remote class \"%s\" to \"%s@%s\"...",
590 p
->name
, p
->name
, p
->hostname
);
591 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
592 "Class \'%s\' deleted by directory services.",
595 cupsArrayRemove(Printers
, p
);
596 cupsdSetStringf(&p
->name
, "%s@%s", p
->name
, p
->hostname
);
597 cupsdSetPrinterAttrs(p
);
598 cupsArrayAdd(Printers
, p
);
600 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
601 "Class \'%s\' added by directory services.",
607 else if (!p
->hostname
)
610 * Hostname not set, so this must be a cached remote printer
611 * that was created for a pending print job...
614 cupsdSetString(&p
->hostname
, host
);
615 cupsdSetString(&p
->uri
, uri
);
616 cupsdSetString(&p
->device_uri
, uri
);
623 * Use the short name for this shared class.
626 strlcpy(name
, resource
+ 9, sizeof(name
));
629 else if (p
&& !p
->hostname
)
632 * Hostname not set, so this must be a cached remote printer
633 * that was created for a pending print job...
636 cupsdSetString(&p
->hostname
, host
);
637 cupsdSetString(&p
->uri
, uri
);
638 cupsdSetString(&p
->device_uri
, uri
);
645 * Class doesn't exist; add it...
648 p
= cupsdAddClass(name
);
650 cupsdLogMessage(CUPSD_LOG_INFO
, "Added remote class \"%s\"...", name
);
652 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
653 "Class \'%s\' added by directory services.", name
);
656 * Force the URI to point to the real server...
659 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
661 cupsdSetString(&p
->uri
, uri
);
662 cupsdSetString(&p
->device_uri
, uri
);
663 cupsdSetString(&p
->hostname
, host
);
671 * Remote destination is a printer...
674 if (strncmp(resource
, "/printers/", 10) == 0)
675 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
679 if ((p
= cupsdFindPrinter(name
)) == NULL
&& BrowseShortNames
)
681 if ((p
= cupsdFindPrinter(resource
+ 10)) != NULL
)
683 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
686 * Nope, this isn't the same host; if the hostname isn't the local host,
687 * add it to the other printer and then find a printer using the full host
691 if (p
->type
& CUPS_PRINTER_REMOTE
)
693 cupsdLogMessage(CUPSD_LOG_INFO
,
694 "Renamed remote printer \"%s\" to \"%s@%s\"...",
695 p
->name
, p
->name
, p
->hostname
);
696 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
697 "Printer \'%s\' deleted by directory services.",
700 cupsArrayRemove(Printers
, p
);
701 cupsdSetStringf(&p
->name
, "%s@%s", p
->name
, p
->hostname
);
702 cupsdSetPrinterAttrs(p
);
703 cupsArrayAdd(Printers
, p
);
705 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
706 "Printer \'%s\' added by directory services.",
712 else if (!p
->hostname
)
715 * Hostname not set, so this must be a cached remote printer
716 * that was created for a pending print job...
719 cupsdSetString(&p
->hostname
, host
);
720 cupsdSetString(&p
->uri
, uri
);
721 cupsdSetString(&p
->device_uri
, uri
);
728 * Use the short name for this shared printer.
731 strlcpy(name
, resource
+ 10, sizeof(name
));
734 else if (p
&& !p
->hostname
)
737 * Hostname not set, so this must be a cached remote printer
738 * that was created for a pending print job...
741 cupsdSetString(&p
->hostname
, host
);
742 cupsdSetString(&p
->uri
, uri
);
743 cupsdSetString(&p
->device_uri
, uri
);
750 * Printer doesn't exist; add it...
753 p
= cupsdAddPrinter(name
);
755 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
756 "Printer \'%s\' added by directory services.", name
);
758 cupsdLogMessage(CUPSD_LOG_INFO
, "Added remote printer \"%s\"...", name
);
761 * Force the URI to point to the real server...
764 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
766 cupsdSetString(&p
->hostname
, host
);
767 cupsdSetString(&p
->uri
, uri
);
768 cupsdSetString(&p
->device_uri
, uri
);
775 * Update the state...
779 p
->browse_time
= time(NULL
);
781 if (type
& CUPS_PRINTER_REJECTING
)
783 type
&= ~CUPS_PRINTER_REJECTING
;
791 else if (!p
->accepting
)
803 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
805 cupsdSetString(&p
->location
, location
);
809 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
811 cupsdSetString(&p
->info
, info
);
815 if (!make_model
|| !make_model
[0])
817 if (type
& CUPS_PRINTER_CLASS
)
818 snprintf(local_make_model
, sizeof(local_make_model
),
819 "Remote Class on %s", host
);
821 snprintf(local_make_model
, sizeof(local_make_model
),
822 "Remote Printer on %s", host
);
825 snprintf(local_make_model
, sizeof(local_make_model
),
826 "%s on %s", make_model
, host
);
828 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
830 cupsdSetString(&p
->make_model
, local_make_model
);
834 if (type
& CUPS_PRINTER_DELETE
)
836 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
837 "%s \'%s\' deleted by directory services.",
838 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
840 cupsdExpireSubscriptions(p
, NULL
);
842 cupsdDeletePrinter(p
, 1);
843 cupsdUpdateImplicitClasses();
847 cupsdSetPrinterAttrs(p
);
848 cupsdUpdateImplicitClasses();
852 * See if we have a default printer... If not, make the first printer the
856 if (DefaultPrinter
== NULL
&& Printers
!= NULL
)
857 DefaultPrinter
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
860 * Do auto-classing if needed...
863 cupsdProcessImplicitClasses();
866 * Update the printcap file...
869 cupsdWritePrintcap();
874 * 'cupsdProcessImplicitClasses()' - Create/update implicit classes as needed.
878 cupsdProcessImplicitClasses(void)
880 int i
; /* Looping var */
881 int update
; /* Update printer attributes? */
882 char name
[IPP_MAX_NAME
], /* Name of printer */
883 *hptr
; /* Pointer into hostname */
884 cupsd_printer_t
*p
, /* Printer information */
885 *pclass
, /* Printer class */
886 *first
; /* First printer in class */
887 int offset
, /* Offset of name */
888 len
; /* Length of name */
891 if (!ImplicitClasses
|| !Printers
)
895 * Loop through all available printers and create classes as needed...
898 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
899 update
= 0, pclass
= NULL
, first
= NULL
;
901 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
904 * Skip implicit classes...
907 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
914 * If len == 0, get the length of this printer name up to the "@"
918 cupsArraySave(Printers
);
921 strncasecmp(p
->name
, name
+ offset
, len
) == 0 &&
922 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
925 * We have more than one printer with the same name; see if
926 * we have a class, and if this printer is a member...
929 if (pclass
&& strcasecmp(pclass
->name
, name
))
932 cupsdSetPrinterAttrs(pclass
);
938 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
941 * Need to add the class...
944 pclass
= cupsdAddPrinter(name
);
945 cupsArrayAdd(ImplicitPrinters
, pclass
);
947 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
948 pclass
->accepting
= 1;
949 pclass
->state
= IPP_PRINTER_IDLE
;
951 cupsdSetString(&pclass
->location
, p
->location
);
952 cupsdSetString(&pclass
->info
, p
->info
);
956 cupsdLogMessage(CUPSD_LOG_INFO
, "Added implicit class \"%s\"...",
962 for (i
= 0; i
< pclass
->num_printers
; i
++)
963 if (pclass
->printers
[i
] == first
)
966 if (i
>= pclass
->num_printers
)
968 first
->in_implicit_class
= 1;
969 cupsdAddPrinterToClass(pclass
, first
);
975 for (i
= 0; i
< pclass
->num_printers
; i
++)
976 if (pclass
->printers
[i
] == p
)
979 if (i
>= pclass
->num_printers
)
981 p
->in_implicit_class
= 1;
982 cupsdAddPrinterToClass(pclass
, p
);
989 * First time around; just get name length and mark it as first
993 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
994 len
= hptr
- p
->name
;
996 len
= strlen(p
->name
);
998 strncpy(name
, p
->name
, len
);
1002 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
1003 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
1006 * Can't use same name as a local printer; add "Any" to the
1007 * front of the name, unless we have explicitly disabled
1008 * the "ImplicitAnyClasses"...
1011 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
1014 * Add "Any" to the class name...
1017 strcpy(name
, "Any");
1018 strncpy(name
+ 3, p
->name
, len
);
1019 name
[len
+ 3] = '\0';
1025 * Don't create an implicit class if we have a local printer
1026 * with the same name...
1030 cupsArrayRestore(Printers
);
1038 cupsArrayRestore(Printers
);
1042 * Update the last printer class as needed...
1045 if (pclass
&& update
)
1046 cupsdSetPrinterAttrs(pclass
);
1051 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
1055 cupsdSaveRemoteCache(void)
1057 int i
; /* Looping var */
1058 cups_file_t
*fp
; /* printers.conf file */
1059 char temp
[1024]; /* Temporary string */
1060 cupsd_printer_t
*printer
; /* Current printer class */
1061 time_t curtime
; /* Current time */
1062 struct tm
*curdate
; /* Current date */
1066 * Create the remote.cache file...
1069 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
1071 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1073 cupsdLogMessage(CUPSD_LOG_ERROR
,
1074 "Unable to save remote.cache - %s", strerror(errno
));
1078 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving remote.cache...");
1081 * Restrict access to the file...
1084 fchown(cupsFileNumber(fp
), getuid(), Group
);
1085 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1088 * Write a small header to the file...
1091 curtime
= time(NULL
);
1092 curdate
= localtime(&curtime
);
1093 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1095 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
1096 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1099 * Write each local printer known to the system...
1102 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1104 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1107 * Skip local destinations...
1110 if (!(printer
->type
& CUPS_PRINTER_REMOTE
))
1114 * Write printers as needed...
1117 if (printer
== DefaultPrinter
)
1118 cupsFilePuts(fp
, "<Default");
1120 cupsFilePutChar(fp
, '<');
1122 if (printer
->type
& CUPS_PRINTER_CLASS
)
1123 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
1125 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
1127 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
1129 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_time
);
1132 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
1134 if (printer
->make_model
)
1135 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
1137 if (printer
->location
)
1138 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
1140 if (printer
->device_uri
)
1141 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
1143 if (printer
->state
== IPP_PRINTER_STOPPED
)
1145 cupsFilePuts(fp
, "State Stopped\n");
1146 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
1149 cupsFilePuts(fp
, "State Idle\n");
1151 if (printer
->accepting
)
1152 cupsFilePuts(fp
, "Accepting Yes\n");
1154 cupsFilePuts(fp
, "Accepting No\n");
1156 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
1157 printer
->job_sheets
[1]);
1159 for (i
= 0; i
< printer
->num_users
; i
++)
1160 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
1163 if (printer
->type
& CUPS_PRINTER_CLASS
)
1164 cupsFilePuts(fp
, "</Class>\n");
1166 cupsFilePuts(fp
, "</Printer>\n");
1174 * 'cupsdSendBrowseDelete()' - Send a "browse delete" message for a printer.
1178 cupsdSendBrowseDelete(
1179 cupsd_printer_t
*p
) /* I - Printer to delete */
1182 * Only announce if browsing is enabled...
1185 if (!Browsing
|| !p
->shared
)
1189 * First mark the printer for deletion...
1192 p
->type
|= CUPS_PRINTER_DELETE
;
1195 * Announce the deletion...
1198 if (BrowseLocalProtocols
& BROWSE_CUPS
)
1199 cupsdSendCUPSBrowse(p
);
1201 if (BrowseLocalProtocols
& BROWSE_SLP
)
1202 slp_dereg_printer(p
);
1203 #endif /* HAVE_LIBSLP */
1208 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
1212 cupsdSendBrowseList(void)
1214 int count
; /* Number of dests to update */
1215 cupsd_printer_t
*p
; /* Current printer */
1216 time_t ut
, /* Minimum update time */
1217 to
; /* Timeout time */
1220 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
1224 * Compute the update and timeout times...
1227 ut
= time(NULL
) - BrowseInterval
;
1228 to
= time(NULL
) - BrowseTimeout
;
1231 * Figure out how many printers need an update...
1234 if (BrowseInterval
> 0)
1236 int max_count
; /* Maximum number to update */
1240 * Throttle the number of printers we'll be updating this time
1241 * around based on the number of queues that need updating and
1242 * the maximum number of queues to update each second...
1245 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
1247 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1248 count
< max_count
&& p
!= NULL
;
1249 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1250 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
1251 p
->shared
&& p
->browse_time
< ut
)
1255 * Loop through all of the printers and send local updates as needed...
1259 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
1261 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1265 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1268 * Check for wraparound...
1272 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1276 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1279 else if (p
->browse_time
< ut
)
1282 * Need to send an update...
1287 p
->browse_time
= time(NULL
);
1289 if (BrowseLocalProtocols
& BROWSE_CUPS
)
1290 cupsdSendCUPSBrowse(p
);
1293 if (BrowseLocalProtocols
& BROWSE_SLP
)
1294 cupsdSendSLPBrowse(p
);
1295 #endif /* HAVE_LIBSLP */
1300 * Save where we left off so that all printers get updated...
1307 * Loop through all of the printers and send local updates as needed...
1310 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1312 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1315 * If this is a remote queue, see if it needs to be timed out...
1318 if (p
->type
& CUPS_PRINTER_REMOTE
)
1320 if (p
->browse_time
< to
)
1322 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1323 "%s \'%s\' deleted by directory services (timeout).",
1324 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
1327 cupsdLogMessage(CUPSD_LOG_INFO
,
1328 "Remote destination \"%s\" has timed out; deleting it...",
1331 cupsArraySave(Printers
);
1332 cupsdDeletePrinter(p
, 1);
1333 cupsArrayRestore(Printers
);
1341 * 'cupsdSendCUPSBrowse()' - Send new browsing information using the CUPS protocol.
1345 cupsdSendCUPSBrowse(cupsd_printer_t
*p
) /* I - Printer to send */
1347 int i
; /* Looping var */
1348 cups_ptype_t type
; /* Printer type */
1349 cupsd_dirsvc_addr_t
*b
; /* Browse address */
1350 int bytes
; /* Length of packet */
1351 char packet
[1453]; /* Browse data packet */
1352 char uri
[1024]; /* Printer URI */
1353 char options
[1024]; /* Browse local options */
1354 cupsd_netif_t
*iface
; /* Network interface */
1358 * Figure out the printer type value...
1361 type
= p
->type
| CUPS_PRINTER_REMOTE
;
1364 type
|= CUPS_PRINTER_REJECTING
;
1367 * Initialize the browse options...
1370 if (BrowseLocalOptions
)
1371 snprintf(options
, sizeof(options
), " ipp-options=%s", BrowseLocalOptions
);
1376 * Send a packet to each browse address...
1379 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
1383 * Send the browse packet to one or more interfaces...
1386 if (strcmp(b
->iface
, "*") == 0)
1389 * Send to all local interfaces...
1394 for (iface
= NetIFList
; iface
!= NULL
; iface
= iface
->next
)
1397 * Only send to local, IPv4 interfaces...
1400 if (!iface
->is_local
|| !iface
->port
||
1401 iface
->address
.addr
.sa_family
!= AF_INET
)
1404 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1405 iface
->hostname
, iface
->port
,
1406 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s%s" :
1409 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
1410 type
, p
->state
, uri
, p
->location
? p
->location
: "",
1411 p
->info
? p
->info
: "",
1412 p
->make_model
? p
->make_model
: "Unknown", options
);
1414 bytes
= strlen(packet
);
1416 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1417 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
1418 iface
->name
, packet
);
1420 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
1422 sendto(BrowseSocket
, packet
, bytes
, 0,
1423 (struct sockaddr
*)&(iface
->broadcast
),
1424 sizeof(struct sockaddr_in
));
1427 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
1430 * Send to the named interface using the IPv4 address...
1434 if (strcasecmp(b
->iface
, iface
->name
))
1439 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
1442 iface
= iface
->next
;
1446 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1447 iface
->hostname
, iface
->port
,
1448 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s%s" :
1451 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
1452 type
, p
->state
, uri
, p
->location
? p
->location
: "",
1453 p
->info
? p
->info
: "",
1454 p
->make_model
? p
->make_model
: "Unknown", options
);
1456 bytes
= strlen(packet
);
1458 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1459 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
1460 iface
->name
, packet
);
1462 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
1464 sendto(BrowseSocket
, packet
, bytes
, 0,
1465 (struct sockaddr
*)&(iface
->broadcast
),
1466 sizeof(struct sockaddr_in
));
1473 * Send the browse packet to the indicated address using
1474 * the default server name...
1477 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
1478 type
, p
->state
, p
->uri
,
1479 p
->location
? p
->location
: "",
1480 p
->info
? p
->info
: "",
1481 p
->make_model
? p
->make_model
: "Unknown", options
);
1483 bytes
= strlen(packet
);
1484 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1485 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
1487 if (sendto(BrowseSocket
, packet
, bytes
, 0,
1488 (struct sockaddr
*)&(b
->to
),
1489 sizeof(struct sockaddr_in
)) <= 0)
1492 * Unable to send browse packet, so remove this address from the
1496 cupsdLogMessage(CUPSD_LOG_ERROR
,
1497 "cupsdSendBrowseList: sendto failed for browser %d - %s.",
1498 b
- Browsers
+ 1, strerror(errno
));
1501 memcpy(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
1512 * 'cupsdSendSLPBrowse()' - Register the specified printer with SLP.
1516 cupsdSendSLPBrowse(cupsd_printer_t
*p
) /* I - Printer to register */
1518 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
1519 attrs
[8192], /* Printer attributes */
1520 finishings
[1024], /* Finishings to support */
1521 make_model
[IPP_MAX_NAME
* 2],
1522 /* Make and model, quoted */
1523 location
[IPP_MAX_NAME
* 2],
1524 /* Location, quoted */
1525 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
1526 *src
, /* Pointer to original string */
1527 *dst
; /* Pointer to destination string */
1528 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
1529 SLPError error
; /* SLP error, if any */
1532 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendSLPBrowse(%p = \"%s\")", p
,
1536 * Make the SLP service URL that conforms to the IANA
1537 * 'printer:' template.
1540 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
1542 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
1545 * Figure out the finishings string...
1548 if (p
->type
& CUPS_PRINTER_STAPLE
)
1549 strcpy(finishings
, "staple");
1551 finishings
[0] = '\0';
1553 if (p
->type
& CUPS_PRINTER_BIND
)
1556 strlcat(finishings
, ",bind", sizeof(finishings
));
1558 strcpy(finishings
, "bind");
1561 if (p
->type
& CUPS_PRINTER_PUNCH
)
1564 strlcat(finishings
, ",punch", sizeof(finishings
));
1566 strcpy(finishings
, "punch");
1569 if (p
->type
& CUPS_PRINTER_COVER
)
1572 strlcat(finishings
, ",cover", sizeof(finishings
));
1574 strcpy(finishings
, "cover");
1577 if (p
->type
& CUPS_PRINTER_SORT
)
1580 strlcat(finishings
, ",sort", sizeof(finishings
));
1582 strcpy(finishings
, "sort");
1586 strcpy(finishings
, "none");
1589 * Quote any commas in the make and model, location, and info strings...
1592 for (src
= p
->make_model
, dst
= make_model
;
1593 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
1595 if (*src
== ',' || *src
== '\\' || *src
== ')')
1604 strcpy(make_model
, "Unknown");
1606 for (src
= p
->location
, dst
= location
;
1607 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
1609 if (*src
== ',' || *src
== '\\' || *src
== ')')
1618 strcpy(location
, "Unknown");
1620 for (src
= p
->info
, dst
= info
;
1621 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
1623 if (*src
== ',' || *src
== '\\' || *src
== ')')
1632 strcpy(info
, "Unknown");
1635 * Get the authentication value...
1638 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
1642 * Make the SLP attribute string list that conforms to
1643 * the IANA 'printer:' template.
1646 snprintf(attrs
, sizeof(attrs
),
1647 "(printer-uri-supported=%s),"
1648 "(uri-authentication-supported=%s>),"
1650 "(uri-security-supported=tls>),"
1652 "(uri-security-supported=none>),"
1653 #endif /* HAVE_SSL */
1654 "(printer-name=%s),"
1655 "(printer-location=%s),"
1656 "(printer-info=%s),"
1657 "(printer-more-info=%s),"
1658 "(printer-make-and-model=%s),"
1659 "(charset-supported=utf-8),"
1660 "(natural-language-configured=%s),"
1661 "(natural-language-supported=de,en,es,fr,it),"
1662 "(color-supported=%s),"
1663 "(finishings-supported=%s),"
1664 "(sides-supported=one-sided%s),"
1665 "(multiple-document-jobs-supported=true)"
1666 "(ipp-versions-supported=1.0,1.1)",
1667 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
1668 info
, p
->uri
, make_model
, DefaultLanguage
,
1669 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
1671 p
->type
& CUPS_PRINTER_DUPLEX
?
1672 ",two-sided-long-edge,two-sided-short-edge" : "");
1674 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
1677 * Register the printer with the SLP server...
1680 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
1681 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
1683 if (error
!= SLP_OK
)
1684 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
1687 #endif /* HAVE_LIBSLP */
1691 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1695 cupsdStartBrowsing(void)
1697 int val
; /* Socket option value */
1698 struct sockaddr_in addr
; /* Broadcast address */
1703 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1706 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
1708 if (BrowseSocket
< 0)
1711 * Create the broadcast socket...
1714 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1716 cupsdLogMessage(CUPSD_LOG_ERROR
,
1717 "cupsdStartBrowsing: Unable to create broadcast "
1718 "socket - %s.", strerror(errno
));
1719 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1720 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1725 * Bind the socket to browse port...
1728 memset(&addr
, 0, sizeof(addr
));
1729 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1730 addr
.sin_family
= AF_INET
;
1731 addr
.sin_port
= htons(BrowsePort
);
1733 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1735 cupsdLogMessage(CUPSD_LOG_ERROR
,
1736 "cupsdStartBrowsing: Unable to bind broadcast "
1737 "socket - %s.", strerror(errno
));
1740 closesocket(BrowseSocket
);
1742 close(BrowseSocket
);
1746 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1747 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1753 * Set the "broadcast" flag...
1757 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1759 cupsdLogMessage(CUPSD_LOG_ERROR
,
1760 "cupsdStartBrowsing: Unable to set broadcast mode - %s.",
1764 closesocket(BrowseSocket
);
1766 close(BrowseSocket
);
1770 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1771 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1776 * Close the socket on exec...
1779 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1782 * Finally, add the socket to the input selection set...
1785 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1786 "cupsdStartBrowsing: Adding fd %d to InputSet...",
1789 FD_SET(BrowseSocket
, InputSet
);
1795 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1798 * Open SLP handle...
1801 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1803 cupsdLogMessage(CUPSD_LOG_ERROR
,
1804 "Unable to open an SLP handle; disabling SLP browsing!");
1805 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1806 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1809 BrowseSLPRefresh
= 0;
1811 #endif /* HAVE_LIBSLP */
1816 * 'cupsdStartPolling()' - Start polling servers as needed.
1820 cupsdStartPolling(void)
1822 int i
; /* Looping var */
1823 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1824 char polld
[1024]; /* Poll daemon path */
1825 char sport
[10]; /* Server port */
1826 char bport
[10]; /* Browser port */
1827 char interval
[10]; /* Poll interval */
1828 int statusfds
[2]; /* Status pipe */
1829 char *argv
[6]; /* Arguments */
1830 char *envp
[100]; /* Environment */
1834 * Don't do anything if we aren't polling...
1840 PollStatusBuffer
= NULL
;
1845 * Setup string arguments for polld, port and interval options.
1848 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1850 sprintf(bport
, "%d", BrowsePort
);
1853 sprintf(interval
, "%d", BrowseInterval
);
1855 strcpy(interval
, "30");
1857 argv
[0] = "cups-polld";
1863 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1866 * Create a pipe that receives the status messages from each
1870 if (cupsdOpenPipe(statusfds
))
1872 cupsdLogMessage(CUPSD_LOG_ERROR
,
1873 "Unable to create polling status pipes - %s.",
1876 PollStatusBuffer
= NULL
;
1880 PollPipe
= statusfds
[0];
1881 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1884 * Run each polling daemon, redirecting stderr to the polling pipe...
1887 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1889 sprintf(sport
, "%d", pollp
->port
);
1891 argv
[1] = pollp
->hostname
;
1893 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1,
1894 0, &(pollp
->pid
)) < 0)
1896 cupsdLogMessage(CUPSD_LOG_ERROR
,
1897 "cupsdStartPolling: Unable to fork polling daemon - %s",
1903 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1904 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1905 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1908 close(statusfds
[1]);
1911 * Finally, add the pipe to the input selection set...
1914 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1915 "cupsdStartPolling: Adding fd %d to InputSet...", PollPipe
);
1917 FD_SET(PollPipe
, InputSet
);
1922 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1926 cupsdStopBrowsing(void)
1928 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1931 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1935 * Close the socket and remove it from the input selection set.
1939 closesocket(BrowseSocket
);
1941 close(BrowseSocket
);
1944 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1945 "cupsdStopBrowsing: Removing fd %d from InputSet...",
1948 FD_CLR(BrowseSocket
, InputSet
);
1953 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1956 * Close SLP handle...
1959 SLPClose(BrowseSLPHandle
);
1961 #endif /* HAVE_LIBSLP */
1966 * 'cupsdStopPolling()' - Stop polling servers as needed.
1970 cupsdStopPolling(void)
1972 int i
; /* Looping var */
1973 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1978 cupsdStatBufDelete(PollStatusBuffer
);
1981 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1982 "cupsdStopPolling: removing fd %d from InputSet.", PollPipe
);
1983 FD_CLR(PollPipe
, InputSet
);
1986 PollStatusBuffer
= NULL
;
1989 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1991 cupsdEndProcess(pollp
->pid
, 0);
1996 * 'cupsdUpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
2000 cupsdUpdateCUPSBrowse(void)
2002 int i
; /* Looping var */
2003 int auth
; /* Authorization status */
2004 int len
; /* Length of name string */
2005 int bytes
; /* Number of bytes left */
2006 char packet
[1541], /* Broadcast packet */
2007 *pptr
; /* Pointer into packet */
2008 socklen_t srclen
; /* Length of source address */
2009 http_addr_t srcaddr
; /* Source address */
2010 char srcname
[1024]; /* Source hostname */
2011 unsigned address
[4]; /* Source address */
2012 unsigned type
; /* Printer type */
2013 unsigned state
; /* Printer state */
2014 char uri
[HTTP_MAX_URI
], /* Printer URI */
2015 method
[HTTP_MAX_URI
], /* Method portion of URI */
2016 username
[HTTP_MAX_URI
], /* Username portion of URI */
2017 host
[HTTP_MAX_URI
], /* Host portion of URI */
2018 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
2019 info
[IPP_MAX_NAME
], /* Information string */
2020 location
[IPP_MAX_NAME
], /* Location string */
2021 make_model
[IPP_MAX_NAME
];/* Make and model string */
2022 int port
; /* Port portion of URI */
2023 cupsd_netif_t
*iface
; /* Network interface */
2024 int num_attrs
; /* Number of attributes */
2025 cups_option_t
*attrs
; /* Attributes */
2029 * Read a packet from the browse socket...
2032 srclen
= sizeof(srcaddr
);
2033 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
2034 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
2037 * "Connection refused" is returned under Linux if the destination port
2038 * or address is unreachable from a previous sendto(); check for the
2039 * error here and ignore it for now...
2042 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
2044 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
2046 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browsing turned off.");
2048 cupsdStopBrowsing();
2055 packet
[bytes
] = '\0';
2058 * If we're about to sleep, ignore incoming browse packets.
2065 * Figure out where it came from...
2069 if (srcaddr
.addr
.sa_family
== AF_INET6
)
2071 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
2072 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
2073 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
2074 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
2077 #endif /* AF_INET6 */
2082 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
2085 if (HostNameLookups
)
2086 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
2088 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
2090 len
= strlen(srcname
);
2098 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
2101 * Access from localhost (127.0.0.1) is always allowed...
2109 * Do authorization checks on the domain/address...
2112 switch (BrowseACL
->order_type
)
2115 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
2118 case AUTH_ALLOW
: /* Order Deny,Allow */
2121 if (cupsdCheckAuth(address
, srcname
, len
,
2122 BrowseACL
->num_deny
, BrowseACL
->deny
))
2125 if (cupsdCheckAuth(address
, srcname
, len
,
2126 BrowseACL
->num_allow
, BrowseACL
->allow
))
2130 case AUTH_DENY
: /* Order Allow,Deny */
2133 if (cupsdCheckAuth(address
, srcname
, len
,
2134 BrowseACL
->num_allow
, BrowseACL
->allow
))
2137 if (cupsdCheckAuth(address
, srcname
, len
,
2138 BrowseACL
->num_deny
, BrowseACL
->deny
))
2147 if (auth
== AUTH_DENY
)
2149 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2150 "cupsdUpdateCUPSBrowse: Refused %d bytes from %s", bytes
,
2155 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2156 "cupsdUpdateCUPSBrowse: (%d bytes from %s) %s", bytes
,
2163 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
2165 cupsdLogMessage(CUPSD_LOG_WARN
,
2166 "cupsdUpdateCUPSBrowse: Garbled browse packet - %s", packet
);
2170 strcpy(location
, "Location Unknown");
2171 strcpy(info
, "No Information Available");
2172 make_model
[0] = '\0';
2176 if ((pptr
= strchr(packet
, '\"')) != NULL
)
2179 * Have extended information; can't use sscanf for it because not all
2180 * sscanf's allow empty strings with %[^\"]...
2183 for (i
= 0, pptr
++;
2184 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
2186 location
[i
] = *pptr
;
2194 while (*pptr
&& isspace(*pptr
& 255))
2199 for (i
= 0, pptr
++;
2200 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
2209 while (*pptr
&& isspace(*pptr
& 255))
2214 for (i
= 0, pptr
++;
2215 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
2217 make_model
[i
] = *pptr
;
2222 make_model
[i
] = '\0';
2225 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
2231 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
2232 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
2233 type
, state
, uri
, location
, info
, make_model
));
2236 * Pull the URI apart to see if this is a local or remote printer...
2239 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, method
, sizeof(method
), username
,
2240 sizeof(username
), host
, sizeof(host
), &port
, resource
,
2243 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
2246 * Check for packets from the local server...
2249 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
2251 cupsFreeOptions(num_attrs
, attrs
);
2257 for (iface
= NetIFList
; iface
!= NULL
; iface
= iface
->next
)
2258 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
2260 cupsFreeOptions(num_attrs
, attrs
);
2268 for (i
= 0; i
< NumRelays
; i
++)
2269 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
2270 if (sendto(BrowseSocket
, packet
, bytes
, 0,
2271 (struct sockaddr
*)&(Relays
[i
].to
),
2272 sizeof(http_addr_t
)) <= 0)
2274 cupsdLogMessage(CUPSD_LOG_ERROR
,
2275 "cupsdUpdateCUPSBrowse: sendto failed for relay %d - %s.",
2276 i
+ 1, strerror(errno
));
2277 cupsFreeOptions(num_attrs
, attrs
);
2282 * Process the browse data...
2285 cupsdProcessBrowseData(uri
, (cups_ptype_t
)type
, (ipp_pstate_t
)state
, location
,
2286 info
, make_model
, num_attrs
, attrs
);
2287 cupsFreeOptions(num_attrs
, attrs
);
2292 * 'cupsdUpdatePolling()' - Read status messages from the poll daemons.
2296 cupsdUpdatePolling(void)
2298 char *ptr
, /* Pointer to end of line in buffer */
2299 message
[1024]; /* Pointer to message text */
2300 int loglevel
; /* Log level for message */
2303 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
2304 message
, sizeof(message
))) != NULL
)
2305 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
2311 * All polling processes have died; stop polling...
2314 cupsdLogMessage(CUPSD_LOG_ERROR
,
2315 "cupsdUpdatePolling: all polling processes have exited!");
2323 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2327 cupsdUpdateSLPBrowse(void)
2329 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2330 *next
; /* Next service in list */
2331 cupsd_printer_t p
; /* Printer information */
2332 const char *uri
; /* Pointer to printer URI */
2333 char method
[HTTP_MAX_URI
],
2334 /* Method portion of URI */
2335 username
[HTTP_MAX_URI
],
2336 /* Username portion of URI */
2338 /* Host portion of URI */
2339 resource
[HTTP_MAX_URI
];
2340 /* Resource portion of URI */
2341 int port
; /* Port portion of URI */
2344 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdUpdateSLPBrowse() Start...");
2347 * Reset the refresh time...
2350 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2353 * Poll for remote printers using SLP...
2358 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2359 slp_url_callback
, &s
);
2362 * Loop through the list of available printers...
2368 * Save the "next" pointer...
2374 * Load a cupsd_printer_t structure with the SLP service attributes...
2377 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2380 * Process this printer entry...
2383 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2385 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2388 * Pull the URI apart to see if this is a local or remote printer...
2391 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, method
, sizeof(method
),
2392 username
, sizeof(username
), host
, sizeof(host
), &port
,
2393 resource
, sizeof(resource
));
2395 if (strcasecmp(host
, ServerName
) == 0)
2399 * OK, at least an IPP printer, see if it is a CUPS printer or
2403 if (strstr(uri
, "/printers/") != NULL
)
2404 cupsdProcessBrowseData(uri
, p
.type
, IPP_PRINTER_IDLE
, p
.location
,
2405 p
.info
, p
.make_model
, 0, NULL
);
2406 else if (strstr(uri
, "/classes/") != NULL
)
2407 cupsdProcessBrowseData(uri
, p
.type
| CUPS_PRINTER_CLASS
, IPP_PRINTER_IDLE
,
2408 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2412 * Free this listing...
2418 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdUpdateSLPBrowse() End...");
2423 * 'slp_attr_callback()' - SLP attribute callback
2426 static SLPBoolean
/* O - SLP_TRUE for success */
2428 SLPHandle hslp
, /* I - SLP handle */
2429 const char *attrlist
, /* I - Attribute list */
2430 SLPError errcode
, /* I - Parsing status for this attr */
2431 void *cookie
) /* I - Current printer */
2434 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
2438 * Let the compiler know we won't be using these...
2444 * Bail if there was an error
2447 if (errcode
!= SLP_OK
)
2451 * Parse the attrlist to obtain things needed to build CUPS browse packet
2454 memset(p
, 0, sizeof(cupsd_printer_t
));
2456 p
->type
= CUPS_PRINTER_REMOTE
;
2458 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
2460 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
2462 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
2465 if (slp_get_attr(attrlist
, "(color-supported=", &tmp
))
2467 if (strcasecmp(tmp
, "true") == 0)
2468 p
->type
|= CUPS_PRINTER_COLOR
;
2470 if (slp_get_attr(attrlist
, "(finishings-supported=", &tmp
))
2472 if (strstr(tmp
, "staple"))
2473 p
->type
|= CUPS_PRINTER_STAPLE
;
2474 if (strstr(tmp
, "bind"))
2475 p
->type
|= CUPS_PRINTER_BIND
;
2476 if (strstr(tmp
, "punch"))
2477 p
->type
|= CUPS_PRINTER_PUNCH
;
2479 if (slp_get_attr(attrlist
, "(sides-supported=", &tmp
))
2481 if (strstr(tmp
,"two-sided"))
2482 p
->type
|= CUPS_PRINTER_DUPLEX
;
2484 cupsdClearString(&tmp
);
2491 * 'slp_dereg_printer()' - SLPDereg() the specified printer
2495 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
2497 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
2500 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
2502 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
2505 * Make the SLP service URL that conforms to the IANA
2506 * 'printer:' template.
2509 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
2512 * Deregister the printer...
2515 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
2521 * 'slp_get_attr()' - Get an attribute from an SLP registration.
2524 static int /* O - 0 on success */
2525 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
2526 const char *tag
, /* I - Name of attribute */
2527 char **valbuf
) /* O - Value */
2529 char *ptr1
, /* Pointer into string */
2533 cupsdClearString(valbuf
);
2535 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
2537 ptr1
+= strlen(tag
);
2539 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
2545 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
2546 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
2549 * Dequote the value...
2552 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
2553 if (*ptr1
== '\\' && ptr1
[1])
2554 _cups_strcpy(ptr1
, ptr1
+ 1);
2565 * 'slp_reg_callback()' - Empty SLPRegReport.
2569 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
2570 SLPError errcode
, /* I - Error code, if any */
2571 void *cookie
) /* I - App data */
2582 * 'slp_url_callback()' - SLP service url callback
2585 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
2587 SLPHandle hslp
, /* I - SLP handle */
2588 const char *srvurl
, /* I - URL of service */
2589 unsigned short lifetime
, /* I - Life of service */
2590 SLPError errcode
, /* I - Existing error code */
2591 void *cookie
) /* I - Pointer to service list */
2593 slpsrvurl_t
*s
, /* New service entry */
2594 **head
; /* Pointer to head of entry */
2598 * Let the compiler know we won't be using these vars...
2605 * Bail if there was an error
2608 if (errcode
!= SLP_OK
)
2612 * Grab the head of the list...
2615 head
= (slpsrvurl_t
**)cookie
;
2618 * Allocate a *temporary* slpsrvurl_t to hold this entry.
2621 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
2625 * Copy the SLP service URL...
2628 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
2631 * Link the SLP service URL into the head of the list
2641 #endif /* HAVE_LIBSLP */
2645 * End of "$Id: dirsvc.c 5023 2006-01-29 14:39:44Z mike $".