]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/dirsvc.c
2 * "$Id: dirsvc.c,v 1.73.2.20 2003/01/07 18:27:19 mike Exp $"
4 * Directory services routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2003 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-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * ProcessBrowseData() - Process new browse data.
27 * SendBrowseList() - Send new browsing information as necessary.
28 * SendCUPSBrowse() - Send new browsing information using the CUPS protocol.
29 * StartBrowsing() - Start sending and receiving broadcast information.
30 * StartPolling() - Start polling servers as needed.
31 * StopBrowsing() - Stop sending and receiving broadcast information.
32 * StopPolling() - Stop polling servers as needed.
33 * UpdateCUPSBrowse() - Update the browse lists using the CUPS protocol.
34 * UpdatePolling() - Read status messages from the poll daemons.
35 * RegReportCallback() - Empty SLPRegReport.
36 * SendSLPBrowse() - Register the specified printer with SLP.
37 * SLPDeregPrinter() - SLPDereg() the specified printer
38 * GetSlpAttrVal() - Get an attribute from an SLP registration.
39 * AttrCallback() - SLP attribute callback
40 * SrvUrlCallback() - SLP service url callback
41 * UpdateSLPBrowse() - Get browsing information via SLP.
45 * Include necessary headers...
53 * 'ProcessBrowseData()' - Process new browse data.
57 ProcessBrowseData(const char *uri
, /* I - URI of printer/class */
58 cups_ptype_t type
, /* I - Printer type */
59 ipp_pstate_t state
, /* I - Printer state */
60 const char *location
,/* I - Printer location */
61 const char *info
, /* I - Printer information */
62 const char *make_model
) /* I - Printer make and model */
64 int i
; /* Looping var */
65 int update
; /* Update printer attributes? */
66 char method
[HTTP_MAX_URI
], /* Method portion of URI */
67 username
[HTTP_MAX_URI
], /* Username portion of URI */
68 host
[HTTP_MAX_URI
], /* Host portion of URI */
69 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
70 int port
; /* Port portion of URI */
71 char name
[IPP_MAX_NAME
], /* Name of printer */
72 *hptr
, /* Pointer into hostname */
73 *sptr
; /* Pointer into ServerName */
74 char local_make_model
[IPP_MAX_NAME
];
75 /* Local make and model */
76 printer_t
*p
, /* Printer information */
77 *pclass
, /* Printer class */
78 *first
, /* First printer in class */
79 *next
; /* Next printer in list */
80 int offset
, /* Offset of name */
81 len
; /* Length of name */
85 * Pull the URI apart to see if this is a local or remote printer...
88 httpSeparate(uri
, method
, username
, host
, &port
, resource
);
91 * Determine if the URI contains any illegal characters in it...
94 if (strncmp(uri
, "ipp://", 6) != 0 ||
96 (strncmp(resource
, "/printers/", 10) != 0 &&
97 strncmp(resource
, "/classes/", 9) != 0))
99 LogMessage(L_ERROR
, "ProcessBrowseData: Bad printer URI in browse data: %s",
104 if (strchr(resource
, '?') != NULL
||
105 (strncmp(resource
, "/printers/", 10) == 0 &&
106 strchr(resource
+ 10, '/') != NULL
) ||
107 (strncmp(resource
, "/classes/", 9) == 0 &&
108 strchr(resource
+ 9, '/') != NULL
))
110 LogMessage(L_ERROR
, "ProcessBrowseData: Bad resource in browse data: %s",
116 * OK, this isn't a local printer; see if we already have it listed in
117 * the Printers list, and add it if not...
121 hptr
= strchr(host
, '.');
122 sptr
= strchr(ServerName
, '.');
124 if (sptr
!= NULL
&& hptr
!= NULL
)
127 * Strip the common domain name components...
132 if (strcasecmp(hptr
, sptr
) == 0)
138 hptr
= strchr(hptr
+ 1, '.');
142 if (type
& CUPS_PRINTER_CLASS
)
145 * Remote destination is a class...
148 if (strncmp(resource
, "/classes/", 9) == 0)
149 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
153 if ((p
= FindClass(name
)) == NULL
&& BrowseShortNames
)
155 if ((p
= FindClass(resource
+ 9)) != NULL
)
157 if (strcasecmp(p
->hostname
, host
) != 0 && p
->hostname
[0])
160 * Nope, this isn't the same host; if the hostname isn't the local host,
161 * add it to the other class and then find a class using the full host
165 if (p
->type
& CUPS_PRINTER_REMOTE
)
167 strlcat(p
->name
, "@", sizeof(p
->name
));
168 strlcat(p
->name
, p
->hostname
, sizeof(p
->name
));
175 else if (!p
->hostname
[0])
177 strlcpy(p
->hostname
, host
, sizeof(p
->hostname
));
178 strlcpy(p
->uri
, uri
, sizeof(p
->uri
));
179 strlcpy(p
->device_uri
, uri
, sizeof(p
->device_uri
));
184 strlcpy(name
, resource
+ 9, sizeof(name
));
186 else if (p
!= NULL
&& !p
->hostname
[0])
188 strlcpy(p
->hostname
, host
, sizeof(p
->hostname
));
189 strlcpy(p
->uri
, uri
, sizeof(p
->uri
));
190 strlcpy(p
->device_uri
, uri
, sizeof(p
->device_uri
));
197 * Class doesn't exist; add it...
202 LogMessage(L_INFO
, "Added remote class \"%s\"...", name
);
205 * Force the URI to point to the real server...
210 strlcpy(p
->uri
, uri
, sizeof(p
->uri
));
211 strlcpy(p
->device_uri
, uri
, sizeof(p
->device_uri
));
212 strlcpy(p
->hostname
, host
, sizeof(p
->hostname
));
220 * Remote destination is a printer...
223 if (strncmp(resource
, "/printers/", 10) == 0)
224 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
228 if ((p
= FindPrinter(name
)) == NULL
&& BrowseShortNames
)
230 if ((p
= FindPrinter(resource
+ 10)) != NULL
)
232 if (strcasecmp(p
->hostname
, host
) != 0 && p
->hostname
[0])
235 * Nope, this isn't the same host; if the hostname isn't the local host,
236 * add it to the other printer and then find a printer using the full host
240 if (p
->type
& CUPS_PRINTER_REMOTE
)
242 strlcat(p
->name
, "@", sizeof(p
->name
));
243 strlcat(p
->name
, p
->hostname
, sizeof(p
->name
));
250 else if (!p
->hostname
[0])
252 strlcpy(p
->hostname
, host
, sizeof(p
->hostname
));
253 strlcpy(p
->uri
, uri
, sizeof(p
->uri
));
254 strlcpy(p
->device_uri
, uri
, sizeof(p
->device_uri
));
259 strlcpy(name
, resource
+ 10, sizeof(name
));
261 else if (p
!= NULL
&& !p
->hostname
[0])
263 strlcpy(p
->hostname
, host
, sizeof(p
->hostname
));
264 strlcpy(p
->uri
, uri
, sizeof(p
->uri
));
265 strlcpy(p
->device_uri
, uri
, sizeof(p
->device_uri
));
272 * Printer doesn't exist; add it...
275 p
= AddPrinter(name
);
277 LogMessage(L_INFO
, "Added remote printer \"%s\"...", name
);
280 * Force the URI to point to the real server...
285 strlcpy(p
->hostname
, host
, sizeof(p
->hostname
));
286 strlcpy(p
->uri
, uri
, sizeof(p
->uri
));
287 strlcpy(p
->device_uri
, uri
, sizeof(p
->device_uri
));
294 * Update the state...
298 p
->browse_time
= time(NULL
);
306 if (strcmp(p
->location
, location
))
308 strlcpy(p
->location
, location
, sizeof(p
->location
));
312 if (strcmp(p
->info
, info
))
314 strlcpy(p
->info
, info
, sizeof(p
->info
));
320 if (type
& CUPS_PRINTER_CLASS
)
321 snprintf(local_make_model
, sizeof(local_make_model
),
322 "Remote Class on %s", host
);
324 snprintf(local_make_model
, sizeof(local_make_model
),
325 "Remote Printer on %s", host
);
328 snprintf(local_make_model
, sizeof(local_make_model
),
329 "%s on %s", make_model
, host
);
331 if (strcmp(p
->make_model
, local_make_model
))
333 strlcpy(p
->make_model
, local_make_model
, sizeof(p
->make_model
));
341 * See if we have a default printer... If not, make the first printer the
345 if (DefaultPrinter
== NULL
&& Printers
!= NULL
)
347 DefaultPrinter
= Printers
;
353 * Do auto-classing if needed...
359 * Loop through all available printers and create classes as needed...
362 for (p
= Printers
, len
= 0, offset
= 0, first
= NULL
;
367 * Get next printer in list...
376 if (p
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
))
383 * If len == 0, get the length of this printer name up to the "@"
388 strncasecmp(p
->name
, name
+ offset
, len
) == 0 &&
389 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
392 * We have more than one printer with the same name; see if
393 * we have a class, and if this printer is a member...
396 if ((pclass
= FindPrinter(name
)) == NULL
)
399 * Need to add the class...
402 pclass
= AddPrinter(name
);
403 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
404 pclass
->accepting
= 1;
405 pclass
->state
= IPP_PRINTER_IDLE
;
407 strcpy(pclass
->location
, p
->location
);
408 strcpy(pclass
->info
, p
->info
);
410 SetPrinterAttrs(pclass
);
412 LogMessage(L_INFO
, "Added implicit class \"%s\"...", name
);
417 for (i
= 0; i
< pclass
->num_printers
; i
++)
418 if (pclass
->printers
[i
] == first
)
421 if (i
>= pclass
->num_printers
)
422 AddPrinterToClass(pclass
, first
);
427 for (i
= 0; i
< pclass
->num_printers
; i
++)
428 if (pclass
->printers
[i
] == p
)
431 if (i
>= pclass
->num_printers
)
432 AddPrinterToClass(pclass
, p
);
437 * First time around; just get name length and mark it as first
441 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
442 len
= hptr
- p
->name
;
444 len
= strlen(p
->name
);
446 strncpy(name
, p
->name
, len
);
450 if ((pclass
= FindPrinter(name
)) != NULL
&&
451 !(pclass
->type
& CUPS_PRINTER_IMPLICIT
))
454 * Can't use same name as a local printer; add "Any" to the
455 * front of the name, unless we have explicitly disabled
456 * the "ImplicitAnyClasses"...
459 if (ImplicitAnyClasses
)
462 * Add "Any" to the class name...
466 strncpy(name
+ 3, p
->name
, len
);
467 name
[len
+ 3] = '\0';
473 * Don't create an implicit class if we have a local printer
474 * with the same name...
490 * 'SendBrowseList()' - Send new browsing information as necessary.
496 int count
; /* Number of dests to update */
497 printer_t
*p
, /* Current printer */
498 *np
; /* Next printer */
499 time_t ut
, /* Minimum update time */
500 to
; /* Timeout time */
503 if (!Browsing
|| !(BrowseProtocols
& BROWSE_CUPS
))
507 * Compute the update and timeout times...
510 ut
= time(NULL
) - BrowseInterval
;
511 to
= time(NULL
) - BrowseTimeout
;
514 * Figure out how many printers need an update...
517 if (BrowseInterval
> 0)
519 for (count
= 0, p
= Printers
; p
!= NULL
; p
= p
->next
)
520 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
525 * Throttle the number of printers we'll be updating this time
529 count
= 2 * count
/ BrowseInterval
+ 1;
535 * Loop through all of the printers and send local updates as needed...
538 for (p
= Printers
; p
!= NULL
; p
= np
)
542 if (p
->type
& CUPS_PRINTER_REMOTE
)
545 * See if this printer needs to be timed out...
548 if (p
->browse_time
< to
)
550 LogMessage(L_INFO
, "Remote destination \"%s\" has timed out; deleting it...",
555 else if (p
->browse_time
< ut
&& count
> 0 &&
556 !(p
->type
& CUPS_PRINTER_IMPLICIT
))
559 * Need to send an update...
564 p
->browse_time
= time(NULL
);
566 if (BrowseProtocols
& BROWSE_CUPS
)
570 if (BrowseProtocols
& BROWSE_SLP
)
572 #endif /* HAVE_LIBSLP */
579 * 'SendCUPSBrowse()' - Send new browsing information using the CUPS protocol.
583 SendCUPSBrowse(printer_t
*p
) /* I - Printer to send */
585 int i
; /* Looping var */
586 dirsvc_addr_t
*b
; /* Browse address */
587 int bytes
; /* Length of packet */
588 char packet
[1453]; /* Browse data packet */
589 cups_netif_t
*iface
; /* Network interface */
593 * Send a packet to each browse address...
596 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
600 * Send the browse packet to one or more interfaces...
603 if (strcmp(b
->iface
, "*") == 0)
606 * Send to all local interfaces...
611 for (iface
= NetIFList
; iface
!= NULL
; iface
= iface
->next
)
614 * Only send to local interfaces...
617 if (!iface
->is_local
)
620 snprintf(packet
, sizeof(packet
), "%x %x ipp://%s/%s/%s \"%s\" \"%s\" \"%s\"\n",
621 p
->type
| CUPS_PRINTER_REMOTE
, p
->state
, iface
->hostname
,
622 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
623 p
->name
, p
->location
, p
->info
, p
->make_model
);
625 bytes
= strlen(packet
);
627 LogMessage(L_DEBUG2
, "SendBrowseList: (%d bytes to \"%s\") %s", bytes
,
628 iface
->name
, packet
);
630 if (iface
->broadcast
.addr
.sa_family
== AF_INET
)
632 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
634 sendto(BrowseSocket
, packet
, bytes
, 0,
635 (struct sockaddr
*)&(iface
->broadcast
),
636 sizeof(struct sockaddr_in
));
640 iface
->broadcast
.ipv6
.sin6_port
= htons(BrowsePort
);
642 sendto(BrowseSocket
, packet
, bytes
, 0,
643 (struct sockaddr
*)&(iface
->broadcast
),
644 sizeof(struct sockaddr_in6
));
648 else if ((iface
= NetIFFind(b
->iface
)) != NULL
)
651 * Send to the named interface...
654 snprintf(packet
, sizeof(packet
), "%x %x ipp://%s/%s/%s \"%s\" \"%s\" \"%s\"\n",
655 p
->type
| CUPS_PRINTER_REMOTE
, p
->state
, iface
->hostname
,
656 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
657 p
->name
, p
->location
, p
->info
, p
->make_model
);
659 bytes
= strlen(packet
);
661 LogMessage(L_DEBUG2
, "SendBrowseList: (%d bytes to \"%s\") %s", bytes
,
662 iface
->name
, packet
);
664 if (iface
->broadcast
.addr
.sa_family
== AF_INET
)
666 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
668 sendto(BrowseSocket
, packet
, bytes
, 0,
669 (struct sockaddr
*)&(iface
->broadcast
),
670 sizeof(struct sockaddr_in
));
674 iface
->broadcast
.ipv6
.sin6_port
= htons(BrowsePort
);
676 sendto(BrowseSocket
, packet
, bytes
, 0,
677 (struct sockaddr
*)&(iface
->broadcast
),
678 sizeof(struct sockaddr_in6
));
685 * Send the browse packet to the indicated address using
686 * the default server name...
689 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\"\n",
690 p
->type
| CUPS_PRINTER_REMOTE
, p
->state
, p
->uri
,
691 p
->location
, p
->info
, p
->make_model
);
693 bytes
= strlen(packet
);
694 LogMessage(L_DEBUG2
, "SendBrowseList: (%d bytes) %s", bytes
, packet
);
696 if (sendto(BrowseSocket
, packet
, bytes
, 0,
697 (struct sockaddr
*)&(b
->to
),
698 b
->to
.addr
.sa_family
== AF_INET
?
699 sizeof(struct sockaddr_in
) :
700 sizeof(struct sockaddr_in6
)) <= 0)
703 * Unable to send browse packet, so remove this address from the
707 LogMessage(L_ERROR
, "SendBrowseList: sendto failed for browser %d - %s.",
708 b
- Browsers
+ 1, strerror(errno
));
711 memcpy(b
, b
+ 1, (i
- 1) * sizeof(dirsvc_addr_t
));
721 * 'StartBrowsing()' - Start sending and receiving broadcast information.
727 int val
; /* Socket option value */
728 struct sockaddr_in addr
; /* Broadcast address */
734 if (BrowseProtocols
& BROWSE_CUPS
)
737 * Create the broadcast socket...
740 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
742 LogMessage(L_ERROR
, "StartBrowsing: Unable to create broadcast socket - %s.",
749 * Set the "broadcast" flag...
753 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
755 LogMessage(L_ERROR
, "StartBrowsing: Unable to set broadcast mode - %s.",
759 closesocket(BrowseSocket
);
770 * Bind the socket to browse port...
773 memset(&addr
, 0, sizeof(addr
));
774 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
775 addr
.sin_family
= AF_INET
;
776 addr
.sin_port
= htons(BrowsePort
);
778 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
780 LogMessage(L_ERROR
, "StartBrowsing: Unable to bind broadcast socket - %s.",
784 closesocket(BrowseSocket
);
795 * Finally, add the socket to the input selection set...
798 LogMessage(L_DEBUG2
, "StartBrowsing: Adding fd %d to InputSet...",
801 FD_SET(BrowseSocket
, &InputSet
);
805 if (BrowseProtocols
& BROWSE_SLP
)
811 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
813 LogMessage(L_ERROR
, "Unable to open an SLP handle; disabling SLP browsing!");
814 BrowseProtocols
&= ~BROWSE_SLP
;
817 BrowseSLPRefresh
= 0;
819 #endif /* HAVE_LIBSLP */
824 * 'StartPolling()' - Start polling servers as needed.
830 int i
; /* Looping var */
831 dirsvc_poll_t
*poll
; /* Current polling server */
832 int pid
; /* New process ID */
833 char sport
[10]; /* Server port */
834 char bport
[10]; /* Browser port */
835 char interval
[10]; /* Poll interval */
836 int statusfds
[2]; /* Status pipe */
837 int fd
; /* Current file descriptor */
841 * Don't do anything if we aren't polling...
851 * Setup string arguments for port and interval options.
854 sprintf(bport
, "%d", BrowsePort
);
857 sprintf(interval
, "%d", BrowseInterval
);
859 strcpy(interval
, "30");
862 * Create a pipe that receives the status messages from each
868 LogMessage(L_ERROR
, "Unable to create polling status pipes - %s.",
874 PollPipe
= statusfds
[0];
877 * Run each polling daemon, redirecting stderr to the polling pipe...
880 for (i
= 0, poll
= Polled
; i
< NumPolled
; i
++, poll
++)
882 sprintf(sport
, "%d", poll
->port
);
884 if ((pid
= fork()) == 0)
893 * Running as root, so change to non-priviledged user...
904 * Reset group membership to just the main one we belong to.
910 * Redirect stdin and stdout to /dev/null, and stderr to the
911 * status pipe. Close all other files.
915 open("/dev/null", O_RDONLY
);
918 open("/dev/null", O_WRONLY
);
923 for (fd
= 3; fd
< MaxFDs
; fd
++)
927 * Execute the polling daemon...
930 execl(CUPS_SERVERBIN
"/daemon/cups-polld", "cups-polld", poll
->hostname
,
931 sport
, interval
, bport
, NULL
);
936 LogMessage(L_ERROR
, "StartPolling: Unable to fork polling daemon - %s",
944 LogMessage(L_DEBUG
, "StartPolling: Started polling daemon for %s:%d, pid = %d",
945 poll
->hostname
, poll
->port
, pid
);
952 * Finally, add the pipe to the input selection set...
955 LogMessage(L_DEBUG2
, "StartPolling: Adding fd %d to InputSet...",
958 FD_SET(PollPipe
, &InputSet
);
963 * 'StopBrowsing()' - Stop sending and receiving broadcast information.
972 if (BrowseProtocols
& BROWSE_CUPS
)
975 * Close the socket and remove it from the input selection set.
978 if (BrowseSocket
>= 0)
981 closesocket(BrowseSocket
);
986 LogMessage(L_DEBUG2
, "StopBrowsing: Removing fd %d from InputSet...",
989 FD_CLR(BrowseSocket
, &InputSet
);
995 if (BrowseProtocols
& BROWSE_SLP
)
998 * Close SLP handle...
1001 SLPClose(BrowseSLPHandle
);
1003 #endif /* HAVE_LIBSLP */
1008 * 'StopPolling()' - Stop polling servers as needed.
1014 int i
; /* Looping var */
1015 dirsvc_poll_t
*poll
; /* Current polling server */
1022 LogMessage(L_DEBUG2
, "StopPolling: removing fd %d from InputSet.",
1024 FD_CLR(PollPipe
, &InputSet
);
1029 for (i
= 0, poll
= Polled
; i
< NumPolled
; i
++, poll
++)
1031 kill(poll
->pid
, SIGTERM
);
1036 * 'UpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
1040 UpdateCUPSBrowse(void)
1042 int i
; /* Looping var */
1043 int auth
; /* Authorization status */
1044 int len
; /* Length of name string */
1045 int bytes
; /* Number of bytes left */
1046 char packet
[1540], /* Broadcast packet */
1047 *pptr
; /* Pointer into packet */
1048 http_addr_t srcaddr
; /* Source address */
1049 char srcname
[1024]; /* Source hostname */
1050 unsigned address
[4], /* Source address */
1051 temp
; /* Temporary address var (host order) */
1052 cups_ptype_t type
; /* Printer type */
1053 ipp_pstate_t state
; /* Printer state */
1054 char uri
[HTTP_MAX_URI
], /* Printer URI */
1055 method
[HTTP_MAX_URI
], /* Method portion of URI */
1056 username
[HTTP_MAX_URI
], /* Username portion of URI */
1057 host
[HTTP_MAX_URI
], /* Host portion of URI */
1058 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
1059 info
[IPP_MAX_NAME
], /* Information string */
1060 location
[IPP_MAX_NAME
], /* Location string */
1061 make_model
[IPP_MAX_NAME
];/* Make and model string */
1062 int port
; /* Port portion of URI */
1063 cups_netif_t
*iface
; /* Network interface */
1067 * Read a packet from the browse socket...
1070 len
= sizeof(srcaddr
);
1071 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
), 0,
1072 (struct sockaddr
*)&srcaddr
, &len
)) <= 0)
1075 * "Connection refused" is returned under Linux if the destination port
1076 * or address is unreachable from a previous sendto(); check for the
1077 * error here and ignore it for now...
1080 if (errno
!= ECONNREFUSED
)
1082 LogMessage(L_ERROR
, "Browse recv failed - %s.", strerror(errno
));
1083 LogMessage(L_ERROR
, "Browsing turned off.");
1092 packet
[bytes
] = '\0';
1095 * Figure out where it came from...
1099 if (srcaddr
.addr
.sa_family
== AF_INET6
)
1101 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
1102 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
1103 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
1104 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
1107 #endif /* AF_INET6 */
1109 temp
= ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
1111 address
[3] = temp
& 255;
1113 address
[2] = temp
& 255;
1115 address
[1] = temp
& 255;
1117 address
[0] = temp
& 255;
1120 if (HostNameLookups
)
1121 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
1123 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
1125 len
= strlen(srcname
);
1131 if (BrowseACL
&& (BrowseACL
->num_allow
|| BrowseACL
->num_deny
))
1133 if (httpAddrLocalhost(&srcaddr
) || strcasecmp(srcname
, "localhost") == 0)
1136 * Access from localhost (127.0.0.1) is always allowed...
1144 * Do authorization checks on the domain/address...
1147 switch (BrowseACL
->order_type
)
1150 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1153 case AUTH_ALLOW
: /* Order Deny,Allow */
1156 if (CheckAuth(address
, srcname
, len
,
1157 BrowseACL
->num_deny
, BrowseACL
->deny
))
1160 if (CheckAuth(address
, srcname
, len
,
1161 BrowseACL
->num_allow
, BrowseACL
->allow
))
1165 case AUTH_DENY
: /* Order Allow,Deny */
1168 if (CheckAuth(address
, srcname
, len
,
1169 BrowseACL
->num_allow
, BrowseACL
->allow
))
1172 if (CheckAuth(address
, srcname
, len
,
1173 BrowseACL
->num_deny
, BrowseACL
->deny
))
1182 if (auth
== AUTH_DENY
)
1184 LogMessage(L_DEBUG
, "UpdateCUPSBrowse: Refused %d bytes from %s", bytes
,
1189 LogMessage(L_DEBUG2
, "UpdateCUPSBrowse: (%d bytes from %s) %s", bytes
, srcname
,
1196 if (sscanf(packet
, "%x%x%1023s", (unsigned *)&type
, (unsigned *)&state
,
1199 LogMessage(L_WARN
, "UpdateCUPSBrowse: Garbled browse packet - %s",
1204 strcpy(location
, "Location Unknown");
1205 strcpy(info
, "No Information Available");
1206 make_model
[0] = '\0';
1208 if ((pptr
= strchr(packet
, '\"')) != NULL
)
1211 * Have extended information; can't use sscanf for it because not all
1212 * sscanf's allow empty strings with %[^\"]...
1215 for (i
= 0, pptr
++;
1216 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
1218 location
[i
] = *pptr
;
1226 while (*pptr
&& isspace(*pptr
))
1231 for (i
= 0, pptr
++;
1232 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
1242 while (*pptr
&& isspace(*pptr
))
1247 for (i
= 0, pptr
++;
1248 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
1250 make_model
[i
] = *pptr
;
1253 make_model
[i
] = '\0';
1259 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
1260 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
1261 type
, state
, uri
, location
, info
, make_model
));
1264 * Pull the URI apart to see if this is a local or remote printer...
1267 httpSeparate(uri
, method
, username
, host
, &port
, resource
);
1269 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1272 * Check for packets from the local server...
1275 if (strcasecmp(host
, ServerName
) == 0)
1280 for (iface
= NetIFList
; iface
!= NULL
; iface
= iface
->next
)
1281 if (strcasecmp(host
, iface
->hostname
) == 0)
1288 for (i
= 0; i
< NumRelays
; i
++)
1289 if (CheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
1290 if (sendto(BrowseSocket
, packet
, bytes
, 0,
1291 (struct sockaddr
*)&(Relays
[i
].to
),
1292 sizeof(http_addr_t
)) <= 0)
1294 LogMessage(L_ERROR
, "UpdateCUPSBrowse: sendto failed for relay %d - %s.",
1295 i
+ 1, strerror(errno
));
1300 * Process the browse data...
1303 ProcessBrowseData(uri
, type
, state
, location
, info
, make_model
);
1308 * 'UpdatePolling()' - Read status messages from the poll daemons.
1314 int bytes
; /* Number of bytes read */
1315 char *lineptr
; /* Pointer to end of line in buffer */
1316 static int bufused
= 0; /* Number of bytes used in buffer */
1317 static char buffer
[1024]; /* Status buffer */
1320 if ((bytes
= read(PollPipe
, buffer
+ bufused
,
1321 sizeof(buffer
) - bufused
- 1)) > 0)
1324 buffer
[bufused
] = '\0';
1325 lineptr
= strchr(buffer
, '\n');
1327 else if (bytes
< 0 && errno
== EINTR
)
1331 lineptr
= buffer
+ bufused
;
1335 if (bytes
== 0 && bufused
== 0)
1338 while (lineptr
!= NULL
)
1341 * Terminate each line and process it...
1346 LogMessage(L_ERROR
, "%s", buffer
);
1349 * Copy over the buffer data we've used up...
1352 strcpy(buffer
, lineptr
);
1353 bufused
-= lineptr
- buffer
;
1358 lineptr
= strchr(buffer
, '\n');
1364 * All polling processes have died; stop polling...
1367 LogMessage(L_ERROR
, "UpdatePolling: all polling processes have exited!");
1373 /***********************************************************************
1374 **** SLP Support Code *************************************************
1375 ***********************************************************************/
1379 * SLP service name for CUPS...
1382 # define SLP_CUPS_SRVTYPE "service:printer"
1383 # define SLP_CUPS_SRVLEN 15
1385 typedef struct _slpsrvurl
1387 struct _slpsrvurl
*next
;
1388 char url
[HTTP_MAX_URI
];
1393 * 'RegReportCallback()' - Empty SLPRegReport.
1397 RegReportCallback(SLPHandle hslp
,
1410 * 'SendSLPBrowse()' - Register the specified printer with SLP.
1414 SendSLPBrowse(printer_t
*p
) /* I - Printer to register */
1416 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
1417 attrs
[8192], /* Printer attributes */
1418 finishings
[1024], /* Finishings to support */
1419 make_model
[IPP_MAX_NAME
* 2],
1420 /* Make and model, quoted */
1421 location
[IPP_MAX_NAME
* 2],
1422 /* Location, quoted */
1423 info
[IPP_MAX_NAME
* 2],
1425 *src
, /* Pointer to original string */
1426 *dst
; /* Pointer to destination string */
1427 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
1428 SLPError error
; /* SLP error, if any */
1431 LogMessage(L_DEBUG
, "SendSLPBrowse(%p = \"%s\")", p
, p
->name
);
1434 * Make the SLP service URL that conforms to the IANA
1435 * 'printer:' template.
1438 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
1440 LogMessage(L_DEBUG2
, "Service URL = \"%s\"", srvurl
);
1443 * Figure out the finishings string...
1446 if (p
->type
& CUPS_PRINTER_STAPLE
)
1447 strcpy(finishings
, "staple");
1449 finishings
[0] = '\0';
1451 if (p
->type
& CUPS_PRINTER_BIND
)
1454 strlcat(finishings
, ",bind", sizeof(finishings
));
1456 strcpy(finishings
, "bind");
1459 if (p
->type
& CUPS_PRINTER_PUNCH
)
1462 strlcat(finishings
, ",punch", sizeof(finishings
));
1464 strcpy(finishings
, "punch");
1467 if (p
->type
& CUPS_PRINTER_COVER
)
1470 strlcat(finishings
, ",cover", sizeof(finishings
));
1472 strcpy(finishings
, "cover");
1475 if (p
->type
& CUPS_PRINTER_SORT
)
1478 strlcat(finishings
, ",sort", sizeof(finishings
));
1480 strcpy(finishings
, "sort");
1484 strcpy(finishings
, "none");
1487 * Quote any commas in the make and model, location, and info strings
1488 * (local strings are twice the size of the ones in the printer_t
1489 * structure, so no buffer overflow is possible...)
1492 for (src
= p
->make_model
, dst
= make_model
; *src
;)
1494 if (*src
== ',' || *src
== '\\' || *src
== ')')
1503 strcpy(make_model
, "Unknown");
1505 for (src
= p
->location
, dst
= location
; *src
;)
1507 if (*src
== ',' || *src
== '\\' || *src
== ')')
1516 strcpy(location
, "Unknown");
1518 for (src
= p
->info
, dst
= info
; *src
;)
1520 if (*src
== ',' || *src
== '\\' || *src
== ')')
1529 strcpy(info
, "Unknown");
1532 * Get the authentication value...
1535 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
1539 * Make the SLP attribute string list that conforms to
1540 * the IANA 'printer:' template.
1543 snprintf(attrs
, sizeof(attrs
),
1544 "(printer-uri-supported=%s),"
1545 "(uri-authentication-supported=%s>),"
1547 "(uri-security-supported=tls>),"
1549 "(uri-security-supported=none>),"
1550 #endif /* HAVE_LIBSSL */
1551 "(printer-name=%s),"
1552 "(printer-location=%s),"
1553 "(printer-info=%s),"
1554 "(printer-more-info=%s),"
1555 "(printer-make-and-model=%s),"
1556 "(charset-supported=utf-8),"
1557 "(natural-language-configured=%s),"
1558 "(natural-language-supported=de,en,es,fr,it),"
1559 "(color-supported=%s),"
1560 "(finishings-supported=%s),"
1561 "(sides-supported=one-sided%s),"
1562 "(multiple-document-jobs-supported=true)"
1563 "(ipp-versions-supported=1.0,1.1)",
1564 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
1565 info
, p
->uri
, make_model
, DefaultLanguage
,
1566 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
1568 p
->type
& CUPS_PRINTER_DUPLEX
?
1569 ",two-sided-long-edge,two-sided-short-edge" : "");
1571 LogMessage(L_DEBUG2
, "Attributes = \"%s\"", attrs
);
1574 * Register the printer with the SLP server...
1577 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
1578 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, RegReportCallback
, 0);
1580 if (error
!= SLP_OK
)
1581 LogMessage(L_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
1587 * 'SLPDeregPrinter()' - SLPDereg() the specified printer
1591 SLPDeregPrinter(printer_t
*p
)
1593 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
1596 if((p
->type
& CUPS_PRINTER_REMOTE
) == 0)
1599 * Make the SLP service URL that conforms to the IANA
1600 * 'printer:' template.
1603 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
1606 * Deregister the printer...
1609 SLPDereg(BrowseSLPHandle
, srvurl
, RegReportCallback
, 0);
1615 * 'GetSlpAttrVal()' - Get an attribute from an SLP registration.
1618 int /* O - 0 on success */
1619 GetSlpAttrVal(const char *attrlist
, /* I - Attribute list string */
1620 const char *tag
, /* I - Name of attribute */
1621 char *valbuf
, /* O - Value */
1622 int valbuflen
) /* I - Max length of value */
1624 char *ptr1
, /* Pointer into string */
1630 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
1632 ptr1
+= strlen(tag
);
1634 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
1636 if (valbuflen
> (ptr2
- ptr1
))
1642 strncpy(valbuf
, ptr1
, ptr2
- ptr1
);
1643 valbuf
[ptr2
- ptr1
] = '\0';
1646 * Dequote the value...
1649 for (ptr1
= valbuf
; *ptr1
; ptr1
++)
1650 if (*ptr1
== '\\' && ptr1
[1])
1651 strcpy(ptr1
, ptr1
+ 1);
1663 * 'AttrCallback()' - SLP attribute callback
1667 AttrCallback(SLPHandle hslp
,
1668 const char *attrlist
,
1672 char tmp
[IPP_MAX_NAME
];
1673 printer_t
*p
= (printer_t
*)cookie
;
1677 * Let the compiler know we won't be using these...
1683 * Bail if there was an error
1686 if (errcode
!= SLP_OK
)
1690 * Parse the attrlist to obtain things needed to build CUPS browse packet
1693 memset(p
, 0, sizeof(printer_t
));
1695 p
->type
= CUPS_PRINTER_REMOTE
;
1697 if (GetSlpAttrVal(attrlist
, "(printer-location=", p
->location
,
1698 sizeof(p
->location
)))
1700 if (GetSlpAttrVal(attrlist
, "(printer-make-and-model=", p
->make_model
,
1701 sizeof(p
->make_model
)))
1704 if (GetSlpAttrVal(attrlist
, "(color-supported=", tmp
, sizeof(tmp
)))
1706 if (strcasecmp(tmp
, "true") == 0)
1707 p
->type
|= CUPS_PRINTER_COLOR
;
1709 if (GetSlpAttrVal(attrlist
, "(finishings-supported=", tmp
, sizeof(tmp
)))
1711 if (strstr(tmp
, "staple"))
1712 p
->type
|= CUPS_PRINTER_STAPLE
;
1713 if (strstr(tmp
, "bind"))
1714 p
->type
|= CUPS_PRINTER_BIND
;
1715 if (strstr(tmp
, "punch"))
1716 p
->type
|= CUPS_PRINTER_PUNCH
;
1718 if (GetSlpAttrVal(attrlist
, "(sides-supported=", tmp
, sizeof(tmp
)))
1720 if (strstr(tmp
,"two-sided"))
1721 p
->type
|= CUPS_PRINTER_DUPLEX
;
1728 * 'SrvUrlCallback()' - SLP service url callback
1731 SLPBoolean
/* O - TRUE = OK, FALSE = error */
1732 SrvUrlCallback(SLPHandle hslp
, /* I - SLP handle */
1733 const char *srvurl
, /* I - URL of service */
1734 unsigned short lifetime
, /* I - Life of service */
1735 SLPError errcode
, /* I - Existing error code */
1736 void *cookie
) /* I - Pointer to service list */
1738 slpsrvurl_t
*s
, /* New service entry */
1739 **head
; /* Pointer to head of entry */
1743 * Let the compiler know we won't be using these vars...
1750 * Bail if there was an error
1753 if (errcode
!= SLP_OK
)
1757 * Grab the head of the list...
1760 head
= (slpsrvurl_t
**)cookie
;
1763 * Allocate a *temporary* slpsrvurl_t to hold this entry.
1766 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
1770 * Copy the SLP service URL...
1773 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
1776 * Link the SLP service URL into the head of the list
1789 * 'UpdateSLPBrowse()' - Get browsing information via SLP.
1793 UpdateSLPBrowse(void)
1795 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1796 *next
; /* Next service in list */
1797 printer_t p
; /* Printer information */
1798 const char *uri
; /* Pointer to printer URI */
1799 char method
[HTTP_MAX_URI
], /* Method portion of URI */
1800 username
[HTTP_MAX_URI
], /* Username portion of URI */
1801 host
[HTTP_MAX_URI
], /* Host portion of URI */
1802 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1803 int port
; /* Port portion of URI */
1806 LogMessage(L_DEBUG
, "UpdateSLPBrowse() Start...");
1809 * Reset the refresh time...
1812 BrowseSLPRefresh
= time(NULL
) + BrowseTimeout
- BrowseInterval
;
1815 * Poll for remote printers using SLP...
1820 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1821 SrvUrlCallback
, &s
);
1824 * Loop through the list of available printers...
1830 * Save the "next" pointer...
1836 * Load a printer_t structure with the SLP service attributes...
1839 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", AttrCallback
, &p
);
1842 * Process this printer entry...
1845 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1847 if (strncmp(uri
, "http://", 7) == 0 ||
1848 strncmp(uri
, "ipp://", 6) == 0)
1851 * Pull the URI apart to see if this is a local or remote printer...
1854 httpSeparate(uri
, method
, username
, host
, &port
, resource
);
1856 if (strcasecmp(host
, ServerName
) == 0)
1860 * OK, at least an IPP printer, see if it is a CUPS printer or
1864 if (strstr(uri
, "/printers/") != NULL
)
1865 ProcessBrowseData(uri
, p
.type
, IPP_PRINTER_IDLE
, p
.location
,
1866 p
.info
, p
.make_model
);
1867 else if (strstr(uri
, "/classes/") != NULL
)
1868 ProcessBrowseData(uri
, p
.type
| CUPS_PRINTER_CLASS
, IPP_PRINTER_IDLE
,
1869 p
.location
, p
.info
, p
.make_model
);
1873 * Free this listing...
1879 LogMessage(L_DEBUG
, "UpdateSLPBrowse() End...");
1881 #endif /* HAVE_LIBSLP */
1885 * End of "$Id: dirsvc.c,v 1.73.2.20 2003/01/07 18:27:19 mike Exp $".