]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/dirsvc.c
ececd13e7bc80927d394090edee1924a9aa9f18d
2 * "$Id: dirsvc.c,v 1.73.2.45 2004/02/24 19:04:05 mike Exp $"
4 * Directory services routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2003 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * ProcessBrowseData() - Process new browse data.
27 * SendBrowseList() - Send new browsing information as necessary.
28 * SendCUPSBrowse() - Send new browsing information using the CUPS protocol.
29 * StartBrowsing() - Start sending and receiving broadcast information.
30 * StartPolling() - Start polling servers as needed.
31 * StopBrowsing() - Stop sending and receiving broadcast information.
32 * StopPolling() - Stop polling servers as needed.
33 * UpdateCUPSBrowse() - Update the browse lists using the CUPS protocol.
34 * UpdatePolling() - Read status messages from the poll daemons.
35 * RegReportCallback() - Empty SLPRegReport.
36 * SendSLPBrowse() - Register the specified printer with SLP.
37 * SLPDeregPrinter() - SLPDereg() the specified printer
38 * GetSlpAttrVal() - Get an attribute from an SLP registration.
39 * AttrCallback() - SLP attribute callback
40 * SrvUrlCallback() - SLP service url callback
41 * UpdateSLPBrowse() - Get browsing information via SLP.
45 * Include necessary headers...
53 * 'ProcessBrowseData()' - Process new browse data.
57 ProcessBrowseData(const char *uri
, /* I - URI of printer/class */
58 cups_ptype_t type
, /* I - Printer type */
59 ipp_pstate_t state
, /* I - Printer state */
60 const char *location
,/* I - Printer location */
61 const char *info
, /* I - Printer information */
62 const char *make_model
) /* I - Printer make and model */
64 int i
; /* Looping var */
65 int update
; /* Update printer attributes? */
66 char method
[HTTP_MAX_URI
], /* Method portion of URI */
67 username
[HTTP_MAX_URI
], /* Username portion of URI */
68 host
[HTTP_MAX_URI
], /* Host portion of URI */
69 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
70 int port
; /* Port portion of URI */
71 char name
[IPP_MAX_NAME
], /* Name of printer */
72 *hptr
, /* Pointer into hostname */
73 *sptr
; /* Pointer into ServerName */
74 char local_make_model
[IPP_MAX_NAME
];
75 /* Local make and model */
76 printer_t
*p
, /* Printer information */
77 *pclass
, /* Printer class */
78 *first
, /* First printer in class */
79 *next
; /* Next printer in list */
80 int offset
, /* Offset of name */
81 len
; /* Length of name */
85 * Pull the URI apart to see if this is a local or remote printer...
88 httpSeparate(uri
, method
, username
, host
, &port
, resource
);
91 * Determine if the URI contains any illegal characters in it...
94 if (strncmp(uri
, "ipp://", 6) != 0 ||
96 (strncmp(resource
, "/printers/", 10) != 0 &&
97 strncmp(resource
, "/classes/", 9) != 0))
99 LogMessage(L_ERROR
, "ProcessBrowseData: Bad printer URI in browse data: %s",
104 if (strchr(resource
, '?') != NULL
||
105 (strncmp(resource
, "/printers/", 10) == 0 &&
106 strchr(resource
+ 10, '/') != NULL
) ||
107 (strncmp(resource
, "/classes/", 9) == 0 &&
108 strchr(resource
+ 9, '/') != NULL
))
110 LogMessage(L_ERROR
, "ProcessBrowseData: Bad resource in browse data: %s",
116 * OK, this isn't a local printer; see if we already have it listed in
117 * the Printers list, and add it if not...
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
);
356 * See if we have a default printer... If not, make the first printer the
360 if (DefaultPrinter
== NULL
&& Printers
!= NULL
)
362 DefaultPrinter
= Printers
;
368 * Do auto-classing if needed...
374 * Loop through all available printers and create classes as needed...
377 for (p
= Printers
, len
= 0, offset
= 0, first
= NULL
;
382 * Get next printer in list...
388 * Skip implicit classes...
391 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
398 * If len == 0, get the length of this printer name up to the "@"
403 strncasecmp(p
->name
, name
+ offset
, len
) == 0 &&
404 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
407 * We have more than one printer with the same name; see if
408 * we have a class, and if this printer is a member...
411 if ((pclass
= FindDest(name
)) == NULL
)
414 * Need to add the class...
417 pclass
= AddPrinter(name
);
418 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
419 pclass
->accepting
= 1;
420 pclass
->state
= IPP_PRINTER_IDLE
;
422 SetString(&pclass
->location
, p
->location
);
423 SetString(&pclass
->info
, p
->info
);
425 SetPrinterAttrs(pclass
);
427 LogMessage(L_INFO
, "Added implicit class \"%s\"...", name
);
432 for (i
= 0; i
< pclass
->num_printers
; i
++)
433 if (pclass
->printers
[i
] == first
)
436 if (i
>= pclass
->num_printers
)
437 AddPrinterToClass(pclass
, first
);
442 for (i
= 0; i
< pclass
->num_printers
; i
++)
443 if (pclass
->printers
[i
] == p
)
446 if (i
>= pclass
->num_printers
)
447 AddPrinterToClass(pclass
, p
);
452 * First time around; just get name length and mark it as first
456 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
457 len
= hptr
- p
->name
;
459 len
= strlen(p
->name
);
461 strncpy(name
, p
->name
, len
);
465 if ((pclass
= FindDest(name
)) != NULL
&&
466 !(pclass
->type
& CUPS_PRINTER_IMPLICIT
))
469 * Can't use same name as a local printer; add "Any" to the
470 * front of the name, unless we have explicitly disabled
471 * the "ImplicitAnyClasses"...
474 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
477 * Add "Any" to the class name...
481 strncpy(name
+ 3, p
->name
, len
);
482 name
[len
+ 3] = '\0';
488 * Don't create an implicit class if we have a local printer
489 * with the same name...
505 * 'SendBrowseList()' - Send new browsing information as necessary.
511 int count
; /* Number of dests to update */
512 printer_t
*p
, /* Current printer */
513 *np
; /* Next printer */
514 time_t ut
, /* Minimum update time */
515 to
; /* Timeout time */
518 if (!Browsing
|| !BrowseProtocols
)
522 * Compute the update and timeout times...
525 ut
= time(NULL
) - BrowseInterval
;
526 to
= time(NULL
) - BrowseTimeout
;
529 * Figure out how many printers need an update...
532 if (BrowseInterval
> 0)
534 for (count
= 0, p
= Printers
; p
!= NULL
; p
= p
->next
)
535 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
540 * Throttle the number of printers we'll be updating this time
544 count
= 2 * count
/ BrowseInterval
+ 1;
550 * Loop through all of the printers and send local updates as needed...
553 for (p
= Printers
; p
!= NULL
; p
= np
)
557 if (p
->type
& CUPS_PRINTER_REMOTE
)
560 * See if this printer needs to be timed out...
563 if (p
->browse_time
< to
)
565 LogMessage(L_INFO
, "Remote destination \"%s\" has timed out; deleting it...",
570 else if (p
->browse_time
< ut
&& count
> 0 &&
571 !(p
->type
& CUPS_PRINTER_IMPLICIT
))
574 * Need to send an update...
579 p
->browse_time
= time(NULL
);
581 if (BrowseProtocols
& BROWSE_CUPS
)
585 if (BrowseProtocols
& BROWSE_SLP
)
587 #endif /* HAVE_LIBSLP */
594 * 'SendCUPSBrowse()' - Send new browsing information using the CUPS protocol.
598 SendCUPSBrowse(printer_t
*p
) /* I - Printer to send */
600 int i
; /* Looping var */
601 cups_ptype_t type
; /* Printer type */
602 dirsvc_addr_t
*b
; /* Browse address */
603 int bytes
; /* Length of packet */
604 char packet
[1453]; /* Browse data packet */
605 cups_netif_t
*iface
; /* Network interface */
609 * Figure out the printer type value...
612 type
= p
->type
| CUPS_PRINTER_REMOTE
;
615 type
|= CUPS_PRINTER_REJECTING
;
618 * Send a packet to each browse address...
621 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
625 * Send the browse packet to one or more interfaces...
628 if (strcmp(b
->iface
, "*") == 0)
631 * Send to all local interfaces...
636 for (iface
= NetIFList
; iface
!= NULL
; iface
= iface
->next
)
639 * Only send to local interfaces...
642 if (!iface
->is_local
)
645 snprintf(packet
, sizeof(packet
), "%x %x ipp://%s/%s/%s \"%s\" \"%s\" \"%s\"\n",
646 type
, p
->state
, iface
->hostname
,
647 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
648 p
->name
, p
->location
? p
->location
: "",
649 p
->info
? p
->info
: "",
650 p
->make_model
? p
->make_model
: "Unknown");
652 bytes
= strlen(packet
);
654 LogMessage(L_DEBUG2
, "SendBrowseList: (%d bytes to \"%s\") %s", bytes
,
655 iface
->name
, packet
);
657 if (iface
->broadcast
.addr
.sa_family
== AF_INET
)
659 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
661 sendto(BrowseSocket
, packet
, bytes
, 0,
662 (struct sockaddr
*)&(iface
->broadcast
),
663 sizeof(struct sockaddr_in
));
668 iface
->broadcast
.ipv6
.sin6_port
= htons(BrowsePort
);
670 sendto(BrowseSocket
, packet
, bytes
, 0,
671 (struct sockaddr
*)&(iface
->broadcast
),
672 sizeof(struct sockaddr_in6
));
674 #endif /* AF_INET6 */
677 else if ((iface
= NetIFFind(b
->iface
)) != NULL
)
680 * Send to the named interface...
683 snprintf(packet
, sizeof(packet
), "%x %x ipp://%s/%s/%s \"%s\" \"%s\" \"%s\"\n",
684 type
, p
->state
, iface
->hostname
,
685 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
686 p
->name
, p
->location
? p
->location
: "",
687 p
->info
? p
->info
: "",
688 p
->make_model
? p
->make_model
: "Unknown");
690 bytes
= strlen(packet
);
692 LogMessage(L_DEBUG2
, "SendBrowseList: (%d bytes to \"%s\") %s", bytes
,
693 iface
->name
, packet
);
695 if (iface
->broadcast
.addr
.sa_family
== AF_INET
)
697 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
699 sendto(BrowseSocket
, packet
, bytes
, 0,
700 (struct sockaddr
*)&(iface
->broadcast
),
701 sizeof(struct sockaddr_in
));
706 iface
->broadcast
.ipv6
.sin6_port
= htons(BrowsePort
);
708 sendto(BrowseSocket
, packet
, bytes
, 0,
709 (struct sockaddr
*)&(iface
->broadcast
),
710 sizeof(struct sockaddr_in6
));
712 #endif /* AF_INET6 */
718 * Send the browse packet to the indicated address using
719 * the default server name...
722 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\"\n",
723 type
, p
->state
, p
->uri
,
724 p
->location
? p
->location
: "",
725 p
->info
? p
->info
: "",
726 p
->make_model
? p
->make_model
: "Unknown");
728 bytes
= strlen(packet
);
729 LogMessage(L_DEBUG2
, "SendBrowseList: (%d bytes) %s", bytes
, packet
);
732 if (sendto(BrowseSocket
, packet
, bytes
, 0,
733 (struct sockaddr
*)&(b
->to
),
734 b
->to
.addr
.sa_family
== AF_INET
?
735 sizeof(struct sockaddr_in
) :
736 sizeof(struct sockaddr_in6
)) <= 0)
738 if (sendto(BrowseSocket
, packet
, bytes
, 0,
739 (struct sockaddr
*)&(b
->to
),
740 sizeof(struct sockaddr_in
)) <= 0)
741 #endif /* AF_INET6 */
744 * Unable to send browse packet, so remove this address from the
748 LogMessage(L_ERROR
, "SendBrowseList: sendto failed for browser %d - %s.",
749 b
- Browsers
+ 1, strerror(errno
));
752 memcpy(b
, b
+ 1, (i
- 1) * sizeof(dirsvc_addr_t
));
762 * 'StartBrowsing()' - Start sending and receiving broadcast information.
768 int val
; /* Socket option value */
769 struct sockaddr_in addr
; /* Broadcast address */
772 if (!Browsing
|| !BrowseProtocols
)
775 if (BrowseProtocols
& BROWSE_CUPS
)
778 * Create the broadcast socket...
781 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
783 LogMessage(L_ERROR
, "StartBrowsing: Unable to create broadcast socket - %s.",
785 BrowseProtocols
&= ~BROWSE_CUPS
;
790 * Set the "broadcast" flag...
794 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
796 LogMessage(L_ERROR
, "StartBrowsing: Unable to set broadcast mode - %s.",
800 closesocket(BrowseSocket
);
806 BrowseProtocols
&= ~BROWSE_CUPS
;
811 * Bind the socket to browse port...
814 memset(&addr
, 0, sizeof(addr
));
815 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
816 addr
.sin_family
= AF_INET
;
817 addr
.sin_port
= htons(BrowsePort
);
819 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
821 LogMessage(L_ERROR
, "StartBrowsing: Unable to bind broadcast socket - %s.",
825 closesocket(BrowseSocket
);
831 BrowseProtocols
&= ~BROWSE_CUPS
;
836 * Finally, add the socket to the input selection set...
839 LogMessage(L_DEBUG2
, "StartBrowsing: Adding fd %d to InputSet...",
842 FD_SET(BrowseSocket
, InputSet
);
848 if (BrowseProtocols
& BROWSE_SLP
)
854 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
856 LogMessage(L_ERROR
, "Unable to open an SLP handle; disabling SLP browsing!");
857 BrowseProtocols
&= ~BROWSE_SLP
;
860 BrowseSLPRefresh
= 0;
862 #endif /* HAVE_LIBSLP */
867 * 'StartPolling()' - Start polling servers as needed.
873 int i
; /* Looping var */
874 dirsvc_poll_t
*poll
; /* Current polling server */
875 int pid
; /* New process ID */
876 char sport
[10]; /* Server port */
877 char bport
[10]; /* Browser port */
878 char interval
[10]; /* Poll interval */
879 int statusfds
[2]; /* Status pipe */
880 int fd
; /* Current file descriptor */
881 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
882 struct sigaction action
; /* POSIX signal handler */
883 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
887 * Don't do anything if we aren't polling...
897 * Setup string arguments for port and interval options.
900 sprintf(bport
, "%d", BrowsePort
);
903 sprintf(interval
, "%d", BrowseInterval
);
905 strcpy(interval
, "30");
908 * Create a pipe that receives the status messages from each
914 LogMessage(L_ERROR
, "Unable to create polling status pipes - %s.",
920 PollPipe
= statusfds
[0];
923 * Run each polling daemon, redirecting stderr to the polling pipe...
926 for (i
= 0, poll
= Polled
; i
< NumPolled
; i
++, poll
++)
928 sprintf(sport
, "%d", poll
->port
);
931 * Block signals before forking...
936 if ((pid
= fork()) == 0)
945 * Running as root, so change to non-priviledged user...
951 if (setgroups(1, &Group
))
960 * Reset group membership to just the main one we belong to.
963 setgroups(1, &Group
);
967 * Redirect stdin and stdout to /dev/null, and stderr to the
968 * status pipe. Close all other files.
972 open("/dev/null", O_RDONLY
);
975 open("/dev/null", O_WRONLY
);
980 for (fd
= 3; fd
< MaxFDs
; fd
++)
984 * Unblock signals before doing the exec...
988 sigset(SIGTERM
, SIG_DFL
);
989 sigset(SIGCHLD
, SIG_DFL
);
990 #elif defined(HAVE_SIGACTION)
991 memset(&action
, 0, sizeof(action
));
993 sigemptyset(&action
.sa_mask
);
994 action
.sa_handler
= SIG_DFL
;
996 sigaction(SIGTERM
, &action
, NULL
);
997 sigaction(SIGCHLD
, &action
, NULL
);
999 signal(SIGTERM
, SIG_DFL
);
1000 signal(SIGCHLD
, SIG_DFL
);
1001 #endif /* HAVE_SIGSET */
1006 * Execute the polling daemon...
1009 execl(CUPS_SERVERBIN
"/daemon/cups-polld", "cups-polld", poll
->hostname
,
1010 sport
, interval
, bport
, NULL
);
1015 LogMessage(L_ERROR
, "StartPolling: Unable to fork polling daemon - %s",
1023 LogMessage(L_DEBUG
, "StartPolling: Started polling daemon for %s:%d, pid = %d",
1024 poll
->hostname
, poll
->port
, pid
);
1030 close(statusfds
[1]);
1033 * Finally, add the pipe to the input selection set...
1036 LogMessage(L_DEBUG2
, "StartPolling: Adding fd %d to InputSet...",
1039 FD_SET(PollPipe
, InputSet
);
1044 * 'StopBrowsing()' - Stop sending and receiving broadcast information.
1050 if (!Browsing
|| !BrowseProtocols
)
1053 if (BrowseProtocols
& BROWSE_CUPS
)
1056 * Close the socket and remove it from the input selection set.
1059 if (BrowseSocket
>= 0)
1062 closesocket(BrowseSocket
);
1064 close(BrowseSocket
);
1067 LogMessage(L_DEBUG2
, "StopBrowsing: Removing fd %d from InputSet...",
1070 FD_CLR(BrowseSocket
, InputSet
);
1076 if (BrowseProtocols
& BROWSE_SLP
)
1079 * Close SLP handle...
1082 SLPClose(BrowseSLPHandle
);
1084 #endif /* HAVE_LIBSLP */
1089 * 'StopPolling()' - Stop polling servers as needed.
1095 int i
; /* Looping var */
1096 dirsvc_poll_t
*poll
; /* Current polling server */
1103 LogMessage(L_DEBUG2
, "StopPolling: removing fd %d from InputSet.",
1105 FD_CLR(PollPipe
, InputSet
);
1110 for (i
= 0, poll
= Polled
; i
< NumPolled
; i
++, poll
++)
1112 kill(poll
->pid
, SIGTERM
);
1117 * 'UpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
1121 UpdateCUPSBrowse(void)
1123 int i
; /* Looping var */
1124 int auth
; /* Authorization status */
1125 int len
; /* Length of name string */
1126 int bytes
; /* Number of bytes left */
1127 char packet
[1540], /* Broadcast packet */
1128 *pptr
; /* Pointer into packet */
1129 http_addr_t srcaddr
; /* Source address */
1130 char srcname
[1024]; /* Source hostname */
1131 unsigned address
[4], /* Source address */
1132 temp
; /* Temporary address var (host order) */
1133 cups_ptype_t type
; /* Printer type */
1134 ipp_pstate_t state
; /* Printer state */
1135 char uri
[HTTP_MAX_URI
], /* Printer URI */
1136 method
[HTTP_MAX_URI
], /* Method portion of URI */
1137 username
[HTTP_MAX_URI
], /* Username portion of URI */
1138 host
[HTTP_MAX_URI
], /* Host portion of URI */
1139 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
1140 info
[IPP_MAX_NAME
], /* Information string */
1141 location
[IPP_MAX_NAME
], /* Location string */
1142 make_model
[IPP_MAX_NAME
];/* Make and model string */
1143 int port
; /* Port portion of URI */
1144 cups_netif_t
*iface
; /* Network interface */
1148 * Read a packet from the browse socket...
1151 len
= sizeof(srcaddr
);
1152 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
), 0,
1153 (struct sockaddr
*)&srcaddr
, &len
)) <= 0)
1156 * "Connection refused" is returned under Linux if the destination port
1157 * or address is unreachable from a previous sendto(); check for the
1158 * error here and ignore it for now...
1161 if (errno
!= ECONNREFUSED
)
1163 LogMessage(L_ERROR
, "Browse recv failed - %s.", strerror(errno
));
1164 LogMessage(L_ERROR
, "Browsing turned off.");
1173 packet
[bytes
] = '\0';
1176 * Figure out where it came from...
1180 if (srcaddr
.addr
.sa_family
== AF_INET6
)
1182 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
1183 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
1184 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
1185 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
1188 #endif /* AF_INET6 */
1190 temp
= ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
1192 address
[3] = temp
& 255;
1194 address
[2] = temp
& 255;
1196 address
[1] = temp
& 255;
1198 address
[0] = temp
& 255;
1201 if (HostNameLookups
)
1202 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
1204 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
1206 len
= strlen(srcname
);
1212 if (BrowseACL
&& (BrowseACL
->num_allow
|| BrowseACL
->num_deny
))
1214 if (httpAddrLocalhost(&srcaddr
) || strcasecmp(srcname
, "localhost") == 0)
1217 * Access from localhost (127.0.0.1) is always allowed...
1225 * Do authorization checks on the domain/address...
1228 switch (BrowseACL
->order_type
)
1231 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1234 case AUTH_ALLOW
: /* Order Deny,Allow */
1237 if (CheckAuth(address
, srcname
, len
,
1238 BrowseACL
->num_deny
, BrowseACL
->deny
))
1241 if (CheckAuth(address
, srcname
, len
,
1242 BrowseACL
->num_allow
, BrowseACL
->allow
))
1246 case AUTH_DENY
: /* Order Allow,Deny */
1249 if (CheckAuth(address
, srcname
, len
,
1250 BrowseACL
->num_allow
, BrowseACL
->allow
))
1253 if (CheckAuth(address
, srcname
, len
,
1254 BrowseACL
->num_deny
, BrowseACL
->deny
))
1263 if (auth
== AUTH_DENY
)
1265 LogMessage(L_DEBUG
, "UpdateCUPSBrowse: Refused %d bytes from %s", bytes
,
1270 LogMessage(L_DEBUG2
, "UpdateCUPSBrowse: (%d bytes from %s) %s", bytes
, srcname
,
1277 if (sscanf(packet
, "%x%x%1023s", (unsigned *)&type
, (unsigned *)&state
,
1280 LogMessage(L_WARN
, "UpdateCUPSBrowse: Garbled browse packet - %s",
1285 strcpy(location
, "Location Unknown");
1286 strcpy(info
, "No Information Available");
1287 make_model
[0] = '\0';
1289 if ((pptr
= strchr(packet
, '\"')) != NULL
)
1292 * Have extended information; can't use sscanf for it because not all
1293 * sscanf's allow empty strings with %[^\"]...
1296 for (i
= 0, pptr
++;
1297 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
1299 location
[i
] = *pptr
;
1307 while (*pptr
&& isspace(*pptr
))
1312 for (i
= 0, pptr
++;
1313 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
1323 while (*pptr
&& isspace(*pptr
))
1328 for (i
= 0, pptr
++;
1329 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
1331 make_model
[i
] = *pptr
;
1334 make_model
[i
] = '\0';
1340 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
1341 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
1342 type
, state
, uri
, location
, info
, make_model
));
1345 * Pull the URI apart to see if this is a local or remote printer...
1348 httpSeparate(uri
, method
, username
, host
, &port
, resource
);
1350 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1353 * Check for packets from the local server...
1356 if (strcasecmp(host
, ServerName
) == 0)
1361 for (iface
= NetIFList
; iface
!= NULL
; iface
= iface
->next
)
1362 if (strcasecmp(host
, iface
->hostname
) == 0)
1369 for (i
= 0; i
< NumRelays
; i
++)
1370 if (CheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
1371 if (sendto(BrowseSocket
, packet
, bytes
, 0,
1372 (struct sockaddr
*)&(Relays
[i
].to
),
1373 sizeof(http_addr_t
)) <= 0)
1375 LogMessage(L_ERROR
, "UpdateCUPSBrowse: sendto failed for relay %d - %s.",
1376 i
+ 1, strerror(errno
));
1381 * Process the browse data...
1384 ProcessBrowseData(uri
, type
, state
, location
, info
, make_model
);
1389 * 'UpdatePolling()' - Read status messages from the poll daemons.
1395 int bytes
; /* Number of bytes read */
1396 char *lineptr
; /* Pointer to end of line in buffer */
1397 static int bufused
= 0; /* Number of bytes used in buffer */
1398 static char buffer
[1024]; /* Status buffer */
1401 if ((bytes
= read(PollPipe
, buffer
+ bufused
,
1402 sizeof(buffer
) - bufused
- 1)) > 0)
1405 buffer
[bufused
] = '\0';
1406 lineptr
= strchr(buffer
, '\n');
1408 else if (bytes
< 0 && errno
== EINTR
)
1412 lineptr
= buffer
+ bufused
;
1416 if (bytes
== 0 && bufused
== 0)
1419 while (lineptr
!= NULL
)
1422 * Terminate each line and process it...
1427 if (!strncmp(buffer
, "ERROR: ", 7))
1428 LogMessage(L_ERROR
, "%s", buffer
+ 7);
1429 else if (!strncmp(buffer
, "DEBUG: ", 7))
1430 LogMessage(L_DEBUG
, "%s", buffer
+ 7);
1431 else if (!strncmp(buffer
, "DEBUG2: ", 8))
1432 LogMessage(L_DEBUG2
, "%s", buffer
+ 8);
1434 LogMessage(L_DEBUG
, "%s", buffer
);
1437 * Copy over the buffer data we've used up...
1440 cups_strcpy(buffer
, lineptr
);
1441 bufused
-= lineptr
- buffer
;
1446 lineptr
= strchr(buffer
, '\n');
1452 * All polling processes have died; stop polling...
1455 LogMessage(L_ERROR
, "UpdatePolling: all polling processes have exited!");
1461 /***********************************************************************
1462 **** SLP Support Code *************************************************
1463 ***********************************************************************/
1467 * SLP service name for CUPS...
1470 # define SLP_CUPS_SRVTYPE "service:printer"
1471 # define SLP_CUPS_SRVLEN 15
1473 typedef struct _slpsrvurl
1475 struct _slpsrvurl
*next
;
1476 char url
[HTTP_MAX_URI
];
1481 * 'RegReportCallback()' - Empty SLPRegReport.
1485 RegReportCallback(SLPHandle hslp
,
1498 * 'SendSLPBrowse()' - Register the specified printer with SLP.
1502 SendSLPBrowse(printer_t
*p
) /* I - Printer to register */
1504 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
1505 attrs
[8192], /* Printer attributes */
1506 finishings
[1024], /* Finishings to support */
1507 make_model
[IPP_MAX_NAME
* 2],
1508 /* Make and model, quoted */
1509 location
[IPP_MAX_NAME
* 2],
1510 /* Location, quoted */
1511 info
[IPP_MAX_NAME
* 2],
1513 *src
, /* Pointer to original string */
1514 *dst
; /* Pointer to destination string */
1515 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
1516 SLPError error
; /* SLP error, if any */
1519 LogMessage(L_DEBUG
, "SendSLPBrowse(%p = \"%s\")", p
, p
->name
);
1522 * Make the SLP service URL that conforms to the IANA
1523 * 'printer:' template.
1526 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
1528 LogMessage(L_DEBUG2
, "Service URL = \"%s\"", srvurl
);
1531 * Figure out the finishings string...
1534 if (p
->type
& CUPS_PRINTER_STAPLE
)
1535 strcpy(finishings
, "staple");
1537 finishings
[0] = '\0';
1539 if (p
->type
& CUPS_PRINTER_BIND
)
1542 strlcat(finishings
, ",bind", sizeof(finishings
));
1544 strcpy(finishings
, "bind");
1547 if (p
->type
& CUPS_PRINTER_PUNCH
)
1550 strlcat(finishings
, ",punch", sizeof(finishings
));
1552 strcpy(finishings
, "punch");
1555 if (p
->type
& CUPS_PRINTER_COVER
)
1558 strlcat(finishings
, ",cover", sizeof(finishings
));
1560 strcpy(finishings
, "cover");
1563 if (p
->type
& CUPS_PRINTER_SORT
)
1566 strlcat(finishings
, ",sort", sizeof(finishings
));
1568 strcpy(finishings
, "sort");
1572 strcpy(finishings
, "none");
1575 * Quote any commas in the make and model, location, and info strings...
1578 for (src
= p
->make_model
, dst
= make_model
;
1579 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
1581 if (*src
== ',' || *src
== '\\' || *src
== ')')
1590 strcpy(make_model
, "Unknown");
1592 for (src
= p
->location
, dst
= location
;
1593 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
1595 if (*src
== ',' || *src
== '\\' || *src
== ')')
1604 strcpy(location
, "Unknown");
1606 for (src
= p
->info
, dst
= info
;
1607 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
1609 if (*src
== ',' || *src
== '\\' || *src
== ')')
1618 strcpy(info
, "Unknown");
1621 * Get the authentication value...
1624 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
1628 * Make the SLP attribute string list that conforms to
1629 * the IANA 'printer:' template.
1632 snprintf(attrs
, sizeof(attrs
),
1633 "(printer-uri-supported=%s),"
1634 "(uri-authentication-supported=%s>),"
1636 "(uri-security-supported=tls>),"
1638 "(uri-security-supported=none>),"
1639 #endif /* HAVE_SSL */
1640 "(printer-name=%s),"
1641 "(printer-location=%s),"
1642 "(printer-info=%s),"
1643 "(printer-more-info=%s),"
1644 "(printer-make-and-model=%s),"
1645 "(charset-supported=utf-8),"
1646 "(natural-language-configured=%s),"
1647 "(natural-language-supported=de,en,es,fr,it),"
1648 "(color-supported=%s),"
1649 "(finishings-supported=%s),"
1650 "(sides-supported=one-sided%s),"
1651 "(multiple-document-jobs-supported=true)"
1652 "(ipp-versions-supported=1.0,1.1)",
1653 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
1654 info
, p
->uri
, make_model
, DefaultLanguage
,
1655 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
1657 p
->type
& CUPS_PRINTER_DUPLEX
?
1658 ",two-sided-long-edge,two-sided-short-edge" : "");
1660 LogMessage(L_DEBUG2
, "Attributes = \"%s\"", attrs
);
1663 * Register the printer with the SLP server...
1666 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
1667 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, RegReportCallback
, 0);
1669 if (error
!= SLP_OK
)
1670 LogMessage(L_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
1676 * 'SLPDeregPrinter()' - SLPDereg() the specified printer
1680 SLPDeregPrinter(printer_t
*p
)
1682 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
1685 if((p
->type
& CUPS_PRINTER_REMOTE
) == 0)
1688 * Make the SLP service URL that conforms to the IANA
1689 * 'printer:' template.
1692 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
1695 * Deregister the printer...
1698 SLPDereg(BrowseSLPHandle
, srvurl
, RegReportCallback
, 0);
1704 * 'GetSlpAttrVal()' - Get an attribute from an SLP registration.
1707 int /* O - 0 on success */
1708 GetSlpAttrVal(const char *attrlist
, /* I - Attribute list string */
1709 const char *tag
, /* I - Name of attribute */
1710 char **valbuf
) /* O - Value */
1712 char *ptr1
, /* Pointer into string */
1716 ClearString(valbuf
);
1718 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
1720 ptr1
+= strlen(tag
);
1722 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
1728 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
1729 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
1732 * Dequote the value...
1735 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
1736 if (*ptr1
== '\\' && ptr1
[1])
1737 cups_strcpy(ptr1
, ptr1
+ 1);
1748 * 'AttrCallback()' - SLP attribute callback
1751 SLPBoolean
/* O - SLP_TRUE for success */
1752 AttrCallback(SLPHandle hslp
, /* I - SLP handle */
1753 const char *attrlist
, /* I - Attribute list */
1754 SLPError errcode
, /* I - Parsing status for this attr */
1755 void *cookie
) /* I - Current printer */
1758 printer_t
*p
= (printer_t
*)cookie
;
1762 * Let the compiler know we won't be using these...
1768 * Bail if there was an error
1771 if (errcode
!= SLP_OK
)
1775 * Parse the attrlist to obtain things needed to build CUPS browse packet
1778 memset(p
, 0, sizeof(printer_t
));
1780 p
->type
= CUPS_PRINTER_REMOTE
;
1782 if (GetSlpAttrVal(attrlist
, "(printer-location=", &(p
->location
)))
1784 if (GetSlpAttrVal(attrlist
, "(printer-info=", &(p
->info
)))
1786 if (GetSlpAttrVal(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
1789 if (GetSlpAttrVal(attrlist
, "(color-supported=", &tmp
))
1791 if (strcasecmp(tmp
, "true") == 0)
1792 p
->type
|= CUPS_PRINTER_COLOR
;
1794 if (GetSlpAttrVal(attrlist
, "(finishings-supported=", &tmp
))
1796 if (strstr(tmp
, "staple"))
1797 p
->type
|= CUPS_PRINTER_STAPLE
;
1798 if (strstr(tmp
, "bind"))
1799 p
->type
|= CUPS_PRINTER_BIND
;
1800 if (strstr(tmp
, "punch"))
1801 p
->type
|= CUPS_PRINTER_PUNCH
;
1803 if (GetSlpAttrVal(attrlist
, "(sides-supported=", &tmp
))
1805 if (strstr(tmp
,"two-sided"))
1806 p
->type
|= CUPS_PRINTER_DUPLEX
;
1815 * 'SrvUrlCallback()' - SLP service url callback
1818 SLPBoolean
/* O - TRUE = OK, FALSE = error */
1819 SrvUrlCallback(SLPHandle hslp
, /* I - SLP handle */
1820 const char *srvurl
, /* I - URL of service */
1821 unsigned short lifetime
, /* I - Life of service */
1822 SLPError errcode
, /* I - Existing error code */
1823 void *cookie
) /* I - Pointer to service list */
1825 slpsrvurl_t
*s
, /* New service entry */
1826 **head
; /* Pointer to head of entry */
1830 * Let the compiler know we won't be using these vars...
1837 * Bail if there was an error
1840 if (errcode
!= SLP_OK
)
1844 * Grab the head of the list...
1847 head
= (slpsrvurl_t
**)cookie
;
1850 * Allocate a *temporary* slpsrvurl_t to hold this entry.
1853 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
1857 * Copy the SLP service URL...
1860 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
1863 * Link the SLP service URL into the head of the list
1876 * 'UpdateSLPBrowse()' - Get browsing information via SLP.
1880 UpdateSLPBrowse(void)
1882 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1883 *next
; /* Next service in list */
1884 printer_t p
; /* Printer information */
1885 const char *uri
; /* Pointer to printer URI */
1886 char method
[HTTP_MAX_URI
], /* Method portion of URI */
1887 username
[HTTP_MAX_URI
], /* Username portion of URI */
1888 host
[HTTP_MAX_URI
], /* Host portion of URI */
1889 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1890 int port
; /* Port portion of URI */
1893 LogMessage(L_DEBUG
, "UpdateSLPBrowse() Start...");
1896 * Reset the refresh time...
1899 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1902 * Poll for remote printers using SLP...
1907 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1908 SrvUrlCallback
, &s
);
1911 * Loop through the list of available printers...
1917 * Save the "next" pointer...
1923 * Load a printer_t structure with the SLP service attributes...
1926 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", AttrCallback
, &p
);
1929 * Process this printer entry...
1932 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1934 if (strncmp(uri
, "http://", 7) == 0 ||
1935 strncmp(uri
, "ipp://", 6) == 0)
1938 * Pull the URI apart to see if this is a local or remote printer...
1941 httpSeparate(uri
, method
, username
, host
, &port
, resource
);
1943 if (strcasecmp(host
, ServerName
) == 0)
1947 * OK, at least an IPP printer, see if it is a CUPS printer or
1951 if (strstr(uri
, "/printers/") != NULL
)
1952 ProcessBrowseData(uri
, p
.type
, IPP_PRINTER_IDLE
, p
.location
,
1953 p
.info
, p
.make_model
);
1954 else if (strstr(uri
, "/classes/") != NULL
)
1955 ProcessBrowseData(uri
, p
.type
| CUPS_PRINTER_CLASS
, IPP_PRINTER_IDLE
,
1956 p
.location
, p
.info
, p
.make_model
);
1960 * Free this listing...
1966 LogMessage(L_DEBUG
, "UpdateSLPBrowse() End...");
1968 #endif /* HAVE_LIBSLP */
1972 * End of "$Id: dirsvc.c,v 1.73.2.45 2004/02/24 19:04:05 mike Exp $".