2 * "$Id: dirsvc.c 5059 2006-02-02 23:17:16Z 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 * cupsdSaveRemoteCache() - Save the remote printer cache.
28 * cupsdSendBrowseDelete() - Send a "browse delete" message for a
30 * cupsdSendBrowseList() - Send new browsing information as necessary.
31 * cupsdSendCUPSBrowse() - Send new browsing information using the
33 * cupsdSendSLPBrowse() - Register the specified printer with SLP.
34 * cupsdStartBrowsing() - Start sending and receiving broadcast
36 * cupsdStartPolling() - Start polling servers as needed.
37 * cupsdStopBrowsing() - Stop sending and receiving broadcast
39 * cupsdStopPolling() - Stop polling servers as needed.
40 * cupsdUpdateCUPSBrowse() - Update the browse lists using the CUPS
42 * cupsdUpdatePolling() - Read status messages from the poll daemons.
43 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
44 * dequote() - Remote quotes from a string.
45 * process_browse_data() - Process new browse data.
46 * process_implicit_classes() - Create/update implicit classes as needed.
47 * slp_attr_callback() - SLP attribute callback
48 * slp_dereg_printer() - SLPDereg() the specified printer
49 * slp_get_attr() - Get an attribute from an SLP registration.
50 * slp_reg_callback() - Empty SLPRegReport.
51 * slp_url_callback() - SLP service url callback
55 * Include necessary headers...
66 static char *dequote(char *d
, const char *s
, int dlen
);
67 static void process_browse_data(const char *uri
, cups_ptype_t type
,
68 ipp_pstate_t state
, const char *location
,
69 const char *info
, const char *make_model
,
70 int num_attrs
, cups_option_t
*attrs
);
71 static void process_implicit_classes(void);
80 * SLP service name for CUPS...
83 # define SLP_CUPS_SRVTYPE "service:printer"
84 # define SLP_CUPS_SRVLEN 15
88 * Printer service URL structure
91 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
93 struct _slpsrvurl_s
*next
; /* Next URL in list */
94 char url
[HTTP_MAX_URI
];
103 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
104 SLPError errcode
, void *cookie
);
105 static void slp_dereg_printer(cupsd_printer_t
*p
);
106 static int slp_get_attr(const char *attrlist
, const char *tag
,
108 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
110 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
111 unsigned short lifetime
,
112 SLPError errcode
, void *cookie
);
113 #endif /* HAVE_LIBSLP */
117 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
121 cupsdLoadRemoteCache(void)
123 cups_file_t
*fp
; /* remote.cache file */
124 int linenum
; /* Current line number */
125 char line
[1024], /* Line from file */
126 *value
, /* Pointer to value */
127 *valueptr
; /* Pointer into value */
128 cupsd_printer_t
*p
; /* Current printer */
129 time_t now
; /* Current time */
133 * Open the remote.cache file...
136 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
137 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
141 * Read printer configurations until we hit EOF...
148 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
151 * Decode the directive...
154 if (!strcasecmp(line
, "<Printer") ||
155 !strcasecmp(line
, "<DefaultPrinter"))
158 * <Printer name> or <DefaultPrinter name>
161 if (p
== NULL
&& value
)
164 * Add the printer and a base file type...
167 cupsdLogMessage(CUPSD_LOG_DEBUG
,
168 "cupsdLoadRemoteCache: Loading printer %s...", value
);
170 p
= cupsdAddPrinter(value
);
172 p
->state
= IPP_PRINTER_IDLE
;
173 p
->type
|= CUPS_PRINTER_REMOTE
;
174 p
->browse_time
= now
+ BrowseTimeout
;
177 * Set the default printer as needed...
180 if (!strcasecmp(line
, "<DefaultPrinter"))
185 cupsdLogMessage(CUPSD_LOG_ERROR
,
186 "Syntax error on line %d of remote.cache.", linenum
);
190 else if (!strcasecmp(line
, "<Class") ||
191 !strcasecmp(line
, "<DefaultClass"))
194 * <Class name> or <DefaultClass name>
197 if (p
== NULL
&& value
)
200 * Add the printer and a base file type...
203 cupsdLogMessage(CUPSD_LOG_DEBUG
,
204 "cupsdLoadRemoteCache: Loading class %s...", value
);
206 p
= cupsdAddClass(value
);
208 p
->state
= IPP_PRINTER_IDLE
;
209 p
->type
|= CUPS_PRINTER_REMOTE
;
210 p
->browse_time
= now
+ BrowseTimeout
;
213 * Set the default printer as needed...
216 if (!strcasecmp(line
, "<DefaultClass"))
221 cupsdLogMessage(CUPSD_LOG_ERROR
,
222 "Syntax error on line %d of remote.cache.", linenum
);
226 else if (!strcasecmp(line
, "</Printer>") ||
227 !strcasecmp(line
, "</Class>"))
232 * Close out the current printer...
235 cupsdSetPrinterAttrs(p
);
241 cupsdLogMessage(CUPSD_LOG_ERROR
,
242 "Syntax error on line %d of remote.cache.", linenum
);
248 cupsdLogMessage(CUPSD_LOG_ERROR
,
249 "Syntax error on line %d of remote.cache.", linenum
);
252 else if (!strcasecmp(line
, "Info"))
255 cupsdSetString(&p
->info
, value
);
257 else if (!strcasecmp(line
, "MakeModel"))
260 cupsdSetString(&p
->make_model
, value
);
262 else if (!strcasecmp(line
, "Location"))
265 cupsdSetString(&p
->location
, value
);
267 else if (!strcasecmp(line
, "DeviceURI"))
271 cupsdSetString(&p
->uri
, value
);
272 cupsdSetString(&p
->device_uri
, value
);
276 cupsdLogMessage(CUPSD_LOG_ERROR
,
277 "Syntax error on line %d of remote.cache.", linenum
);
281 else if (!strcasecmp(line
, "State"))
284 * Set the initial queue state...
287 if (value
&& !strcasecmp(value
, "idle"))
288 p
->state
= IPP_PRINTER_IDLE
;
289 else if (value
&& !strcasecmp(value
, "stopped"))
290 p
->state
= IPP_PRINTER_STOPPED
;
293 cupsdLogMessage(CUPSD_LOG_ERROR
,
294 "Syntax error on line %d of remote.cache.", linenum
);
298 else if (!strcasecmp(line
, "StateMessage"))
301 * Set the initial queue state message...
305 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
307 else if (!strcasecmp(line
, "Accepting"))
310 * Set the initial accepting state...
314 (!strcasecmp(value
, "yes") ||
315 !strcasecmp(value
, "on") ||
316 !strcasecmp(value
, "true")))
319 (!strcasecmp(value
, "no") ||
320 !strcasecmp(value
, "off") ||
321 !strcasecmp(value
, "false")))
325 cupsdLogMessage(CUPSD_LOG_ERROR
,
326 "Syntax error on line %d of remote.cache.", linenum
);
330 else if (!strcasecmp(line
, "Type"))
333 p
->type
= atoi(value
);
336 cupsdLogMessage(CUPSD_LOG_ERROR
,
337 "Syntax error on line %d of remote.cache.", linenum
);
341 else if (!strcasecmp(line
, "BrowseTime"))
345 time_t t
= atoi(value
);
347 if (t
> (now
+ BrowseInterval
))
352 cupsdLogMessage(CUPSD_LOG_ERROR
,
353 "Syntax error on line %d of remote.cache.", linenum
);
357 else if (!strcasecmp(line
, "JobSheets"))
360 * Set the initial job sheets...
365 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
370 cupsdSetString(&p
->job_sheets
[0], value
);
372 while (isspace(*valueptr
& 255))
377 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
382 cupsdSetString(&p
->job_sheets
[1], value
);
387 cupsdLogMessage(CUPSD_LOG_ERROR
,
388 "Syntax error on line %d of remote.cache.", linenum
);
392 else if (!strcasecmp(line
, "AllowUser"))
397 cupsdAddPrinterUser(p
, value
);
401 cupsdLogMessage(CUPSD_LOG_ERROR
,
402 "Syntax error on line %d of remote.cache.", linenum
);
406 else if (!strcasecmp(line
, "DenyUser"))
411 cupsdAddPrinterUser(p
, value
);
415 cupsdLogMessage(CUPSD_LOG_ERROR
,
416 "Syntax error on line %d of remote.cache.", linenum
);
423 * Something else we don't understand...
426 cupsdLogMessage(CUPSD_LOG_ERROR
,
427 "Unknown configuration directive %s on line %d of remote.cache.",
435 * Do auto-classing if needed...
438 process_implicit_classes();
443 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
447 cupsdSaveRemoteCache(void)
449 int i
; /* Looping var */
450 cups_file_t
*fp
; /* printers.conf file */
451 char temp
[1024]; /* Temporary string */
452 cupsd_printer_t
*printer
; /* Current printer class */
453 time_t curtime
; /* Current time */
454 struct tm
*curdate
; /* Current date */
458 * Create the remote.cache file...
461 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
463 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
465 cupsdLogMessage(CUPSD_LOG_ERROR
,
466 "Unable to save remote.cache - %s", strerror(errno
));
470 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving remote.cache...");
473 * Restrict access to the file...
476 fchown(cupsFileNumber(fp
), getuid(), Group
);
477 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
480 * Write a small header to the file...
483 curtime
= time(NULL
);
484 curdate
= localtime(&curtime
);
485 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
487 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
488 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
491 * Write each local printer known to the system...
494 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
496 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
499 * Skip local destinations...
502 if (!(printer
->type
& CUPS_PRINTER_REMOTE
))
506 * Write printers as needed...
509 if (printer
== DefaultPrinter
)
510 cupsFilePuts(fp
, "<Default");
512 cupsFilePutChar(fp
, '<');
514 if (printer
->type
& CUPS_PRINTER_CLASS
)
515 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
517 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
519 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
521 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_time
);
524 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
526 if (printer
->make_model
)
527 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
529 if (printer
->location
)
530 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
532 if (printer
->device_uri
)
533 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
535 if (printer
->state
== IPP_PRINTER_STOPPED
)
537 cupsFilePuts(fp
, "State Stopped\n");
538 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
541 cupsFilePuts(fp
, "State Idle\n");
543 if (printer
->accepting
)
544 cupsFilePuts(fp
, "Accepting Yes\n");
546 cupsFilePuts(fp
, "Accepting No\n");
548 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
549 printer
->job_sheets
[1]);
551 for (i
= 0; i
< printer
->num_users
; i
++)
552 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
555 if (printer
->type
& CUPS_PRINTER_CLASS
)
556 cupsFilePuts(fp
, "</Class>\n");
558 cupsFilePuts(fp
, "</Printer>\n");
566 * 'cupsdSendBrowseDelete()' - Send a "browse delete" message for a printer.
570 cupsdSendBrowseDelete(
571 cupsd_printer_t
*p
) /* I - Printer to delete */
574 * Only announce if browsing is enabled...
577 if (!Browsing
|| !p
->shared
)
581 * First mark the printer for deletion...
584 p
->type
|= CUPS_PRINTER_DELETE
;
587 * Announce the deletion...
590 if (BrowseLocalProtocols
& BROWSE_CUPS
)
591 cupsdSendCUPSBrowse(p
);
593 if (BrowseLocalProtocols
& BROWSE_SLP
)
594 slp_dereg_printer(p
);
595 #endif /* HAVE_LIBSLP */
600 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
604 cupsdSendBrowseList(void)
606 int count
; /* Number of dests to update */
607 cupsd_printer_t
*p
; /* Current printer */
608 time_t ut
, /* Minimum update time */
609 to
; /* Timeout time */
612 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
616 * Compute the update and timeout times...
619 ut
= time(NULL
) - BrowseInterval
;
620 to
= time(NULL
) - BrowseTimeout
;
623 * Figure out how many printers need an update...
626 if (BrowseInterval
> 0)
628 int max_count
; /* Maximum number to update */
632 * Throttle the number of printers we'll be updating this time
633 * around based on the number of queues that need updating and
634 * the maximum number of queues to update each second...
637 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
639 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
640 count
< max_count
&& p
!= NULL
;
641 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
642 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
643 p
->shared
&& p
->browse_time
< ut
)
647 * Loop through all of the printers and send local updates as needed...
651 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
653 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
657 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
660 * Check for wraparound...
664 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
668 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
671 else if (p
->browse_time
< ut
)
674 * Need to send an update...
679 p
->browse_time
= time(NULL
);
681 if (BrowseLocalProtocols
& BROWSE_CUPS
)
682 cupsdSendCUPSBrowse(p
);
685 if (BrowseLocalProtocols
& BROWSE_SLP
)
686 cupsdSendSLPBrowse(p
);
687 #endif /* HAVE_LIBSLP */
692 * Save where we left off so that all printers get updated...
699 * Loop through all of the printers and send local updates as needed...
702 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
704 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
707 * If this is a remote queue, see if it needs to be timed out...
710 if (p
->type
& CUPS_PRINTER_REMOTE
)
712 if (p
->browse_time
< to
)
714 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
715 "%s \'%s\' deleted by directory services (timeout).",
716 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
719 cupsdLogMessage(CUPSD_LOG_INFO
,
720 "Remote destination \"%s\" has timed out; deleting it...",
723 cupsArraySave(Printers
);
724 cupsdDeletePrinter(p
, 1);
725 cupsArrayRestore(Printers
);
733 * 'cupsdSendCUPSBrowse()' - Send new browsing information using the CUPS protocol.
737 cupsdSendCUPSBrowse(cupsd_printer_t
*p
) /* I - Printer to send */
739 int i
; /* Looping var */
740 cups_ptype_t type
; /* Printer type */
741 cupsd_dirsvc_addr_t
*b
; /* Browse address */
742 int bytes
; /* Length of packet */
743 char packet
[1453], /* Browse data packet */
744 uri
[1024], /* Printer URI */
745 options
[1024], /* Browse local options */
746 location
[1024], /* printer-location */
747 info
[1024], /* printer-info */
749 /* printer-make-and-model */
750 cupsd_netif_t
*iface
; /* Network interface */
754 * Figure out the printer type value...
757 type
= p
->type
| CUPS_PRINTER_REMOTE
;
760 type
|= CUPS_PRINTER_REJECTING
;
762 if (p
== DefaultPrinter
)
763 type
|= CUPS_PRINTER_DEFAULT
;
766 * Initialize the browse options...
769 if (BrowseLocalOptions
)
770 snprintf(options
, sizeof(options
), " ipp-options=%s", BrowseLocalOptions
);
775 * Remove quotes from printer-info, printer-location, and
776 * printer-make-and-model attributes...
779 dequote(location
, p
->location
, sizeof(p
->location
));
780 dequote(info
, p
->info
, sizeof(p
->info
));
781 dequote(make_model
, p
->make_model
? p
->make_model
: "Unknown",
785 * Send a packet to each browse address...
788 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
792 * Send the browse packet to one or more interfaces...
795 if (!strcmp(b
->iface
, "*"))
798 * Send to all local interfaces...
803 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
805 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
808 * Only send to local, IPv4 interfaces...
811 if (!iface
->is_local
|| !iface
->port
||
812 iface
->address
.addr
.sa_family
!= AF_INET
)
815 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
816 iface
->hostname
, iface
->port
,
817 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s%s" :
820 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
821 type
, p
->state
, uri
, location
, info
, make_model
, options
);
823 bytes
= strlen(packet
);
825 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
826 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
827 iface
->name
, packet
);
829 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
831 sendto(BrowseSocket
, packet
, bytes
, 0,
832 (struct sockaddr
*)&(iface
->broadcast
),
833 sizeof(struct sockaddr_in
));
836 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
839 * Send to the named interface using the IPv4 address...
843 if (strcmp(b
->iface
, iface
->name
))
848 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
851 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
855 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
856 iface
->hostname
, iface
->port
,
857 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s%s" :
860 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
861 type
, p
->state
, uri
, location
, info
, make_model
, options
);
863 bytes
= strlen(packet
);
865 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
866 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
867 iface
->name
, packet
);
869 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
871 sendto(BrowseSocket
, packet
, bytes
, 0,
872 (struct sockaddr
*)&(iface
->broadcast
),
873 sizeof(struct sockaddr_in
));
880 * Send the browse packet to the indicated address using
881 * the default server name...
884 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
885 type
, p
->state
, p
->uri
, location
, info
, make_model
, options
);
887 bytes
= strlen(packet
);
888 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
889 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
891 if (sendto(BrowseSocket
, packet
, bytes
, 0,
892 (struct sockaddr
*)&(b
->to
),
893 sizeof(struct sockaddr_in
)) <= 0)
896 * Unable to send browse packet, so remove this address from the
900 cupsdLogMessage(CUPSD_LOG_ERROR
,
901 "cupsdSendBrowseList: sendto failed for browser %d - %s.",
902 b
- Browsers
+ 1, strerror(errno
));
905 memcpy(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
916 * 'cupsdSendSLPBrowse()' - Register the specified printer with SLP.
920 cupsdSendSLPBrowse(cupsd_printer_t
*p
) /* I - Printer to register */
922 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
923 attrs
[8192], /* Printer attributes */
924 finishings
[1024], /* Finishings to support */
925 make_model
[IPP_MAX_NAME
* 2],
926 /* Make and model, quoted */
927 location
[IPP_MAX_NAME
* 2],
928 /* Location, quoted */
929 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
930 *src
, /* Pointer to original string */
931 *dst
; /* Pointer to destination string */
932 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
933 SLPError error
; /* SLP error, if any */
936 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSendSLPBrowse(%p = \"%s\")", p
,
940 * Make the SLP service URL that conforms to the IANA
941 * 'printer:' template.
944 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
946 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
949 * Figure out the finishings string...
952 if (p
->type
& CUPS_PRINTER_STAPLE
)
953 strcpy(finishings
, "staple");
955 finishings
[0] = '\0';
957 if (p
->type
& CUPS_PRINTER_BIND
)
960 strlcat(finishings
, ",bind", sizeof(finishings
));
962 strcpy(finishings
, "bind");
965 if (p
->type
& CUPS_PRINTER_PUNCH
)
968 strlcat(finishings
, ",punch", sizeof(finishings
));
970 strcpy(finishings
, "punch");
973 if (p
->type
& CUPS_PRINTER_COVER
)
976 strlcat(finishings
, ",cover", sizeof(finishings
));
978 strcpy(finishings
, "cover");
981 if (p
->type
& CUPS_PRINTER_SORT
)
984 strlcat(finishings
, ",sort", sizeof(finishings
));
986 strcpy(finishings
, "sort");
990 strcpy(finishings
, "none");
993 * Quote any commas in the make and model, location, and info strings...
996 for (src
= p
->make_model
, dst
= make_model
;
997 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
999 if (*src
== ',' || *src
== '\\' || *src
== ')')
1008 strcpy(make_model
, "Unknown");
1010 for (src
= p
->location
, dst
= location
;
1011 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
1013 if (*src
== ',' || *src
== '\\' || *src
== ')')
1022 strcpy(location
, "Unknown");
1024 for (src
= p
->info
, dst
= info
;
1025 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
1027 if (*src
== ',' || *src
== '\\' || *src
== ')')
1036 strcpy(info
, "Unknown");
1039 * Get the authentication value...
1042 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
1046 * Make the SLP attribute string list that conforms to
1047 * the IANA 'printer:' template.
1050 snprintf(attrs
, sizeof(attrs
),
1051 "(printer-uri-supported=%s),"
1052 "(uri-authentication-supported=%s>),"
1054 "(uri-security-supported=tls>),"
1056 "(uri-security-supported=none>),"
1057 #endif /* HAVE_SSL */
1058 "(printer-name=%s),"
1059 "(printer-location=%s),"
1060 "(printer-info=%s),"
1061 "(printer-more-info=%s),"
1062 "(printer-make-and-model=%s),"
1063 "(charset-supported=utf-8),"
1064 "(natural-language-configured=%s),"
1065 "(natural-language-supported=de,en,es,fr,it),"
1066 "(color-supported=%s),"
1067 "(finishings-supported=%s),"
1068 "(sides-supported=one-sided%s),"
1069 "(multiple-document-jobs-supported=true)"
1070 "(ipp-versions-supported=1.0,1.1)",
1071 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
1072 info
, p
->uri
, make_model
, DefaultLanguage
,
1073 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
1075 p
->type
& CUPS_PRINTER_DUPLEX
?
1076 ",two-sided-long-edge,two-sided-short-edge" : "");
1078 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
1081 * Register the printer with the SLP server...
1084 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
1085 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
1087 if (error
!= SLP_OK
)
1088 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
1091 #endif /* HAVE_LIBSLP */
1095 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1099 cupsdStartBrowsing(void)
1101 int val
; /* Socket option value */
1102 struct sockaddr_in addr
; /* Broadcast address */
1107 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1110 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
1112 if (BrowseSocket
< 0)
1115 * Create the broadcast socket...
1118 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1120 cupsdLogMessage(CUPSD_LOG_ERROR
,
1121 "cupsdStartBrowsing: Unable to create broadcast "
1122 "socket - %s.", strerror(errno
));
1123 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1124 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1129 * Bind the socket to browse port...
1132 memset(&addr
, 0, sizeof(addr
));
1133 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1134 addr
.sin_family
= AF_INET
;
1135 addr
.sin_port
= htons(BrowsePort
);
1137 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1139 cupsdLogMessage(CUPSD_LOG_ERROR
,
1140 "cupsdStartBrowsing: Unable to bind broadcast "
1141 "socket - %s.", strerror(errno
));
1144 closesocket(BrowseSocket
);
1146 close(BrowseSocket
);
1150 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1151 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1157 * Set the "broadcast" flag...
1161 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1163 cupsdLogMessage(CUPSD_LOG_ERROR
,
1164 "cupsdStartBrowsing: Unable to set broadcast mode - %s.",
1168 closesocket(BrowseSocket
);
1170 close(BrowseSocket
);
1174 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1175 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1180 * Close the socket on exec...
1183 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1186 * Finally, add the socket to the input selection set...
1189 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1190 "cupsdStartBrowsing: Adding fd %d to InputSet...",
1193 FD_SET(BrowseSocket
, InputSet
);
1199 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1202 * Open SLP handle...
1205 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1207 cupsdLogMessage(CUPSD_LOG_ERROR
,
1208 "Unable to open an SLP handle; disabling SLP browsing!");
1209 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1210 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1213 BrowseSLPRefresh
= 0;
1215 #endif /* HAVE_LIBSLP */
1220 * 'cupsdStartPolling()' - Start polling servers as needed.
1224 cupsdStartPolling(void)
1226 int i
; /* Looping var */
1227 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1228 char polld
[1024]; /* Poll daemon path */
1229 char sport
[10]; /* Server port */
1230 char bport
[10]; /* Browser port */
1231 char interval
[10]; /* Poll interval */
1232 int statusfds
[2]; /* Status pipe */
1233 char *argv
[6]; /* Arguments */
1234 char *envp
[100]; /* Environment */
1238 * Don't do anything if we aren't polling...
1244 PollStatusBuffer
= NULL
;
1249 * Setup string arguments for polld, port and interval options.
1252 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1254 sprintf(bport
, "%d", BrowsePort
);
1257 sprintf(interval
, "%d", BrowseInterval
);
1259 strcpy(interval
, "30");
1261 argv
[0] = "cups-polld";
1267 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1270 * Create a pipe that receives the status messages from each
1274 if (cupsdOpenPipe(statusfds
))
1276 cupsdLogMessage(CUPSD_LOG_ERROR
,
1277 "Unable to create polling status pipes - %s.",
1280 PollStatusBuffer
= NULL
;
1284 PollPipe
= statusfds
[0];
1285 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1288 * Run each polling daemon, redirecting stderr to the polling pipe...
1291 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1293 sprintf(sport
, "%d", pollp
->port
);
1295 argv
[1] = pollp
->hostname
;
1297 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1,
1298 0, &(pollp
->pid
)) < 0)
1300 cupsdLogMessage(CUPSD_LOG_ERROR
,
1301 "cupsdStartPolling: Unable to fork polling daemon - %s",
1307 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1308 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1309 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1312 close(statusfds
[1]);
1315 * Finally, add the pipe to the input selection set...
1318 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1319 "cupsdStartPolling: Adding fd %d to InputSet...", PollPipe
);
1321 FD_SET(PollPipe
, InputSet
);
1326 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1330 cupsdStopBrowsing(void)
1332 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1335 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1339 * Close the socket and remove it from the input selection set.
1343 closesocket(BrowseSocket
);
1345 close(BrowseSocket
);
1348 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1349 "cupsdStopBrowsing: Removing fd %d from InputSet...",
1352 FD_CLR(BrowseSocket
, InputSet
);
1357 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1360 * Close SLP handle...
1363 SLPClose(BrowseSLPHandle
);
1365 #endif /* HAVE_LIBSLP */
1370 * 'cupsdStopPolling()' - Stop polling servers as needed.
1374 cupsdStopPolling(void)
1376 int i
; /* Looping var */
1377 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1382 cupsdStatBufDelete(PollStatusBuffer
);
1385 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1386 "cupsdStopPolling: removing fd %d from InputSet.", PollPipe
);
1387 FD_CLR(PollPipe
, InputSet
);
1390 PollStatusBuffer
= NULL
;
1393 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1395 cupsdEndProcess(pollp
->pid
, 0);
1400 * 'cupsdUpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
1404 cupsdUpdateCUPSBrowse(void)
1406 int i
; /* Looping var */
1407 int auth
; /* Authorization status */
1408 int len
; /* Length of name string */
1409 int bytes
; /* Number of bytes left */
1410 char packet
[1541], /* Broadcast packet */
1411 *pptr
; /* Pointer into packet */
1412 socklen_t srclen
; /* Length of source address */
1413 http_addr_t srcaddr
; /* Source address */
1414 char srcname
[1024]; /* Source hostname */
1415 unsigned address
[4]; /* Source address */
1416 unsigned type
; /* Printer type */
1417 unsigned state
; /* Printer state */
1418 char uri
[HTTP_MAX_URI
], /* Printer URI */
1419 method
[HTTP_MAX_URI
], /* Method portion of URI */
1420 username
[HTTP_MAX_URI
], /* Username portion of URI */
1421 host
[HTTP_MAX_URI
], /* Host portion of URI */
1422 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
1423 info
[IPP_MAX_NAME
], /* Information string */
1424 location
[IPP_MAX_NAME
], /* Location string */
1425 make_model
[IPP_MAX_NAME
];/* Make and model string */
1426 int port
; /* Port portion of URI */
1427 cupsd_netif_t
*iface
; /* Network interface */
1428 int num_attrs
; /* Number of attributes */
1429 cups_option_t
*attrs
; /* Attributes */
1433 * Read a packet from the browse socket...
1436 srclen
= sizeof(srcaddr
);
1437 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
1438 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
1441 * "Connection refused" is returned under Linux if the destination port
1442 * or address is unreachable from a previous sendto(); check for the
1443 * error here and ignore it for now...
1446 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
1448 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
1450 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browsing turned off.");
1452 cupsdStopBrowsing();
1459 packet
[bytes
] = '\0';
1462 * If we're about to sleep, ignore incoming browse packets.
1469 * Figure out where it came from...
1473 if (srcaddr
.addr
.sa_family
== AF_INET6
)
1475 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
1476 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
1477 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
1478 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
1481 #endif /* AF_INET6 */
1486 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
1489 if (HostNameLookups
)
1490 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
1492 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
1494 len
= strlen(srcname
);
1502 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
1505 * Access from localhost (127.0.0.1) is always allowed...
1513 * Do authorization checks on the domain/address...
1516 switch (BrowseACL
->order_type
)
1519 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1522 case AUTH_ALLOW
: /* Order Deny,Allow */
1525 if (cupsdCheckAuth(address
, srcname
, len
,
1526 BrowseACL
->num_deny
, BrowseACL
->deny
))
1529 if (cupsdCheckAuth(address
, srcname
, len
,
1530 BrowseACL
->num_allow
, BrowseACL
->allow
))
1534 case AUTH_DENY
: /* Order Allow,Deny */
1537 if (cupsdCheckAuth(address
, srcname
, len
,
1538 BrowseACL
->num_allow
, BrowseACL
->allow
))
1541 if (cupsdCheckAuth(address
, srcname
, len
,
1542 BrowseACL
->num_deny
, BrowseACL
->deny
))
1551 if (auth
== AUTH_DENY
)
1553 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1554 "cupsdUpdateCUPSBrowse: Refused %d bytes from %s", bytes
,
1559 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1560 "cupsdUpdateCUPSBrowse: (%d bytes from %s) %s", bytes
,
1567 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
1569 cupsdLogMessage(CUPSD_LOG_WARN
,
1570 "cupsdUpdateCUPSBrowse: Garbled browse packet - %s", packet
);
1574 strcpy(location
, "Location Unknown");
1575 strcpy(info
, "No Information Available");
1576 make_model
[0] = '\0';
1580 if ((pptr
= strchr(packet
, '\"')) != NULL
)
1583 * Have extended information; can't use sscanf for it because not all
1584 * sscanf's allow empty strings with %[^\"]...
1587 for (i
= 0, pptr
++;
1588 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
1590 location
[i
] = *pptr
;
1598 while (*pptr
&& isspace(*pptr
& 255))
1603 for (i
= 0, pptr
++;
1604 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
1613 while (*pptr
&& isspace(*pptr
& 255))
1618 for (i
= 0, pptr
++;
1619 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
1621 make_model
[i
] = *pptr
;
1626 make_model
[i
] = '\0';
1629 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
1635 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
1636 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
1637 type
, state
, uri
, location
, info
, make_model
));
1640 * Pull the URI apart to see if this is a local or remote printer...
1643 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, method
, sizeof(method
), username
,
1644 sizeof(username
), host
, sizeof(host
), &port
, resource
,
1647 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1650 * Check for packets from the local server...
1653 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
1655 cupsFreeOptions(num_attrs
, attrs
);
1661 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1663 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1664 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
1666 cupsFreeOptions(num_attrs
, attrs
);
1674 for (i
= 0; i
< NumRelays
; i
++)
1675 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
1676 if (sendto(BrowseSocket
, packet
, bytes
, 0,
1677 (struct sockaddr
*)&(Relays
[i
].to
),
1678 sizeof(http_addr_t
)) <= 0)
1680 cupsdLogMessage(CUPSD_LOG_ERROR
,
1681 "cupsdUpdateCUPSBrowse: sendto failed for relay %d - %s.",
1682 i
+ 1, strerror(errno
));
1683 cupsFreeOptions(num_attrs
, attrs
);
1688 * Process the browse data...
1691 process_browse_data(uri
, (cups_ptype_t
)type
, (ipp_pstate_t
)state
, location
,
1692 info
, make_model
, num_attrs
, attrs
);
1693 cupsFreeOptions(num_attrs
, attrs
);
1698 * 'cupsdUpdatePolling()' - Read status messages from the poll daemons.
1702 cupsdUpdatePolling(void)
1704 char *ptr
, /* Pointer to end of line in buffer */
1705 message
[1024]; /* Pointer to message text */
1706 int loglevel
; /* Log level for message */
1709 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
1710 message
, sizeof(message
))) != NULL
)
1711 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
1717 * All polling processes have died; stop polling...
1720 cupsdLogMessage(CUPSD_LOG_ERROR
,
1721 "cupsdUpdatePolling: all polling processes have exited!");
1729 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1733 cupsdUpdateSLPBrowse(void)
1735 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1736 *next
; /* Next service in list */
1737 cupsd_printer_t p
; /* Printer information */
1738 const char *uri
; /* Pointer to printer URI */
1739 char method
[HTTP_MAX_URI
],
1740 /* Method portion of URI */
1741 username
[HTTP_MAX_URI
],
1742 /* Username portion of URI */
1744 /* Host portion of URI */
1745 resource
[HTTP_MAX_URI
];
1746 /* Resource portion of URI */
1747 int port
; /* Port portion of URI */
1750 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdUpdateSLPBrowse() Start...");
1753 * Reset the refresh time...
1756 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1759 * Poll for remote printers using SLP...
1764 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1765 slp_url_callback
, &s
);
1768 * Loop through the list of available printers...
1774 * Save the "next" pointer...
1780 * Load a cupsd_printer_t structure with the SLP service attributes...
1783 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1786 * Process this printer entry...
1789 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1791 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1794 * Pull the URI apart to see if this is a local or remote printer...
1797 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, method
, sizeof(method
),
1798 username
, sizeof(username
), host
, sizeof(host
), &port
,
1799 resource
, sizeof(resource
));
1801 if (!strcasecmp(host
, ServerName
))
1805 * OK, at least an IPP printer, see if it is a CUPS printer or
1809 if (strstr(uri
, "/printers/") != NULL
)
1810 process_browse_data(uri
, p
.type
, IPP_PRINTER_IDLE
, p
.location
,
1811 p
.info
, p
.make_model
, 0, NULL
);
1812 else if (strstr(uri
, "/classes/") != NULL
)
1813 process_browse_data(uri
, p
.type
| CUPS_PRINTER_CLASS
, IPP_PRINTER_IDLE
,
1814 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1818 * Free this listing...
1824 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdUpdateSLPBrowse() End...");
1826 #endif /* HAVE_LIBSLP */
1830 * 'dequote()' - Remote quotes from a string.
1833 static char * /* O - Dequoted string */
1834 dequote(char *d
, /* I - Destination string */
1835 const char *s
, /* I - Source string */
1836 int dlen
) /* I - Destination length */
1838 char *dptr
; /* Pointer into destination */
1843 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1860 * 'process_browse_data()' - Process new browse data.
1864 process_browse_data(
1865 const char *uri
, /* I - URI of printer/class */
1866 cups_ptype_t type
, /* I - Printer type */
1867 ipp_pstate_t state
, /* I - Printer state */
1868 const char *location
, /* I - Printer location */
1869 const char *info
, /* I - Printer information */
1870 const char *make_model
, /* I - Printer make and model */
1871 int num_attrs
, /* I - Number of attributes */
1872 cups_option_t
*attrs
) /* I - Attributes */
1874 int update
; /* Update printer attributes? */
1875 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
1876 method
[HTTP_MAX_URI
], /* Method portion of URI */
1877 username
[HTTP_MAX_URI
], /* Username portion of URI */
1878 host
[HTTP_MAX_URI
], /* Host portion of URI */
1879 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1880 int port
; /* Port portion of URI */
1881 char name
[IPP_MAX_NAME
], /* Name of printer */
1882 *hptr
, /* Pointer into hostname */
1883 *sptr
; /* Pointer into ServerName */
1884 char local_make_model
[IPP_MAX_NAME
];
1885 /* Local make and model */
1886 cupsd_printer_t
*p
; /* Printer information */
1887 const char *ipp_options
; /* ipp-options value */
1891 * Pull the URI apart to see if this is a local or remote printer...
1894 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, method
, sizeof(method
), username
,
1895 sizeof(username
), host
, sizeof(host
), &port
, resource
,
1899 * Determine if the URI contains any illegal characters in it...
1902 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
1903 (strncmp(resource
, "/printers/", 10) &&
1904 strncmp(resource
, "/classes/", 9)))
1906 cupsdLogMessage(CUPSD_LOG_ERROR
,
1907 "process_browse_data: Bad printer URI in browse data: %s",
1912 if (strchr(resource
, '?') ||
1913 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
1914 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
1916 cupsdLogMessage(CUPSD_LOG_ERROR
,
1917 "process_browse_data: Bad resource in browse data: %s",
1923 * OK, this isn't a local printer; add any remote options...
1926 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
1928 if (BrowseRemoteOptions
)
1930 if (BrowseRemoteOptions
[0] == '?')
1933 * Override server-supplied options...
1936 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
1938 else if (ipp_options
)
1941 * Combine the server and local options...
1944 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
1945 BrowseRemoteOptions
);
1950 * Just use the local options...
1953 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
1958 else if (ipp_options
)
1961 * Just use the server-supplied options...
1964 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
1969 * See if we already have it listed in the Printers list, and add it if not...
1972 type
|= CUPS_PRINTER_REMOTE
;
1973 type
&= ~CUPS_PRINTER_IMPLICIT
;
1975 hptr
= strchr(host
, '.');
1976 sptr
= strchr(ServerName
, '.');
1978 if (sptr
!= NULL
&& hptr
!= NULL
)
1981 * Strip the common domain name components...
1984 while (hptr
!= NULL
)
1986 if (!strcasecmp(hptr
, sptr
))
1992 hptr
= strchr(hptr
+ 1, '.');
1996 if (type
& CUPS_PRINTER_CLASS
)
1999 * Remote destination is a class...
2002 if (!strncmp(resource
, "/classes/", 9))
2003 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
2007 if ((p
= cupsdFindClass(name
)) == NULL
&& BrowseShortNames
)
2009 if ((p
= cupsdFindClass(resource
+ 9)) != NULL
)
2011 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2014 * Nope, this isn't the same host; if the hostname isn't the local host,
2015 * add it to the other class and then find a class using the full host
2019 if (p
->type
& CUPS_PRINTER_REMOTE
)
2021 cupsdLogMessage(CUPSD_LOG_INFO
,
2022 "Renamed remote class \"%s\" to \"%s@%s\"...",
2023 p
->name
, p
->name
, p
->hostname
);
2024 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2025 "Class \'%s\' deleted by directory services.",
2028 cupsArrayRemove(Printers
, p
);
2029 cupsdSetStringf(&p
->name
, "%s@%s", p
->name
, p
->hostname
);
2030 cupsdSetPrinterAttrs(p
);
2031 cupsArrayAdd(Printers
, p
);
2033 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2034 "Class \'%s\' added by directory services.",
2040 else if (!p
->hostname
)
2043 * Hostname not set, so this must be a cached remote printer
2044 * that was created for a pending print job...
2047 cupsdSetString(&p
->hostname
, host
);
2048 cupsdSetString(&p
->uri
, uri
);
2049 cupsdSetString(&p
->device_uri
, uri
);
2056 * Use the short name for this shared class.
2059 strlcpy(name
, resource
+ 9, sizeof(name
));
2062 else if (p
&& !p
->hostname
)
2065 * Hostname not set, so this must be a cached remote printer
2066 * that was created for a pending print job...
2069 cupsdSetString(&p
->hostname
, host
);
2070 cupsdSetString(&p
->uri
, uri
);
2071 cupsdSetString(&p
->device_uri
, uri
);
2078 * Class doesn't exist; add it...
2081 p
= cupsdAddClass(name
);
2083 cupsdLogMessage(CUPSD_LOG_INFO
, "Added remote class \"%s\"...", name
);
2085 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2086 "Class \'%s\' added by directory services.", name
);
2089 * Force the URI to point to the real server...
2092 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2094 cupsdSetString(&p
->uri
, uri
);
2095 cupsdSetString(&p
->device_uri
, uri
);
2096 cupsdSetString(&p
->hostname
, host
);
2104 * Remote destination is a printer...
2107 if (!strncmp(resource
, "/printers/", 10))
2108 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
2112 if ((p
= cupsdFindPrinter(name
)) == NULL
&& BrowseShortNames
)
2114 if ((p
= cupsdFindPrinter(resource
+ 10)) != NULL
)
2116 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2119 * Nope, this isn't the same host; if the hostname isn't the local host,
2120 * add it to the other printer and then find a printer using the full host
2124 if (p
->type
& CUPS_PRINTER_REMOTE
)
2126 cupsdLogMessage(CUPSD_LOG_INFO
,
2127 "Renamed remote printer \"%s\" to \"%s@%s\"...",
2128 p
->name
, p
->name
, p
->hostname
);
2129 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2130 "Printer \'%s\' deleted by directory services.",
2133 cupsArrayRemove(Printers
, p
);
2134 cupsdSetStringf(&p
->name
, "%s@%s", p
->name
, p
->hostname
);
2135 cupsdSetPrinterAttrs(p
);
2136 cupsArrayAdd(Printers
, p
);
2138 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2139 "Printer \'%s\' added by directory services.",
2145 else if (!p
->hostname
)
2148 * Hostname not set, so this must be a cached remote printer
2149 * that was created for a pending print job...
2152 cupsdSetString(&p
->hostname
, host
);
2153 cupsdSetString(&p
->uri
, uri
);
2154 cupsdSetString(&p
->device_uri
, uri
);
2161 * Use the short name for this shared printer.
2164 strlcpy(name
, resource
+ 10, sizeof(name
));
2167 else if (p
&& !p
->hostname
)
2170 * Hostname not set, so this must be a cached remote printer
2171 * that was created for a pending print job...
2174 cupsdSetString(&p
->hostname
, host
);
2175 cupsdSetString(&p
->uri
, uri
);
2176 cupsdSetString(&p
->device_uri
, uri
);
2183 * Printer doesn't exist; add it...
2186 p
= cupsdAddPrinter(name
);
2188 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2189 "Printer \'%s\' added by directory services.", name
);
2191 cupsdLogMessage(CUPSD_LOG_INFO
, "Added remote printer \"%s\"...", name
);
2194 * Force the URI to point to the real server...
2197 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2199 cupsdSetString(&p
->hostname
, host
);
2200 cupsdSetString(&p
->uri
, uri
);
2201 cupsdSetString(&p
->device_uri
, uri
);
2208 * Update the state...
2212 p
->browse_time
= time(NULL
);
2214 if (type
& CUPS_PRINTER_REJECTING
)
2216 type
&= ~CUPS_PRINTER_REJECTING
;
2224 else if (!p
->accepting
)
2230 if (p
->type
!= type
)
2236 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2238 cupsdSetString(&p
->location
, location
);
2242 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2244 cupsdSetString(&p
->info
, info
);
2248 if (!make_model
|| !make_model
[0])
2250 if (type
& CUPS_PRINTER_CLASS
)
2251 snprintf(local_make_model
, sizeof(local_make_model
),
2252 "Remote Class on %s", host
);
2254 snprintf(local_make_model
, sizeof(local_make_model
),
2255 "Remote Printer on %s", host
);
2258 snprintf(local_make_model
, sizeof(local_make_model
),
2259 "%s on %s", make_model
, host
);
2261 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2263 cupsdSetString(&p
->make_model
, local_make_model
);
2267 if (type
& CUPS_PRINTER_DELETE
)
2269 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2270 "%s \'%s\' deleted by directory services.",
2271 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2273 cupsdExpireSubscriptions(p
, NULL
);
2275 cupsdDeletePrinter(p
, 1);
2276 cupsdUpdateImplicitClasses();
2280 cupsdSetPrinterAttrs(p
);
2281 cupsdUpdateImplicitClasses();
2285 * See if we have a default printer... If not, make the first printer the
2289 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2292 * Find the first network default printer and use it...
2295 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2297 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2298 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2306 * Do auto-classing if needed...
2309 process_implicit_classes();
2312 * Update the printcap file...
2315 cupsdWritePrintcap();
2320 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2324 process_implicit_classes(void)
2326 int i
; /* Looping var */
2327 int update
; /* Update printer attributes? */
2328 char name
[IPP_MAX_NAME
], /* Name of printer */
2329 *hptr
; /* Pointer into hostname */
2330 cupsd_printer_t
*p
, /* Printer information */
2331 *pclass
, /* Printer class */
2332 *first
; /* First printer in class */
2333 int offset
, /* Offset of name */
2334 len
; /* Length of name */
2337 if (!ImplicitClasses
|| !Printers
)
2341 * Loop through all available printers and create classes as needed...
2344 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2345 update
= 0, pclass
= NULL
, first
= NULL
;
2347 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2350 * Skip implicit classes...
2353 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2360 * If len == 0, get the length of this printer name up to the "@"
2364 cupsArraySave(Printers
);
2367 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2368 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2371 * We have more than one printer with the same name; see if
2372 * we have a class, and if this printer is a member...
2375 if (pclass
&& strcasecmp(pclass
->name
, name
))
2378 cupsdSetPrinterAttrs(pclass
);
2384 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2387 * Need to add the class...
2390 pclass
= cupsdAddPrinter(name
);
2391 cupsArrayAdd(ImplicitPrinters
, pclass
);
2393 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2394 pclass
->accepting
= 1;
2395 pclass
->state
= IPP_PRINTER_IDLE
;
2397 cupsdSetString(&pclass
->location
, p
->location
);
2398 cupsdSetString(&pclass
->info
, p
->info
);
2402 cupsdLogMessage(CUPSD_LOG_INFO
, "Added implicit class \"%s\"...",
2408 for (i
= 0; i
< pclass
->num_printers
; i
++)
2409 if (pclass
->printers
[i
] == first
)
2412 if (i
>= pclass
->num_printers
)
2414 first
->in_implicit_class
= 1;
2415 cupsdAddPrinterToClass(pclass
, first
);
2421 for (i
= 0; i
< pclass
->num_printers
; i
++)
2422 if (pclass
->printers
[i
] == p
)
2425 if (i
>= pclass
->num_printers
)
2427 p
->in_implicit_class
= 1;
2428 cupsdAddPrinterToClass(pclass
, p
);
2435 * First time around; just get name length and mark it as first
2439 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
2440 len
= hptr
- p
->name
;
2442 len
= strlen(p
->name
);
2444 strncpy(name
, p
->name
, len
);
2448 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
2449 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
2452 * Can't use same name as a local printer; add "Any" to the
2453 * front of the name, unless we have explicitly disabled
2454 * the "ImplicitAnyClasses"...
2457 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
2460 * Add "Any" to the class name...
2463 strcpy(name
, "Any");
2464 strncpy(name
+ 3, p
->name
, len
);
2465 name
[len
+ 3] = '\0';
2471 * Don't create an implicit class if we have a local printer
2472 * with the same name...
2476 cupsArrayRestore(Printers
);
2484 cupsArrayRestore(Printers
);
2488 * Update the last printer class as needed...
2491 if (pclass
&& update
)
2492 cupsdSetPrinterAttrs(pclass
);
2498 * 'slp_attr_callback()' - SLP attribute callback
2501 static SLPBoolean
/* O - SLP_TRUE for success */
2503 SLPHandle hslp
, /* I - SLP handle */
2504 const char *attrlist
, /* I - Attribute list */
2505 SLPError errcode
, /* I - Parsing status for this attr */
2506 void *cookie
) /* I - Current printer */
2509 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
2513 * Let the compiler know we won't be using these...
2519 * Bail if there was an error
2522 if (errcode
!= SLP_OK
)
2526 * Parse the attrlist to obtain things needed to build CUPS browse packet
2529 memset(p
, 0, sizeof(cupsd_printer_t
));
2531 p
->type
= CUPS_PRINTER_REMOTE
;
2533 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
2535 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
2537 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
2540 if (slp_get_attr(attrlist
, "(color-supported=", &tmp
))
2542 if (!strcasecmp(tmp
, "true"))
2543 p
->type
|= CUPS_PRINTER_COLOR
;
2545 if (slp_get_attr(attrlist
, "(finishings-supported=", &tmp
))
2547 if (strstr(tmp
, "staple"))
2548 p
->type
|= CUPS_PRINTER_STAPLE
;
2549 if (strstr(tmp
, "bind"))
2550 p
->type
|= CUPS_PRINTER_BIND
;
2551 if (strstr(tmp
, "punch"))
2552 p
->type
|= CUPS_PRINTER_PUNCH
;
2554 if (slp_get_attr(attrlist
, "(sides-supported=", &tmp
))
2556 if (strstr(tmp
,"two-sided"))
2557 p
->type
|= CUPS_PRINTER_DUPLEX
;
2559 cupsdClearString(&tmp
);
2566 * 'slp_dereg_printer()' - SLPDereg() the specified printer
2570 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
2572 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
2575 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
2577 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
2580 * Make the SLP service URL that conforms to the IANA
2581 * 'printer:' template.
2584 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
2587 * Deregister the printer...
2590 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
2596 * 'slp_get_attr()' - Get an attribute from an SLP registration.
2599 static int /* O - 0 on success */
2600 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
2601 const char *tag
, /* I - Name of attribute */
2602 char **valbuf
) /* O - Value */
2604 char *ptr1
, /* Pointer into string */
2608 cupsdClearString(valbuf
);
2610 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
2612 ptr1
+= strlen(tag
);
2614 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
2620 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
2621 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
2624 * Dequote the value...
2627 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
2628 if (*ptr1
== '\\' && ptr1
[1])
2629 _cups_strcpy(ptr1
, ptr1
+ 1);
2640 * 'slp_reg_callback()' - Empty SLPRegReport.
2644 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
2645 SLPError errcode
, /* I - Error code, if any */
2646 void *cookie
) /* I - App data */
2657 * 'slp_url_callback()' - SLP service url callback
2660 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
2662 SLPHandle hslp
, /* I - SLP handle */
2663 const char *srvurl
, /* I - URL of service */
2664 unsigned short lifetime
, /* I - Life of service */
2665 SLPError errcode
, /* I - Existing error code */
2666 void *cookie
) /* I - Pointer to service list */
2668 slpsrvurl_t
*s
, /* New service entry */
2669 **head
; /* Pointer to head of entry */
2673 * Let the compiler know we won't be using these vars...
2680 * Bail if there was an error
2683 if (errcode
!= SLP_OK
)
2687 * Grab the head of the list...
2690 head
= (slpsrvurl_t
**)cookie
;
2693 * Allocate a *temporary* slpsrvurl_t to hold this entry.
2696 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
2700 * Copy the SLP service URL...
2703 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
2706 * Link the SLP service URL into the head of the list
2716 #endif /* HAVE_LIBSLP */
2720 * End of "$Id: dirsvc.c 5059 2006-02-02 23:17:16Z mike $".