]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/dirsvc.c
2 * "$Id: dirsvc.c,v 1.131 2004/05/27 18:04:32 mike Exp $"
4 * Directory services routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2004 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...
120 type
|= CUPS_PRINTER_REMOTE
;
122 hptr
= strchr(host
, '.');
123 sptr
= strchr(ServerName
, '.');
125 if (sptr
!= NULL
&& hptr
!= NULL
)
128 * Strip the common domain name components...
133 if (strcasecmp(hptr
, sptr
) == 0)
139 hptr
= strchr(hptr
+ 1, '.');
143 if (type
& CUPS_PRINTER_CLASS
)
146 * Remote destination is a class...
149 if (strncmp(resource
, "/classes/", 9) == 0)
150 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
154 if ((p
= FindClass(name
)) == NULL
&& BrowseShortNames
)
156 if ((p
= FindClass(resource
+ 9)) != NULL
)
158 if (p
->hostname
&& strcasecmp(p
->hostname
, host
) != 0)
161 * Nope, this isn't the same host; if the hostname isn't the local host,
162 * add it to the other class and then find a class using the full host
166 if (p
->type
& CUPS_PRINTER_REMOTE
)
168 SetStringf(&p
->name
, "%s@%s", p
->name
, p
->hostname
);
175 else if (!p
->hostname
)
177 SetString(&p
->hostname
, host
);
178 SetString(&p
->uri
, uri
);
179 SetString(&p
->device_uri
, uri
);
184 strlcpy(name
, resource
+ 9, sizeof(name
));
186 else if (p
!= NULL
&& !p
->hostname
)
188 SetString(&p
->hostname
, host
);
189 SetString(&p
->uri
, uri
);
190 SetString(&p
->device_uri
, 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...
208 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
210 SetString(&p
->uri
, uri
);
211 SetString(&p
->device_uri
, uri
);
212 SetString(&p
->hostname
, host
);
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 (p
->hostname
&& strcasecmp(p
->hostname
, host
) != 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 SetStringf(&p
->name
, "%s@%s", p
->name
, p
->hostname
);
249 else if (!p
->hostname
)
251 SetString(&p
->hostname
, host
);
252 SetString(&p
->uri
, uri
);
253 SetString(&p
->device_uri
, uri
);
258 strlcpy(name
, resource
+ 10, sizeof(name
));
260 else if (p
!= NULL
&& !p
->hostname
)
262 SetString(&p
->hostname
, host
);
263 SetString(&p
->uri
, uri
);
264 SetString(&p
->device_uri
, uri
);
271 * Printer doesn't exist; add it...
274 p
= AddPrinter(name
);
276 LogMessage(L_INFO
, "Added remote printer \"%s\"...", name
);
279 * Force the URI to point to the real server...
282 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
284 SetString(&p
->hostname
, host
);
285 SetString(&p
->uri
, uri
);
286 SetString(&p
->device_uri
, uri
);
293 * Update the state...
297 p
->browse_time
= time(NULL
);
299 if (type
& CUPS_PRINTER_REJECTING
)
301 type
&= ~CUPS_PRINTER_REJECTING
;
309 else if (!p
->accepting
)
321 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
323 SetString(&p
->location
, location
);
327 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
329 SetString(&p
->info
, info
);
333 if (!make_model
|| !make_model
[0])
335 if (type
& CUPS_PRINTER_CLASS
)
336 snprintf(local_make_model
, sizeof(local_make_model
),
337 "Remote Class on %s", host
);
339 snprintf(local_make_model
, sizeof(local_make_model
),
340 "Remote Printer on %s", host
);
343 snprintf(local_make_model
, sizeof(local_make_model
),
344 "%s on %s", make_model
, host
);
346 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
348 SetString(&p
->make_model
, local_make_model
);
355 UpdateImplicitClasses();
359 * See if we have a default printer... If not, make the first printer the
363 if (DefaultPrinter
== NULL
&& Printers
!= NULL
)
365 DefaultPrinter
= Printers
;
371 * Do auto-classing if needed...
377 * Loop through all available printers and create classes as needed...
380 for (p
= Printers
, len
= 0, offset
= 0, first
= NULL
;
385 * Get next printer in list...
391 * Skip implicit classes...
394 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
401 * If len == 0, get the length of this printer name up to the "@"
406 strncasecmp(p
->name
, name
+ offset
, len
) == 0 &&
407 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
410 * We have more than one printer with the same name; see if
411 * we have a class, and if this printer is a member...
414 if ((pclass
= FindDest(name
)) == NULL
)
417 * Need to add the class...
420 pclass
= AddPrinter(name
);
421 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
422 pclass
->accepting
= 1;
423 pclass
->state
= IPP_PRINTER_IDLE
;
425 SetString(&pclass
->location
, p
->location
);
426 SetString(&pclass
->info
, p
->info
);
428 SetPrinterAttrs(pclass
);
430 LogMessage(L_INFO
, "Added implicit class \"%s\"...", name
);
435 for (i
= 0; i
< pclass
->num_printers
; i
++)
436 if (pclass
->printers
[i
] == first
)
439 if (i
>= pclass
->num_printers
)
440 AddPrinterToClass(pclass
, first
);
445 for (i
= 0; i
< pclass
->num_printers
; i
++)
446 if (pclass
->printers
[i
] == p
)
449 if (i
>= pclass
->num_printers
)
450 AddPrinterToClass(pclass
, p
);
455 * First time around; just get name length and mark it as first
459 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
460 len
= hptr
- p
->name
;
462 len
= strlen(p
->name
);
464 strncpy(name
, p
->name
, len
);
468 if ((pclass
= FindDest(name
)) != NULL
&&
469 !(pclass
->type
& CUPS_PRINTER_IMPLICIT
))
472 * Can't use same name as a local printer; add "Any" to the
473 * front of the name, unless we have explicitly disabled
474 * the "ImplicitAnyClasses"...
477 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
480 * Add "Any" to the class name...
484 strncpy(name
+ 3, p
->name
, len
);
485 name
[len
+ 3] = '\0';
491 * Don't create an implicit class if we have a local printer
492 * with the same name...
508 * 'SendBrowseList()' - Send new browsing information as necessary.
514 int count
; /* Number of dests to update */
515 printer_t
*p
, /* Current printer */
516 *np
; /* Next printer */
517 time_t ut
, /* Minimum update time */
518 to
; /* Timeout time */
521 if (!Browsing
|| !BrowseProtocols
)
525 * Compute the update and timeout times...
528 ut
= time(NULL
) - BrowseInterval
;
529 to
= time(NULL
) - BrowseTimeout
;
532 * Figure out how many printers need an update...
535 if (BrowseInterval
> 0)
537 for (count
= 0, p
= Printers
; p
!= NULL
; p
= p
->next
)
538 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
543 * Throttle the number of printers we'll be updating this time
547 count
= 2 * count
/ BrowseInterval
+ 1;
553 * Loop through all of the printers and send local updates as needed...
556 for (p
= Printers
; p
!= NULL
; p
= np
)
560 if (p
->type
& CUPS_PRINTER_REMOTE
)
563 * See if this printer needs to be timed out...
566 if (p
->browse_time
< to
)
568 LogMessage(L_INFO
, "Remote destination \"%s\" has timed out; deleting it...",
573 else if (p
->browse_time
< ut
&& count
> 0 &&
574 !(p
->type
& CUPS_PRINTER_IMPLICIT
))
577 * Need to send an update...
582 p
->browse_time
= time(NULL
);
584 if (BrowseProtocols
& BROWSE_CUPS
)
588 if (BrowseProtocols
& BROWSE_SLP
)
590 #endif /* HAVE_LIBSLP */
597 * 'SendCUPSBrowse()' - Send new browsing information using the CUPS protocol.
601 SendCUPSBrowse(printer_t
*p
) /* I - Printer to send */
603 int i
; /* Looping var */
604 cups_ptype_t type
; /* Printer type */
605 dirsvc_addr_t
*b
; /* Browse address */
606 int bytes
; /* Length of packet */
607 char packet
[1453]; /* Browse data packet */
608 cups_netif_t
*iface
; /* Network interface */
612 * Figure out the printer type value...
615 type
= p
->type
| CUPS_PRINTER_REMOTE
;
618 type
|= CUPS_PRINTER_REJECTING
;
621 * Send a packet to each browse address...
624 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
628 * Send the browse packet to one or more interfaces...
631 if (strcmp(b
->iface
, "*") == 0)
634 * Send to all local interfaces...
639 for (iface
= NetIFList
; iface
!= NULL
; iface
= iface
->next
)
642 * Only send to local interfaces...
645 if (!iface
->is_local
)
648 snprintf(packet
, sizeof(packet
), "%x %x ipp://%s/%s/%s \"%s\" \"%s\" \"%s\"\n",
649 type
, p
->state
, iface
->hostname
,
650 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
651 p
->name
, p
->location
? p
->location
: "",
652 p
->info
? p
->info
: "",
653 p
->make_model
? p
->make_model
: "Unknown");
655 bytes
= strlen(packet
);
657 LogMessage(L_DEBUG2
, "SendBrowseList: (%d bytes to \"%s\") %s", bytes
,
658 iface
->name
, packet
);
660 iface
->broadcast
.sin_port
= htons(BrowsePort
);
662 sendto(BrowseSocket
, packet
, bytes
, 0,
663 (struct sockaddr
*)&(iface
->broadcast
),
664 sizeof(struct sockaddr_in
));
667 else if ((iface
= NetIFFind(b
->iface
)) != NULL
)
670 * Send to the named interface...
673 snprintf(packet
, sizeof(packet
), "%x %x ipp://%s/%s/%s \"%s\" \"%s\" \"%s\"\n",
674 type
, p
->state
, iface
->hostname
,
675 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
676 p
->name
, p
->location
? p
->location
: "",
677 p
->info
? p
->info
: "",
678 p
->make_model
? p
->make_model
: "Unknown");
680 bytes
= strlen(packet
);
682 LogMessage(L_DEBUG2
, "SendBrowseList: (%d bytes to \"%s\") %s", bytes
,
683 iface
->name
, packet
);
685 iface
->broadcast
.sin_port
= htons(BrowsePort
);
687 sendto(BrowseSocket
, packet
, bytes
, 0,
688 (struct sockaddr
*)&(iface
->broadcast
),
689 sizeof(struct sockaddr_in
));
695 * Send the browse packet to the indicated address using
696 * the default server name...
699 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\"\n",
700 type
, p
->state
, p
->uri
,
701 p
->location
? p
->location
: "",
702 p
->info
? p
->info
: "",
703 p
->make_model
? p
->make_model
: "Unknown");
705 bytes
= strlen(packet
);
706 LogMessage(L_DEBUG2
, "SendBrowseList: (%d bytes to %x) %s", bytes
,
707 (unsigned)ntohl(b
->to
.sin_addr
.s_addr
), packet
);
709 if (sendto(BrowseSocket
, packet
, bytes
, 0,
710 (struct sockaddr
*)&(b
->to
), sizeof(struct sockaddr_in
)) <= 0)
713 * Unable to send browse packet, so remove this address from the
717 LogMessage(L_ERROR
, "SendBrowseList: sendto failed for browser %d - %s.",
718 b
- Browsers
+ 1, strerror(errno
));
721 memcpy(b
, b
+ 1, (i
- 1) * sizeof(dirsvc_addr_t
));
731 * 'StartBrowsing()' - Start sending and receiving broadcast information.
737 int val
; /* Socket option value */
738 struct sockaddr_in addr
; /* Broadcast address */
741 if (!Browsing
|| !BrowseProtocols
)
744 if (BrowseProtocols
& BROWSE_CUPS
)
747 * Create the broadcast socket...
750 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
752 LogMessage(L_ERROR
, "StartBrowsing: Unable to create broadcast socket - %s.",
754 BrowseProtocols
&= ~BROWSE_CUPS
;
759 * Set the "broadcast" flag...
763 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
765 LogMessage(L_ERROR
, "StartBrowsing: Unable to set broadcast mode - %s.",
769 closesocket(BrowseSocket
);
775 BrowseProtocols
&= ~BROWSE_CUPS
;
780 * Bind the socket to browse port...
783 memset(&addr
, 0, sizeof(addr
));
784 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
785 addr
.sin_family
= AF_INET
;
786 addr
.sin_port
= htons(BrowsePort
);
788 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
790 LogMessage(L_ERROR
, "StartBrowsing: Unable to bind broadcast socket - %s.",
794 closesocket(BrowseSocket
);
800 BrowseProtocols
&= ~BROWSE_CUPS
;
805 * Finally, add the socket to the input selection set...
808 LogMessage(L_DEBUG2
, "StartBrowsing: Adding fd %d to InputSet...",
811 FD_SET(BrowseSocket
, InputSet
);
817 if (BrowseProtocols
& BROWSE_SLP
)
823 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
825 LogMessage(L_ERROR
, "Unable to open an SLP handle; disabling SLP browsing!");
826 BrowseProtocols
&= ~BROWSE_SLP
;
829 BrowseSLPRefresh
= 0;
831 #endif /* HAVE_LIBSLP */
836 * 'StartPolling()' - Start polling servers as needed.
842 int i
; /* Looping var */
843 dirsvc_poll_t
*poll
; /* Current polling server */
844 int pid
; /* New process ID */
845 char sport
[10]; /* Server port */
846 char bport
[10]; /* Browser port */
847 char interval
[10]; /* Poll interval */
848 int statusfds
[2]; /* Status pipe */
849 int fd
; /* Current file descriptor */
850 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
851 struct sigaction action
; /* POSIX signal handler */
852 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
856 * Don't do anything if we aren't polling...
866 * Setup string arguments for port and interval options.
869 sprintf(bport
, "%d", BrowsePort
);
872 sprintf(interval
, "%d", BrowseInterval
);
874 strcpy(interval
, "30");
877 * Create a pipe that receives the status messages from each
883 LogMessage(L_ERROR
, "Unable to create polling status pipes - %s.",
889 PollPipe
= statusfds
[0];
892 * Run each polling daemon, redirecting stderr to the polling pipe...
895 for (i
= 0, poll
= Polled
; i
< NumPolled
; i
++, poll
++)
897 sprintf(sport
, "%d", poll
->port
);
900 * Block signals before forking...
905 if ((pid
= fork()) == 0)
914 * Running as root, so change to non-priviledged user...
920 if (setgroups(1, &Group
))
929 * Reset group membership to just the main one we belong to.
932 setgroups(1, &Group
);
936 * Redirect stdin and stdout to /dev/null, and stderr to the
937 * status pipe. Close all other files.
941 open("/dev/null", O_RDONLY
);
944 open("/dev/null", O_WRONLY
);
949 for (fd
= 3; fd
< MaxFDs
; fd
++)
953 * Unblock signals before doing the exec...
957 sigset(SIGTERM
, SIG_DFL
);
958 sigset(SIGCHLD
, SIG_DFL
);
959 #elif defined(HAVE_SIGACTION)
960 memset(&action
, 0, sizeof(action
));
962 sigemptyset(&action
.sa_mask
);
963 action
.sa_handler
= SIG_DFL
;
965 sigaction(SIGTERM
, &action
, NULL
);
966 sigaction(SIGCHLD
, &action
, NULL
);
968 signal(SIGTERM
, SIG_DFL
);
969 signal(SIGCHLD
, SIG_DFL
);
970 #endif /* HAVE_SIGSET */
975 * Execute the polling daemon...
978 execl(CUPS_SERVERBIN
"/daemon/cups-polld", "cups-polld", poll
->hostname
,
979 sport
, interval
, bport
, NULL
);
984 LogMessage(L_ERROR
, "StartPolling: Unable to fork polling daemon - %s",
992 LogMessage(L_DEBUG
, "StartPolling: Started polling daemon for %s:%d, pid = %d",
993 poll
->hostname
, poll
->port
, pid
);
1002 * Finally, add the pipe to the input selection set...
1005 LogMessage(L_DEBUG2
, "StartPolling: Adding fd %d to InputSet...",
1008 FD_SET(PollPipe
, InputSet
);
1013 * 'StopBrowsing()' - Stop sending and receiving broadcast information.
1019 if (!Browsing
|| !BrowseProtocols
)
1022 if (BrowseProtocols
& BROWSE_CUPS
)
1025 * Close the socket and remove it from the input selection set.
1028 if (BrowseSocket
>= 0)
1031 closesocket(BrowseSocket
);
1033 close(BrowseSocket
);
1036 LogMessage(L_DEBUG2
, "StopBrowsing: Removing fd %d from InputSet...",
1039 FD_CLR(BrowseSocket
, InputSet
);
1045 if (BrowseProtocols
& BROWSE_SLP
)
1048 * Close SLP handle...
1051 SLPClose(BrowseSLPHandle
);
1053 #endif /* HAVE_LIBSLP */
1058 * 'StopPolling()' - Stop polling servers as needed.
1064 int i
; /* Looping var */
1065 dirsvc_poll_t
*poll
; /* Current polling server */
1072 LogMessage(L_DEBUG2
, "StopPolling: removing fd %d from InputSet.",
1074 FD_CLR(PollPipe
, InputSet
);
1079 for (i
= 0, poll
= Polled
; i
< NumPolled
; i
++, poll
++)
1081 kill(poll
->pid
, SIGTERM
);
1086 * 'UpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
1090 UpdateCUPSBrowse(void)
1092 int i
; /* Looping var */
1093 int auth
; /* Authorization status */
1094 int len
; /* Length of name string */
1095 int bytes
; /* Number of bytes left */
1096 char packet
[1540], /* Broadcast packet */
1097 *pptr
; /* Pointer into packet */
1098 struct sockaddr_in srcaddr
; /* Source address */
1099 char srcname
[1024]; /* Source hostname */
1100 unsigned address
; /* Source address (host order) */
1101 struct hostent
*srchost
; /* Host entry for source address */
1102 unsigned type
; /* Printer type */
1103 unsigned state
; /* Printer state */
1104 char uri
[HTTP_MAX_URI
], /* Printer URI */
1105 method
[HTTP_MAX_URI
], /* Method portion of URI */
1106 username
[HTTP_MAX_URI
], /* Username portion of URI */
1107 host
[HTTP_MAX_URI
], /* Host portion of URI */
1108 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
1109 info
[IPP_MAX_NAME
], /* Information string */
1110 location
[IPP_MAX_NAME
], /* Location string */
1111 make_model
[IPP_MAX_NAME
];/* Make and model string */
1112 int port
; /* Port portion of URI */
1113 cups_netif_t
*iface
; /* Network interface */
1117 * Read a packet from the browse socket...
1120 len
= sizeof(srcaddr
);
1121 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
), 0,
1122 (struct sockaddr
*)&srcaddr
, &len
)) <= 0)
1125 * "Connection refused" is returned under Linux if the destination port
1126 * or address is unreachable from a previous sendto(); check for the
1127 * error here and ignore it for now...
1130 if (errno
!= ECONNREFUSED
)
1132 LogMessage(L_ERROR
, "Browse recv failed - %s.", strerror(errno
));
1133 LogMessage(L_ERROR
, "Browsing turned off.");
1142 packet
[bytes
] = '\0';
1145 * Figure out where it came from...
1148 address
= ntohl(srcaddr
.sin_addr
.s_addr
);
1150 if (HostNameLookups
)
1152 srchost
= gethostbyaddr((char *)&(srcaddr
.sin_addr
), sizeof(struct in_addr
),
1155 srchost
= gethostbyaddr(&(srcaddr
.sin_addr
), sizeof(struct in_addr
),
1161 if (srchost
== NULL
)
1162 sprintf(srcname
, "%d.%d.%d.%d", address
>> 24, (address
>> 16) & 255,
1163 (address
>> 8) & 255, address
& 255);
1165 strlcpy(srcname
, srchost
->h_name
, sizeof(srcname
));
1167 len
= strlen(srcname
);
1173 if (BrowseACL
&& (BrowseACL
->num_allow
|| BrowseACL
->num_deny
))
1175 if (address
== 0x7f000001 || strcasecmp(srcname
, "localhost") == 0)
1178 * Access from localhost (127.0.0.1) is always allowed...
1186 * Do authorization checks on the domain/address...
1189 switch (BrowseACL
->order_type
)
1192 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1195 case AUTH_ALLOW
: /* Order Deny,Allow */
1198 if (CheckAuth(address
, srcname
, len
,
1199 BrowseACL
->num_deny
, BrowseACL
->deny
))
1202 if (CheckAuth(address
, srcname
, len
,
1203 BrowseACL
->num_allow
, BrowseACL
->allow
))
1207 case AUTH_DENY
: /* Order Allow,Deny */
1210 if (CheckAuth(address
, srcname
, len
,
1211 BrowseACL
->num_allow
, BrowseACL
->allow
))
1214 if (CheckAuth(address
, srcname
, len
,
1215 BrowseACL
->num_deny
, BrowseACL
->deny
))
1224 if (auth
== AUTH_DENY
)
1226 LogMessage(L_DEBUG
, "UpdateCUPSBrowse: Refused %d bytes from %s", bytes
,
1231 LogMessage(L_DEBUG2
, "UpdateCUPSBrowse: (%d bytes from %s) %s", bytes
, srcname
,
1238 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
1240 LogMessage(L_WARN
, "UpdateCUPSBrowse: Garbled browse packet - %s",
1245 strcpy(location
, "Location Unknown");
1246 strcpy(info
, "No Information Available");
1247 make_model
[0] = '\0';
1249 if ((pptr
= strchr(packet
, '\"')) != NULL
)
1252 * Have extended information; can't use sscanf for it because not all
1253 * sscanf's allow empty strings with %[^\"]...
1256 for (i
= 0, pptr
++;
1257 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
1259 location
[i
] = *pptr
;
1267 while (*pptr
&& isspace(*pptr
& 255))
1272 for (i
= 0, pptr
++;
1273 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
1283 while (*pptr
&& isspace(*pptr
& 255))
1288 for (i
= 0, pptr
++;
1289 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
1291 make_model
[i
] = *pptr
;
1294 make_model
[i
] = '\0';
1300 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
1301 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
1302 type
, state
, uri
, location
, info
, make_model
));
1305 * Pull the URI apart to see if this is a local or remote printer...
1308 httpSeparate(uri
, method
, username
, host
, &port
, resource
);
1310 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1313 * Check for packets from the local server...
1316 if (strcasecmp(host
, ServerName
) == 0)
1321 for (iface
= NetIFList
; iface
!= NULL
; iface
= iface
->next
)
1322 if (strcasecmp(host
, iface
->hostname
) == 0)
1329 for (i
= 0; i
< NumRelays
; i
++)
1330 if (CheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
1331 if (sendto(BrowseSocket
, packet
, bytes
, 0,
1332 (struct sockaddr
*)&(Relays
[i
].to
),
1333 sizeof(struct sockaddr_in
)) <= 0)
1335 LogMessage(L_ERROR
, "UpdateCUPSBrowse: sendto failed for relay %d - %s.",
1336 i
+ 1, strerror(errno
));
1341 * Process the browse data...
1344 ProcessBrowseData(uri
, (cups_ptype_t
)type
, (ipp_pstate_t
)state
, location
,
1350 * 'UpdatePolling()' - Read status messages from the poll daemons.
1356 int bytes
; /* Number of bytes read */
1357 char *lineptr
; /* Pointer to end of line in buffer */
1358 static int bufused
= 0; /* Number of bytes used in buffer */
1359 static char buffer
[1024]; /* Status buffer */
1362 if ((bytes
= read(PollPipe
, buffer
+ bufused
,
1363 sizeof(buffer
) - bufused
- 1)) > 0)
1366 buffer
[bufused
] = '\0';
1367 lineptr
= strchr(buffer
, '\n');
1369 else if (bytes
< 0 && errno
== EINTR
)
1373 lineptr
= buffer
+ bufused
;
1377 if (bytes
== 0 && bufused
== 0)
1380 while (lineptr
!= NULL
)
1383 * Terminate each line and process it...
1388 if (!strncmp(buffer
, "ERROR: ", 7))
1389 LogMessage(L_ERROR
, "%s", buffer
+ 7);
1390 else if (!strncmp(buffer
, "DEBUG: ", 7))
1391 LogMessage(L_DEBUG
, "%s", buffer
+ 7);
1392 else if (!strncmp(buffer
, "DEBUG2: ", 8))
1393 LogMessage(L_DEBUG2
, "%s", buffer
+ 8);
1395 LogMessage(L_DEBUG
, "%s", buffer
);
1398 * Copy over the buffer data we've used up...
1401 cups_strcpy(buffer
, lineptr
);
1402 bufused
-= lineptr
- buffer
;
1407 lineptr
= strchr(buffer
, '\n');
1413 * All polling processes have died; stop polling...
1416 LogMessage(L_ERROR
, "UpdatePolling: all polling processes have exited!");
1422 /***********************************************************************
1423 **** SLP Support Code *************************************************
1424 ***********************************************************************/
1428 * SLP service name for CUPS...
1431 # define SLP_CUPS_SRVTYPE "service:printer"
1432 # define SLP_CUPS_SRVLEN 15
1436 * Printer service URL structure
1439 typedef struct _slpsrvurl
1441 struct _slpsrvurl
*next
;
1442 char url
[HTTP_MAX_URI
];
1447 * 'RegReportCallback()' - Empty SLPRegReport.
1451 RegReportCallback(SLPHandle hslp
,
1464 * 'SendSLPBrowse()' - Register the specified printer with SLP.
1468 SendSLPBrowse(printer_t
*p
) /* I - Printer to register */
1470 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
1471 attrs
[8192], /* Printer attributes */
1472 finishings
[1024], /* Finishings to support */
1473 make_model
[IPP_MAX_NAME
* 2],
1474 /* Make and model, quoted */
1475 location
[IPP_MAX_NAME
* 2],
1476 /* Location, quoted */
1477 info
[IPP_MAX_NAME
* 2],
1479 *src
, /* Pointer to original string */
1480 *dst
; /* Pointer to destination string */
1481 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
1482 SLPError error
; /* SLP error, if any */
1485 LogMessage(L_DEBUG
, "SendSLPBrowse(%p = \"%s\")", p
, p
->name
);
1488 * Make the SLP service URL that conforms to the IANA
1489 * 'printer:' template.
1492 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
1494 LogMessage(L_DEBUG2
, "Service URL = \"%s\"", srvurl
);
1497 * Figure out the finishings string...
1500 if (p
->type
& CUPS_PRINTER_STAPLE
)
1501 strcpy(finishings
, "staple");
1503 finishings
[0] = '\0';
1505 if (p
->type
& CUPS_PRINTER_BIND
)
1508 strlcat(finishings
, ",bind", sizeof(finishings
));
1510 strcpy(finishings
, "bind");
1513 if (p
->type
& CUPS_PRINTER_PUNCH
)
1516 strlcat(finishings
, ",punch", sizeof(finishings
));
1518 strcpy(finishings
, "punch");
1521 if (p
->type
& CUPS_PRINTER_COVER
)
1524 strlcat(finishings
, ",cover", sizeof(finishings
));
1526 strcpy(finishings
, "cover");
1529 if (p
->type
& CUPS_PRINTER_SORT
)
1532 strlcat(finishings
, ",sort", sizeof(finishings
));
1534 strcpy(finishings
, "sort");
1538 strcpy(finishings
, "none");
1541 * Quote any commas in the make and model, location, and info strings...
1544 for (src
= p
->make_model
, dst
= make_model
;
1545 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
1547 if (*src
== ',' || *src
== '\\' || *src
== ')')
1556 strcpy(make_model
, "Unknown");
1558 for (src
= p
->location
, dst
= location
;
1559 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
1561 if (*src
== ',' || *src
== '\\' || *src
== ')')
1570 strcpy(location
, "Unknown");
1572 for (src
= p
->info
, dst
= info
;
1573 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
1575 if (*src
== ',' || *src
== '\\' || *src
== ')')
1584 strcpy(info
, "Unknown");
1587 * Get the authentication value...
1590 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
1594 * Make the SLP attribute string list that conforms to
1595 * the IANA 'printer:' template.
1598 snprintf(attrs
, sizeof(attrs
),
1599 "(printer-uri-supported=%s),"
1600 "(uri-authentication-supported=%s>),"
1602 "(uri-security-supported=tls>),"
1604 "(uri-security-supported=none>),"
1605 #endif /* HAVE_SSL */
1606 "(printer-name=%s),"
1607 "(printer-location=%s),"
1608 "(printer-info=%s),"
1609 "(printer-more-info=%s),"
1610 "(printer-make-and-model=%s),"
1611 "(charset-supported=utf-8),"
1612 "(natural-language-configured=%s),"
1613 "(natural-language-supported=de,en,es,fr,it),"
1614 "(color-supported=%s),"
1615 "(finishings-supported=%s),"
1616 "(sides-supported=one-sided%s),"
1617 "(multiple-document-jobs-supported=true)"
1618 "(ipp-versions-supported=1.0,1.1)",
1619 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
1620 info
, p
->uri
, make_model
, DefaultLanguage
,
1621 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
1623 p
->type
& CUPS_PRINTER_DUPLEX
?
1624 ",two-sided-long-edge,two-sided-short-edge" : "");
1626 LogMessage(L_DEBUG2
, "Attributes = \"%s\"", attrs
);
1629 * Register the printer with the SLP server...
1632 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
1633 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, RegReportCallback
, 0);
1635 if (error
!= SLP_OK
)
1636 LogMessage(L_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
1642 * 'SLPDeregPrinter()' - SLPDereg() the specified printer
1646 SLPDeregPrinter(printer_t
*p
)
1648 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
1651 if((p
->type
& CUPS_PRINTER_REMOTE
) == 0)
1654 * Make the SLP service URL that conforms to the IANA
1655 * 'printer:' template.
1658 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
1661 * Deregister the printer...
1664 SLPDereg(BrowseSLPHandle
, srvurl
, RegReportCallback
, 0);
1670 * 'GetSlpAttrVal()' - Get an attribute from an SLP registration.
1673 int /* O - 0 on success */
1674 GetSlpAttrVal(const char *attrlist
, /* I - Attribute list string */
1675 const char *tag
, /* I - Name of attribute */
1676 char **valbuf
) /* O - Value */
1678 char *ptr1
, /* Pointer into string */
1682 ClearString(valbuf
);
1684 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
1686 ptr1
+= strlen(tag
);
1688 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
1694 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
1695 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
1698 * Dequote the value...
1701 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
1702 if (*ptr1
== '\\' && ptr1
[1])
1703 cups_strcpy(ptr1
, ptr1
+ 1);
1714 * 'AttrCallback()' - SLP attribute callback
1717 SLPBoolean
/* O - SLP_TRUE for success */
1718 AttrCallback(SLPHandle hslp
, /* I - SLP handle */
1719 const char *attrlist
, /* I - Attribute list */
1720 SLPError errcode
, /* I - Parsing status for this attr */
1721 void *cookie
) /* I - Current printer */
1724 printer_t
*p
= (printer_t
*)cookie
;
1728 * Let the compiler know we won't be using these...
1734 * Bail if there was an error
1737 if (errcode
!= SLP_OK
)
1741 * Parse the attrlist to obtain things needed to build CUPS browse packet
1744 memset(p
, 0, sizeof(printer_t
));
1746 p
->type
= CUPS_PRINTER_REMOTE
;
1748 if (GetSlpAttrVal(attrlist
, "(printer-location=", &(p
->location
)))
1750 if (GetSlpAttrVal(attrlist
, "(printer-info=", &(p
->info
)))
1752 if (GetSlpAttrVal(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
1755 if (GetSlpAttrVal(attrlist
, "(color-supported=", &tmp
))
1757 if (strcasecmp(tmp
, "true") == 0)
1758 p
->type
|= CUPS_PRINTER_COLOR
;
1760 if (GetSlpAttrVal(attrlist
, "(finishings-supported=", &tmp
))
1762 if (strstr(tmp
, "staple"))
1763 p
->type
|= CUPS_PRINTER_STAPLE
;
1764 if (strstr(tmp
, "bind"))
1765 p
->type
|= CUPS_PRINTER_BIND
;
1766 if (strstr(tmp
, "punch"))
1767 p
->type
|= CUPS_PRINTER_PUNCH
;
1769 if (GetSlpAttrVal(attrlist
, "(sides-supported=", &tmp
))
1771 if (strstr(tmp
,"two-sided"))
1772 p
->type
|= CUPS_PRINTER_DUPLEX
;
1781 * 'SrvUrlCallback()' - SLP service url callback
1784 SLPBoolean
/* O - TRUE = OK, FALSE = error */
1785 SrvUrlCallback(SLPHandle hslp
, /* I - SLP handle */
1786 const char *srvurl
, /* I - URL of service */
1787 unsigned short lifetime
, /* I - Life of service */
1788 SLPError errcode
, /* I - Existing error code */
1789 void *cookie
) /* I - Pointer to service list */
1791 slpsrvurl_t
*s
, /* New service entry */
1792 **head
; /* Pointer to head of entry */
1796 * Let the compiler know we won't be using these vars...
1803 * Bail if there was an error
1806 if (errcode
!= SLP_OK
)
1810 * Grab the head of the list...
1813 head
= (slpsrvurl_t
**)cookie
;
1816 * Allocate a *temporary* slpsrvurl_t to hold this entry.
1819 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
1823 * Copy the SLP service URL...
1826 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
1829 * Link the SLP service URL into the head of the list
1842 * 'UpdateSLPBrowse()' - Get browsing information via SLP.
1846 UpdateSLPBrowse(void)
1848 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1849 *next
; /* Next service in list */
1850 printer_t p
; /* Printer information */
1851 const char *uri
; /* Pointer to printer URI */
1852 char method
[HTTP_MAX_URI
], /* Method portion of URI */
1853 username
[HTTP_MAX_URI
], /* Username portion of URI */
1854 host
[HTTP_MAX_URI
], /* Host portion of URI */
1855 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1856 int port
; /* Port portion of URI */
1859 LogMessage(L_DEBUG
, "UpdateSLPBrowse() Start...");
1862 * Reset the refresh time...
1865 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1868 * Poll for remote printers using SLP...
1873 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1874 SrvUrlCallback
, &s
);
1877 * Loop through the list of available printers...
1883 * Save the "next" pointer...
1889 * Load a printer_t structure with the SLP service attributes...
1892 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", AttrCallback
, &p
);
1895 * Process this printer entry...
1898 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1900 if (strncmp(uri
, "http://", 7) == 0 ||
1901 strncmp(uri
, "ipp://", 6) == 0)
1904 * Pull the URI apart to see if this is a local or remote printer...
1907 httpSeparate(uri
, method
, username
, host
, &port
, resource
);
1909 if (strcasecmp(host
, ServerName
) == 0)
1913 * OK, at least an IPP printer, see if it is a CUPS printer or
1917 if (strstr(uri
, "/printers/") != NULL
)
1918 ProcessBrowseData(uri
, p
.type
, IPP_PRINTER_IDLE
, p
.location
,
1919 p
.info
, p
.make_model
);
1920 else if (strstr(uri
, "/classes/") != NULL
)
1921 ProcessBrowseData(uri
, p
.type
| CUPS_PRINTER_CLASS
, IPP_PRINTER_IDLE
,
1922 p
.location
, p
.info
, p
.make_model
);
1926 * Free this listing...
1932 LogMessage(L_DEBUG
, "UpdateSLPBrowse() End...");
1934 #endif /* HAVE_LIBSLP */
1938 * End of "$Id: dirsvc.c,v 1.131 2004/05/27 18:04:32 mike Exp $".