]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/dirsvc.c
2 * "$Id: dirsvc.c,v 1.102 2002/12/17 19:00:15 swdev 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 iface
->broadcast
.sin_port
= htons(BrowsePort
);
632 sendto(BrowseSocket
, packet
, bytes
, 0,
633 (struct sockaddr
*)&(iface
->broadcast
),
634 sizeof(struct sockaddr_in
));
637 else if ((iface
= NetIFFind(b
->iface
)) != NULL
)
640 * Send to the named interface...
643 snprintf(packet
, sizeof(packet
), "%x %x ipp://%s/%s/%s \"%s\" \"%s\" \"%s\"\n",
644 p
->type
| CUPS_PRINTER_REMOTE
, p
->state
, iface
->hostname
,
645 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
646 p
->name
, p
->location
, p
->info
, p
->make_model
);
648 bytes
= strlen(packet
);
650 LogMessage(L_DEBUG2
, "SendBrowseList: (%d bytes to \"%s\") %s", bytes
,
651 iface
->name
, packet
);
653 iface
->broadcast
.sin_port
= htons(BrowsePort
);
655 sendto(BrowseSocket
, packet
, bytes
, 0,
656 (struct sockaddr
*)&(iface
->broadcast
),
657 sizeof(struct sockaddr_in
));
663 * Send the browse packet to the indicated address using
664 * the default server name...
667 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\"\n",
668 p
->type
| CUPS_PRINTER_REMOTE
, p
->state
, p
->uri
,
669 p
->location
, p
->info
, p
->make_model
);
671 bytes
= strlen(packet
);
672 LogMessage(L_DEBUG2
, "SendBrowseList: (%d bytes to %x) %s", bytes
,
673 ntohl(b
->to
.sin_addr
.s_addr
), packet
);
675 if (sendto(BrowseSocket
, packet
, bytes
, 0,
676 (struct sockaddr
*)&(b
->to
), sizeof(struct sockaddr_in
)) <= 0)
679 * Unable to send browse packet, so remove this address from the
683 LogMessage(L_ERROR
, "SendBrowseList: sendto failed for browser %d - %s.",
684 b
- Browsers
+ 1, strerror(errno
));
687 memcpy(b
, b
+ 1, (i
- 1) * sizeof(dirsvc_addr_t
));
697 * 'StartBrowsing()' - Start sending and receiving broadcast information.
703 int val
; /* Socket option value */
704 struct sockaddr_in addr
; /* Broadcast address */
710 if (BrowseProtocols
& BROWSE_CUPS
)
713 * Create the broadcast socket...
716 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
718 LogMessage(L_ERROR
, "StartBrowsing: Unable to create broadcast socket - %s.",
725 * Set the "broadcast" flag...
729 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
731 LogMessage(L_ERROR
, "StartBrowsing: Unable to set broadcast mode - %s.",
735 closesocket(BrowseSocket
);
746 * Bind the socket to browse port...
749 memset(&addr
, 0, sizeof(addr
));
750 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
751 addr
.sin_family
= AF_INET
;
752 addr
.sin_port
= htons(BrowsePort
);
754 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
756 LogMessage(L_ERROR
, "StartBrowsing: Unable to bind broadcast socket - %s.",
760 closesocket(BrowseSocket
);
771 * Finally, add the socket to the input selection set...
774 LogMessage(L_DEBUG2
, "StartBrowsing: Adding fd %d to InputSet...",
777 FD_SET(BrowseSocket
, &InputSet
);
781 if (BrowseProtocols
& BROWSE_SLP
)
787 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
789 LogMessage(L_ERROR
, "Unable to open an SLP handle; disabling SLP browsing!");
790 BrowseProtocols
&= ~BROWSE_SLP
;
793 BrowseSLPRefresh
= 0;
795 #endif /* HAVE_LIBSLP */
800 * 'StartPolling()' - Start polling servers as needed.
806 int i
; /* Looping var */
807 dirsvc_poll_t
*poll
; /* Current polling server */
808 int pid
; /* New process ID */
809 char sport
[10]; /* Server port */
810 char bport
[10]; /* Browser port */
811 char interval
[10]; /* Poll interval */
812 int statusfds
[2]; /* Status pipe */
813 int fd
; /* Current file descriptor */
817 * Don't do anything if we aren't polling...
827 * Setup string arguments for port and interval options.
830 sprintf(bport
, "%d", BrowsePort
);
833 sprintf(interval
, "%d", BrowseInterval
);
835 strcpy(interval
, "30");
838 * Create a pipe that receives the status messages from each
844 LogMessage(L_ERROR
, "Unable to create polling status pipes - %s.",
850 PollPipe
= statusfds
[0];
853 * Run each polling daemon, redirecting stderr to the polling pipe...
856 for (i
= 0, poll
= Polled
; i
< NumPolled
; i
++, poll
++)
858 sprintf(sport
, "%d", poll
->port
);
860 if ((pid
= fork()) == 0)
869 * Running as root, so change to non-priviledged user...
880 * Reset group membership to just the main one we belong to.
886 * Redirect stdin and stdout to /dev/null, and stderr to the
887 * status pipe. Close all other files.
891 open("/dev/null", O_RDONLY
);
894 open("/dev/null", O_WRONLY
);
899 for (fd
= 3; fd
< MaxFDs
; fd
++)
903 * Execute the polling daemon...
906 execl(CUPS_SERVERBIN
"/daemon/cups-polld", "cups-polld", poll
->hostname
,
907 sport
, interval
, bport
, NULL
);
912 LogMessage(L_ERROR
, "StartPolling: Unable to fork polling daemon - %s",
920 LogMessage(L_DEBUG
, "StartPolling: Started polling daemon for %s:%d, pid = %d",
921 poll
->hostname
, poll
->port
, pid
);
928 * Finally, add the pipe to the input selection set...
931 LogMessage(L_DEBUG2
, "StartPolling: Adding fd %d to InputSet...",
934 FD_SET(PollPipe
, &InputSet
);
939 * 'StopBrowsing()' - Stop sending and receiving broadcast information.
948 if (BrowseProtocols
& BROWSE_CUPS
)
951 * Close the socket and remove it from the input selection set.
954 if (BrowseSocket
>= 0)
957 closesocket(BrowseSocket
);
962 LogMessage(L_DEBUG2
, "StopBrowsing: Removing fd %d from InputSet...",
965 FD_CLR(BrowseSocket
, &InputSet
);
971 if (BrowseProtocols
& BROWSE_SLP
)
974 * Close SLP handle...
977 SLPClose(BrowseSLPHandle
);
979 #endif /* HAVE_LIBSLP */
984 * 'StopPolling()' - Stop polling servers as needed.
990 int i
; /* Looping var */
991 dirsvc_poll_t
*poll
; /* Current polling server */
998 LogMessage(L_DEBUG2
, "StopPolling: removing fd %d from InputSet.",
1000 FD_CLR(PollPipe
, &InputSet
);
1005 for (i
= 0, poll
= Polled
; i
< NumPolled
; i
++, poll
++)
1007 kill(poll
->pid
, SIGTERM
);
1012 * 'UpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
1016 UpdateCUPSBrowse(void)
1018 int i
; /* Looping var */
1019 int auth
; /* Authorization status */
1020 int len
; /* Length of name string */
1021 int bytes
; /* Number of bytes left */
1022 char packet
[1540], /* Broadcast packet */
1023 *pptr
; /* Pointer into packet */
1024 struct sockaddr_in srcaddr
; /* Source address */
1025 char srcname
[1024]; /* Source hostname */
1026 unsigned address
; /* Source address (host order) */
1027 struct hostent
*srchost
; /* Host entry for source address */
1028 cups_ptype_t type
; /* Printer type */
1029 ipp_pstate_t state
; /* Printer state */
1030 char uri
[HTTP_MAX_URI
], /* Printer URI */
1031 method
[HTTP_MAX_URI
], /* Method portion of URI */
1032 username
[HTTP_MAX_URI
], /* Username portion of URI */
1033 host
[HTTP_MAX_URI
], /* Host portion of URI */
1034 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
1035 info
[IPP_MAX_NAME
], /* Information string */
1036 location
[IPP_MAX_NAME
], /* Location string */
1037 make_model
[IPP_MAX_NAME
];/* Make and model string */
1038 int port
; /* Port portion of URI */
1039 cups_netif_t
*iface
; /* Network interface */
1043 * Read a packet from the browse socket...
1046 len
= sizeof(srcaddr
);
1047 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
), 0,
1048 (struct sockaddr
*)&srcaddr
, &len
)) <= 0)
1051 * "Connection refused" is returned under Linux if the destination port
1052 * or address is unreachable from a previous sendto(); check for the
1053 * error here and ignore it for now...
1056 if (errno
!= ECONNREFUSED
)
1058 LogMessage(L_ERROR
, "Browse recv failed - %s.", strerror(errno
));
1059 LogMessage(L_ERROR
, "Browsing turned off.");
1068 packet
[bytes
] = '\0';
1071 * Figure out where it came from...
1074 address
= ntohl(srcaddr
.sin_addr
.s_addr
);
1076 if (HostNameLookups
)
1078 srchost
= gethostbyaddr((char *)&(srcaddr
.sin_addr
), sizeof(struct in_addr
),
1081 srchost
= gethostbyaddr(&(srcaddr
.sin_addr
), sizeof(struct in_addr
),
1087 if (srchost
== NULL
)
1088 sprintf(srcname
, "%d.%d.%d.%d", address
>> 24, (address
>> 16) & 255,
1089 (address
>> 8) & 255, address
& 255);
1091 strlcpy(srcname
, srchost
->h_name
, sizeof(srcname
));
1093 len
= strlen(srcname
);
1099 if (BrowseACL
&& (BrowseACL
->num_allow
|| BrowseACL
->num_deny
))
1101 if (address
== 0x7f000001 || strcasecmp(srcname
, "localhost") == 0)
1104 * Access from localhost (127.0.0.1) is always allowed...
1112 * Do authorization checks on the domain/address...
1115 switch (BrowseACL
->order_type
)
1118 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1121 case AUTH_ALLOW
: /* Order Deny,Allow */
1124 if (CheckAuth(address
, srcname
, len
,
1125 BrowseACL
->num_deny
, BrowseACL
->deny
))
1128 if (CheckAuth(address
, srcname
, len
,
1129 BrowseACL
->num_allow
, BrowseACL
->allow
))
1133 case AUTH_DENY
: /* Order Allow,Deny */
1136 if (CheckAuth(address
, srcname
, len
,
1137 BrowseACL
->num_allow
, BrowseACL
->allow
))
1140 if (CheckAuth(address
, srcname
, len
,
1141 BrowseACL
->num_deny
, BrowseACL
->deny
))
1150 if (auth
== AUTH_DENY
)
1152 LogMessage(L_DEBUG
, "UpdateCUPSBrowse: Refused %d bytes from %s", bytes
,
1157 LogMessage(L_DEBUG2
, "UpdateCUPSBrowse: (%d bytes from %s) %s", bytes
, srcname
,
1164 if (sscanf(packet
, "%x%x%1023s", (unsigned *)&type
, (unsigned *)&state
,
1167 LogMessage(L_WARN
, "UpdateCUPSBrowse: Garbled browse packet - %s",
1172 strcpy(location
, "Location Unknown");
1173 strcpy(info
, "No Information Available");
1174 make_model
[0] = '\0';
1176 if ((pptr
= strchr(packet
, '\"')) != NULL
)
1179 * Have extended information; can't use sscanf for it because not all
1180 * sscanf's allow empty strings with %[^\"]...
1183 for (i
= 0, pptr
++;
1184 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
1186 location
[i
] = *pptr
;
1194 while (*pptr
&& isspace(*pptr
))
1199 for (i
= 0, pptr
++;
1200 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
1210 while (*pptr
&& isspace(*pptr
))
1215 for (i
= 0, pptr
++;
1216 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
1218 make_model
[i
] = *pptr
;
1221 make_model
[i
] = '\0';
1227 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
1228 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
1229 type
, state
, uri
, location
, info
, make_model
));
1232 * Pull the URI apart to see if this is a local or remote printer...
1235 httpSeparate(uri
, method
, username
, host
, &port
, resource
);
1237 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1240 * Check for packets from the local server...
1243 if (strcasecmp(host
, ServerName
) == 0)
1248 for (iface
= NetIFList
; iface
!= NULL
; iface
= iface
->next
)
1249 if (strcasecmp(host
, iface
->hostname
) == 0)
1256 for (i
= 0; i
< NumRelays
; i
++)
1257 if (CheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
1258 if (sendto(BrowseSocket
, packet
, bytes
, 0,
1259 (struct sockaddr
*)&(Relays
[i
].to
),
1260 sizeof(struct sockaddr_in
)) <= 0)
1262 LogMessage(L_ERROR
, "UpdateCUPSBrowse: sendto failed for relay %d - %s.",
1263 i
+ 1, strerror(errno
));
1268 * Process the browse data...
1271 ProcessBrowseData(uri
, type
, state
, location
, info
, make_model
);
1276 * 'UpdatePolling()' - Read status messages from the poll daemons.
1282 int bytes
; /* Number of bytes read */
1283 char *lineptr
; /* Pointer to end of line in buffer */
1284 static int bufused
= 0; /* Number of bytes used in buffer */
1285 static char buffer
[1024]; /* Status buffer */
1288 if ((bytes
= read(PollPipe
, buffer
+ bufused
,
1289 sizeof(buffer
) - bufused
- 1)) > 0)
1292 buffer
[bufused
] = '\0';
1293 lineptr
= strchr(buffer
, '\n');
1295 else if (bytes
< 0 && errno
== EINTR
)
1299 lineptr
= buffer
+ bufused
;
1303 if (bytes
== 0 && bufused
== 0)
1306 while (lineptr
!= NULL
)
1309 * Terminate each line and process it...
1314 LogMessage(L_ERROR
, "%s", buffer
);
1317 * Copy over the buffer data we've used up...
1320 strcpy(buffer
, lineptr
);
1321 bufused
-= lineptr
- buffer
;
1326 lineptr
= strchr(buffer
, '\n');
1332 * All polling processes have died; stop polling...
1335 LogMessage(L_ERROR
, "UpdatePolling: all polling processes have exited!");
1341 /***********************************************************************
1342 **** SLP Support Code *************************************************
1343 ***********************************************************************/
1347 * SLP service name for CUPS...
1350 # define SLP_CUPS_SRVTYPE "service:printer"
1351 # define SLP_CUPS_SRVLEN 15
1355 * Printer service URL structure
1358 typedef struct _slpsrvurl
1360 struct _slpsrvurl
*next
;
1361 char url
[HTTP_MAX_URI
];
1366 * 'RegReportCallback()' - Empty SLPRegReport.
1370 RegReportCallback(SLPHandle hslp
,
1383 * 'SendSLPBrowse()' - Register the specified printer with SLP.
1387 SendSLPBrowse(printer_t
*p
) /* I - Printer to register */
1389 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
1390 attrs
[8192], /* Printer attributes */
1391 finishings
[1024], /* Finishings to support */
1392 make_model
[IPP_MAX_NAME
* 2],
1393 /* Make and model, quoted */
1394 location
[IPP_MAX_NAME
* 2],
1395 /* Location, quoted */
1396 info
[IPP_MAX_NAME
* 2],
1398 *src
, /* Pointer to original string */
1399 *dst
; /* Pointer to destination string */
1400 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
1401 SLPError error
; /* SLP error, if any */
1404 LogMessage(L_DEBUG
, "SendSLPBrowse(%p = \"%s\")", p
, p
->name
);
1407 * Make the SLP service URL that conforms to the IANA
1408 * 'printer:' template.
1411 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
1413 LogMessage(L_DEBUG2
, "Service URL = \"%s\"", srvurl
);
1416 * Figure out the finishings string...
1419 if (p
->type
& CUPS_PRINTER_STAPLE
)
1420 strcpy(finishings
, "staple");
1422 finishings
[0] = '\0';
1424 if (p
->type
& CUPS_PRINTER_BIND
)
1427 strlcat(finishings
, ",bind", sizeof(finishings
));
1429 strcpy(finishings
, "bind");
1432 if (p
->type
& CUPS_PRINTER_PUNCH
)
1435 strlcat(finishings
, ",punch", sizeof(finishings
));
1437 strcpy(finishings
, "punch");
1440 if (p
->type
& CUPS_PRINTER_COVER
)
1443 strlcat(finishings
, ",cover", sizeof(finishings
));
1445 strcpy(finishings
, "cover");
1448 if (p
->type
& CUPS_PRINTER_SORT
)
1451 strlcat(finishings
, ",sort", sizeof(finishings
));
1453 strcpy(finishings
, "sort");
1457 strcpy(finishings
, "none");
1460 * Quote any commas in the make and model, location, and info strings
1461 * (local strings are twice the size of the ones in the printer_t
1462 * structure, so no buffer overflow is possible...)
1465 for (src
= p
->make_model
, dst
= make_model
; *src
;)
1467 if (*src
== ',' || *src
== '\\' || *src
== ')')
1476 strcpy(make_model
, "Unknown");
1478 for (src
= p
->location
, dst
= location
; *src
;)
1480 if (*src
== ',' || *src
== '\\' || *src
== ')')
1489 strcpy(location
, "Unknown");
1491 for (src
= p
->info
, dst
= info
; *src
;)
1493 if (*src
== ',' || *src
== '\\' || *src
== ')')
1502 strcpy(info
, "Unknown");
1505 * Get the authentication value...
1508 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
1512 * Make the SLP attribute string list that conforms to
1513 * the IANA 'printer:' template.
1516 snprintf(attrs
, sizeof(attrs
),
1517 "(printer-uri-supported=%s),"
1518 "(uri-authentication-supported=%s>),"
1520 "(uri-security-supported=tls>),"
1522 "(uri-security-supported=none>),"
1523 #endif /* HAVE_LIBSSL */
1524 "(printer-name=%s),"
1525 "(printer-location=%s),"
1526 "(printer-info=%s),"
1527 "(printer-more-info=%s),"
1528 "(printer-make-and-model=%s),"
1529 "(charset-supported=utf-8),"
1530 "(natural-language-configured=%s),"
1531 "(natural-language-supported=de,en,es,fr,it),"
1532 "(color-supported=%s),"
1533 "(finishings-supported=%s),"
1534 "(sides-supported=one-sided%s),"
1535 "(multiple-document-jobs-supported=true)"
1536 "(ipp-versions-supported=1.0,1.1)",
1537 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
1538 info
, p
->uri
, make_model
, DefaultLanguage
,
1539 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
1541 p
->type
& CUPS_PRINTER_DUPLEX
?
1542 ",two-sided-long-edge,two-sided-short-edge" : "");
1544 LogMessage(L_DEBUG2
, "Attributes = \"%s\"", attrs
);
1547 * Register the printer with the SLP server...
1550 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
1551 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, RegReportCallback
, 0);
1553 if (error
!= SLP_OK
)
1554 LogMessage(L_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
1560 * 'SLPDeregPrinter()' - SLPDereg() the specified printer
1564 SLPDeregPrinter(printer_t
*p
)
1566 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
1569 if((p
->type
& CUPS_PRINTER_REMOTE
) == 0)
1572 * Make the SLP service URL that conforms to the IANA
1573 * 'printer:' template.
1576 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
1579 * Deregister the printer...
1582 SLPDereg(BrowseSLPHandle
, srvurl
, RegReportCallback
, 0);
1588 * 'GetSlpAttrVal()' - Get an attribute from an SLP registration.
1591 int /* O - 0 on success */
1592 GetSlpAttrVal(const char *attrlist
, /* I - Attribute list string */
1593 const char *tag
, /* I - Name of attribute */
1594 char *valbuf
, /* O - Value */
1595 int valbuflen
) /* I - Max length of value */
1597 char *ptr1
, /* Pointer into string */
1603 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
1605 ptr1
+= strlen(tag
);
1607 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
1609 if (valbuflen
> (ptr2
- ptr1
))
1615 strncpy(valbuf
, ptr1
, ptr2
- ptr1
);
1616 valbuf
[ptr2
- ptr1
] = '\0';
1619 * Dequote the value...
1622 for (ptr1
= valbuf
; *ptr1
; ptr1
++)
1623 if (*ptr1
== '\\' && ptr1
[1])
1624 strcpy(ptr1
, ptr1
+ 1);
1636 * 'AttrCallback()' - SLP attribute callback
1640 AttrCallback(SLPHandle hslp
,
1641 const char *attrlist
,
1645 char tmp
[IPP_MAX_NAME
];
1646 printer_t
*p
= (printer_t
*)cookie
;
1650 * Let the compiler know we won't be using these...
1656 * Bail if there was an error
1659 if (errcode
!= SLP_OK
)
1663 * Parse the attrlist to obtain things needed to build CUPS browse packet
1666 memset(p
, 0, sizeof(printer_t
));
1668 p
->type
= CUPS_PRINTER_REMOTE
;
1670 if (GetSlpAttrVal(attrlist
, "(printer-location=", p
->location
,
1671 sizeof(p
->location
)))
1673 if (GetSlpAttrVal(attrlist
, "(printer-make-and-model=", p
->make_model
,
1674 sizeof(p
->make_model
)))
1677 if (GetSlpAttrVal(attrlist
, "(color-supported=", tmp
, sizeof(tmp
)))
1679 if (strcasecmp(tmp
, "true") == 0)
1680 p
->type
|= CUPS_PRINTER_COLOR
;
1682 if (GetSlpAttrVal(attrlist
, "(finishings-supported=", tmp
, sizeof(tmp
)))
1684 if (strstr(tmp
, "staple"))
1685 p
->type
|= CUPS_PRINTER_STAPLE
;
1686 if (strstr(tmp
, "bind"))
1687 p
->type
|= CUPS_PRINTER_BIND
;
1688 if (strstr(tmp
, "punch"))
1689 p
->type
|= CUPS_PRINTER_PUNCH
;
1691 if (GetSlpAttrVal(attrlist
, "(sides-supported=", tmp
, sizeof(tmp
)))
1693 if (strstr(tmp
,"two-sided"))
1694 p
->type
|= CUPS_PRINTER_DUPLEX
;
1701 * 'SrvUrlCallback()' - SLP service url callback
1704 SLPBoolean
/* O - TRUE = OK, FALSE = error */
1705 SrvUrlCallback(SLPHandle hslp
, /* I - SLP handle */
1706 const char *srvurl
, /* I - URL of service */
1707 unsigned short lifetime
, /* I - Life of service */
1708 SLPError errcode
, /* I - Existing error code */
1709 void *cookie
) /* I - Pointer to service list */
1711 slpsrvurl_t
*s
, /* New service entry */
1712 **head
; /* Pointer to head of entry */
1716 * Let the compiler know we won't be using these vars...
1723 * Bail if there was an error
1726 if (errcode
!= SLP_OK
)
1730 * Grab the head of the list...
1733 head
= (slpsrvurl_t
**)cookie
;
1736 * Allocate a *temporary* slpsrvurl_t to hold this entry.
1739 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
1743 * Copy the SLP service URL...
1746 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
1749 * Link the SLP service URL into the head of the list
1762 * 'UpdateSLPBrowse()' - Get browsing information via SLP.
1766 UpdateSLPBrowse(void)
1768 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1769 *next
; /* Next service in list */
1770 printer_t p
; /* Printer information */
1771 const char *uri
; /* Pointer to printer URI */
1772 char method
[HTTP_MAX_URI
], /* Method portion of URI */
1773 username
[HTTP_MAX_URI
], /* Username portion of URI */
1774 host
[HTTP_MAX_URI
], /* Host portion of URI */
1775 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1776 int port
; /* Port portion of URI */
1779 LogMessage(L_DEBUG
, "UpdateSLPBrowse() Start...");
1782 * Reset the refresh time...
1785 BrowseSLPRefresh
= time(NULL
) + BrowseTimeout
- BrowseInterval
;
1788 * Poll for remote printers using SLP...
1793 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1794 SrvUrlCallback
, &s
);
1797 * Loop through the list of available printers...
1803 * Save the "next" pointer...
1809 * Load a printer_t structure with the SLP service attributes...
1812 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", AttrCallback
, &p
);
1815 * Process this printer entry...
1818 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1820 if (strncmp(uri
, "http://", 7) == 0 ||
1821 strncmp(uri
, "ipp://", 6) == 0)
1824 * Pull the URI apart to see if this is a local or remote printer...
1827 httpSeparate(uri
, method
, username
, host
, &port
, resource
);
1829 if (strcasecmp(host
, ServerName
) == 0)
1833 * OK, at least an IPP printer, see if it is a CUPS printer or
1837 if (strstr(uri
, "/printers/") != NULL
)
1838 ProcessBrowseData(uri
, p
.type
, IPP_PRINTER_IDLE
, p
.location
,
1839 p
.info
, p
.make_model
);
1840 else if (strstr(uri
, "/classes/") != NULL
)
1841 ProcessBrowseData(uri
, p
.type
| CUPS_PRINTER_CLASS
, IPP_PRINTER_IDLE
,
1842 p
.location
, p
.info
, p
.make_model
);
1846 * Free this listing...
1852 LogMessage(L_DEBUG
, "UpdateSLPBrowse() End...");
1854 #endif /* HAVE_LIBSLP */
1858 * End of "$Id: dirsvc.c,v 1.102 2002/12/17 19:00:15 swdev Exp $".