]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/dirsvc.c
4 * Directory services routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2002 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 * OK, this isn't a local printer; see if we already have it listed in
92 * the Printers list, and add it if not...
96 hptr
= strchr(host
, '.');
97 sptr
= strchr(ServerName
, '.');
99 if (sptr
!= NULL
&& hptr
!= NULL
)
102 * Strip the common domain name components...
107 if (strcasecmp(hptr
, sptr
) == 0)
113 hptr
= strchr(hptr
+ 1, '.');
117 if (type
& CUPS_PRINTER_CLASS
)
120 * Remote destination is a class...
123 if (strncmp(resource
, "/classes/", 9) == 0)
124 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
128 if ((p
= FindClass(name
)) == NULL
&& BrowseShortNames
)
130 if ((p
= FindClass(resource
+ 9)) != NULL
)
132 if (strcasecmp(p
->hostname
, host
) != 0 && p
->hostname
[0])
135 * Nope, this isn't the same host; if the hostname isn't the local host,
136 * add it to the other class and then find a class using the full host
140 if (p
->type
& CUPS_PRINTER_REMOTE
)
142 strlcat(p
->name
, "@", sizeof(p
->name
));
143 strlcat(p
->name
, p
->hostname
, sizeof(p
->name
));
150 else if (!p
->hostname
[0])
152 strlcpy(p
->hostname
, host
, sizeof(p
->hostname
));
153 strlcpy(p
->uri
, uri
, sizeof(p
->uri
));
154 strlcpy(p
->device_uri
, uri
, sizeof(p
->device_uri
));
159 strlcpy(name
, resource
+ 9, sizeof(name
));
161 else if (p
!= NULL
&& !p
->hostname
[0])
163 strlcpy(p
->hostname
, host
, sizeof(p
->hostname
));
164 strlcpy(p
->uri
, uri
, sizeof(p
->uri
));
165 strlcpy(p
->device_uri
, uri
, sizeof(p
->device_uri
));
172 * Class doesn't exist; add it...
177 LogMessage(L_INFO
, "Added remote class \"%s\"...", name
);
180 * Force the URI to point to the real server...
184 strlcpy(p
->uri
, uri
, sizeof(p
->uri
));
185 strlcpy(p
->device_uri
, uri
, sizeof(p
->device_uri
));
186 strlcpy(p
->hostname
, host
, sizeof(p
->hostname
));
194 * Remote destination is a printer...
197 if (strncmp(resource
, "/printers/", 10) == 0)
198 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
202 if ((p
= FindPrinter(name
)) == NULL
&& BrowseShortNames
)
204 if ((p
= FindPrinter(resource
+ 10)) != NULL
)
206 if (strcasecmp(p
->hostname
, host
) != 0 && p
->hostname
[0])
209 * Nope, this isn't the same host; if the hostname isn't the local host,
210 * add it to the other printer and then find a printer using the full host
214 if (p
->type
& CUPS_PRINTER_REMOTE
)
216 strlcat(p
->name
, "@", sizeof(p
->name
));
217 strlcat(p
->name
, p
->hostname
, sizeof(p
->name
));
224 else if (!p
->hostname
[0])
226 strlcpy(p
->hostname
, host
, sizeof(p
->hostname
));
227 strlcpy(p
->uri
, uri
, sizeof(p
->uri
));
228 strlcpy(p
->device_uri
, uri
, sizeof(p
->device_uri
));
233 strlcpy(name
, resource
+ 10, sizeof(name
));
235 else if (p
!= NULL
&& !p
->hostname
[0])
237 strlcpy(p
->hostname
, host
, sizeof(p
->hostname
));
238 strlcpy(p
->uri
, uri
, sizeof(p
->uri
));
239 strlcpy(p
->device_uri
, uri
, sizeof(p
->device_uri
));
246 * Printer doesn't exist; add it...
249 p
= AddPrinter(name
);
251 LogMessage(L_INFO
, "Added remote printer \"%s\"...", name
);
254 * Force the URI to point to the real server...
258 strlcpy(p
->hostname
, host
, sizeof(p
->hostname
));
259 strlcpy(p
->uri
, uri
, sizeof(p
->uri
));
260 strlcpy(p
->device_uri
, uri
, sizeof(p
->device_uri
));
267 * Update the state...
271 p
->accepting
= state
!= IPP_PRINTER_STOPPED
;
272 p
->browse_time
= time(NULL
);
280 if (strcmp(p
->location
, location
))
282 strlcpy(p
->location
, location
, sizeof(p
->location
));
286 if (strcmp(p
->info
, info
))
288 strlcpy(p
->info
, info
, sizeof(p
->info
));
294 if (type
& CUPS_PRINTER_CLASS
)
295 snprintf(local_make_model
, sizeof(local_make_model
),
296 "Remote Class on %s", host
);
298 snprintf(local_make_model
, sizeof(local_make_model
),
299 "Remote Printer on %s", host
);
302 snprintf(local_make_model
, sizeof(local_make_model
),
303 "%s on %s", make_model
, host
);
305 if (strcmp(p
->make_model
, local_make_model
))
307 strlcpy(p
->make_model
, local_make_model
, sizeof(p
->make_model
));
315 * See if we have a default printer... If not, make the first printer the
319 if (DefaultPrinter
== NULL
&& Printers
!= NULL
)
321 DefaultPrinter
= Printers
;
327 * Do auto-classing if needed...
333 * Loop through all available printers and create classes as needed...
336 for (p
= Printers
, len
= 0, offset
= 0, first
= NULL
;
341 * Get next printer in list...
350 if (p
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_CLASS
))
357 * If len == 0, get the length of this printer name up to the "@"
362 strncasecmp(p
->name
, name
+ offset
, len
) == 0 &&
363 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
366 * We have more than one printer with the same name; see if
367 * we have a class, and if this printer is a member...
370 if ((pclass
= FindPrinter(name
)) == NULL
)
373 * Need to add the class...
376 pclass
= AddPrinter(name
);
377 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
378 pclass
->accepting
= 1;
379 pclass
->state
= IPP_PRINTER_IDLE
;
381 strcpy(pclass
->location
, p
->location
);
382 strcpy(pclass
->info
, p
->info
);
384 SetPrinterAttrs(pclass
);
386 LogMessage(L_INFO
, "Added implicit class \"%s\"...", name
);
391 for (i
= 0; i
< pclass
->num_printers
; i
++)
392 if (pclass
->printers
[i
] == first
)
395 if (i
>= pclass
->num_printers
)
396 AddPrinterToClass(pclass
, first
);
401 for (i
= 0; i
< pclass
->num_printers
; i
++)
402 if (pclass
->printers
[i
] == p
)
405 if (i
>= pclass
->num_printers
)
406 AddPrinterToClass(pclass
, p
);
411 * First time around; just get name length and mark it as first
415 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
416 len
= hptr
- p
->name
;
418 len
= strlen(p
->name
);
420 strncpy(name
, p
->name
, len
);
424 if ((pclass
= FindPrinter(name
)) != NULL
&&
425 !(pclass
->type
& CUPS_PRINTER_IMPLICIT
))
428 * Can't use same name as a local printer; add "Any" to the
429 * front of the name, unless we have explicitly disabled
430 * the "ImplicitAnyClasses"...
433 if (ImplicitAnyClasses
)
436 * Add "Any" to the class name...
440 strncpy(name
+ 3, p
->name
, len
);
441 name
[len
+ 3] = '\0';
447 * Don't create an implicit class if we have a local printer
448 * with the same name...
464 * 'SendBrowseList()' - Send new browsing information as necessary.
470 int count
; /* Number of dests to update */
471 printer_t
*p
, /* Current printer */
472 *np
; /* Next printer */
473 time_t ut
, /* Minimum update time */
474 to
; /* Timeout time */
477 if (!Browsing
|| !(BrowseProtocols
& BROWSE_CUPS
))
481 * Compute the update and timeout times...
484 ut
= time(NULL
) - BrowseInterval
;
485 to
= time(NULL
) - BrowseTimeout
;
488 * Figure out how many printers need an update...
491 if (BrowseInterval
> 0)
493 for (count
= 0, p
= Printers
; p
!= NULL
; p
= p
->next
)
494 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
499 * Throttle the number of printers we'll be updating this time
503 count
= 2 * count
/ BrowseInterval
+ 1;
509 * Loop through all of the printers and send local updates as needed...
512 for (p
= Printers
; p
!= NULL
; p
= np
)
516 if (p
->type
& CUPS_PRINTER_REMOTE
)
519 * See if this printer needs to be timed out...
522 if (p
->browse_time
< to
)
524 LogMessage(L_INFO
, "Remote destination \"%s\" has timed out; deleting it...",
529 else if (p
->browse_time
< ut
&& count
> 0 &&
530 !(p
->type
& CUPS_PRINTER_IMPLICIT
))
533 * Need to send an update...
538 p
->browse_time
= time(NULL
);
540 if (BrowseProtocols
& BROWSE_CUPS
)
544 if (BrowseProtocols
& BROWSE_SLP
)
546 #endif /* HAVE_LIBSLP */
553 * 'SendCUPSBrowse()' - Send new browsing information using the CUPS protocol.
557 SendCUPSBrowse(printer_t
*p
) /* I - Printer to send */
559 int i
; /* Looping var */
560 dirsvc_addr_t
*b
; /* Browse address */
561 int bytes
; /* Length of packet */
562 char packet
[1453]; /* Browse data packet */
563 cups_netif_t
*iface
; /* Network interface */
567 * Send a packet to each browse address...
570 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
574 * Send the browse packet to one or more interfaces...
577 if (strcmp(b
->iface
, "*") == 0)
580 * Send to all local interfaces...
585 for (iface
= NetIFList
; iface
!= NULL
; iface
= iface
->next
)
588 * Only send to local interfaces...
591 if (!iface
->is_local
)
594 snprintf(packet
, sizeof(packet
), "%x %x ipp://%s/%s/%s \"%s\" \"%s\" \"%s\"\n",
595 p
->type
| CUPS_PRINTER_REMOTE
, p
->state
, iface
->hostname
,
596 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
597 p
->name
, p
->location
, p
->info
, p
->make_model
);
599 bytes
= strlen(packet
);
601 LogMessage(L_DEBUG2
, "SendBrowseList: (%d bytes to \"%s\") %s", bytes
,
602 iface
->name
, packet
);
604 iface
->broadcast
.sin_port
= htons(BrowsePort
);
606 sendto(BrowseSocket
, packet
, bytes
, 0,
607 (struct sockaddr
*)&(iface
->broadcast
),
608 sizeof(struct sockaddr_in
));
611 else if ((iface
= NetIFFind(b
->iface
)) != NULL
)
614 * Send to the named interface...
617 snprintf(packet
, sizeof(packet
), "%x %x ipp://%s/%s/%s \"%s\" \"%s\" \"%s\"\n",
618 p
->type
| CUPS_PRINTER_REMOTE
, p
->state
, iface
->hostname
,
619 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
620 p
->name
, p
->location
, p
->info
, p
->make_model
);
622 bytes
= strlen(packet
);
624 LogMessage(L_DEBUG2
, "SendBrowseList: (%d bytes to \"%s\") %s", bytes
,
625 iface
->name
, packet
);
627 iface
->broadcast
.sin_port
= htons(BrowsePort
);
629 sendto(BrowseSocket
, packet
, bytes
, 0,
630 (struct sockaddr
*)&(iface
->broadcast
),
631 sizeof(struct sockaddr_in
));
637 * Send the browse packet to the indicated address using
638 * the default server name...
641 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\"\n",
642 p
->type
| CUPS_PRINTER_REMOTE
, p
->state
, p
->uri
,
643 p
->location
, p
->info
, p
->make_model
);
645 bytes
= strlen(packet
);
646 LogMessage(L_DEBUG2
, "SendBrowseList: (%d bytes to %x) %s", bytes
,
647 ntohl(b
->to
.sin_addr
.s_addr
), packet
);
649 if (sendto(BrowseSocket
, packet
, bytes
, 0,
650 (struct sockaddr
*)&(b
->to
), sizeof(struct sockaddr_in
)) <= 0)
653 * Unable to send browse packet, so remove this address from the
657 LogMessage(L_ERROR
, "SendBrowseList: sendto failed for browser %d - %s.",
658 b
- Browsers
+ 1, strerror(errno
));
661 memcpy(b
, b
+ 1, (i
- 1) * sizeof(dirsvc_addr_t
));
671 * 'StartBrowsing()' - Start sending and receiving broadcast information.
677 int val
; /* Socket option value */
678 struct sockaddr_in addr
; /* Broadcast address */
684 if (BrowseProtocols
& BROWSE_CUPS
)
687 * Create the broadcast socket...
690 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
692 LogMessage(L_ERROR
, "StartBrowsing: Unable to create broadcast socket - %s.",
699 * Set the "broadcast" flag...
703 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
705 LogMessage(L_ERROR
, "StartBrowsing: Unable to set broadcast mode - %s.",
709 closesocket(BrowseSocket
);
720 * Bind the socket to browse port...
723 memset(&addr
, 0, sizeof(addr
));
724 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
725 addr
.sin_family
= AF_INET
;
726 addr
.sin_port
= htons(BrowsePort
);
728 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
730 LogMessage(L_ERROR
, "StartBrowsing: Unable to bind broadcast socket - %s.",
734 closesocket(BrowseSocket
);
745 * Finally, add the socket to the input selection set...
748 LogMessage(L_DEBUG2
, "StartBrowsing: Adding fd %d to InputSet...",
751 FD_SET(BrowseSocket
, &InputSet
);
755 if (BrowseProtocols
& BROWSE_SLP
)
761 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
763 LogMessage(L_ERROR
, "Unable to open an SLP handle; disabling SLP browsing!");
764 BrowseProtocols
&= ~BROWSE_SLP
;
767 BrowseSLPRefresh
= 0;
769 #endif /* HAVE_LIBSLP */
774 * 'StartPolling()' - Start polling servers as needed.
780 int i
; /* Looping var */
781 dirsvc_poll_t
*poll
; /* Current polling server */
782 int pid
; /* New process ID */
783 char sport
[10]; /* Server port */
784 char bport
[10]; /* Browser port */
785 char interval
[10]; /* Poll interval */
786 int statusfds
[2]; /* Status pipe */
787 int fd
; /* Current file descriptor */
791 * Don't do anything if we aren't polling...
801 * Setup string arguments for port and interval options.
804 sprintf(bport
, "%d", BrowsePort
);
807 sprintf(interval
, "%d", BrowseInterval
);
809 strcpy(interval
, "30");
812 * Create a pipe that receives the status messages from each
818 LogMessage(L_ERROR
, "Unable to create polling status pipes - %s.",
824 PollPipe
= statusfds
[0];
827 * Run each polling daemon, redirecting stderr to the polling pipe...
830 for (i
= 0, poll
= Polled
; i
< NumPolled
; i
++, poll
++)
832 sprintf(sport
, "%d", poll
->port
);
834 if ((pid
= fork()) == 0)
843 * Running as root, so change to non-priviledged user...
854 * Reset group membership to just the main one we belong to.
860 * Redirect stdin and stdout to /dev/null, and stderr to the
861 * status pipe. Close all other files.
865 open("/dev/null", O_RDONLY
);
868 open("/dev/null", O_WRONLY
);
873 for (fd
= 3; fd
< MaxFDs
; fd
++)
877 * Execute the polling daemon...
880 execl(CUPS_SERVERBIN
"/daemon/cups-polld", "cups-polld", poll
->hostname
,
881 sport
, interval
, bport
, NULL
);
886 LogMessage(L_ERROR
, "StartPolling: Unable to fork polling daemon - %s",
894 LogMessage(L_DEBUG
, "StartPolling: Started polling daemon for %s:%d, pid = %d",
895 poll
->hostname
, poll
->port
, pid
);
902 * Finally, add the pipe to the input selection set...
905 LogMessage(L_DEBUG2
, "StartPolling: Adding fd %d to InputSet...",
908 FD_SET(PollPipe
, &InputSet
);
913 * 'StopBrowsing()' - Stop sending and receiving broadcast information.
922 if (BrowseProtocols
& BROWSE_CUPS
)
925 * Close the socket and remove it from the input selection set.
928 if (BrowseSocket
>= 0)
931 closesocket(BrowseSocket
);
936 LogMessage(L_DEBUG2
, "StopBrowsing: Removing fd %d from InputSet...",
939 FD_CLR(BrowseSocket
, &InputSet
);
945 if (BrowseProtocols
& BROWSE_SLP
)
948 * Close SLP handle...
951 SLPClose(BrowseSLPHandle
);
953 #endif /* HAVE_LIBSLP */
958 * 'StopPolling()' - Stop polling servers as needed.
964 int i
; /* Looping var */
965 dirsvc_poll_t
*poll
; /* Current polling server */
972 LogMessage(L_DEBUG2
, "StopPolling: removing fd %d from InputSet.",
974 FD_CLR(PollPipe
, &InputSet
);
979 for (i
= 0, poll
= Polled
; i
< NumPolled
; i
++, poll
++)
981 kill(poll
->pid
, SIGTERM
);
986 * 'UpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
990 UpdateCUPSBrowse(void)
992 int i
; /* Looping var */
993 int auth
; /* Authorization status */
994 int len
; /* Length of name string */
995 int bytes
; /* Number of bytes left */
996 char packet
[1540], /* Broadcast packet */
997 *pptr
; /* Pointer into packet */
998 struct sockaddr_in srcaddr
; /* Source address */
999 char srcname
[1024]; /* Source hostname */
1000 unsigned address
; /* Source address (host order) */
1001 struct hostent
*srchost
; /* Host entry for source address */
1002 cups_ptype_t type
; /* Printer type */
1003 ipp_pstate_t state
; /* Printer state */
1004 char uri
[HTTP_MAX_URI
], /* Printer URI */
1005 method
[HTTP_MAX_URI
], /* Method portion of URI */
1006 username
[HTTP_MAX_URI
], /* Username portion of URI */
1007 host
[HTTP_MAX_URI
], /* Host portion of URI */
1008 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
1009 info
[IPP_MAX_NAME
], /* Information string */
1010 location
[IPP_MAX_NAME
], /* Location string */
1011 make_model
[IPP_MAX_NAME
];/* Make and model string */
1012 int port
; /* Port portion of URI */
1013 cups_netif_t
*iface
; /* Network interface */
1017 * Read a packet from the browse socket...
1020 len
= sizeof(srcaddr
);
1021 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
), 0,
1022 (struct sockaddr
*)&srcaddr
, &len
)) <= 0)
1025 * "Connection refused" is returned under Linux if the destination port
1026 * or address is unreachable from a previous sendto(); check for the
1027 * error here and ignore it for now...
1030 if (errno
!= ECONNREFUSED
)
1032 LogMessage(L_ERROR
, "Browse recv failed - %s.", strerror(errno
));
1033 LogMessage(L_ERROR
, "Browsing turned off.");
1042 packet
[bytes
] = '\0';
1045 * Figure out where it came from...
1048 address
= ntohl(srcaddr
.sin_addr
.s_addr
);
1050 if (HostNameLookups
)
1052 srchost
= gethostbyaddr((char *)&(srcaddr
.sin_addr
), sizeof(struct in_addr
),
1055 srchost
= gethostbyaddr(&(srcaddr
.sin_addr
), sizeof(struct in_addr
),
1061 if (srchost
== NULL
)
1062 sprintf(srcname
, "%d.%d.%d.%d", address
>> 24, (address
>> 16) & 255,
1063 (address
>> 8) & 255, address
& 255);
1065 strlcpy(srcname
, srchost
->h_name
, sizeof(srcname
));
1067 len
= strlen(srcname
);
1073 if (BrowseACL
&& (BrowseACL
->num_allow
|| BrowseACL
->num_deny
))
1075 if (address
== 0x7f000001 || strcasecmp(srcname
, "localhost") == 0)
1078 * Access from localhost (127.0.0.1) is always allowed...
1086 * Do authorization checks on the domain/address...
1089 switch (BrowseACL
->order_type
)
1092 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1095 case AUTH_ALLOW
: /* Order Deny,Allow */
1098 if (CheckAuth(address
, srcname
, len
,
1099 BrowseACL
->num_deny
, BrowseACL
->deny
))
1102 if (CheckAuth(address
, srcname
, len
,
1103 BrowseACL
->num_allow
, BrowseACL
->allow
))
1107 case AUTH_DENY
: /* Order Allow,Deny */
1110 if (CheckAuth(address
, srcname
, len
,
1111 BrowseACL
->num_allow
, BrowseACL
->allow
))
1114 if (CheckAuth(address
, srcname
, len
,
1115 BrowseACL
->num_deny
, BrowseACL
->deny
))
1124 if (auth
== AUTH_DENY
)
1126 LogMessage(L_DEBUG
, "UpdateCUPSBrowse: Refused %d bytes from %s", bytes
,
1131 LogMessage(L_DEBUG2
, "UpdateCUPSBrowse: (%d bytes from %s) %s", bytes
, srcname
,
1138 if (sscanf(packet
, "%x%x%1023s", (unsigned *)&type
, (unsigned *)&state
,
1141 LogMessage(L_WARN
, "UpdateCUPSBrowse: Garbled browse packet - %s",
1146 strcpy(location
, "Location Unknown");
1147 strcpy(info
, "No Information Available");
1148 make_model
[0] = '\0';
1150 if ((pptr
= strchr(packet
, '\"')) != NULL
)
1153 * Have extended information; can't use sscanf for it because not all
1154 * sscanf's allow empty strings with %[^\"]...
1157 for (i
= 0, pptr
++;
1158 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
1160 location
[i
] = *pptr
;
1168 while (*pptr
&& isspace(*pptr
))
1173 for (i
= 0, pptr
++;
1174 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
1184 while (*pptr
&& isspace(*pptr
))
1189 for (i
= 0, pptr
++;
1190 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
1192 make_model
[i
] = *pptr
;
1195 make_model
[i
] = '\0';
1201 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
1202 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
1203 type
, state
, uri
, location
, info
, make_model
));
1206 * Pull the URI apart to see if this is a local or remote printer...
1209 httpSeparate(uri
, method
, username
, host
, &port
, resource
);
1211 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1214 * Check for packets from the local server...
1217 if (strcasecmp(host
, ServerName
) == 0)
1222 for (iface
= NetIFList
; iface
!= NULL
; iface
= iface
->next
)
1223 if (strcasecmp(host
, iface
->hostname
) == 0)
1230 for (i
= 0; i
< NumRelays
; i
++)
1231 if (CheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
1232 if (sendto(BrowseSocket
, packet
, bytes
, 0,
1233 (struct sockaddr
*)&(Relays
[i
].to
),
1234 sizeof(struct sockaddr_in
)) <= 0)
1236 LogMessage(L_ERROR
, "UpdateCUPSBrowse: sendto failed for relay %d - %s.",
1237 i
+ 1, strerror(errno
));
1242 * Process the browse data...
1245 ProcessBrowseData(uri
, type
, state
, location
, info
, make_model
);
1250 * 'UpdatePolling()' - Read status messages from the poll daemons.
1256 int bytes
; /* Number of bytes read */
1257 char *lineptr
; /* Pointer to end of line in buffer */
1258 static int bufused
= 0; /* Number of bytes used in buffer */
1259 static char buffer
[1024]; /* Status buffer */
1262 if ((bytes
= read(PollPipe
, buffer
+ bufused
,
1263 sizeof(buffer
) - bufused
- 1)) > 0)
1266 buffer
[bufused
] = '\0';
1267 lineptr
= strchr(buffer
, '\n');
1269 else if (bytes
< 0 && errno
== EINTR
)
1273 lineptr
= buffer
+ bufused
;
1277 if (bytes
== 0 && bufused
== 0)
1280 while (lineptr
!= NULL
)
1283 * Terminate each line and process it...
1288 LogMessage(L_ERROR
, "%s", buffer
);
1291 * Copy over the buffer data we've used up...
1294 strcpy(buffer
, lineptr
);
1295 bufused
-= lineptr
- buffer
;
1300 lineptr
= strchr(buffer
, '\n');
1306 * All polling processes have died; stop polling...
1309 LogMessage(L_ERROR
, "UpdatePolling: all polling processes have exited!");
1315 /***********************************************************************
1316 **** SLP Support Code *************************************************
1317 ***********************************************************************/
1321 * SLP service name for CUPS...
1324 # define SLP_CUPS_SRVTYPE "service:printer"
1325 # define SLP_CUPS_SRVLEN 15
1329 * Printer service URL structure
1332 typedef struct _slpsrvurl
1334 struct _slpsrvurl
*next
;
1335 char url
[HTTP_MAX_URI
];
1340 * 'RegReportCallback()' - Empty SLPRegReport.
1344 RegReportCallback(SLPHandle hslp
,
1357 * 'SendSLPBrowse()' - Register the specified printer with SLP.
1361 SendSLPBrowse(printer_t
*p
) /* I - Printer to register */
1363 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
1364 attrs
[8192], /* Printer attributes */
1365 finishings
[1024], /* Finishings to support */
1366 make_model
[IPP_MAX_NAME
* 2],
1367 /* Make and model, quoted */
1368 location
[IPP_MAX_NAME
* 2],
1369 /* Location, quoted */
1370 info
[IPP_MAX_NAME
* 2],
1372 *src
, /* Pointer to original string */
1373 *dst
; /* Pointer to destination string */
1374 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
1375 SLPError error
; /* SLP error, if any */
1378 LogMessage(L_DEBUG
, "SendSLPBrowse(%p = \"%s\")", p
, p
->name
);
1381 * Make the SLP service URL that conforms to the IANA
1382 * 'printer:' template.
1385 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
1387 LogMessage(L_DEBUG2
, "Service URL = \"%s\"", srvurl
);
1390 * Figure out the finishings string...
1393 if (p
->type
& CUPS_PRINTER_STAPLE
)
1394 strcpy(finishings
, "staple");
1396 finishings
[0] = '\0';
1398 if (p
->type
& CUPS_PRINTER_BIND
)
1401 strlcat(finishings
, ",bind", sizeof(finishings
));
1403 strcpy(finishings
, "bind");
1406 if (p
->type
& CUPS_PRINTER_PUNCH
)
1409 strlcat(finishings
, ",punch", sizeof(finishings
));
1411 strcpy(finishings
, "punch");
1414 if (p
->type
& CUPS_PRINTER_COVER
)
1417 strlcat(finishings
, ",cover", sizeof(finishings
));
1419 strcpy(finishings
, "cover");
1422 if (p
->type
& CUPS_PRINTER_SORT
)
1425 strlcat(finishings
, ",sort", sizeof(finishings
));
1427 strcpy(finishings
, "sort");
1431 strcpy(finishings
, "none");
1434 * Quote any commas in the make and model, location, and info strings
1435 * (local strings are twice the size of the ones in the printer_t
1436 * structure, so no buffer overflow is possible...)
1439 for (src
= p
->make_model
, dst
= make_model
; *src
;)
1441 if (*src
== ',' || *src
== '\\' || *src
== ')')
1450 strcpy(make_model
, "Unknown");
1452 for (src
= p
->location
, dst
= location
; *src
;)
1454 if (*src
== ',' || *src
== '\\' || *src
== ')')
1463 strcpy(location
, "Unknown");
1465 for (src
= p
->info
, dst
= info
; *src
;)
1467 if (*src
== ',' || *src
== '\\' || *src
== ')')
1476 strcpy(info
, "Unknown");
1479 * Get the authentication value...
1482 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
1486 * Make the SLP attribute string list that conforms to
1487 * the IANA 'printer:' template.
1490 snprintf(attrs
, sizeof(attrs
),
1491 "(printer-uri-supported=%s),"
1492 "(uri-authentication-supported=%s>),"
1494 "(uri-security-supported=tls>),"
1496 "(uri-security-supported=none>),"
1497 #endif /* HAVE_LIBSSL */
1498 "(printer-name=%s),"
1499 "(printer-location=%s),"
1500 "(printer-info=%s),"
1501 "(printer-more-info=%s),"
1502 "(printer-make-and-model=%s),"
1503 "(charset-supported=utf-8),"
1504 "(natural-language-configured=%s),"
1505 "(natural-language-supported=de,en,es,fr,it),"
1506 "(color-supported=%s),"
1507 "(finishings-supported=%s),"
1508 "(sides-supported=one-sided%s),"
1509 "(multiple-document-jobs-supported=true)"
1510 "(ipp-versions-supported=1.0,1.1)",
1511 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
1512 info
, p
->uri
, make_model
, DefaultLanguage
,
1513 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
1515 p
->type
& CUPS_PRINTER_DUPLEX
?
1516 ",two-sided-long-edge,two-sided-short-edge" : "");
1518 LogMessage(L_DEBUG2
, "Attributes = \"%s\"", attrs
);
1521 * Register the printer with the SLP server...
1524 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
1525 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, RegReportCallback
, 0);
1527 if (error
!= SLP_OK
)
1528 LogMessage(L_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
1534 * 'SLPDeregPrinter()' - SLPDereg() the specified printer
1538 SLPDeregPrinter(printer_t
*p
)
1540 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
1543 if((p
->type
& CUPS_PRINTER_REMOTE
) == 0)
1546 * Make the SLP service URL that conforms to the IANA
1547 * 'printer:' template.
1550 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
1553 * Deregister the printer...
1556 SLPDereg(BrowseSLPHandle
, srvurl
, RegReportCallback
, 0);
1562 * 'GetSlpAttrVal()' - Get an attribute from an SLP registration.
1565 int /* O - 0 on success */
1566 GetSlpAttrVal(const char *attrlist
, /* I - Attribute list string */
1567 const char *tag
, /* I - Name of attribute */
1568 char *valbuf
, /* O - Value */
1569 int valbuflen
) /* I - Max length of value */
1571 char *ptr1
, /* Pointer into string */
1577 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
1579 ptr1
+= strlen(tag
);
1581 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
1583 if (valbuflen
> (ptr2
- ptr1
))
1589 strncpy(valbuf
, ptr1
, ptr2
- ptr1
);
1590 valbuf
[ptr2
- ptr1
] = '\0';
1593 * Dequote the value...
1596 for (ptr1
= valbuf
; *ptr1
; ptr1
++)
1597 if (*ptr1
== '\\' && ptr1
[1])
1598 strcpy(ptr1
, ptr1
+ 1);
1610 * 'AttrCallback()' - SLP attribute callback
1614 AttrCallback(SLPHandle hslp
,
1615 const char *attrlist
,
1619 char tmp
[IPP_MAX_NAME
];
1620 printer_t
*p
= (printer_t
*)cookie
;
1624 * Let the compiler know we won't be using these...
1630 * Bail if there was an error
1633 if (errcode
!= SLP_OK
)
1637 * Parse the attrlist to obtain things needed to build CUPS browse packet
1640 memset(p
, 0, sizeof(printer_t
));
1642 p
->type
= CUPS_PRINTER_REMOTE
;
1644 if (GetSlpAttrVal(attrlist
, "(printer-location=", p
->location
,
1645 sizeof(p
->location
)))
1647 if (GetSlpAttrVal(attrlist
, "(printer-make-and-model=", p
->make_model
,
1648 sizeof(p
->make_model
)))
1651 if (GetSlpAttrVal(attrlist
, "(color-supported=", tmp
, sizeof(tmp
)))
1653 if (strcasecmp(tmp
, "true") == 0)
1654 p
->type
|= CUPS_PRINTER_COLOR
;
1656 if (GetSlpAttrVal(attrlist
, "(finishings-supported=", tmp
, sizeof(tmp
)))
1658 if (strstr(tmp
, "staple"))
1659 p
->type
|= CUPS_PRINTER_STAPLE
;
1660 if (strstr(tmp
, "bind"))
1661 p
->type
|= CUPS_PRINTER_BIND
;
1662 if (strstr(tmp
, "punch"))
1663 p
->type
|= CUPS_PRINTER_PUNCH
;
1665 if (GetSlpAttrVal(attrlist
, "(sides-supported=", tmp
, sizeof(tmp
)))
1667 if (strstr(tmp
,"two-sided"))
1668 p
->type
|= CUPS_PRINTER_DUPLEX
;
1675 * 'SrvUrlCallback()' - SLP service url callback
1678 SLPBoolean
/* O - TRUE = OK, FALSE = error */
1679 SrvUrlCallback(SLPHandle hslp
, /* I - SLP handle */
1680 const char *srvurl
, /* I - URL of service */
1681 unsigned short lifetime
, /* I - Life of service */
1682 SLPError errcode
, /* I - Existing error code */
1683 void *cookie
) /* I - Pointer to service list */
1685 slpsrvurl_t
*s
, /* New service entry */
1686 **head
; /* Pointer to head of entry */
1690 * Let the compiler know we won't be using these vars...
1697 * Bail if there was an error
1700 if (errcode
!= SLP_OK
)
1704 * Grab the head of the list...
1707 head
= (slpsrvurl_t
**)cookie
;
1710 * Allocate a *temporary* slpsrvurl_t to hold this entry.
1713 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
1717 * Copy the SLP service URL...
1720 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
1723 * Link the SLP service URL into the head of the list
1736 * 'UpdateSLPBrowse()' - Get browsing information via SLP.
1740 UpdateSLPBrowse(void)
1742 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1743 *next
; /* Next service in list */
1744 printer_t p
; /* Printer information */
1745 const char *uri
; /* Pointer to printer URI */
1746 char method
[HTTP_MAX_URI
], /* Method portion of URI */
1747 username
[HTTP_MAX_URI
], /* Username portion of URI */
1748 host
[HTTP_MAX_URI
], /* Host portion of URI */
1749 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1750 int port
; /* Port portion of URI */
1753 LogMessage(L_DEBUG
, "UpdateSLPBrowse() Start...");
1756 * Reset the refresh time...
1759 BrowseSLPRefresh
= time(NULL
) + BrowseTimeout
- BrowseInterval
;
1762 * Poll for remote printers using SLP...
1767 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1768 SrvUrlCallback
, &s
);
1771 * Loop through the list of available printers...
1777 * Load a printer_t structure with the SLP service attributes...
1780 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", AttrCallback
, &p
);
1783 * Process this printer entry...
1786 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1788 if (strncmp(uri
, "http://", 7) == 0 ||
1789 strncmp(uri
, "ipp://", 6) == 0)
1792 * Pull the URI apart to see if this is a local or remote printer...
1795 httpSeparate(uri
, method
, username
, host
, &port
, resource
);
1797 if (strcasecmp(host
, ServerName
) == 0)
1801 * OK, at least an IPP printer, see if it is a CUPS printer or
1805 if (strstr(uri
, "/printers/") != NULL
)
1806 ProcessBrowseData(uri
, p
.type
, IPP_PRINTER_IDLE
, p
.location
,
1807 p
.info
, p
.make_model
);
1808 else if (strstr(uri
, "/classes/") != NULL
)
1809 ProcessBrowseData(uri
, p
.type
| CUPS_PRINTER_CLASS
, IPP_PRINTER_IDLE
,
1810 p
.location
, p
.info
, p
.make_model
);
1814 * Save the "next" pointer and free this listing...
1821 LogMessage(L_DEBUG
, "UpdateSLPBrowse() End...");
1823 #endif /* HAVE_LIBSLP */