]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
b19ccc9e | 2 | * "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $" |
ef416fc2 | 3 | * |
4 | * Directory services routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
080811b1 | 6 | * Copyright 2007-2008 by Apple Inc. |
b86bc4cf | 7 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | * |
15 | * Contents: | |
16 | * | |
c5571a1d MS |
17 | * cupsdDeregisterPrinter() - Stop sending broadcast information for a local |
18 | * printer and remove any pending references to | |
19 | * remote printers. | |
20 | * cupsdLoadRemoteCache() - Load the remote printer cache. | |
21 | * cupsdRegisterPrinter() - Start sending broadcast information for a | |
22 | * printer or update the broadcast contents. | |
23 | * cupsdRestartPolling() - Restart polling servers as needed. | |
24 | * cupsdSaveRemoteCache() - Save the remote printer cache. | |
25 | * cupsdSendBrowseList() - Send new browsing information as necessary. | |
26 | * ldap_rebind_proc() - Callback function for LDAP rebind | |
27 | * ldap_connect() - Start new LDAP connection | |
28 | * ldap_reconnect() - Reconnect to LDAP Server | |
29 | * ldap_disconnect() - Disconnect from LDAP Server | |
30 | * cupsdStartBrowsing() - Start sending and receiving broadcast | |
31 | * information. | |
32 | * cupsdStartPolling() - Start polling servers as needed. | |
33 | * cupsdStopBrowsing() - Stop sending and receiving broadcast | |
34 | * information. | |
35 | * cupsdStopPolling() - Stop polling servers as needed. | |
36 | * cupsdUpdateDNSSDName() - Update the computer name we use for | |
37 | * browsing... | |
38 | * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP... | |
39 | * cupsdUpdateSLPBrowse() - Get browsing information via SLP. | |
40 | * dequote() - Remote quotes from a string. | |
41 | * dnssdBuildTxtRecord() - Build a TXT record from printer info. | |
42 | * dnssdComparePrinters() - Compare the registered names of two printers. | |
43 | * dnssdDeregisterPrinter() - Stop sending broadcast information for a | |
44 | * printer. | |
45 | * dnssdPackTxtRecord() - Pack an array of key/value pairs into the TXT | |
46 | * record format. | |
47 | * dnssdRegisterCallback() - DNSServiceRegister callback. | |
48 | * dnssdRegisterPrinter() - Start sending broadcast information for a | |
49 | * printer or update the broadcast contents. | |
50 | * dnssdUpdate() - Handle DNS-SD queries. | |
51 | * get_hostconfig() - Get an /etc/hostconfig service setting. | |
52 | * is_local_queue() - Determine whether the URI points at a local | |
53 | * queue. | |
54 | * process_browse_data() - Process new browse data. | |
55 | * process_implicit_classes() - Create/update implicit classes as needed. | |
56 | * send_cups_browse() - Send new browsing information using the CUPS | |
57 | * protocol. | |
58 | * ldap_search_rec() - LDAP Search with reconnect | |
59 | * ldap_freeres() - Free LDAPMessage | |
60 | * ldap_getval_char() - Get first LDAP value and convert to string | |
61 | * send_ldap_ou() - Send LDAP ou registrations. | |
62 | * send_ldap_browse() - Send LDAP printer registrations. | |
63 | * ldap_dereg_printer() - Delete printer from directory | |
64 | * send_slp_browse() - Register the specified printer with SLP. | |
65 | * slp_attr_callback() - SLP attribute callback | |
66 | * slp_dereg_printer() - SLPDereg() the specified printer | |
67 | * slp_get_attr() - Get an attribute from an SLP registration. | |
68 | * slp_reg_callback() - Empty SLPRegReport. | |
69 | * slp_url_callback() - SLP service url callback | |
70 | * update_cups_browse() - Update the browse lists using the CUPS | |
71 | * protocol. | |
72 | * update_lpd() - Update the LPD configuration as needed. | |
73 | * update_polling() - Read status messages from the poll daemons. | |
74 | * update_smb() - Update the SMB configuration as needed. | |
ef416fc2 | 75 | */ |
76 | ||
77 | /* | |
78 | * Include necessary headers... | |
79 | */ | |
80 | ||
81 | #include "cupsd.h" | |
82 | #include <grp.h> | |
83 | ||
f7deaa1a | 84 | #ifdef HAVE_DNSSD |
85 | # include <dns_sd.h> | |
cc0d019f MS |
86 | # ifdef __APPLE__ |
87 | # include <nameser.h> | |
88 | # ifdef HAVE_COREFOUNDATION | |
89 | # include <CoreFoundation/CoreFoundation.h> | |
90 | # endif /* HAVE_COREFOUNDATION */ | |
91 | # ifdef HAVE_SYSTEMCONFIGURATION | |
92 | # include <SystemConfiguration/SystemConfiguration.h> | |
93 | # endif /* HAVE_SYSTEMCONFIGURATION */ | |
94 | # endif /* __APPLE__ */ | |
f7deaa1a | 95 | #endif /* HAVE_DNSSD */ |
96 | ||
ef416fc2 | 97 | |
98 | /* | |
e00b005a | 99 | * Local functions... |
ef416fc2 | 100 | */ |
101 | ||
e00b005a | 102 | static char *dequote(char *d, const char *s, int dlen); |
a603edef MS |
103 | #ifdef __APPLE__ |
104 | static int get_hostconfig(const char *name); | |
105 | #endif /* __APPLE__ */ | |
b423cd4c | 106 | static int is_local_queue(const char *uri, char *host, int hostlen, |
107 | char *resource, int resourcelen); | |
108 | static void process_browse_data(const char *uri, const char *host, | |
109 | const char *resource, cups_ptype_t type, | |
e00b005a | 110 | ipp_pstate_t state, const char *location, |
111 | const char *info, const char *make_model, | |
112 | int num_attrs, cups_option_t *attrs); | |
3dfe78b3 | 113 | static void process_implicit_classes(void); |
e1d6a774 | 114 | static void send_cups_browse(cupsd_printer_t *p); |
115 | #ifdef HAVE_LDAP | |
749b1e90 MS |
116 | static LDAP *ldap_connect(void); |
117 | static void ldap_reconnect(void); | |
118 | static void ldap_disconnect(LDAP *ld); | |
119 | static int ldap_search_rec(LDAP *ld, char *base, int scope, | |
120 | char *filter, char *attrs[], | |
121 | int attrsonly, LDAPMessage **res); | |
122 | static int ldap_getval_firststring(LDAP *ld, LDAPMessage *entry, | |
123 | char *attr, char *retval, | |
124 | unsigned long maxsize); | |
125 | static void ldap_freeres(LDAPMessage *entry); | |
126 | static void send_ldap_ou(char *ou, char *basedn, char *descstring); | |
e1d6a774 | 127 | static void send_ldap_browse(cupsd_printer_t *p); |
749b1e90 MS |
128 | static void ldap_dereg_printer(cupsd_printer_t *p); |
129 | static void ldap_dereg_ou(char *ou, char *basedn); | |
130 | # ifdef HAVE_LDAP_REBIND_PROC | |
131 | # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) | |
132 | static int ldap_rebind_proc(LDAP *RebindLDAPHandle, | |
133 | LDAP_CONST char *refsp, | |
134 | ber_tag_t request, | |
135 | ber_int_t msgid, | |
136 | void *params); | |
137 | # else | |
138 | static int ldap_rebind_proc(LDAP *RebindLDAPHandle, | |
139 | char **dnp, | |
140 | char **passwdp, | |
141 | int *authmethodp, | |
142 | int freeit, | |
143 | void *arg); | |
144 | # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */ | |
145 | # endif /* HAVE_LDAP_REBIND_PROC */ | |
e1d6a774 | 146 | #endif /* HAVE_LDAP */ |
80ca4592 | 147 | #ifdef HAVE_LIBSLP |
e1d6a774 | 148 | static void send_slp_browse(cupsd_printer_t *p); |
80ca4592 | 149 | #endif /* HAVE_LIBSLP */ |
f899b121 | 150 | static void update_cups_browse(void); |
2e4ff8af | 151 | static void update_lpd(int onoff); |
f899b121 | 152 | static void update_polling(void); |
2e4ff8af | 153 | static void update_smb(int onoff); |
f899b121 | 154 | |
e00b005a | 155 | |
7a14d768 MS |
156 | #ifdef HAVE_DNSSD |
157 | static char *dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p, | |
158 | int for_lpd); | |
159 | static int dnssdComparePrinters(cupsd_printer_t *a, cupsd_printer_t *b); | |
160 | static void dnssdDeregisterPrinter(cupsd_printer_t *p); | |
161 | static char *dnssdPackTxtRecord(int *txt_len, char *keyvalue[][2], | |
162 | int count); | |
163 | static void dnssdRegisterCallback(DNSServiceRef sdRef, | |
164 | DNSServiceFlags flags, | |
165 | DNSServiceErrorType errorCode, | |
166 | const char *name, const char *regtype, | |
167 | const char *domain, void *context); | |
168 | static void dnssdRegisterPrinter(cupsd_printer_t *p); | |
75bd9771 | 169 | static void dnssdUpdate(void); |
7a14d768 MS |
170 | #endif /* HAVE_DNSSD */ |
171 | ||
749b1e90 | 172 | #ifdef HAVE_LDAP |
b423cd4c | 173 | static const char * const ldap_attrs[] =/* CUPS LDAP attributes */ |
174 | { | |
175 | "printerDescription", | |
176 | "printerLocation", | |
177 | "printerMakeAndModel", | |
178 | "printerType", | |
179 | "printerURI", | |
180 | NULL | |
181 | }; | |
749b1e90 | 182 | #endif /* HAVE_LDAP */ |
b423cd4c | 183 | |
ef416fc2 | 184 | #ifdef HAVE_LIBSLP |
e00b005a | 185 | /* |
186 | * SLP definitions... | |
187 | */ | |
188 | ||
ef416fc2 | 189 | /* |
190 | * SLP service name for CUPS... | |
191 | */ | |
192 | ||
193 | # define SLP_CUPS_SRVTYPE "service:printer" | |
194 | # define SLP_CUPS_SRVLEN 15 | |
195 | ||
196 | ||
197 | /* | |
198 | * Printer service URL structure | |
199 | */ | |
200 | ||
201 | typedef struct _slpsrvurl_s /**** SLP URL list ****/ | |
202 | { | |
203 | struct _slpsrvurl_s *next; /* Next URL in list */ | |
204 | char url[HTTP_MAX_URI]; | |
205 | /* URL */ | |
206 | } slpsrvurl_t; | |
207 | ||
208 | ||
209 | /* | |
210 | * Local functions... | |
211 | */ | |
212 | ||
213 | static SLPBoolean slp_attr_callback(SLPHandle hslp, const char *attrlist, | |
214 | SLPError errcode, void *cookie); | |
215 | static void slp_dereg_printer(cupsd_printer_t *p); | |
216 | static int slp_get_attr(const char *attrlist, const char *tag, | |
217 | char **valbuf); | |
218 | static void slp_reg_callback(SLPHandle hslp, SLPError errcode, | |
219 | void *cookie); | |
220 | static SLPBoolean slp_url_callback(SLPHandle hslp, const char *srvurl, | |
221 | unsigned short lifetime, | |
222 | SLPError errcode, void *cookie); | |
223 | #endif /* HAVE_LIBSLP */ | |
224 | ||
f7deaa1a | 225 | |
226 | /* | |
227 | * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a | |
228 | * local printer and remove any pending | |
229 | * references to remote printers. | |
230 | */ | |
231 | ||
232 | void | |
233 | cupsdDeregisterPrinter( | |
234 | cupsd_printer_t *p, /* I - Printer to register */ | |
235 | int removeit) /* I - Printer being permanently removed */ | |
236 | { | |
237 | /* | |
7a14d768 | 238 | * Only deregister if browsing is enabled and it's a local printer... |
f7deaa1a | 239 | */ |
240 | ||
241 | if (!Browsing || !p->shared || | |
09a101d6 | 242 | (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))) |
f7deaa1a | 243 | return; |
244 | ||
245 | /* | |
246 | * Announce the deletion... | |
247 | */ | |
248 | ||
0a682745 | 249 | if ((BrowseLocalProtocols & BROWSE_CUPS) && BrowseSocket >= 0) |
f7deaa1a | 250 | { |
251 | cups_ptype_t savedtype = p->type; /* Saved printer type */ | |
252 | ||
253 | p->type |= CUPS_PRINTER_DELETE; | |
254 | ||
255 | send_cups_browse(p); | |
256 | ||
257 | p->type = savedtype; | |
258 | } | |
259 | ||
260 | #ifdef HAVE_LIBSLP | |
261 | if (BrowseLocalProtocols & BROWSE_SLP) | |
262 | slp_dereg_printer(p); | |
263 | #endif /* HAVE_LIBSLP */ | |
264 | ||
749b1e90 MS |
265 | #ifdef HAVE_LDAP |
266 | if (BrowseLocalProtocols & BROWSE_LDAP) | |
267 | ldap_dereg_printer(p); | |
268 | #endif /* HAVE_LDAP */ | |
269 | ||
f7deaa1a | 270 | #ifdef HAVE_DNSSD |
bdd6c45b | 271 | if (removeit && (BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef) |
f7deaa1a | 272 | dnssdDeregisterPrinter(p); |
273 | #endif /* HAVE_DNSSD */ | |
274 | } | |
275 | ||
ef416fc2 | 276 | |
277 | /* | |
278 | * 'cupsdLoadRemoteCache()' - Load the remote printer cache. | |
279 | */ | |
280 | ||
281 | void | |
282 | cupsdLoadRemoteCache(void) | |
283 | { | |
284 | cups_file_t *fp; /* remote.cache file */ | |
285 | int linenum; /* Current line number */ | |
286 | char line[1024], /* Line from file */ | |
287 | *value, /* Pointer to value */ | |
bd7854cb | 288 | *valueptr, /* Pointer into value */ |
289 | scheme[32], /* Scheme portion of URI */ | |
290 | username[64], /* Username portion of URI */ | |
291 | host[HTTP_MAX_HOST], | |
292 | /* Hostname portion of URI */ | |
293 | resource[HTTP_MAX_URI]; | |
294 | /* Resource portion of URI */ | |
295 | int port; /* Port number */ | |
ef416fc2 | 296 | cupsd_printer_t *p; /* Current printer */ |
297 | time_t now; /* Current time */ | |
298 | ||
299 | ||
411affcf | 300 | /* |
7a14d768 | 301 | * Don't load the cache if the remote protocols are disabled... |
411affcf | 302 | */ |
303 | ||
7a14d768 | 304 | if (!Browsing) |
411affcf | 305 | { |
306 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
307 | "cupsdLoadRemoteCache: Not loading remote cache."); | |
308 | return; | |
309 | } | |
310 | ||
ef416fc2 | 311 | /* |
312 | * Open the remote.cache file... | |
313 | */ | |
314 | ||
315 | snprintf(line, sizeof(line), "%s/remote.cache", CacheDir); | |
316 | if ((fp = cupsFileOpen(line, "r")) == NULL) | |
317 | return; | |
318 | ||
319 | /* | |
320 | * Read printer configurations until we hit EOF... | |
321 | */ | |
322 | ||
323 | linenum = 0; | |
324 | p = NULL; | |
325 | now = time(NULL); | |
326 | ||
327 | while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) | |
328 | { | |
329 | /* | |
330 | * Decode the directive... | |
331 | */ | |
332 | ||
333 | if (!strcasecmp(line, "<Printer") || | |
334 | !strcasecmp(line, "<DefaultPrinter")) | |
335 | { | |
336 | /* | |
337 | * <Printer name> or <DefaultPrinter name> | |
338 | */ | |
339 | ||
340 | if (p == NULL && value) | |
341 | { | |
342 | /* | |
343 | * Add the printer and a base file type... | |
344 | */ | |
345 | ||
346 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
347 | "cupsdLoadRemoteCache: Loading printer %s...", value); | |
348 | ||
bd7854cb | 349 | if ((p = cupsdFindDest(value)) != NULL) |
350 | { | |
351 | if (p->type & CUPS_PRINTER_CLASS) | |
352 | { | |
353 | cupsdLogMessage(CUPSD_LOG_WARN, | |
354 | "Cached remote printer \"%s\" conflicts with " | |
355 | "existing class!", | |
356 | value); | |
357 | p = NULL; | |
358 | continue; | |
359 | } | |
360 | } | |
361 | else | |
362 | p = cupsdAddPrinter(value); | |
363 | ||
8ca02f3c | 364 | p->accepting = 1; |
365 | p->state = IPP_PRINTER_IDLE; | |
09a101d6 | 366 | p->type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED; |
8ca02f3c | 367 | p->browse_time = now; |
368 | p->browse_expire = now + BrowseTimeout; | |
ef416fc2 | 369 | |
370 | /* | |
371 | * Set the default printer as needed... | |
372 | */ | |
373 | ||
374 | if (!strcasecmp(line, "<DefaultPrinter")) | |
375 | DefaultPrinter = p; | |
376 | } | |
377 | else | |
378 | { | |
379 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
380 | "Syntax error on line %d of remote.cache.", linenum); | |
91c84a35 | 381 | break; |
ef416fc2 | 382 | } |
383 | } | |
384 | else if (!strcasecmp(line, "<Class") || | |
385 | !strcasecmp(line, "<DefaultClass")) | |
386 | { | |
387 | /* | |
388 | * <Class name> or <DefaultClass name> | |
389 | */ | |
390 | ||
391 | if (p == NULL && value) | |
392 | { | |
393 | /* | |
394 | * Add the printer and a base file type... | |
395 | */ | |
396 | ||
397 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
398 | "cupsdLoadRemoteCache: Loading class %s...", value); | |
399 | ||
bd7854cb | 400 | if ((p = cupsdFindDest(value)) != NULL) |
401 | p->type = CUPS_PRINTER_CLASS; | |
402 | else | |
403 | p = cupsdAddClass(value); | |
404 | ||
8ca02f3c | 405 | p->accepting = 1; |
406 | p->state = IPP_PRINTER_IDLE; | |
09a101d6 | 407 | p->type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED; |
8ca02f3c | 408 | p->browse_time = now; |
409 | p->browse_expire = now + BrowseTimeout; | |
ef416fc2 | 410 | |
411 | /* | |
412 | * Set the default printer as needed... | |
413 | */ | |
414 | ||
415 | if (!strcasecmp(line, "<DefaultClass")) | |
416 | DefaultPrinter = p; | |
417 | } | |
418 | else | |
419 | { | |
420 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
421 | "Syntax error on line %d of remote.cache.", linenum); | |
91c84a35 | 422 | break; |
ef416fc2 | 423 | } |
424 | } | |
425 | else if (!strcasecmp(line, "</Printer>") || | |
426 | !strcasecmp(line, "</Class>")) | |
427 | { | |
428 | if (p != NULL) | |
429 | { | |
430 | /* | |
431 | * Close out the current printer... | |
432 | */ | |
433 | ||
434 | cupsdSetPrinterAttrs(p); | |
435 | ||
436 | p = NULL; | |
437 | } | |
438 | else | |
ef416fc2 | 439 | cupsdLogMessage(CUPSD_LOG_ERROR, |
440 | "Syntax error on line %d of remote.cache.", linenum); | |
ef416fc2 | 441 | } |
442 | else if (!p) | |
443 | { | |
444 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
445 | "Syntax error on line %d of remote.cache.", linenum); | |
ef416fc2 | 446 | } |
447 | else if (!strcasecmp(line, "Info")) | |
448 | { | |
449 | if (value) | |
450 | cupsdSetString(&p->info, value); | |
451 | } | |
452 | else if (!strcasecmp(line, "MakeModel")) | |
453 | { | |
454 | if (value) | |
455 | cupsdSetString(&p->make_model, value); | |
456 | } | |
457 | else if (!strcasecmp(line, "Location")) | |
458 | { | |
459 | if (value) | |
460 | cupsdSetString(&p->location, value); | |
461 | } | |
462 | else if (!strcasecmp(line, "DeviceURI")) | |
463 | { | |
464 | if (value) | |
4400e98d | 465 | { |
bd7854cb | 466 | httpSeparateURI(HTTP_URI_CODING_ALL, value, scheme, sizeof(scheme), |
467 | username, sizeof(username), host, sizeof(host), &port, | |
468 | resource, sizeof(resource)); | |
469 | ||
470 | cupsdSetString(&p->hostname, host); | |
4400e98d | 471 | cupsdSetString(&p->uri, value); |
0af14961 | 472 | cupsdSetDeviceURI(p, value); |
4400e98d | 473 | } |
ef416fc2 | 474 | else |
ef416fc2 | 475 | cupsdLogMessage(CUPSD_LOG_ERROR, |
476 | "Syntax error on line %d of remote.cache.", linenum); | |
ef416fc2 | 477 | } |
b423cd4c | 478 | else if (!strcasecmp(line, "Option") && value) |
479 | { | |
480 | /* | |
481 | * Option name value | |
482 | */ | |
483 | ||
484 | for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++); | |
485 | ||
486 | if (!*valueptr) | |
487 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
488 | "Syntax error on line %d of remote.cache.", linenum); | |
489 | else | |
490 | { | |
491 | for (; *valueptr && isspace(*valueptr & 255); *valueptr++ = '\0'); | |
492 | ||
493 | p->num_options = cupsAddOption(value, valueptr, p->num_options, | |
494 | &(p->options)); | |
495 | } | |
496 | } | |
0af14961 MS |
497 | else if (!strcasecmp(line, "Reason")) |
498 | { | |
499 | if (value && | |
500 | p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0]))) | |
501 | { | |
502 | p->reasons[p->num_reasons] = _cupsStrAlloc(value); | |
503 | p->num_reasons ++; | |
504 | } | |
505 | else | |
506 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
507 | "Syntax error on line %d of remote.cache.", linenum); | |
508 | } | |
ef416fc2 | 509 | else if (!strcasecmp(line, "State")) |
510 | { | |
511 | /* | |
512 | * Set the initial queue state... | |
513 | */ | |
514 | ||
515 | if (value && !strcasecmp(value, "idle")) | |
516 | p->state = IPP_PRINTER_IDLE; | |
517 | else if (value && !strcasecmp(value, "stopped")) | |
518 | p->state = IPP_PRINTER_STOPPED; | |
519 | else | |
ef416fc2 | 520 | cupsdLogMessage(CUPSD_LOG_ERROR, |
521 | "Syntax error on line %d of remote.cache.", linenum); | |
ef416fc2 | 522 | } |
523 | else if (!strcasecmp(line, "StateMessage")) | |
524 | { | |
525 | /* | |
526 | * Set the initial queue state message... | |
527 | */ | |
528 | ||
529 | if (value) | |
530 | strlcpy(p->state_message, value, sizeof(p->state_message)); | |
531 | } | |
532 | else if (!strcasecmp(line, "Accepting")) | |
533 | { | |
534 | /* | |
535 | * Set the initial accepting state... | |
536 | */ | |
537 | ||
538 | if (value && | |
539 | (!strcasecmp(value, "yes") || | |
540 | !strcasecmp(value, "on") || | |
541 | !strcasecmp(value, "true"))) | |
542 | p->accepting = 1; | |
543 | else if (value && | |
544 | (!strcasecmp(value, "no") || | |
545 | !strcasecmp(value, "off") || | |
546 | !strcasecmp(value, "false"))) | |
547 | p->accepting = 0; | |
548 | else | |
ef416fc2 | 549 | cupsdLogMessage(CUPSD_LOG_ERROR, |
550 | "Syntax error on line %d of remote.cache.", linenum); | |
ef416fc2 | 551 | } |
552 | else if (!strcasecmp(line, "Type")) | |
553 | { | |
554 | if (value) | |
555 | p->type = atoi(value); | |
556 | else | |
ef416fc2 | 557 | cupsdLogMessage(CUPSD_LOG_ERROR, |
558 | "Syntax error on line %d of remote.cache.", linenum); | |
ef416fc2 | 559 | } |
560 | else if (!strcasecmp(line, "BrowseTime")) | |
561 | { | |
562 | if (value) | |
563 | { | |
564 | time_t t = atoi(value); | |
565 | ||
8ca02f3c | 566 | if (t > p->browse_expire) |
567 | p->browse_expire = t; | |
ef416fc2 | 568 | } |
569 | else | |
ef416fc2 | 570 | cupsdLogMessage(CUPSD_LOG_ERROR, |
571 | "Syntax error on line %d of remote.cache.", linenum); | |
ef416fc2 | 572 | } |
573 | else if (!strcasecmp(line, "JobSheets")) | |
574 | { | |
575 | /* | |
576 | * Set the initial job sheets... | |
577 | */ | |
578 | ||
579 | if (value) | |
580 | { | |
581 | for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++); | |
582 | ||
583 | if (*valueptr) | |
584 | *valueptr++ = '\0'; | |
585 | ||
586 | cupsdSetString(&p->job_sheets[0], value); | |
587 | ||
588 | while (isspace(*valueptr & 255)) | |
589 | valueptr ++; | |
590 | ||
591 | if (*valueptr) | |
592 | { | |
593 | for (value = valueptr; *valueptr && !isspace(*valueptr & 255); valueptr ++); | |
594 | ||
595 | if (*valueptr) | |
1f0275e3 | 596 | *valueptr = '\0'; |
ef416fc2 | 597 | |
598 | cupsdSetString(&p->job_sheets[1], value); | |
599 | } | |
600 | } | |
601 | else | |
ef416fc2 | 602 | cupsdLogMessage(CUPSD_LOG_ERROR, |
603 | "Syntax error on line %d of remote.cache.", linenum); | |
ef416fc2 | 604 | } |
605 | else if (!strcasecmp(line, "AllowUser")) | |
606 | { | |
607 | if (value) | |
608 | { | |
609 | p->deny_users = 0; | |
610 | cupsdAddPrinterUser(p, value); | |
611 | } | |
612 | else | |
ef416fc2 | 613 | cupsdLogMessage(CUPSD_LOG_ERROR, |
614 | "Syntax error on line %d of remote.cache.", linenum); | |
ef416fc2 | 615 | } |
616 | else if (!strcasecmp(line, "DenyUser")) | |
617 | { | |
618 | if (value) | |
619 | { | |
620 | p->deny_users = 1; | |
621 | cupsdAddPrinterUser(p, value); | |
622 | } | |
623 | else | |
ef416fc2 | 624 | cupsdLogMessage(CUPSD_LOG_ERROR, |
625 | "Syntax error on line %d of remote.cache.", linenum); | |
ef416fc2 | 626 | } |
627 | else | |
628 | { | |
629 | /* | |
630 | * Something else we don't understand... | |
631 | */ | |
632 | ||
633 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
634 | "Unknown configuration directive %s on line %d of remote.cache.", | |
635 | line, linenum); | |
636 | } | |
637 | } | |
638 | ||
639 | cupsFileClose(fp); | |
640 | ||
641 | /* | |
642 | * Do auto-classing if needed... | |
643 | */ | |
644 | ||
3dfe78b3 | 645 | process_implicit_classes(); |
ef416fc2 | 646 | } |
647 | ||
648 | ||
f7deaa1a | 649 | /* |
650 | * 'cupsdRegisterPrinter()' - Start sending broadcast information for a | |
651 | * printer or update the broadcast contents. | |
652 | */ | |
653 | ||
654 | void | |
655 | cupsdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */ | |
656 | { | |
7a14d768 | 657 | if (!Browsing || !BrowseLocalProtocols || |
09a101d6 | 658 | (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))) |
f7deaa1a | 659 | return; |
660 | ||
661 | #ifdef HAVE_LIBSLP | |
662 | /* if (BrowseLocalProtocols & BROWSE_SLP) | |
663 | slpRegisterPrinter(p); */ | |
664 | #endif /* HAVE_LIBSLP */ | |
665 | ||
666 | #ifdef HAVE_DNSSD | |
bdd6c45b | 667 | if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef) |
f7deaa1a | 668 | dnssdRegisterPrinter(p); |
669 | #endif /* HAVE_DNSSD */ | |
670 | } | |
671 | ||
672 | ||
d09495fa | 673 | /* |
674 | * 'cupsdRestartPolling()' - Restart polling servers as needed. | |
675 | */ | |
676 | ||
677 | void | |
678 | cupsdRestartPolling(void) | |
679 | { | |
680 | int i; /* Looping var */ | |
681 | cupsd_dirsvc_poll_t *pollp; /* Current polling server */ | |
682 | ||
683 | ||
684 | for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++) | |
685 | if (pollp->pid) | |
686 | kill(pollp->pid, SIGHUP); | |
687 | } | |
688 | ||
689 | ||
ef416fc2 | 690 | /* |
e00b005a | 691 | * 'cupsdSaveRemoteCache()' - Save the remote printer cache. |
ef416fc2 | 692 | */ |
693 | ||
694 | void | |
e00b005a | 695 | cupsdSaveRemoteCache(void) |
ef416fc2 | 696 | { |
e00b005a | 697 | int i; /* Looping var */ |
698 | cups_file_t *fp; /* printers.conf file */ | |
58dc1933 MS |
699 | char temp[1024], /* Temporary string */ |
700 | value[2048]; /* Value string */ | |
e00b005a | 701 | cupsd_printer_t *printer; /* Current printer class */ |
702 | time_t curtime; /* Current time */ | |
703 | struct tm *curdate; /* Current date */ | |
b423cd4c | 704 | cups_option_t *option; /* Current option */ |
ef416fc2 | 705 | |
ef416fc2 | 706 | |
707 | /* | |
e00b005a | 708 | * Create the remote.cache file... |
ef416fc2 | 709 | */ |
710 | ||
e00b005a | 711 | snprintf(temp, sizeof(temp), "%s/remote.cache", CacheDir); |
ef416fc2 | 712 | |
e00b005a | 713 | if ((fp = cupsFileOpen(temp, "w")) == NULL) |
ef416fc2 | 714 | { |
715 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
e00b005a | 716 | "Unable to save remote.cache - %s", strerror(errno)); |
ef416fc2 | 717 | return; |
718 | } | |
e00b005a | 719 | else |
f7deaa1a | 720 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Saving remote.cache..."); |
ef416fc2 | 721 | |
722 | /* | |
e00b005a | 723 | * Restrict access to the file... |
ef416fc2 | 724 | */ |
725 | ||
e00b005a | 726 | fchown(cupsFileNumber(fp), getuid(), Group); |
727 | fchmod(cupsFileNumber(fp), ConfigFilePerm); | |
ef416fc2 | 728 | |
e00b005a | 729 | /* |
730 | * Write a small header to the file... | |
731 | */ | |
ef416fc2 | 732 | |
e00b005a | 733 | curtime = time(NULL); |
734 | curdate = localtime(&curtime); | |
735 | strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate); | |
ef416fc2 | 736 | |
e00b005a | 737 | cupsFilePuts(fp, "# Remote cache file for " CUPS_SVERSION "\n"); |
738 | cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp); | |
ef416fc2 | 739 | |
740 | /* | |
e00b005a | 741 | * Write each local printer known to the system... |
ef416fc2 | 742 | */ |
743 | ||
e00b005a | 744 | for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers); |
745 | printer; | |
746 | printer = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
ef416fc2 | 747 | { |
748 | /* | |
e00b005a | 749 | * Skip local destinations... |
ef416fc2 | 750 | */ |
751 | ||
09a101d6 | 752 | if (!(printer->type & CUPS_PRINTER_DISCOVERED)) |
e00b005a | 753 | continue; |
ef416fc2 | 754 | |
ef416fc2 | 755 | /* |
e00b005a | 756 | * Write printers as needed... |
ef416fc2 | 757 | */ |
758 | ||
e00b005a | 759 | if (printer == DefaultPrinter) |
760 | cupsFilePuts(fp, "<Default"); | |
ef416fc2 | 761 | else |
e00b005a | 762 | cupsFilePutChar(fp, '<'); |
ef416fc2 | 763 | |
e00b005a | 764 | if (printer->type & CUPS_PRINTER_CLASS) |
765 | cupsFilePrintf(fp, "Class %s>\n", printer->name); | |
766 | else | |
767 | cupsFilePrintf(fp, "Printer %s>\n", printer->name); | |
ef416fc2 | 768 | |
8ca02f3c | 769 | cupsFilePrintf(fp, "BrowseTime %d\n", (int)printer->browse_expire); |
ef416fc2 | 770 | |
e00b005a | 771 | if (printer->info) |
58dc1933 | 772 | cupsFilePutConf(fp, "Info", printer->info); |
ef416fc2 | 773 | |
e00b005a | 774 | if (printer->location) |
58dc1933 MS |
775 | cupsFilePutConf(fp, "Location", printer->location); |
776 | ||
777 | if (printer->make_model) | |
778 | cupsFilePutConf(fp, "MakeModel", printer->make_model); | |
ef416fc2 | 779 | |
58dc1933 | 780 | cupsFilePutConf(fp, "DeviceURI", printer->device_uri); |
ef416fc2 | 781 | |
e00b005a | 782 | if (printer->state == IPP_PRINTER_STOPPED) |
e00b005a | 783 | cupsFilePuts(fp, "State Stopped\n"); |
e00b005a | 784 | else |
785 | cupsFilePuts(fp, "State Idle\n"); | |
ef416fc2 | 786 | |
0af14961 | 787 | for (i = 0; i < printer->num_reasons; i ++) |
58dc1933 MS |
788 | cupsFilePutConf(fp, "Reason", printer->reasons[i]); |
789 | ||
790 | cupsFilePrintf(fp, "Type %d\n", printer->type); | |
0af14961 | 791 | |
e00b005a | 792 | if (printer->accepting) |
793 | cupsFilePuts(fp, "Accepting Yes\n"); | |
794 | else | |
795 | cupsFilePuts(fp, "Accepting No\n"); | |
ef416fc2 | 796 | |
58dc1933 MS |
797 | snprintf(value, sizeof(value), "%s %s", printer->job_sheets[0], |
798 | printer->job_sheets[1]); | |
799 | cupsFilePutConf(fp, "JobSheets", value); | |
ef416fc2 | 800 | |
e00b005a | 801 | for (i = 0; i < printer->num_users; i ++) |
58dc1933 MS |
802 | cupsFilePutConf(fp, printer->deny_users ? "DenyUser" : "AllowUser", |
803 | printer->users[i]); | |
ef416fc2 | 804 | |
b423cd4c | 805 | for (i = printer->num_options, option = printer->options; |
806 | i > 0; | |
807 | i --, option ++) | |
58dc1933 MS |
808 | { |
809 | snprintf(value, sizeof(value), "%s %s", option->name, option->value); | |
810 | cupsFilePutConf(fp, "Option", value); | |
811 | } | |
b423cd4c | 812 | |
e00b005a | 813 | if (printer->type & CUPS_PRINTER_CLASS) |
814 | cupsFilePuts(fp, "</Class>\n"); | |
815 | else | |
816 | cupsFilePuts(fp, "</Printer>\n"); | |
817 | } | |
ef416fc2 | 818 | |
e00b005a | 819 | cupsFileClose(fp); |
820 | } | |
ef416fc2 | 821 | |
ef416fc2 | 822 | |
e00b005a | 823 | /* |
824 | * 'cupsdSendBrowseList()' - Send new browsing information as necessary. | |
825 | */ | |
ef416fc2 | 826 | |
e00b005a | 827 | void |
828 | cupsdSendBrowseList(void) | |
829 | { | |
830 | int count; /* Number of dests to update */ | |
831 | cupsd_printer_t *p; /* Current printer */ | |
832 | time_t ut, /* Minimum update time */ | |
833 | to; /* Timeout time */ | |
ef416fc2 | 834 | |
e00b005a | 835 | |
1f0275e3 | 836 | if (!Browsing || !Printers) |
e00b005a | 837 | return; |
838 | ||
839 | /* | |
840 | * Compute the update and timeout times... | |
841 | */ | |
842 | ||
b423cd4c | 843 | to = time(NULL); |
844 | ut = to - BrowseInterval; | |
e00b005a | 845 | |
846 | /* | |
847 | * Figure out how many printers need an update... | |
848 | */ | |
849 | ||
1f0275e3 | 850 | if (BrowseInterval > 0 && BrowseLocalProtocols) |
e00b005a | 851 | { |
852 | int max_count; /* Maximum number to update */ | |
853 | ||
854 | ||
855 | /* | |
856 | * Throttle the number of printers we'll be updating this time | |
857 | * around based on the number of queues that need updating and | |
858 | * the maximum number of queues to update each second... | |
859 | */ | |
860 | ||
861 | max_count = 2 * cupsArrayCount(Printers) / BrowseInterval + 1; | |
862 | ||
863 | for (count = 0, p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
864 | count < max_count && p != NULL; | |
865 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
09a101d6 | 866 | if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) && |
e00b005a | 867 | p->shared && p->browse_time < ut) |
868 | count ++; | |
869 | ||
870 | /* | |
871 | * Loop through all of the printers and send local updates as needed... | |
872 | */ | |
873 | ||
874 | if (BrowseNext) | |
875 | p = (cupsd_printer_t *)cupsArrayFind(Printers, BrowseNext); | |
876 | else | |
877 | p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
878 | ||
879 | for (; | |
880 | count > 0; | |
881 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
ef416fc2 | 882 | { |
883 | /* | |
e00b005a | 884 | * Check for wraparound... |
ef416fc2 | 885 | */ |
886 | ||
e00b005a | 887 | if (!p) |
888 | p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
ef416fc2 | 889 | |
e00b005a | 890 | if (!p) |
891 | break; | |
09a101d6 | 892 | else if ((p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) || |
e00b005a | 893 | !p->shared) |
894 | continue; | |
895 | else if (p->browse_time < ut) | |
896 | { | |
897 | /* | |
898 | * Need to send an update... | |
899 | */ | |
ef416fc2 | 900 | |
e00b005a | 901 | count --; |
ef416fc2 | 902 | |
e00b005a | 903 | p->browse_time = time(NULL); |
ef416fc2 | 904 | |
0a682745 | 905 | if ((BrowseLocalProtocols & BROWSE_CUPS) && BrowseSocket >= 0) |
e1d6a774 | 906 | send_cups_browse(p); |
ef416fc2 | 907 | |
e00b005a | 908 | #ifdef HAVE_LIBSLP |
909 | if (BrowseLocalProtocols & BROWSE_SLP) | |
e1d6a774 | 910 | send_slp_browse(p); |
e00b005a | 911 | #endif /* HAVE_LIBSLP */ |
b423cd4c | 912 | |
913 | #ifdef HAVE_LDAP | |
914 | if (BrowseLocalProtocols & BROWSE_LDAP) | |
e1d6a774 | 915 | send_ldap_browse(p); |
b423cd4c | 916 | #endif /* HAVE_LDAP */ |
e00b005a | 917 | } |
ef416fc2 | 918 | } |
e00b005a | 919 | |
920 | /* | |
921 | * Save where we left off so that all printers get updated... | |
922 | */ | |
923 | ||
924 | BrowseNext = p; | |
ef416fc2 | 925 | } |
926 | ||
927 | /* | |
1f0275e3 | 928 | * Loop through all of the printers and timeout old printers as needed... |
ef416fc2 | 929 | */ |
930 | ||
3dfe78b3 | 931 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); |
e00b005a | 932 | p; |
933 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
ef416fc2 | 934 | { |
e00b005a | 935 | /* |
936 | * If this is a remote queue, see if it needs to be timed out... | |
937 | */ | |
ef416fc2 | 938 | |
1f0275e3 MS |
939 | if ((p->type & CUPS_PRINTER_DISCOVERED) && |
940 | !(p->type & CUPS_PRINTER_IMPLICIT) && | |
941 | p->browse_expire < to) | |
ef416fc2 | 942 | { |
1f0275e3 MS |
943 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL, |
944 | "%s \'%s\' deleted by directory services (timeout).", | |
945 | (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer", | |
946 | p->name); | |
947 | ||
948 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
949 | "Remote destination \"%s\" has timed out; " | |
950 | "deleting it...", | |
e00b005a | 951 | p->name); |
952 | ||
1f0275e3 MS |
953 | cupsArraySave(Printers); |
954 | cupsdDeletePrinter(p, 1); | |
955 | cupsArrayRestore(Printers); | |
956 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE); | |
ef416fc2 | 957 | } |
958 | } | |
e00b005a | 959 | } |
ef416fc2 | 960 | |
ef416fc2 | 961 | |
749b1e90 MS |
962 | #ifdef HAVE_LDAP_REBIND_PROC |
963 | # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) | |
964 | /* | |
965 | * 'ldap_rebind_proc()' - Callback function for LDAP rebind | |
966 | */ | |
967 | ||
968 | static int | |
969 | ldap_rebind_proc (LDAP *RebindLDAPHandle, | |
970 | LDAP_CONST char *refsp, | |
971 | ber_tag_t request, | |
972 | ber_int_t msgid, | |
973 | void *params) | |
974 | { | |
975 | int rc; | |
976 | ||
977 | /* | |
978 | * Bind to new LDAP server... | |
979 | */ | |
980 | ||
981 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
982 | "ldap_rebind_proc: Rebind to %s", refsp); | |
983 | ||
984 | # if LDAP_API_VERSION > 3000 | |
985 | struct berval bval; | |
986 | bval.bv_val = BrowseLDAPPassword; | |
987 | bval.bv_len = (BrowseLDAPPassword == NULL) ? 0 : strlen(BrowseLDAPPassword); | |
988 | ||
989 | rc = ldap_sasl_bind_s(RebindLDAPHandle, BrowseLDAPBindDN, LDAP_SASL_SIMPLE, &bval, NULL, NULL, NULL); | |
990 | # else | |
991 | rc = ldap_bind_s(RebindLDAPHandle, BrowseLDAPBindDN, | |
992 | BrowseLDAPPassword, LDAP_AUTH_SIMPLE); | |
993 | # endif /* LDAP_API_VERSION > 3000 */ | |
994 | ||
995 | return (rc); | |
996 | } | |
997 | ||
998 | # else /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */ | |
999 | ||
1000 | /* | |
1001 | * 'ldap_rebind_proc()' - Callback function for LDAP rebind | |
1002 | */ | |
1003 | ||
1004 | static int | |
1005 | ldap_rebind_proc (LDAP *RebindLDAPHandle, | |
1006 | char **dnp, | |
1007 | char **passwdp, | |
1008 | int *authmethodp, | |
1009 | int freeit, | |
1010 | void *arg) | |
1011 | { | |
1012 | switch ( freeit ) { | |
1013 | ||
1014 | case 1: | |
1015 | ||
1016 | /* | |
1017 | * Free current values... | |
1018 | */ | |
1019 | ||
1020 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1021 | "ldap_rebind_proc: Free values..."); | |
1022 | ||
1023 | if ( dnp && *dnp ) { | |
1024 | free( *dnp ); | |
1025 | } | |
1026 | if ( passwdp && *passwdp ) { | |
1027 | free( *passwdp ); | |
1028 | } | |
1029 | break; | |
1030 | ||
1031 | case 0: | |
1032 | ||
1033 | /* | |
1034 | * Return credentials for LDAP referal... | |
1035 | */ | |
1036 | ||
1037 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1038 | "ldap_rebind_proc: Return necessary values..."); | |
1039 | ||
1040 | *dnp = strdup(BrowseLDAPBindDN); | |
1041 | *passwdp = strdup(BrowseLDAPPassword); | |
1042 | *authmethodp = LDAP_AUTH_SIMPLE; | |
1043 | break; | |
1044 | ||
1045 | default: | |
1046 | ||
1047 | /* | |
1048 | * Should never happen... | |
1049 | */ | |
1050 | ||
1051 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1052 | "LDAP rebind has been called with wrong freeit value!"); | |
1053 | break; | |
1054 | ||
1055 | } | |
1056 | ||
1057 | return (LDAP_SUCCESS); | |
1058 | } | |
1059 | # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */ | |
1060 | #endif /* HAVE_LDAP_REBIND_PROC */ | |
1061 | ||
1062 | ||
1063 | #ifdef HAVE_LDAP | |
1064 | /* | |
1065 | * 'ldap_connect()' - Start new LDAP connection | |
1066 | */ | |
1067 | ||
1068 | static LDAP * | |
1069 | ldap_connect(void) | |
1070 | { | |
1071 | /* | |
1072 | * Open LDAP handle... | |
1073 | */ | |
1074 | ||
1075 | int rc; /* LDAP API status */ | |
1076 | int version = 3; /* LDAP version */ | |
1077 | struct berval bv = {0, ""}; /* SASL bind value */ | |
1078 | LDAP *TempBrowseLDAPHandle=NULL; /* Temporary LDAP Handle */ | |
1079 | # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) | |
1080 | int ldap_ssl = 0; /* LDAP SSL indicator */ | |
1081 | int ssl_err = 0; /* LDAP SSL error value */ | |
1082 | # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */ | |
1083 | ||
1084 | # ifdef HAVE_OPENLDAP | |
1085 | # ifdef HAVE_LDAP_SSL | |
1086 | ||
1087 | /* | |
1088 | * Set the certificate file to use for encrypted LDAP sessions... | |
1089 | */ | |
1090 | ||
1091 | if (BrowseLDAPCACertFile) | |
1092 | { | |
1093 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1094 | "cupsdStartBrowsing: Setting CA certificate file \"%s\"", | |
1095 | BrowseLDAPCACertFile); | |
1096 | ||
1097 | if ((rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, | |
1098 | (void *)BrowseLDAPCACertFile)) != LDAP_SUCCESS) | |
1099 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1100 | "Unable to set CA certificate file for LDAP " | |
1101 | "connections: %d - %s", rc, ldap_err2string(rc)); | |
1102 | } | |
1103 | ||
1104 | # endif /* HAVE_LDAP_SSL */ | |
1105 | /* | |
1106 | * Initialize OPENLDAP connection... | |
1107 | * LDAP stuff currently only supports ldapi EXTERNAL SASL binds... | |
1108 | */ | |
1109 | ||
1110 | if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost")) | |
1111 | rc = ldap_initialize(&TempBrowseLDAPHandle, "ldapi:///"); | |
1112 | else | |
1113 | rc = ldap_initialize(&TempBrowseLDAPHandle, BrowseLDAPServer); | |
1114 | ||
1115 | # else /* HAVE_OPENLDAP */ | |
1116 | ||
1117 | int ldap_port = 0; /* LDAP port */ | |
1118 | char ldap_protocol[11], /* LDAP protocol */ | |
1119 | ldap_host[255]; /* LDAP host */ | |
1120 | ||
1121 | /* | |
1122 | * Split LDAP URI into its components... | |
1123 | */ | |
1124 | ||
1125 | if (! BrowseLDAPServer) | |
1126 | { | |
1127 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1128 | "BrowseLDAPServer not configured! Disable LDAP browsing!"); | |
1129 | BrowseLocalProtocols &= ~BROWSE_LDAP; | |
1130 | BrowseRemoteProtocols &= ~BROWSE_LDAP; | |
1131 | return (NULL); | |
1132 | } | |
1133 | ||
1134 | sscanf(BrowseLDAPServer, "%10[^:]://%254[^:/]:%d", ldap_protocol, ldap_host, &ldap_port); | |
1135 | ||
1136 | if (strcmp(ldap_protocol, "ldap") == 0) { | |
1137 | ldap_ssl = 0; | |
1138 | } else if (strcmp(ldap_protocol, "ldaps") == 0) { | |
1139 | ldap_ssl = 1; | |
1140 | } else { | |
1141 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1142 | "unrecognised ldap protocol (%s)!", ldap_protocol); | |
1143 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1144 | "Disable LDAP browsing!"); | |
1145 | BrowseLocalProtocols &= ~BROWSE_LDAP; | |
1146 | BrowseRemoteProtocols &= ~BROWSE_LDAP; | |
1147 | return (NULL); | |
1148 | } | |
1149 | ||
1150 | if (ldap_port == 0) | |
1151 | { | |
1152 | if (ldap_ssl) | |
1153 | ldap_port = LDAPS_PORT; | |
1154 | else | |
1155 | ldap_port = LDAP_PORT; | |
1156 | } | |
1157 | ||
1158 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1159 | "LDAP Connection Details: PROT:%s HOST:%s PORT:%d", | |
1160 | ldap_protocol, ldap_host, ldap_port); | |
1161 | ||
1162 | /* | |
1163 | * Initialize LDAP connection... | |
1164 | */ | |
1165 | ||
1166 | if (! ldap_ssl) | |
1167 | { | |
1168 | if ((TempBrowseLDAPHandle = ldap_init(ldap_host, ldap_port)) == NULL) | |
1169 | rc = LDAP_OPERATIONS_ERROR; | |
1170 | else | |
1171 | rc = LDAP_SUCCESS; | |
1172 | ||
1173 | # ifdef HAVE_LDAP_SSL | |
1174 | } | |
1175 | else | |
1176 | { | |
1177 | ||
1178 | /* | |
1179 | * Initialize SSL LDAP connection... | |
1180 | */ | |
1181 | if (BrowseLDAPCACertFile) | |
1182 | { | |
1183 | rc = ldapssl_client_init(BrowseLDAPCACertFile, (void *)NULL); | |
1184 | if (rc != LDAP_SUCCESS) { | |
1185 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1186 | "Failed to initialize LDAP SSL client!"); | |
1187 | rc = LDAP_OPERATIONS_ERROR; | |
1188 | } else { | |
1189 | if ((TempBrowseLDAPHandle = ldapssl_init(ldap_host, ldap_port, 1)) == NULL) | |
1190 | rc = LDAP_OPERATIONS_ERROR; | |
1191 | else | |
1192 | rc = LDAP_SUCCESS; | |
1193 | } | |
1194 | } | |
1195 | else | |
1196 | { | |
1197 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1198 | "LDAP SSL certificate file/database not configured!"); | |
1199 | rc = LDAP_OPERATIONS_ERROR; | |
1200 | } | |
1201 | ||
1202 | # else /* HAVE_LDAP_SSL */ | |
1203 | ||
1204 | /* | |
1205 | * Return error, because client libraries doesn't support SSL | |
1206 | */ | |
1207 | ||
1208 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1209 | "LDAP client libraries does not support TLS"); | |
1210 | rc = LDAP_OPERATIONS_ERROR; | |
1211 | ||
1212 | # endif /* HAVE_LDAP_SSL */ | |
1213 | } | |
1214 | # endif /* HAVE_OPENLDAP */ | |
1215 | ||
1216 | /* | |
1217 | * Check return code from LDAP initialize... | |
1218 | */ | |
1219 | ||
1220 | if (rc != LDAP_SUCCESS) | |
1221 | { | |
1222 | if ((rc == LDAP_SERVER_DOWN) || (rc == LDAP_CONNECT_ERROR)) | |
1223 | { | |
1224 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1225 | "Unable to initialize LDAP! Temporary disable LDAP browsing..."); | |
1226 | } | |
1227 | else | |
1228 | { | |
1229 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1230 | "Unable to initialize LDAP! Disable LDAP browsing!"); | |
1231 | BrowseLocalProtocols &= ~BROWSE_LDAP; | |
1232 | BrowseRemoteProtocols &= ~BROWSE_LDAP; | |
1233 | } | |
1234 | ||
1235 | ldap_disconnect(TempBrowseLDAPHandle); | |
1236 | TempBrowseLDAPHandle = NULL; | |
1237 | } | |
1238 | ||
1239 | /* | |
1240 | * Upgrade LDAP version... | |
1241 | */ | |
1242 | ||
1243 | else if (ldap_set_option(TempBrowseLDAPHandle, LDAP_OPT_PROTOCOL_VERSION, | |
1244 | (const void *)&version) != LDAP_SUCCESS) | |
1245 | { | |
1246 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1247 | "Unable to set LDAP protocol version %d! Disable LDAP browsing!", | |
1248 | version); | |
1249 | BrowseLocalProtocols &= ~BROWSE_LDAP; | |
1250 | BrowseRemoteProtocols &= ~BROWSE_LDAP; | |
1251 | ldap_disconnect(TempBrowseLDAPHandle); | |
1252 | TempBrowseLDAPHandle = NULL; | |
1253 | } | |
1254 | else | |
1255 | { | |
1256 | ||
1257 | /* | |
1258 | * Register LDAP rebind procedure... | |
1259 | */ | |
1260 | ||
1261 | # ifdef HAVE_LDAP_REBIND_PROC | |
1262 | # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) | |
1263 | ||
1264 | rc = ldap_set_rebind_proc(TempBrowseLDAPHandle, &ldap_rebind_proc, (void *)NULL); | |
1265 | if ( rc != LDAP_SUCCESS ) | |
1266 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1267 | "Setting LDAP rebind function failed with status %d: %s", | |
1268 | rc, ldap_err2string(rc)); | |
1269 | ||
1270 | # else | |
1271 | ||
1272 | ldap_set_rebind_proc(TempBrowseLDAPHandle, &ldap_rebind_proc, (void *)NULL); | |
1273 | ||
1274 | # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */ | |
1275 | # endif /* HAVE_LDAP_REBIND_PROC */ | |
1276 | ||
1277 | /* | |
1278 | * Start LDAP bind... | |
1279 | */ | |
1280 | ||
1281 | # if LDAP_API_VERSION > 3000 | |
1282 | struct berval bval; | |
1283 | bval.bv_val = BrowseLDAPPassword; | |
1284 | bval.bv_len = (BrowseLDAPPassword == NULL) ? 0 : strlen(BrowseLDAPPassword); | |
1285 | ||
1286 | if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost")) | |
1287 | rc = ldap_sasl_bind_s(TempBrowseLDAPHandle, NULL, "EXTERNAL", &bv, NULL, | |
1288 | NULL, NULL); | |
1289 | else | |
1290 | rc = ldap_sasl_bind_s(TempBrowseLDAPHandle, BrowseLDAPBindDN, LDAP_SASL_SIMPLE, &bval, NULL, NULL, NULL); | |
1291 | # else | |
1292 | rc = ldap_bind_s(TempBrowseLDAPHandle, BrowseLDAPBindDN, | |
1293 | BrowseLDAPPassword, LDAP_AUTH_SIMPLE); | |
1294 | # endif /* LDAP_API_VERSION > 3000 */ | |
1295 | ||
1296 | if (rc != LDAP_SUCCESS) | |
1297 | { | |
1298 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1299 | "LDAP bind failed with error %d: %s", | |
1300 | rc, ldap_err2string(rc)); | |
1301 | # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) | |
1302 | if (ldap_ssl && ((rc == LDAP_SERVER_DOWN) || (rc == LDAP_CONNECT_ERROR))) | |
1303 | { | |
1304 | ssl_err = PORT_GetError(); | |
1305 | if (ssl_err != 0) | |
1306 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1307 | "LDAP SSL error %d: %s", | |
1308 | ssl_err, ldapssl_err2string(ssl_err)); | |
1309 | } | |
1310 | # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */ | |
1311 | ldap_disconnect(TempBrowseLDAPHandle); | |
1312 | TempBrowseLDAPHandle = NULL; | |
1313 | } | |
1314 | else | |
1315 | { | |
1316 | cupsdLogMessage(CUPSD_LOG_INFO, | |
1317 | "LDAP connection established"); | |
1318 | } | |
1319 | ||
1320 | } | |
1321 | return (TempBrowseLDAPHandle); | |
1322 | } | |
1323 | ||
1324 | ||
1325 | /* | |
1326 | * 'ldap_reconnect()' - Reconnect to LDAP Server | |
1327 | */ | |
1328 | ||
1329 | static void | |
1330 | ldap_reconnect(void) | |
1331 | { | |
1332 | LDAP *TempBrowseLDAPHandle = NULL; /* Temp Handle to LDAP server */ | |
1333 | ||
1334 | cupsdLogMessage(CUPSD_LOG_INFO, | |
1335 | "Try LDAP reconnect..."); | |
1336 | ||
1337 | /* | |
1338 | * Get a new LDAP Handle and replace the global Handle | |
1339 | * if the new connection was successful | |
1340 | */ | |
1341 | ||
1342 | TempBrowseLDAPHandle = ldap_connect(); | |
1343 | ||
1344 | if (TempBrowseLDAPHandle != NULL) | |
1345 | { | |
1346 | if (BrowseLDAPHandle != NULL) | |
1347 | { | |
1348 | ldap_disconnect(BrowseLDAPHandle); | |
1349 | } | |
1350 | BrowseLDAPHandle = TempBrowseLDAPHandle; | |
1351 | } | |
1352 | } | |
1353 | ||
1354 | ||
1355 | /* | |
1356 | * 'ldap_disconnect()' - Disconnect from LDAP Server | |
1357 | */ | |
1358 | ||
1359 | static void | |
1360 | ldap_disconnect(LDAP *ld) /* I - LDAP handle */ | |
1361 | { | |
1362 | int rc; /* return code */ | |
1363 | ||
1364 | /* | |
1365 | * Close LDAP handle... | |
1366 | */ | |
1367 | ||
1368 | # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 | |
1369 | rc = ldap_unbind_ext_s(ld, NULL, NULL); | |
1370 | # else | |
1371 | rc = ldap_unbind_s(ld); | |
1372 | # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */ | |
1373 | if (rc != LDAP_SUCCESS) | |
1374 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1375 | "Unbind from LDAP server failed with status %d: %s", | |
1376 | rc, ldap_err2string(rc)); | |
1377 | } | |
1378 | #endif /* HAVE_LDAP */ | |
1379 | ||
1380 | ||
e00b005a | 1381 | /* |
e1d6a774 | 1382 | * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information. |
e00b005a | 1383 | */ |
ef416fc2 | 1384 | |
e00b005a | 1385 | void |
e1d6a774 | 1386 | cupsdStartBrowsing(void) |
e00b005a | 1387 | { |
e1d6a774 | 1388 | int val; /* Socket option value */ |
1389 | struct sockaddr_in addr; /* Broadcast address */ | |
f7deaa1a | 1390 | cupsd_printer_t *p; /* Current printer */ |
ef416fc2 | 1391 | |
ef416fc2 | 1392 | |
e1d6a774 | 1393 | BrowseNext = NULL; |
ef416fc2 | 1394 | |
e1d6a774 | 1395 | if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols)) |
1396 | return; | |
ef416fc2 | 1397 | |
e1d6a774 | 1398 | if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS) |
1399 | { | |
1400 | if (BrowseSocket < 0) | |
1401 | { | |
1402 | /* | |
1403 | * Create the broadcast socket... | |
1404 | */ | |
ef416fc2 | 1405 | |
e1d6a774 | 1406 | if ((BrowseSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) |
1407 | { | |
1408 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
bc44d920 | 1409 | "Unable to create broadcast socket - %s.", |
1410 | strerror(errno)); | |
e1d6a774 | 1411 | BrowseLocalProtocols &= ~BROWSE_CUPS; |
1412 | BrowseRemoteProtocols &= ~BROWSE_CUPS; | |
49d87452 MS |
1413 | |
1414 | if (FatalErrors & CUPSD_FATAL_BROWSE) | |
1415 | cupsdEndProcess(getpid(), 0); | |
e1d6a774 | 1416 | } |
49d87452 | 1417 | } |
ef416fc2 | 1418 | |
49d87452 MS |
1419 | if (BrowseSocket >= 0) |
1420 | { | |
ef416fc2 | 1421 | /* |
e1d6a774 | 1422 | * Bind the socket to browse port... |
ef416fc2 | 1423 | */ |
1424 | ||
e1d6a774 | 1425 | memset(&addr, 0, sizeof(addr)); |
1426 | addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
1427 | addr.sin_family = AF_INET; | |
1428 | addr.sin_port = htons(BrowsePort); | |
1429 | ||
1430 | if (bind(BrowseSocket, (struct sockaddr *)&addr, sizeof(addr))) | |
ef416fc2 | 1431 | { |
e1d6a774 | 1432 | cupsdLogMessage(CUPSD_LOG_ERROR, |
bc44d920 | 1433 | "Unable to bind broadcast socket - %s.", |
1434 | strerror(errno)); | |
ef416fc2 | 1435 | |
e1d6a774 | 1436 | #ifdef WIN32 |
1437 | closesocket(BrowseSocket); | |
1438 | #else | |
1439 | close(BrowseSocket); | |
1440 | #endif /* WIN32 */ | |
ef416fc2 | 1441 | |
e1d6a774 | 1442 | BrowseSocket = -1; |
1443 | BrowseLocalProtocols &= ~BROWSE_CUPS; | |
1444 | BrowseRemoteProtocols &= ~BROWSE_CUPS; | |
49d87452 MS |
1445 | |
1446 | if (FatalErrors & CUPSD_FATAL_BROWSE) | |
1447 | cupsdEndProcess(getpid(), 0); | |
e1d6a774 | 1448 | } |
1449 | } | |
ef416fc2 | 1450 | |
49d87452 | 1451 | if (BrowseSocket >= 0) |
e1d6a774 | 1452 | { |
49d87452 MS |
1453 | /* |
1454 | * Set the "broadcast" flag... | |
1455 | */ | |
1456 | ||
1457 | val = 1; | |
1458 | if (setsockopt(BrowseSocket, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))) | |
1459 | { | |
1460 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to set broadcast mode - %s.", | |
1461 | strerror(errno)); | |
ef416fc2 | 1462 | |
e1d6a774 | 1463 | #ifdef WIN32 |
49d87452 | 1464 | closesocket(BrowseSocket); |
e1d6a774 | 1465 | #else |
49d87452 | 1466 | close(BrowseSocket); |
e1d6a774 | 1467 | #endif /* WIN32 */ |
ef416fc2 | 1468 | |
49d87452 MS |
1469 | BrowseSocket = -1; |
1470 | BrowseLocalProtocols &= ~BROWSE_CUPS; | |
1471 | BrowseRemoteProtocols &= ~BROWSE_CUPS; | |
e00b005a | 1472 | |
49d87452 MS |
1473 | if (FatalErrors & CUPSD_FATAL_BROWSE) |
1474 | cupsdEndProcess(getpid(), 0); | |
1475 | } | |
1476 | } | |
e00b005a | 1477 | |
49d87452 MS |
1478 | if (BrowseSocket >= 0) |
1479 | { | |
1480 | /* | |
1481 | * Close the socket on exec... | |
1482 | */ | |
e00b005a | 1483 | |
49d87452 | 1484 | fcntl(BrowseSocket, F_SETFD, fcntl(BrowseSocket, F_GETFD) | FD_CLOEXEC); |
ef416fc2 | 1485 | |
8ca02f3c | 1486 | /* |
49d87452 | 1487 | * Finally, add the socket to the input selection set as needed... |
8ca02f3c | 1488 | */ |
ef416fc2 | 1489 | |
49d87452 MS |
1490 | if (BrowseRemoteProtocols & BROWSE_CUPS) |
1491 | { | |
1492 | /* | |
1493 | * We only listen if we want remote printers... | |
1494 | */ | |
1495 | ||
1496 | cupsdAddSelect(BrowseSocket, (cupsd_selfunc_t)update_cups_browse, | |
1497 | NULL, NULL); | |
1498 | } | |
8ca02f3c | 1499 | } |
e1d6a774 | 1500 | } |
1501 | else | |
1502 | BrowseSocket = -1; | |
ef416fc2 | 1503 | |
7a14d768 MS |
1504 | #ifdef HAVE_DNSSD |
1505 | if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_DNSSD) | |
1506 | { | |
1507 | DNSServiceErrorType error; /* Error from service creation */ | |
1508 | cupsd_listener_t *lis; /* Current listening socket */ | |
1509 | ||
1510 | ||
1511 | /* | |
1512 | * First create a "master" connection for all registrations... | |
1513 | */ | |
1514 | ||
1515 | if ((error = DNSServiceCreateConnection(&DNSSDRef)) | |
1516 | != kDNSServiceErr_NoError) | |
49d87452 | 1517 | { |
7a14d768 MS |
1518 | cupsdLogMessage(CUPSD_LOG_ERROR, |
1519 | "Unable to create master DNS-SD reference: %d", error); | |
49d87452 MS |
1520 | |
1521 | if (FatalErrors & CUPSD_FATAL_BROWSE) | |
1522 | cupsdEndProcess(getpid(), 0); | |
1523 | } | |
7a14d768 MS |
1524 | else |
1525 | { | |
1526 | /* | |
1527 | * Add the master connection to the select list... | |
1528 | */ | |
1529 | ||
1530 | cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef), | |
75bd9771 | 1531 | (cupsd_selfunc_t)dnssdUpdate, NULL, NULL); |
7a14d768 MS |
1532 | |
1533 | /* | |
1534 | * Then get the port we use for registrations. If we are not listening | |
1535 | * on any non-local ports, there is no sense sharing local printers via | |
1536 | * Bonjour... | |
1537 | */ | |
1538 | ||
1539 | DNSSDPort = 0; | |
1540 | ||
1541 | for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); | |
1542 | lis; | |
1543 | lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) | |
1544 | { | |
1545 | if (httpAddrLocalhost(&(lis->address))) | |
1546 | continue; | |
1547 | ||
1548 | if (lis->address.addr.sa_family == AF_INET) | |
1549 | { | |
1550 | DNSSDPort = ntohs(lis->address.ipv4.sin_port); | |
1551 | break; | |
1552 | } | |
1553 | else if (lis->address.addr.sa_family == AF_INET6) | |
1554 | { | |
1555 | DNSSDPort = ntohs(lis->address.ipv6.sin6_port); | |
1556 | break; | |
1557 | } | |
1558 | } | |
1559 | ||
1560 | /* | |
1561 | * Create an array to track the printers we share... | |
1562 | */ | |
1563 | ||
1564 | if (BrowseRemoteProtocols & BROWSE_DNSSD) | |
1565 | DNSSDPrinters = cupsArrayNew((cups_array_func_t)dnssdComparePrinters, | |
1566 | NULL); | |
1567 | ||
1568 | /* | |
1569 | * Set the computer name and register the web interface... | |
1570 | */ | |
1571 | ||
1572 | cupsdUpdateDNSSDName(); | |
1573 | } | |
1574 | } | |
1575 | #endif /* HAVE_DNSSD */ | |
1576 | ||
e1d6a774 | 1577 | #ifdef HAVE_LIBSLP |
1578 | if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) | |
1579 | { | |
1580 | /* | |
1581 | * Open SLP handle... | |
1582 | */ | |
ef416fc2 | 1583 | |
e1d6a774 | 1584 | if (SLPOpen("en", SLP_FALSE, &BrowseSLPHandle) != SLP_OK) |
1585 | { | |
1586 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1587 | "Unable to open an SLP handle; disabling SLP browsing!"); | |
1588 | BrowseLocalProtocols &= ~BROWSE_SLP; | |
1589 | BrowseRemoteProtocols &= ~BROWSE_SLP; | |
49d87452 MS |
1590 | BrowseSLPHandle = NULL; |
1591 | ||
1592 | if (FatalErrors & CUPSD_FATAL_BROWSE) | |
1593 | cupsdEndProcess(getpid(), 0); | |
e1d6a774 | 1594 | } |
e00b005a | 1595 | |
e1d6a774 | 1596 | BrowseSLPRefresh = 0; |
1597 | } | |
f301802f | 1598 | else |
1599 | BrowseSLPHandle = NULL; | |
e1d6a774 | 1600 | #endif /* HAVE_LIBSLP */ |
1601 | ||
749b1e90 | 1602 | #ifdef HAVE_LDAP |
e1d6a774 | 1603 | if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP) |
1604 | { | |
1605 | if (!BrowseLDAPDN) | |
1606 | { | |
1607 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1608 | "Need to set BrowseLDAPDN to use LDAP browsing!"); | |
1609 | BrowseLocalProtocols &= ~BROWSE_LDAP; | |
1610 | BrowseRemoteProtocols &= ~BROWSE_LDAP; | |
49d87452 MS |
1611 | |
1612 | if (FatalErrors & CUPSD_FATAL_BROWSE) | |
1613 | cupsdEndProcess(getpid(), 0); | |
ef416fc2 | 1614 | } |
1615 | else | |
1616 | { | |
49d87452 MS |
1617 | /* |
1618 | * Open LDAP handle... | |
1619 | */ | |
1620 | ||
1621 | if ((BrowseLDAPHandle = ldap_connect()) == NULL && | |
1622 | (FatalErrors & CUPSD_FATAL_BROWSE)) | |
1623 | cupsdEndProcess(getpid(), 0); | |
ef416fc2 | 1624 | } |
e1d6a774 | 1625 | |
1626 | BrowseLDAPRefresh = 0; | |
1627 | } | |
749b1e90 | 1628 | #endif /* HAVE_LDAP */ |
f7deaa1a | 1629 | |
2e4ff8af MS |
1630 | /* |
1631 | * Enable LPD and SMB printer sharing as needed through external programs... | |
1632 | */ | |
1633 | ||
1634 | if (BrowseLocalProtocols & BROWSE_LPD) | |
1635 | update_lpd(1); | |
1636 | ||
1637 | if (BrowseLocalProtocols & BROWSE_SMB) | |
1638 | update_smb(1); | |
1639 | ||
f7deaa1a | 1640 | /* |
1641 | * Register the individual printers | |
1642 | */ | |
1643 | ||
1644 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
1645 | p; | |
1646 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
1647 | if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))) | |
1648 | cupsdRegisterPrinter(p); | |
ef416fc2 | 1649 | } |
1650 | ||
1651 | ||
b423cd4c | 1652 | /* |
e1d6a774 | 1653 | * 'cupsdStartPolling()' - Start polling servers as needed. |
b423cd4c | 1654 | */ |
1655 | ||
e1d6a774 | 1656 | void |
1657 | cupsdStartPolling(void) | |
b423cd4c | 1658 | { |
e1d6a774 | 1659 | int i; /* Looping var */ |
1660 | cupsd_dirsvc_poll_t *pollp; /* Current polling server */ | |
1661 | char polld[1024]; /* Poll daemon path */ | |
07725fee | 1662 | char sport[255]; /* Server port */ |
1663 | char bport[255]; /* Browser port */ | |
1664 | char interval[255]; /* Poll interval */ | |
e1d6a774 | 1665 | int statusfds[2]; /* Status pipe */ |
1666 | char *argv[6]; /* Arguments */ | |
1667 | char *envp[100]; /* Environment */ | |
b423cd4c | 1668 | |
b423cd4c | 1669 | |
1670 | /* | |
e1d6a774 | 1671 | * Don't do anything if we aren't polling... |
b423cd4c | 1672 | */ |
1673 | ||
2abf387c | 1674 | if (NumPolled == 0 || BrowseSocket < 0) |
e1d6a774 | 1675 | { |
1676 | PollPipe = -1; | |
1677 | PollStatusBuffer = NULL; | |
1678 | return; | |
1679 | } | |
b423cd4c | 1680 | |
e1d6a774 | 1681 | /* |
1682 | * Setup string arguments for polld, port and interval options. | |
1683 | */ | |
b423cd4c | 1684 | |
e1d6a774 | 1685 | snprintf(polld, sizeof(polld), "%s/daemon/cups-polld", ServerBin); |
b423cd4c | 1686 | |
e1d6a774 | 1687 | sprintf(bport, "%d", BrowsePort); |
b423cd4c | 1688 | |
e1d6a774 | 1689 | if (BrowseInterval) |
1690 | sprintf(interval, "%d", BrowseInterval); | |
1691 | else | |
1692 | strcpy(interval, "30"); | |
b423cd4c | 1693 | |
e1d6a774 | 1694 | argv[0] = "cups-polld"; |
1695 | argv[2] = sport; | |
1696 | argv[3] = interval; | |
1697 | argv[4] = bport; | |
1698 | argv[5] = NULL; | |
b423cd4c | 1699 | |
e1d6a774 | 1700 | cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); |
1701 | ||
1702 | /* | |
1703 | * Create a pipe that receives the status messages from each | |
1704 | * polling daemon... | |
1705 | */ | |
1706 | ||
1707 | if (cupsdOpenPipe(statusfds)) | |
b423cd4c | 1708 | { |
e1d6a774 | 1709 | cupsdLogMessage(CUPSD_LOG_ERROR, |
1710 | "Unable to create polling status pipes - %s.", | |
1711 | strerror(errno)); | |
1712 | PollPipe = -1; | |
1713 | PollStatusBuffer = NULL; | |
1714 | return; | |
1715 | } | |
b423cd4c | 1716 | |
e1d6a774 | 1717 | PollPipe = statusfds[0]; |
1718 | PollStatusBuffer = cupsdStatBufNew(PollPipe, "[Poll]"); | |
b423cd4c | 1719 | |
e1d6a774 | 1720 | /* |
1721 | * Run each polling daemon, redirecting stderr to the polling pipe... | |
1722 | */ | |
b423cd4c | 1723 | |
e1d6a774 | 1724 | for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++) |
b423cd4c | 1725 | { |
e1d6a774 | 1726 | sprintf(sport, "%d", pollp->port); |
b423cd4c | 1727 | |
e1d6a774 | 1728 | argv[1] = pollp->hostname; |
b423cd4c | 1729 | |
f7deaa1a | 1730 | if (cupsdStartProcess(polld, argv, envp, -1, -1, statusfds[1], -1, -1, |
a4924f6c | 1731 | 0, DefaultProfile, &(pollp->pid)) < 0) |
b423cd4c | 1732 | { |
b423cd4c | 1733 | cupsdLogMessage(CUPSD_LOG_ERROR, |
e1d6a774 | 1734 | "cupsdStartPolling: Unable to fork polling daemon - %s", |
1735 | strerror(errno)); | |
1736 | pollp->pid = 0; | |
1737 | break; | |
1738 | } | |
1739 | else | |
1740 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1741 | "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d", | |
1742 | pollp->hostname, pollp->port, pollp->pid); | |
b423cd4c | 1743 | } |
e1d6a774 | 1744 | |
1745 | close(statusfds[1]); | |
1746 | ||
1747 | /* | |
1748 | * Finally, add the pipe to the input selection set... | |
1749 | */ | |
1750 | ||
f899b121 | 1751 | cupsdAddSelect(PollPipe, (cupsd_selfunc_t)update_polling, NULL, NULL); |
b423cd4c | 1752 | } |
b423cd4c | 1753 | |
1754 | ||
ef416fc2 | 1755 | /* |
e1d6a774 | 1756 | * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information. |
ef416fc2 | 1757 | */ |
1758 | ||
e1d6a774 | 1759 | void |
1760 | cupsdStopBrowsing(void) | |
ef416fc2 | 1761 | { |
f7deaa1a | 1762 | cupsd_printer_t *p; /* Current printer */ |
1763 | ||
1764 | ||
e1d6a774 | 1765 | if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols)) |
1766 | return; | |
ef416fc2 | 1767 | |
f7deaa1a | 1768 | /* |
1769 | * De-register the individual printers | |
1770 | */ | |
1771 | ||
1772 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
1773 | p; | |
1774 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
1775 | if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))) | |
1776 | cupsdDeregisterPrinter(p, 1); | |
1777 | ||
1778 | /* | |
1779 | * Shut down browsing sockets... | |
1780 | */ | |
1781 | ||
e1d6a774 | 1782 | if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS) && |
1783 | BrowseSocket >= 0) | |
1784 | { | |
1785 | /* | |
1786 | * Close the socket and remove it from the input selection set. | |
1787 | */ | |
ef416fc2 | 1788 | |
e1d6a774 | 1789 | #ifdef WIN32 |
1790 | closesocket(BrowseSocket); | |
1791 | #else | |
1792 | close(BrowseSocket); | |
1793 | #endif /* WIN32 */ | |
ef416fc2 | 1794 | |
f7deaa1a | 1795 | cupsdRemoveSelect(BrowseSocket); |
e1d6a774 | 1796 | BrowseSocket = -1; |
1797 | } | |
ef416fc2 | 1798 | |
7a14d768 MS |
1799 | #ifdef HAVE_DNSSD |
1800 | if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef) | |
1801 | { | |
1802 | if (WebIFRef) | |
1803 | { | |
1804 | DNSServiceRefDeallocate(WebIFRef); | |
1805 | WebIFRef = NULL; | |
1806 | } | |
1807 | ||
1808 | if (RemoteRef) | |
1809 | { | |
1810 | DNSServiceRefDeallocate(RemoteRef); | |
1811 | RemoteRef = NULL; | |
1812 | } | |
1813 | ||
1814 | cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef)); | |
1815 | ||
1816 | DNSServiceRefDeallocate(DNSSDRef); | |
1817 | DNSSDRef = NULL; | |
1818 | ||
1819 | cupsArrayDelete(DNSSDPrinters); | |
1820 | DNSSDPrinters = NULL; | |
bdd6c45b MS |
1821 | |
1822 | DNSSDPort = 0; | |
7a14d768 MS |
1823 | } |
1824 | #endif /* HAVE_DNSSD */ | |
1825 | ||
e1d6a774 | 1826 | #ifdef HAVE_LIBSLP |
1827 | if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) && | |
1828 | BrowseSLPHandle) | |
1829 | { | |
1830 | /* | |
1831 | * Close SLP handle... | |
1832 | */ | |
ef416fc2 | 1833 | |
e1d6a774 | 1834 | SLPClose(BrowseSLPHandle); |
1835 | BrowseSLPHandle = NULL; | |
e00b005a | 1836 | } |
e1d6a774 | 1837 | #endif /* HAVE_LIBSLP */ |
ef416fc2 | 1838 | |
749b1e90 | 1839 | #ifdef HAVE_LDAP |
e1d6a774 | 1840 | if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP) && |
1841 | BrowseLDAPHandle) | |
e00b005a | 1842 | { |
749b1e90 MS |
1843 | ldap_dereg_ou(ServerName, BrowseLDAPDN); |
1844 | ldap_disconnect(BrowseLDAPHandle); | |
e1d6a774 | 1845 | BrowseLDAPHandle = NULL; |
e00b005a | 1846 | } |
e1d6a774 | 1847 | #endif /* HAVE_OPENLDAP */ |
2e4ff8af MS |
1848 | |
1849 | /* | |
1850 | * Disable LPD and SMB printer sharing as needed through external programs... | |
1851 | */ | |
1852 | ||
1853 | if (BrowseLocalProtocols & BROWSE_LPD) | |
1854 | update_lpd(0); | |
1855 | ||
1856 | if (BrowseLocalProtocols & BROWSE_SMB) | |
1857 | update_smb(0); | |
e1d6a774 | 1858 | } |
ef416fc2 | 1859 | |
ef416fc2 | 1860 | |
e1d6a774 | 1861 | /* |
1862 | * 'cupsdStopPolling()' - Stop polling servers as needed. | |
1863 | */ | |
ef416fc2 | 1864 | |
e1d6a774 | 1865 | void |
1866 | cupsdStopPolling(void) | |
1867 | { | |
1868 | int i; /* Looping var */ | |
1869 | cupsd_dirsvc_poll_t *pollp; /* Current polling server */ | |
ef416fc2 | 1870 | |
ef416fc2 | 1871 | |
e1d6a774 | 1872 | if (PollPipe >= 0) |
e00b005a | 1873 | { |
e1d6a774 | 1874 | cupsdStatBufDelete(PollStatusBuffer); |
1875 | close(PollPipe); | |
ef416fc2 | 1876 | |
f7deaa1a | 1877 | cupsdRemoveSelect(PollPipe); |
e1d6a774 | 1878 | |
1879 | PollPipe = -1; | |
1880 | PollStatusBuffer = NULL; | |
e00b005a | 1881 | } |
ef416fc2 | 1882 | |
e1d6a774 | 1883 | for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++) |
1884 | if (pollp->pid) | |
1885 | cupsdEndProcess(pollp->pid, 0); | |
1886 | } | |
ef416fc2 | 1887 | |
ef416fc2 | 1888 | |
f899b121 | 1889 | #ifdef HAVE_DNSSD |
7a14d768 MS |
1890 | /* |
1891 | * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing... | |
1892 | */ | |
1893 | ||
1894 | void | |
1895 | cupsdUpdateDNSSDName(void) | |
1896 | { | |
1897 | DNSServiceErrorType error; /* Error from service creation */ | |
1898 | char webif[1024]; /* Web interface share name */ | |
1899 | #ifdef HAVE_COREFOUNDATION_H | |
1900 | CFStringRef nameRef; /* Computer name CFString */ | |
1901 | char nameBuffer[1024]; /* C-string buffer */ | |
1902 | CFStringEncoding nameEncoding; /* Computer name encoding */ | |
1903 | #endif /* HAVE_COREFOUNDATION_H */ | |
1904 | ||
1905 | ||
1906 | /* | |
1907 | * Only share the web interface and printers when non-local listening is | |
1908 | * enabled... | |
1909 | */ | |
1910 | ||
1911 | if (!DNSSDPort) | |
1912 | return; | |
1913 | ||
1914 | /* | |
1915 | * Get the computer name as a c-string... | |
1916 | */ | |
1917 | ||
1918 | #ifdef HAVE_COREFOUNDATION_H | |
1919 | cupsdClearString(&DNSSDName); | |
1920 | ||
1921 | if ((nameRef = SCDynamicStoreCopyComputerName(NULL, | |
1922 | &nameEncoding)) != NULL) | |
1923 | { | |
1924 | if (CFStringGetCString(nameRef, nameBuffer, sizeof(nameBuffer), | |
1925 | kCFStringEncodingUTF8)) | |
1926 | cupsdSetString(&DNSSDName, nameBuffer); | |
ef416fc2 | 1927 | |
7a14d768 | 1928 | CFRelease(nameRef); |
f899b121 | 1929 | } |
7a14d768 MS |
1930 | |
1931 | #else | |
1932 | cupsdSetString(&DNSSDName, ServerName); | |
1933 | #endif /* HAVE_COREFOUNDATION_H */ | |
1934 | ||
1935 | /* | |
b19ccc9e | 1936 | * Then (re)register the web interface if enabled... |
7a14d768 MS |
1937 | */ |
1938 | ||
b19ccc9e MS |
1939 | if (BrowseWebIF) |
1940 | { | |
1941 | if (DNSSDName) | |
1942 | snprintf(webif, sizeof(webif), "CUPS @ %s", DNSSDName); | |
1943 | else | |
1944 | strlcpy(webif, "CUPS Web Interface", sizeof(webif)); | |
1945 | ||
1946 | if (WebIFRef) | |
1947 | DNSServiceRefDeallocate(WebIFRef); | |
1948 | ||
1949 | WebIFRef = DNSSDRef; | |
1950 | if ((error = DNSServiceRegister(&WebIFRef, | |
1951 | kDNSServiceFlagsShareConnection, | |
1952 | 0, webif, "_http._tcp", NULL, | |
1953 | NULL, htons(DNSSDPort), 7, | |
1954 | "\006path=/", dnssdRegisterCallback, | |
1955 | NULL)) != kDNSServiceErr_NoError) | |
1956 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1957 | "DNS-SD web interface registration failed: %d", error); | |
1958 | } | |
f899b121 | 1959 | } |
1960 | #endif /* HAVE_DNSSD */ | |
ef416fc2 | 1961 | |
ef416fc2 | 1962 | |
749b1e90 | 1963 | #ifdef HAVE_LDAP |
f899b121 | 1964 | /* |
1965 | * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP... | |
1966 | */ | |
1967 | ||
1968 | void | |
1969 | cupsdUpdateLDAPBrowse(void) | |
1970 | { | |
1971 | char uri[HTTP_MAX_URI], /* Printer URI */ | |
1972 | host[HTTP_MAX_URI], /* Hostname */ | |
1973 | resource[HTTP_MAX_URI], /* Resource path */ | |
1974 | location[1024], /* Printer location */ | |
1975 | info[1024], /* Printer information */ | |
1976 | make_model[1024], /* Printer make and model */ | |
749b1e90 | 1977 | type_num[30]; /* Printer type number */ |
f899b121 | 1978 | int type; /* Printer type */ |
1979 | int rc; /* LDAP status */ | |
1980 | int limit; /* Size limit */ | |
1981 | LDAPMessage *res, /* LDAP search results */ | |
1982 | *e; /* Current entry from search */ | |
ef416fc2 | 1983 | |
f899b121 | 1984 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "UpdateLDAPBrowse: %s", ServerName); |
ef416fc2 | 1985 | |
f899b121 | 1986 | BrowseLDAPRefresh = time(NULL) + BrowseInterval; |
ef416fc2 | 1987 | |
749b1e90 MS |
1988 | /* |
1989 | * Reconnect if LDAP Handle is invalid... | |
1990 | */ | |
1991 | ||
1992 | if (! BrowseLDAPHandle) | |
e1d6a774 | 1993 | { |
749b1e90 | 1994 | ldap_reconnect(); |
f899b121 | 1995 | return; |
e1d6a774 | 1996 | } |
ef416fc2 | 1997 | |
749b1e90 MS |
1998 | /* |
1999 | * Search for cups printers in LDAP directory... | |
2000 | */ | |
2001 | ||
2002 | rc = ldap_search_rec(BrowseLDAPHandle, BrowseLDAPDN, LDAP_SCOPE_SUBTREE, | |
2003 | "(objectclass=cupsPrinter)", (char **)ldap_attrs, 0, &res); | |
2004 | ||
2005 | /* | |
2006 | * If ldap search was successfull then exit function | |
2007 | * and temporary disable LDAP updates... | |
2008 | */ | |
2009 | ||
2010 | if (rc != LDAP_SUCCESS) | |
2011 | { | |
2012 | if (BrowseLDAPUpdate && ((rc == LDAP_SERVER_DOWN) || (rc == LDAP_CONNECT_ERROR))) | |
2013 | { | |
2014 | BrowseLDAPUpdate = FALSE; | |
2015 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2016 | "LDAP update temporary disabled"); | |
2017 | } | |
2018 | return; | |
2019 | } | |
2020 | ||
2021 | /* | |
2022 | * If LDAP updates were disabled, we will reenable them... | |
2023 | */ | |
2024 | ||
2025 | if (! BrowseLDAPUpdate) | |
2026 | { | |
2027 | BrowseLDAPUpdate = TRUE; | |
2028 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2029 | "LDAP update enabled"); | |
2030 | } | |
2031 | ||
2032 | /* | |
2033 | * Count LDAP entries and return if no entry exist... | |
2034 | */ | |
2035 | ||
f899b121 | 2036 | limit = ldap_count_entries(BrowseLDAPHandle, res); |
2037 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "LDAP search returned %d entries", limit); | |
2038 | if (limit < 1) | |
749b1e90 MS |
2039 | { |
2040 | ldap_freeres(res); | |
f899b121 | 2041 | return; |
749b1e90 | 2042 | } |
ef416fc2 | 2043 | |
e1d6a774 | 2044 | /* |
f899b121 | 2045 | * Loop through the available printers... |
e1d6a774 | 2046 | */ |
ef416fc2 | 2047 | |
f899b121 | 2048 | for (e = ldap_first_entry(BrowseLDAPHandle, res); |
2049 | e; | |
2050 | e = ldap_next_entry(BrowseLDAPHandle, e)) | |
e00b005a | 2051 | { |
f899b121 | 2052 | /* |
2053 | * Get the required values from this entry... | |
2054 | */ | |
ef416fc2 | 2055 | |
749b1e90 MS |
2056 | if (ldap_getval_firststring(BrowseLDAPHandle, e, |
2057 | "printerDescription", info, sizeof(info)) == -1) | |
f899b121 | 2058 | continue; |
ef416fc2 | 2059 | |
749b1e90 MS |
2060 | if (ldap_getval_firststring(BrowseLDAPHandle, e, |
2061 | "printerLocation", location, sizeof(location)) == -1) | |
f899b121 | 2062 | continue; |
ef416fc2 | 2063 | |
749b1e90 MS |
2064 | if (ldap_getval_firststring(BrowseLDAPHandle, e, |
2065 | "printerMakeAndModel", make_model, sizeof(make_model)) == -1) | |
f899b121 | 2066 | continue; |
ef416fc2 | 2067 | |
749b1e90 MS |
2068 | if (ldap_getval_firststring(BrowseLDAPHandle, e, |
2069 | "printerType", type_num, sizeof(type_num)) == -1) | |
f899b121 | 2070 | continue; |
ef416fc2 | 2071 | |
749b1e90 | 2072 | type = atoi(type_num); |
ef416fc2 | 2073 | |
749b1e90 MS |
2074 | if (ldap_getval_firststring(BrowseLDAPHandle, e, |
2075 | "printerURI", uri, sizeof(uri)) == -1) | |
f899b121 | 2076 | continue; |
ef416fc2 | 2077 | |
f899b121 | 2078 | /* |
2079 | * Process the entry as browse data... | |
2080 | */ | |
2081 | ||
2082 | if (!is_local_queue(uri, host, sizeof(host), resource, sizeof(resource))) | |
2083 | process_browse_data(uri, host, resource, type, IPP_PRINTER_IDLE, | |
2084 | location, info, make_model, 0, NULL); | |
ef416fc2 | 2085 | |
e00b005a | 2086 | } |
749b1e90 MS |
2087 | |
2088 | ldap_freeres(res); | |
f899b121 | 2089 | } |
749b1e90 | 2090 | #endif /* HAVE_LDAP */ |
ef416fc2 | 2091 | |
e1d6a774 | 2092 | |
f899b121 | 2093 | #ifdef HAVE_LIBSLP |
2094 | /* | |
2095 | * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP. | |
2096 | */ | |
ef416fc2 | 2097 | |
f899b121 | 2098 | void |
2099 | cupsdUpdateSLPBrowse(void) | |
2100 | { | |
2101 | slpsrvurl_t *s, /* Temporary list of service URLs */ | |
2102 | *next; /* Next service in list */ | |
2103 | cupsd_printer_t p; /* Printer information */ | |
2104 | const char *uri; /* Pointer to printer URI */ | |
2105 | char host[HTTP_MAX_URI], /* Host portion of URI */ | |
2106 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
e00b005a | 2107 | |
b423cd4c | 2108 | |
f899b121 | 2109 | /* |
2110 | * Reset the refresh time... | |
2111 | */ | |
b423cd4c | 2112 | |
f899b121 | 2113 | BrowseSLPRefresh = time(NULL) + BrowseInterval; |
b423cd4c | 2114 | |
f899b121 | 2115 | /* |
2116 | * Poll for remote printers using SLP... | |
2117 | */ | |
b423cd4c | 2118 | |
f899b121 | 2119 | s = NULL; |
b423cd4c | 2120 | |
f899b121 | 2121 | SLPFindSrvs(BrowseSLPHandle, SLP_CUPS_SRVTYPE, "", "", |
2122 | slp_url_callback, &s); | |
b423cd4c | 2123 | |
f899b121 | 2124 | /* |
2125 | * Loop through the list of available printers... | |
2126 | */ | |
e1d6a774 | 2127 | |
f899b121 | 2128 | for (; s; s = next) |
2129 | { | |
2130 | /* | |
2131 | * Save the "next" pointer... | |
2132 | */ | |
b423cd4c | 2133 | |
f899b121 | 2134 | next = s->next; |
e1d6a774 | 2135 | |
f899b121 | 2136 | /* |
2137 | * Load a cupsd_printer_t structure with the SLP service attributes... | |
2138 | */ | |
e1d6a774 | 2139 | |
f899b121 | 2140 | SLPFindAttrs(BrowseSLPHandle, s->url, "", "", slp_attr_callback, &p); |
ef416fc2 | 2141 | |
f899b121 | 2142 | /* |
2143 | * Process this printer entry... | |
2144 | */ | |
ef416fc2 | 2145 | |
f899b121 | 2146 | uri = s->url + SLP_CUPS_SRVLEN + 1; |
ef416fc2 | 2147 | |
f899b121 | 2148 | if (!strncmp(uri, "http://", 7) || !strncmp(uri, "ipp://", 6)) |
2149 | { | |
2150 | /* | |
2151 | * Pull the URI apart to see if this is a local or remote printer... | |
2152 | */ | |
ef416fc2 | 2153 | |
f899b121 | 2154 | if (!is_local_queue(uri, host, sizeof(host), resource, sizeof(resource))) |
2155 | process_browse_data(uri, host, resource, p.type, IPP_PRINTER_IDLE, | |
2156 | p.location, p.info, p.make_model, 0, NULL); | |
2157 | } | |
ef416fc2 | 2158 | |
f899b121 | 2159 | /* |
2160 | * Free this listing... | |
2161 | */ | |
ef416fc2 | 2162 | |
f899b121 | 2163 | cupsdClearString(&p.info); |
2164 | cupsdClearString(&p.location); | |
2165 | cupsdClearString(&p.make_model); | |
ef416fc2 | 2166 | |
f899b121 | 2167 | free(s); |
2168 | } | |
e1d6a774 | 2169 | } |
f899b121 | 2170 | #endif /* HAVE_LIBSLP */ |
ef416fc2 | 2171 | |
ef416fc2 | 2172 | |
f7deaa1a | 2173 | /* |
f899b121 | 2174 | * 'dequote()' - Remote quotes from a string. |
f7deaa1a | 2175 | */ |
2176 | ||
f899b121 | 2177 | static char * /* O - Dequoted string */ |
2178 | dequote(char *d, /* I - Destination string */ | |
2179 | const char *s, /* I - Source string */ | |
2180 | int dlen) /* I - Destination length */ | |
f7deaa1a | 2181 | { |
f899b121 | 2182 | char *dptr; /* Pointer into destination */ |
f7deaa1a | 2183 | |
2184 | ||
f899b121 | 2185 | if (s) |
f7deaa1a | 2186 | { |
f899b121 | 2187 | for (dptr = d, dlen --; *s && dlen > 0; s ++) |
2188 | if (*s != '\"') | |
2189 | { | |
2190 | *dptr++ = *s; | |
2191 | dlen --; | |
2192 | } | |
f7deaa1a | 2193 | |
f899b121 | 2194 | *dptr = '\0'; |
f7deaa1a | 2195 | } |
f899b121 | 2196 | else |
2197 | *d = '\0'; | |
2198 | ||
2199 | return (d); | |
f7deaa1a | 2200 | } |
f7deaa1a | 2201 | |
2202 | ||
7a14d768 | 2203 | #ifdef HAVE_DNSSD |
a603edef | 2204 | /* |
7a14d768 | 2205 | * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info. |
a603edef MS |
2206 | */ |
2207 | ||
7a14d768 MS |
2208 | static char * /* O - TXT record */ |
2209 | dnssdBuildTxtRecord( | |
2210 | int *txt_len, /* O - TXT record length */ | |
2211 | cupsd_printer_t *p, /* I - Printer information */ | |
2212 | int for_lpd) /* I - 1 = LPD, 0 = IPP */ | |
a603edef | 2213 | { |
7a14d768 MS |
2214 | int i, j; /* Looping vars */ |
2215 | char type_str[32], /* Type to string buffer */ | |
2216 | state_str[32], /* State to string buffer */ | |
2217 | rp_str[1024], /* Queue name string buffer */ | |
2218 | air_str[1024], /* auth-info-required string buffer */ | |
2219 | *keyvalue[32][2]; /* Table of key/value pairs */ | |
a603edef MS |
2220 | |
2221 | ||
2222 | /* | |
7a14d768 | 2223 | * Load up the key value pairs... |
a603edef MS |
2224 | */ |
2225 | ||
7a14d768 | 2226 | i = 0; |
a603edef | 2227 | |
7a14d768 MS |
2228 | keyvalue[i ][0] = "txtvers"; |
2229 | keyvalue[i++][1] = "1"; | |
a603edef | 2230 | |
7a14d768 MS |
2231 | keyvalue[i ][0] = "qtotal"; |
2232 | keyvalue[i++][1] = "1"; | |
a603edef | 2233 | |
7a14d768 MS |
2234 | keyvalue[i ][0] = "rp"; |
2235 | keyvalue[i++][1] = rp_str; | |
2236 | if (for_lpd) | |
2237 | strlcpy(rp_str, p->name, sizeof(rp_str)); | |
2238 | else | |
2239 | snprintf(rp_str, sizeof(rp_str), "%s/%s", | |
2240 | (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers", p->name); | |
a603edef | 2241 | |
7a14d768 MS |
2242 | keyvalue[i ][0] = "ty"; |
2243 | keyvalue[i++][1] = p->make_model; | |
a603edef | 2244 | |
7a14d768 MS |
2245 | if (p->location && *p->location != '\0') |
2246 | { | |
2247 | keyvalue[i ][0] = "note"; | |
2248 | keyvalue[i++][1] = p->location; | |
a603edef MS |
2249 | } |
2250 | ||
7a14d768 MS |
2251 | keyvalue[i ][0] = "priority"; |
2252 | keyvalue[i++][1] = for_lpd ? "100" : "0"; | |
e1d6a774 | 2253 | |
7a14d768 MS |
2254 | keyvalue[i ][0] = "product"; |
2255 | keyvalue[i++][1] = p->product ? p->product : "Unknown"; | |
e1d6a774 | 2256 | |
7a14d768 MS |
2257 | snprintf(type_str, sizeof(type_str), "0x%X", p->type | CUPS_PRINTER_REMOTE); |
2258 | snprintf(state_str, sizeof(state_str), "%d", p->state); | |
ef416fc2 | 2259 | |
7a14d768 MS |
2260 | keyvalue[i ][0] = "printer-state"; |
2261 | keyvalue[i++][1] = state_str; | |
ef416fc2 | 2262 | |
7a14d768 MS |
2263 | keyvalue[i ][0] = "printer-type"; |
2264 | keyvalue[i++][1] = type_str; | |
ef416fc2 | 2265 | |
7a14d768 MS |
2266 | keyvalue[i ][0] = "Transparent"; |
2267 | keyvalue[i++][1] = "T"; | |
ef416fc2 | 2268 | |
7a14d768 MS |
2269 | keyvalue[i ][0] = "Binary"; |
2270 | keyvalue[i++][1] = "T"; | |
ef416fc2 | 2271 | |
7a14d768 MS |
2272 | if ((p->type & CUPS_PRINTER_FAX)) |
2273 | { | |
2274 | keyvalue[i ][0] = "Fax"; | |
2275 | keyvalue[i++][1] = "T"; | |
2276 | } | |
ed486911 | 2277 | |
7a14d768 MS |
2278 | if ((p->type & CUPS_PRINTER_COLOR)) |
2279 | { | |
2280 | keyvalue[i ][0] = "Color"; | |
2281 | keyvalue[i++][1] = "T"; | |
2282 | } | |
ef416fc2 | 2283 | |
7a14d768 MS |
2284 | if ((p->type & CUPS_PRINTER_DUPLEX)) |
2285 | { | |
2286 | keyvalue[i ][0] = "Duplex"; | |
2287 | keyvalue[i++][1] = "T"; | |
2288 | } | |
ed486911 | 2289 | |
7a14d768 MS |
2290 | if ((p->type & CUPS_PRINTER_STAPLE)) |
2291 | { | |
2292 | keyvalue[i ][0] = "Staple"; | |
2293 | keyvalue[i++][1] = "T"; | |
2294 | } | |
e1d6a774 | 2295 | |
7a14d768 MS |
2296 | if ((p->type & CUPS_PRINTER_COPIES)) |
2297 | { | |
2298 | keyvalue[i ][0] = "Copies"; | |
2299 | keyvalue[i++][1] = "T"; | |
2300 | } | |
ef416fc2 | 2301 | |
7a14d768 MS |
2302 | if ((p->type & CUPS_PRINTER_COLLATE)) |
2303 | { | |
2304 | keyvalue[i ][0] = "Collate"; | |
2305 | keyvalue[i++][1] = "T"; | |
2306 | } | |
ef416fc2 | 2307 | |
7a14d768 | 2308 | if ((p->type & CUPS_PRINTER_PUNCH)) |
b423cd4c | 2309 | { |
7a14d768 MS |
2310 | keyvalue[i ][0] = "Punch"; |
2311 | keyvalue[i++][1] = "T"; | |
b423cd4c | 2312 | } |
2313 | ||
7a14d768 | 2314 | if ((p->type & CUPS_PRINTER_BIND)) |
e1d6a774 | 2315 | { |
7a14d768 MS |
2316 | keyvalue[i ][0] = "Bind"; |
2317 | keyvalue[i++][1] = "T"; | |
e1d6a774 | 2318 | } |
b423cd4c | 2319 | |
7a14d768 MS |
2320 | if ((p->type & CUPS_PRINTER_SORT)) |
2321 | { | |
2322 | keyvalue[i ][0] = "Sort"; | |
2323 | keyvalue[i++][1] = "T"; | |
2324 | } | |
b423cd4c | 2325 | |
7a14d768 MS |
2326 | keyvalue[i ][0] = "pdl"; |
2327 | keyvalue[i++][1] = p->pdl ? p->pdl : "application/postscript"; | |
b423cd4c | 2328 | |
7a14d768 | 2329 | if (p->num_auth_info_required) |
b423cd4c | 2330 | { |
7a14d768 | 2331 | char *air = air_str; /* Pointer into string */ |
b423cd4c | 2332 | |
b423cd4c | 2333 | |
7a14d768 | 2334 | for (j = 0; j < p->num_auth_info_required; j ++) |
e1d6a774 | 2335 | { |
7a14d768 MS |
2336 | if (air >= (air_str + sizeof(air_str) - 2)) |
2337 | break; | |
b423cd4c | 2338 | |
7a14d768 MS |
2339 | if (j) |
2340 | *air++ = ','; | |
b423cd4c | 2341 | |
7a14d768 MS |
2342 | strlcpy(air, p->auth_info_required[j], sizeof(air_str) - (air - air_str)); |
2343 | air += strlen(air); | |
2344 | } | |
ef416fc2 | 2345 | |
7a14d768 | 2346 | keyvalue[i ][0] = "air"; |
49d87452 | 2347 | keyvalue[i++][1] = air_str; |
ef416fc2 | 2348 | } |
ef416fc2 | 2349 | |
e00b005a | 2350 | /* |
7a14d768 | 2351 | * Then pack them into a proper txt record... |
e00b005a | 2352 | */ |
2353 | ||
7a14d768 MS |
2354 | return (dnssdPackTxtRecord(txt_len, keyvalue, i)); |
2355 | } | |
e00b005a | 2356 | |
e00b005a | 2357 | |
7a14d768 MS |
2358 | /* |
2359 | * 'dnssdComparePrinters()' - Compare the registered names of two printers. | |
2360 | */ | |
2361 | ||
2362 | static int /* O - Result of comparison */ | |
2363 | dnssdComparePrinters(cupsd_printer_t *a,/* I - First printer */ | |
2364 | cupsd_printer_t *b)/* I - Second printer */ | |
2365 | { | |
2366 | return (strcasecmp(a->reg_name, b->reg_name)); | |
2367 | } | |
2368 | ||
2369 | ||
2370 | /* | |
2371 | * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a | |
2372 | * printer. | |
2373 | */ | |
2374 | ||
2375 | static void | |
2376 | dnssdDeregisterPrinter( | |
2377 | cupsd_printer_t *p) /* I - Printer */ | |
2378 | { | |
2379 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdDeregisterPrinter(%s)", p->name); | |
2380 | ||
2381 | /* | |
2382 | * Closing the socket deregisters the service | |
2383 | */ | |
2384 | ||
2385 | if (p->ipp_ref) | |
2386 | { | |
2387 | DNSServiceRefDeallocate(p->ipp_ref); | |
2388 | p->ipp_ref = NULL; | |
e1d6a774 | 2389 | } |
e00b005a | 2390 | |
7a14d768 MS |
2391 | cupsArrayRemove(DNSSDPrinters, p); |
2392 | cupsdClearString(&p->reg_name); | |
2393 | ||
2394 | if (p->ipp_txt) | |
e1d6a774 | 2395 | { |
e00b005a | 2396 | /* |
7a14d768 | 2397 | * p->ipp_txt is malloc'd, not _cupsStrAlloc'd... |
e00b005a | 2398 | */ |
2399 | ||
7a14d768 MS |
2400 | free(p->ipp_txt); |
2401 | p->ipp_txt = NULL; | |
2402 | } | |
2403 | } | |
e00b005a | 2404 | |
d09495fa | 2405 | |
7a14d768 MS |
2406 | /* |
2407 | * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the | |
2408 | * TXT record format. | |
2409 | */ | |
e00b005a | 2410 | |
7a14d768 MS |
2411 | static char * /* O - TXT record */ |
2412 | dnssdPackTxtRecord(int *txt_len, /* O - TXT record length */ | |
2413 | char *keyvalue[][2], /* I - Table of key value pairs */ | |
2414 | int count) /* I - Items in table */ | |
2415 | { | |
2416 | int i; /* Looping var */ | |
2417 | int length; /* Length of TXT record */ | |
2418 | int length2; /* Length of value */ | |
2419 | char *txtRecord; /* TXT record buffer */ | |
2420 | char *cursor; /* Looping pointer */ | |
e00b005a | 2421 | |
e00b005a | 2422 | |
7a14d768 MS |
2423 | /* |
2424 | * Calculate the buffer size | |
2425 | */ | |
e00b005a | 2426 | |
7a14d768 MS |
2427 | for (length = i = 0; i < count; i++) |
2428 | length += 1 + strlen(keyvalue[i][0]) + | |
2429 | (keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0); | |
e00b005a | 2430 | |
7a14d768 MS |
2431 | /* |
2432 | * Allocate and fill it | |
2433 | */ | |
e00b005a | 2434 | |
7a14d768 MS |
2435 | txtRecord = malloc(length); |
2436 | if (txtRecord) | |
2437 | { | |
2438 | *txt_len = length; | |
2439 | ||
2440 | for (cursor = txtRecord, i = 0; i < count; i++) | |
e00b005a | 2441 | { |
2442 | /* | |
7a14d768 | 2443 | * Drop in the p-string style length byte followed by the data |
e00b005a | 2444 | */ |
2445 | ||
7a14d768 MS |
2446 | length = strlen(keyvalue[i][0]); |
2447 | length2 = keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0; | |
e00b005a | 2448 | |
7a14d768 | 2449 | *cursor++ = (unsigned char)(length + length2); |
e00b005a | 2450 | |
7a14d768 MS |
2451 | memcpy(cursor, keyvalue[i][0], length); |
2452 | cursor += length; | |
e00b005a | 2453 | |
7a14d768 MS |
2454 | if (length2) |
2455 | { | |
2456 | length2 --; | |
2457 | *cursor++ = '='; | |
2458 | memcpy(cursor, keyvalue[i][1], length2); | |
2459 | cursor += length2; | |
2460 | } | |
2461 | } | |
2462 | } | |
e00b005a | 2463 | |
7a14d768 MS |
2464 | return (txtRecord); |
2465 | } | |
e00b005a | 2466 | |
e00b005a | 2467 | |
7a14d768 MS |
2468 | /* |
2469 | * 'dnssdRegisterCallback()' - DNSServiceRegister callback. | |
2470 | */ | |
e00b005a | 2471 | |
7a14d768 MS |
2472 | static void |
2473 | dnssdRegisterCallback( | |
2474 | DNSServiceRef sdRef, /* I - DNS Service reference */ | |
2475 | DNSServiceFlags flags, /* I - Reserved for future use */ | |
2476 | DNSServiceErrorType errorCode, /* I - Error code */ | |
2477 | const char *name, /* I - Service name */ | |
2478 | const char *regtype, /* I - Service type */ | |
2479 | const char *domain, /* I - Domain. ".local" for now */ | |
2480 | void *context) /* I - User-defined context */ | |
2481 | { | |
2482 | cupsd_printer_t *p = (cupsd_printer_t *)context; | |
2483 | /* Current printer */ | |
3dfe78b3 | 2484 | |
7a14d768 MS |
2485 | |
2486 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterCallback(%s, %s) for %s", | |
2487 | name, regtype, p ? p->name : "Web Interface"); | |
2488 | ||
2489 | if (errorCode) | |
2490 | { | |
2491 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2492 | "DNSServiceRegister failed with error %d", (int)errorCode); | |
2493 | return; | |
ef416fc2 | 2494 | } |
7a14d768 | 2495 | else if (p && strcasecmp(name, p->reg_name)) |
ef416fc2 | 2496 | { |
7a14d768 MS |
2497 | cupsdLogMessage(CUPSD_LOG_INFO, "Using service name \"%s\" for \"%s\"", |
2498 | name, p->name); | |
e00b005a | 2499 | |
7a14d768 MS |
2500 | cupsArrayRemove(DNSSDPrinters, p); |
2501 | cupsdSetString(&p->reg_name, name); | |
2502 | cupsArrayAdd(DNSSDPrinters, p); | |
e00b005a | 2503 | |
7a14d768 MS |
2504 | LastEvent |= CUPSD_EVENT_PRINTER_MODIFIED; |
2505 | } | |
2506 | } | |
d09495fa | 2507 | |
ef416fc2 | 2508 | |
7a14d768 MS |
2509 | /* |
2510 | * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer | |
2511 | * or update the broadcast contents. | |
2512 | */ | |
ef416fc2 | 2513 | |
7a14d768 MS |
2514 | static void |
2515 | dnssdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */ | |
2516 | { | |
2517 | DNSServiceErrorType se; /* dnssd errors */ | |
2518 | char *ipp_txt, /* IPP TXT record buffer */ | |
2519 | *printer_txt, /* LPD TXT record buffer */ | |
2520 | name[1024], /* Service name */ | |
2521 | *nameptr; /* Pointer into name */ | |
2522 | int ipp_len, /* IPP TXT record length */ | |
2523 | printer_len; /* LPD TXT record length */ | |
2524 | char resource[1024]; /* Resource path for printer */ | |
2525 | const char *regtype; /* Registration type */ | |
2526 | const char *domain; /* Registration domain */ | |
2527 | cupsd_location_t *location, /* Printer location */ | |
2528 | *policy; /* Operation policy for Print-Job */ | |
2529 | unsigned address[4]; /* INADDR_ANY address */ | |
ef416fc2 | 2530 | |
ef416fc2 | 2531 | |
7a14d768 MS |
2532 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name, |
2533 | !p->ipp_ref ? "new" : "update"); | |
e00b005a | 2534 | |
7a14d768 MS |
2535 | /* |
2536 | * If per-printer sharing was just disabled make sure we're not | |
2537 | * registered before returning. | |
2538 | */ | |
e00b005a | 2539 | |
7a14d768 MS |
2540 | if (!p->shared) |
2541 | { | |
2542 | dnssdDeregisterPrinter(p); | |
2543 | return; | |
2544 | } | |
ef416fc2 | 2545 | |
7a14d768 MS |
2546 | /* |
2547 | * The registered name takes the form of "<printer-info> @ <computer name>"... | |
2548 | */ | |
e00b005a | 2549 | |
7a14d768 MS |
2550 | if (p->info && strlen(p->info) > 0) |
2551 | { | |
2552 | if (DNSSDName) | |
2553 | snprintf(name, sizeof(name), "%s @ %s", p->info, DNSSDName); | |
2554 | else | |
2555 | strlcpy(name, p->info, sizeof(name)); | |
2556 | } | |
2557 | else if (DNSSDName) | |
2558 | snprintf(name, sizeof(name), "%s @ %s", p->name, DNSSDName); | |
2559 | else | |
2560 | strlcpy(name, p->name, sizeof(name)); | |
ef416fc2 | 2561 | |
7a14d768 MS |
2562 | /* |
2563 | * If an existing printer was renamed, unregister it and start over... | |
2564 | */ | |
ef416fc2 | 2565 | |
7a14d768 MS |
2566 | if (p->reg_name && strcmp(p->reg_name, name)) |
2567 | dnssdDeregisterPrinter(p); | |
ef416fc2 | 2568 | |
7a14d768 MS |
2569 | if (!p->reg_name) |
2570 | { | |
2571 | cupsdSetString(&p->reg_name, name); | |
2572 | cupsArrayAdd(DNSSDPrinters, p); | |
2573 | } | |
ef416fc2 | 2574 | |
7a14d768 MS |
2575 | /* |
2576 | * If 'Allow printing from the Internet' is enabled (i.e. from any address) | |
2577 | * let dnssd decide on the domain, otherwise restrict it to ".local". | |
2578 | */ | |
ef416fc2 | 2579 | |
7a14d768 MS |
2580 | if (p->type & CUPS_PRINTER_CLASS) |
2581 | snprintf(resource, sizeof(resource), "/classes/%s", p->name); | |
2582 | else | |
2583 | snprintf(resource, sizeof(resource), "/printers/%s", p->name); | |
ef416fc2 | 2584 | |
7a14d768 MS |
2585 | address[0] = address[1] = address[2] = address[3] = 0; |
2586 | location = cupsdFindBest(resource, HTTP_POST); | |
2587 | policy = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB); | |
3dfe78b3 | 2588 | |
7a14d768 MS |
2589 | if ((location && !cupsdCheckAccess(address, "", 0, location)) || |
2590 | (policy && !cupsdCheckAccess(address, "", 0, policy))) | |
2591 | domain = "local."; | |
2592 | else | |
2593 | domain = NULL; | |
ef416fc2 | 2594 | |
2595 | /* | |
7a14d768 | 2596 | * Register IPP and (optionally) LPD... |
ef416fc2 | 2597 | */ |
2598 | ||
49d87452 | 2599 | ipp_len = 0; /* anti-compiler-warning-code */ |
7a14d768 | 2600 | ipp_txt = dnssdBuildTxtRecord(&ipp_len, p, 0); |
ef416fc2 | 2601 | |
7a14d768 | 2602 | if (!p->ipp_ref) |
b423cd4c | 2603 | { |
2604 | /* | |
7a14d768 | 2605 | * Initial registration. Use the _fax subtype for fax queues... |
b423cd4c | 2606 | */ |
2607 | ||
7a14d768 MS |
2608 | regtype = (p->type & CUPS_PRINTER_FAX) ? "_fax-ipp._tcp" : |
2609 | "_ipp._tcp,_cups"; | |
b423cd4c | 2610 | |
7a14d768 MS |
2611 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
2612 | "Registering DNS-SD printer %s with name \"%s\", " | |
2613 | "type \"%s\", and domain \"%s\"", p->name, name, regtype, | |
2614 | domain ? domain : "(null)"); | |
b423cd4c | 2615 | |
7a14d768 MS |
2616 | /* |
2617 | * Register the queue, dropping characters as needed until we succeed... | |
2618 | */ | |
ef416fc2 | 2619 | |
7a14d768 MS |
2620 | nameptr = name + strlen(name); |
2621 | ||
2622 | do | |
ef416fc2 | 2623 | { |
7a14d768 MS |
2624 | p->ipp_ref = DNSSDRef; |
2625 | if ((se = DNSServiceRegister(&p->ipp_ref, kDNSServiceFlagsShareConnection, | |
2626 | 0, name, regtype, domain, NULL, | |
2627 | htons(DNSSDPort), ipp_len, ipp_txt, | |
2628 | dnssdRegisterCallback, | |
2629 | p)) == kDNSServiceErr_BadParam) | |
2630 | { | |
2631 | /* | |
2632 | * Name is too long, drop trailing characters, taking into account | |
2633 | * UTF-8 encoding... | |
2634 | */ | |
2635 | ||
2636 | nameptr --; | |
2637 | ||
2638 | while (nameptr > name && (*nameptr & 0xc0) == 0x80) | |
2639 | nameptr --; | |
2640 | ||
2641 | if (nameptr > name) | |
2642 | *nameptr = '\0'; | |
2643 | } | |
2644 | } | |
2645 | while (se == kDNSServiceErr_BadParam && nameptr > name); | |
2646 | ||
2647 | if (se == kDNSServiceErr_NoError) | |
2648 | { | |
2649 | p->ipp_txt = ipp_txt; | |
2650 | p->ipp_len = ipp_len; | |
2651 | ipp_txt = NULL; | |
ef416fc2 | 2652 | } |
7a14d768 MS |
2653 | else |
2654 | cupsdLogMessage(CUPSD_LOG_WARN, | |
2655 | "DNS-SD IPP registration of \"%s\" failed: %d", | |
2656 | p->name, se); | |
ef416fc2 | 2657 | } |
7a14d768 | 2658 | else if (ipp_len != p->ipp_len || memcmp(ipp_txt, p->ipp_txt, ipp_len)) |
e00b005a | 2659 | { |
7a14d768 MS |
2660 | /* |
2661 | * Update the existing registration... | |
2662 | */ | |
ef416fc2 | 2663 | |
7a14d768 MS |
2664 | /* A TTL of 0 means use record's original value (Radar 3176248) */ |
2665 | DNSServiceUpdateRecord(p->ipp_ref, NULL, 0, ipp_len, ipp_txt, 0); | |
ef416fc2 | 2666 | |
7a14d768 MS |
2667 | if (p->ipp_txt) |
2668 | free(p->ipp_txt); | |
2669 | ||
2670 | p->ipp_txt = ipp_txt; | |
2671 | p->ipp_len = ipp_len; | |
2672 | ipp_txt = NULL; | |
e00b005a | 2673 | } |
ef416fc2 | 2674 | |
7a14d768 MS |
2675 | if (ipp_txt) |
2676 | free(ipp_txt); | |
2677 | ||
2678 | if (BrowseLocalProtocols & BROWSE_LPD) | |
e00b005a | 2679 | { |
49d87452 | 2680 | printer_len = 0; /* anti-compiler-warning-code */ |
7a14d768 | 2681 | printer_txt = dnssdBuildTxtRecord(&printer_len, p, 1); |
3dfe78b3 | 2682 | |
7a14d768 MS |
2683 | if (!p->printer_ref) |
2684 | { | |
2685 | /* | |
2686 | * Initial registration... | |
2687 | */ | |
ef416fc2 | 2688 | |
7a14d768 MS |
2689 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
2690 | "Registering DNS-SD printer %s with name \"%s\", " | |
2691 | "type \"_printer._tcp\", and domain \"%s\"", p->name, | |
2692 | name, domain ? domain : "(null)"); | |
2693 | ||
2694 | p->printer_ref = DNSSDRef; | |
2695 | if ((se = DNSServiceRegister(&p->printer_ref, | |
2696 | kDNSServiceFlagsShareConnection, | |
2697 | 0, name, "_printer._tcp", domain, NULL, | |
2698 | htons(515), printer_len, printer_txt, | |
2699 | dnssdRegisterCallback, | |
2700 | p)) == kDNSServiceErr_NoError) | |
2701 | { | |
2702 | p->printer_txt = printer_txt; | |
2703 | p->printer_len = printer_len; | |
2704 | printer_txt = NULL; | |
2705 | } | |
2706 | else | |
2707 | cupsdLogMessage(CUPSD_LOG_WARN, | |
2708 | "DNS-SD LPD registration of \"%s\" failed: %d", | |
2709 | p->name, se); | |
2710 | } | |
2711 | else if (printer_len != p->printer_len || | |
2712 | memcmp(printer_txt, p->printer_txt, printer_len)) | |
b423cd4c | 2713 | { |
2714 | /* | |
7a14d768 | 2715 | * Update the existing registration... |
b423cd4c | 2716 | */ |
2717 | ||
7a14d768 MS |
2718 | /* A TTL of 0 means use record's original value (Radar 3176248) */ |
2719 | DNSServiceUpdateRecord(p->printer_ref, NULL, 0, printer_len, | |
2720 | printer_txt, 0); | |
b423cd4c | 2721 | |
7a14d768 MS |
2722 | if (p->printer_txt) |
2723 | free(p->printer_txt); | |
b423cd4c | 2724 | |
7a14d768 MS |
2725 | p->printer_txt = printer_txt; |
2726 | p->printer_len = printer_len; | |
2727 | printer_txt = NULL; | |
2728 | } | |
2729 | ||
2730 | if (printer_txt) | |
2731 | free(printer_txt); | |
b423cd4c | 2732 | } |
7a14d768 | 2733 | } |
75bd9771 MS |
2734 | |
2735 | ||
2736 | /* | |
2737 | * 'dnssdUpdate()' - Handle DNS-SD queries. | |
2738 | */ | |
2739 | ||
2740 | static void | |
2741 | dnssdUpdate(void) | |
2742 | { | |
2743 | DNSServiceErrorType sdErr; /* Service discovery error */ | |
2744 | ||
2745 | ||
2746 | if ((sdErr = DNSServiceProcessResult(DNSSDRef)) != kDNSServiceErr_NoError) | |
2747 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2748 | "DNS Service Discovery registration error %d!", | |
2749 | sdErr); | |
2750 | } | |
7a14d768 | 2751 | #endif /* HAVE_DNSSD */ |
b423cd4c | 2752 | |
b423cd4c | 2753 | |
7a14d768 MS |
2754 | #ifdef __APPLE__ |
2755 | /* | |
2756 | * 'get_hostconfig()' - Get an /etc/hostconfig service setting. | |
2757 | */ | |
2758 | ||
2759 | static int /* O - 1 for YES or AUTOMATIC, 0 for NO */ | |
2760 | get_hostconfig(const char *name) /* I - Name of service */ | |
2761 | { | |
2762 | cups_file_t *fp; /* Hostconfig file */ | |
2763 | char line[1024], /* Line from file */ | |
2764 | *ptr; /* Pointer to value */ | |
2765 | int state = 1; /* State of service */ | |
ef416fc2 | 2766 | |
ef416fc2 | 2767 | |
2768 | /* | |
7a14d768 MS |
2769 | * Try opening the /etc/hostconfig file; if we can't open it, assume that |
2770 | * the service is enabled/auto. | |
ef416fc2 | 2771 | */ |
2772 | ||
7a14d768 | 2773 | if ((fp = cupsFileOpen("/etc/hostconfig", "r")) != NULL) |
e00b005a | 2774 | { |
2775 | /* | |
7a14d768 | 2776 | * Read lines from the file until we find the service... |
e00b005a | 2777 | */ |
2778 | ||
7a14d768 MS |
2779 | while (cupsFileGets(fp, line, sizeof(line))) |
2780 | { | |
2781 | if (line[0] == '#' || (ptr = strchr(line, '=')) == NULL) | |
2782 | continue; | |
2783 | ||
2784 | *ptr++ = '\0'; | |
2785 | ||
2786 | if (!strcasecmp(line, name)) | |
ef416fc2 | 2787 | { |
7a14d768 MS |
2788 | /* |
2789 | * Found the service, see if it is set to "-NO-"... | |
2790 | */ | |
2791 | ||
2792 | if (!strncasecmp(ptr, "-NO-", 4)) | |
2793 | state = 0; | |
2794 | break; | |
ef416fc2 | 2795 | } |
7a14d768 | 2796 | } |
ef416fc2 | 2797 | |
7a14d768 MS |
2798 | cupsFileClose(fp); |
2799 | } | |
ef416fc2 | 2800 | |
7a14d768 | 2801 | return (state); |
ef416fc2 | 2802 | } |
7a14d768 | 2803 | #endif /* __APPLE__ */ |
ef416fc2 | 2804 | |
2805 | ||
f7deaa1a | 2806 | /* |
7a14d768 | 2807 | * 'is_local_queue()' - Determine whether the URI points at a local queue. |
f7deaa1a | 2808 | */ |
2809 | ||
7a14d768 MS |
2810 | static int /* O - 1 = local, 0 = remote, -1 = bad URI */ |
2811 | is_local_queue(const char *uri, /* I - Printer URI */ | |
2812 | char *host, /* O - Host string */ | |
2813 | int hostlen, /* I - Length of host buffer */ | |
2814 | char *resource, /* O - Resource string */ | |
2815 | int resourcelen) /* I - Length of resource buffer */ | |
f7deaa1a | 2816 | { |
7a14d768 MS |
2817 | char scheme[32], /* Scheme portion of URI */ |
2818 | username[HTTP_MAX_URI]; /* Username portion of URI */ | |
2819 | int port; /* Port portion of URI */ | |
2820 | cupsd_netif_t *iface; /* Network interface */ | |
f7deaa1a | 2821 | |
2822 | ||
2823 | /* | |
7a14d768 | 2824 | * Pull the URI apart to see if this is a local or remote printer... |
f7deaa1a | 2825 | */ |
2826 | ||
7a14d768 MS |
2827 | if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), |
2828 | username, sizeof(username), host, hostlen, &port, | |
2829 | resource, resourcelen) < HTTP_URI_OK) | |
2830 | return (-1); | |
f7deaa1a | 2831 | |
7a14d768 | 2832 | DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host, ServerName)); |
f7deaa1a | 2833 | |
7a14d768 MS |
2834 | /* |
2835 | * Check for local server addresses... | |
2836 | */ | |
f7deaa1a | 2837 | |
7a14d768 MS |
2838 | if (!strcasecmp(host, ServerName) && port == LocalPort) |
2839 | return (1); | |
f7deaa1a | 2840 | |
7a14d768 | 2841 | cupsdNetIFUpdate(); |
f7deaa1a | 2842 | |
7a14d768 MS |
2843 | for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList); |
2844 | iface; | |
2845 | iface = (cupsd_netif_t *)cupsArrayNext(NetIFList)) | |
2846 | if (!strcasecmp(host, iface->hostname) && port == iface->port) | |
2847 | return (1); | |
f7deaa1a | 2848 | |
7a14d768 MS |
2849 | /* |
2850 | * If we get here, the printer is remote... | |
2851 | */ | |
f7deaa1a | 2852 | |
7a14d768 MS |
2853 | return (0); |
2854 | } | |
f7deaa1a | 2855 | |
f7deaa1a | 2856 | |
7a14d768 MS |
2857 | /* |
2858 | * 'process_browse_data()' - Process new browse data. | |
2859 | */ | |
f7deaa1a | 2860 | |
7a14d768 MS |
2861 | static void |
2862 | process_browse_data( | |
2863 | const char *uri, /* I - URI of printer/class */ | |
2864 | const char *host, /* I - Hostname */ | |
2865 | const char *resource, /* I - Resource path */ | |
2866 | cups_ptype_t type, /* I - Printer type */ | |
2867 | ipp_pstate_t state, /* I - Printer state */ | |
2868 | const char *location, /* I - Printer location */ | |
2869 | const char *info, /* I - Printer information */ | |
2870 | const char *make_model, /* I - Printer make and model */ | |
2871 | int num_attrs, /* I - Number of attributes */ | |
2872 | cups_option_t *attrs) /* I - Attributes */ | |
2873 | { | |
2874 | int i; /* Looping var */ | |
2875 | int update; /* Update printer attributes? */ | |
2876 | char finaluri[HTTP_MAX_URI], /* Final URI for printer */ | |
2877 | name[IPP_MAX_NAME], /* Name of printer */ | |
2878 | newname[IPP_MAX_NAME], /* New name of printer */ | |
2879 | *hptr, /* Pointer into hostname */ | |
2880 | *sptr; /* Pointer into ServerName */ | |
1f0275e3 | 2881 | const char *shortname; /* Short queue name (queue) */ |
7a14d768 MS |
2882 | char local_make_model[IPP_MAX_NAME]; |
2883 | /* Local make and model */ | |
2884 | cupsd_printer_t *p; /* Printer information */ | |
2885 | const char *ipp_options, /* ipp-options value */ | |
2886 | *lease_duration; /* lease-duration value */ | |
1f0275e3 MS |
2887 | int is_class; /* Is this queue a class? */ |
2888 | ||
f7deaa1a | 2889 | |
1f0275e3 MS |
2890 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
2891 | "process_browse_data(uri=\"%s\", host=\"%s\", " | |
2892 | "resource=\"%s\", type=%x, state=%d, location=\"%s\", " | |
2893 | "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)", | |
2894 | uri, host, resource, type, state, | |
2895 | location ? location : "(nil)", info ? info : "(nil)", | |
2896 | make_model ? make_model : "(nil)", num_attrs, attrs); | |
f7deaa1a | 2897 | |
7a14d768 MS |
2898 | /* |
2899 | * Determine if the URI contains any illegal characters in it... | |
2900 | */ | |
f7deaa1a | 2901 | |
7a14d768 MS |
2902 | if (strncmp(uri, "ipp://", 6) || !host[0] || |
2903 | (strncmp(resource, "/printers/", 10) && | |
2904 | strncmp(resource, "/classes/", 9))) | |
f7deaa1a | 2905 | { |
1f0275e3 | 2906 | cupsdLogMessage(CUPSD_LOG_ERROR, "Bad printer URI in browse data: %s", uri); |
7a14d768 | 2907 | return; |
f7deaa1a | 2908 | } |
2909 | ||
7a14d768 MS |
2910 | if (strchr(resource, '?') || |
2911 | (!strncmp(resource, "/printers/", 10) && strchr(resource + 10, '/')) || | |
2912 | (!strncmp(resource, "/classes/", 9) && strchr(resource + 9, '/'))) | |
f7deaa1a | 2913 | { |
1f0275e3 | 2914 | cupsdLogMessage(CUPSD_LOG_ERROR, "Bad resource in browse data: %s", |
7a14d768 MS |
2915 | resource); |
2916 | return; | |
f7deaa1a | 2917 | } |
2918 | ||
7a14d768 MS |
2919 | /* |
2920 | * OK, this isn't a local printer; add any remote options... | |
2921 | */ | |
f7deaa1a | 2922 | |
7a14d768 MS |
2923 | ipp_options = cupsGetOption("ipp-options", num_attrs, attrs); |
2924 | ||
2925 | if (BrowseRemoteOptions) | |
f7deaa1a | 2926 | { |
7a14d768 MS |
2927 | if (BrowseRemoteOptions[0] == '?') |
2928 | { | |
2929 | /* | |
2930 | * Override server-supplied options... | |
2931 | */ | |
f7deaa1a | 2932 | |
7a14d768 MS |
2933 | snprintf(finaluri, sizeof(finaluri), "%s%s", uri, BrowseRemoteOptions); |
2934 | } | |
2935 | else if (ipp_options) | |
2936 | { | |
2937 | /* | |
2938 | * Combine the server and local options... | |
2939 | */ | |
f7deaa1a | 2940 | |
7a14d768 MS |
2941 | snprintf(finaluri, sizeof(finaluri), "%s?%s+%s", uri, ipp_options, |
2942 | BrowseRemoteOptions); | |
2943 | } | |
2944 | else | |
2945 | { | |
2946 | /* | |
2947 | * Just use the local options... | |
2948 | */ | |
f7deaa1a | 2949 | |
7a14d768 MS |
2950 | snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, BrowseRemoteOptions); |
2951 | } | |
f7deaa1a | 2952 | |
7a14d768 | 2953 | uri = finaluri; |
f7deaa1a | 2954 | } |
7a14d768 | 2955 | else if (ipp_options) |
7594b224 | 2956 | { |
7a14d768 MS |
2957 | /* |
2958 | * Just use the server-supplied options... | |
2959 | */ | |
7594b224 | 2960 | |
7a14d768 MS |
2961 | snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, ipp_options); |
2962 | uri = finaluri; | |
7594b224 | 2963 | } |
2964 | ||
f7deaa1a | 2965 | /* |
7a14d768 | 2966 | * See if we already have it listed in the Printers list, and add it if not... |
f7deaa1a | 2967 | */ |
2968 | ||
1f0275e3 MS |
2969 | type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED; |
2970 | type &= ~CUPS_PRINTER_IMPLICIT; | |
2971 | update = 0; | |
2972 | hptr = strchr(host, '.'); | |
2973 | sptr = strchr(ServerName, '.'); | |
2974 | is_class = type & CUPS_PRINTER_CLASS; | |
f7deaa1a | 2975 | |
7a14d768 | 2976 | if (!ServerNameIsIP && sptr != NULL && hptr != NULL) |
f7deaa1a | 2977 | { |
7a14d768 MS |
2978 | /* |
2979 | * Strip the common domain name components... | |
2980 | */ | |
f7deaa1a | 2981 | |
7a14d768 MS |
2982 | while (hptr != NULL) |
2983 | { | |
2984 | if (!strcasecmp(hptr, sptr)) | |
2985 | { | |
2986 | *hptr = '\0'; | |
2987 | break; | |
2988 | } | |
2989 | else | |
2990 | hptr = strchr(hptr + 1, '.'); | |
2991 | } | |
2992 | } | |
f7deaa1a | 2993 | |
1f0275e3 | 2994 | if (is_class) |
f7deaa1a | 2995 | { |
2996 | /* | |
7a14d768 | 2997 | * Remote destination is a class... |
f7deaa1a | 2998 | */ |
2999 | ||
7a14d768 MS |
3000 | if (!strncmp(resource, "/classes/", 9)) |
3001 | snprintf(name, sizeof(name), "%s@%s", resource + 9, host); | |
3002 | else | |
3003 | return; | |
f7deaa1a | 3004 | |
1f0275e3 MS |
3005 | shortname = resource + 9; |
3006 | } | |
3007 | else | |
3008 | { | |
3009 | /* | |
3010 | * Remote destination is a printer... | |
3011 | */ | |
f7deaa1a | 3012 | |
1f0275e3 MS |
3013 | if (!strncmp(resource, "/printers/", 10)) |
3014 | snprintf(name, sizeof(name), "%s@%s", resource + 10, host); | |
3015 | else | |
3016 | return; | |
f7deaa1a | 3017 | |
1f0275e3 MS |
3018 | shortname = resource + 10; |
3019 | } | |
f7deaa1a | 3020 | |
1f0275e3 MS |
3021 | if (hptr && !*hptr) |
3022 | *hptr = '.'; /* Resource FQDN */ | |
f7deaa1a | 3023 | |
1f0275e3 MS |
3024 | if ((p = cupsdFindDest(name)) == NULL && BrowseShortNames) |
3025 | { | |
3026 | /* | |
3027 | * Long name doesn't exist, try short name... | |
3028 | */ | |
3029 | ||
3030 | cupsdLogMessage(CUPSD_LOG_DEBUG, "process_browse_data: %s not found...", | |
3031 | name); | |
3032 | ||
3033 | if ((p = cupsdFindDest(shortname)) == NULL) | |
7a14d768 MS |
3034 | { |
3035 | /* | |
1f0275e3 | 3036 | * Short name doesn't exist, use it for this shared queue. |
7a14d768 | 3037 | */ |
f7deaa1a | 3038 | |
1f0275e3 MS |
3039 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "process_browse_data: %s not found...", |
3040 | shortname); | |
3041 | strlcpy(name, shortname, sizeof(name)); | |
7a14d768 | 3042 | } |
1f0275e3 | 3043 | else |
f7deaa1a | 3044 | { |
3045 | /* | |
1f0275e3 | 3046 | * Short name exists... |
f7deaa1a | 3047 | */ |
3048 | ||
1f0275e3 MS |
3049 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
3050 | "process_browse_data: %s found, type=%x, hostname=%s...", | |
3051 | shortname, p->type, p->hostname ? p->hostname : "(nil)"); | |
f7deaa1a | 3052 | |
1f0275e3 MS |
3053 | if (p->type & CUPS_PRINTER_IMPLICIT) |
3054 | p = NULL; /* Don't replace implicit classes */ | |
3055 | else if (p->hostname && strcasecmp(p->hostname, host)) | |
3056 | { | |
3057 | /* | |
3058 | * Short name exists but is for a different host. If this is a remote | |
3059 | * queue, rename it and use the long name... | |
3060 | */ | |
f7deaa1a | 3061 | |
1f0275e3 MS |
3062 | if (p->type & CUPS_PRINTER_REMOTE) |
3063 | { | |
3064 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
3065 | "Renamed remote %s \"%s\" to \"%s@%s\"...", | |
3066 | is_class ? "class" : "printer", p->name, p->name, | |
3067 | p->hostname); | |
3068 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL, | |
3069 | "%s \'%s\' deleted by directory services.", | |
3070 | is_class ? "Class" : "Printer", p->name); | |
3071 | ||
3072 | snprintf(newname, sizeof(newname), "%s@%s", p->name, p->hostname); | |
3073 | cupsdRenamePrinter(p, newname); | |
3074 | ||
3075 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL, | |
3076 | "%s \'%s\' added by directory services.", | |
3077 | is_class ? "Class" : "Printer", p->name); | |
3078 | } | |
f7deaa1a | 3079 | |
1f0275e3 MS |
3080 | /* |
3081 | * Force creation with long name... | |
3082 | */ | |
f7deaa1a | 3083 | |
1f0275e3 MS |
3084 | p = NULL; |
3085 | } | |
7a14d768 MS |
3086 | } |
3087 | } | |
5d6412a9 | 3088 | else if (p) |
1f0275e3 MS |
3089 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
3090 | "process_browse_data: %s found, type=%x, hostname=%s...", | |
3091 | name, p->type, p->hostname ? p->hostname : "(nil)"); | |
3092 | ||
3093 | if (!p) | |
7a14d768 MS |
3094 | { |
3095 | /* | |
1f0275e3 | 3096 | * Queue doesn't exist; add it... |
7a14d768 | 3097 | */ |
f7deaa1a | 3098 | |
1f0275e3 MS |
3099 | if (is_class) |
3100 | p = cupsdAddClass(name); | |
7a14d768 | 3101 | else |
1f0275e3 | 3102 | p = cupsdAddPrinter(name); |
f7deaa1a | 3103 | |
0af14961 MS |
3104 | if (!p) |
3105 | return; | |
3106 | ||
1f0275e3 | 3107 | cupsdClearString(&(p->hostname)); |
f7deaa1a | 3108 | |
1f0275e3 MS |
3109 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added remote %s \"%s\"...", |
3110 | is_class ? "class" : "printer", name); | |
f7deaa1a | 3111 | |
1f0275e3 MS |
3112 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL, |
3113 | "%s \'%s\' added by directory services.", | |
3114 | is_class ? "Class" : "Printer", name); | |
f7deaa1a | 3115 | |
1f0275e3 MS |
3116 | /* |
3117 | * Force the URI to point to the real server... | |
3118 | */ | |
f7deaa1a | 3119 | |
1f0275e3 MS |
3120 | p->type = type & ~CUPS_PRINTER_REJECTING; |
3121 | p->accepting = 1; | |
f7deaa1a | 3122 | |
1f0275e3 MS |
3123 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP); |
3124 | } | |
f7deaa1a | 3125 | |
1f0275e3 MS |
3126 | if (!p->hostname) |
3127 | { | |
3128 | /* | |
3129 | * Hostname not set, so this must be a cached remote printer | |
3130 | * that was created for a pending print job... | |
3131 | */ | |
f7deaa1a | 3132 | |
1f0275e3 MS |
3133 | cupsdSetString(&p->hostname, host); |
3134 | cupsdSetString(&p->uri, uri); | |
3135 | cupsdSetString(&p->device_uri, uri); | |
3136 | update = 1; | |
f7deaa1a | 3137 | |
1f0275e3 | 3138 | cupsdMarkDirty(CUPSD_DIRTY_REMOTE); |
7a14d768 | 3139 | } |
f7deaa1a | 3140 | |
3141 | /* | |
7a14d768 | 3142 | * Update the state... |
f7deaa1a | 3143 | */ |
3144 | ||
7a14d768 MS |
3145 | p->state = state; |
3146 | p->browse_time = time(NULL); | |
f7deaa1a | 3147 | |
7a14d768 MS |
3148 | if ((lease_duration = cupsGetOption("lease-duration", num_attrs, |
3149 | attrs)) != NULL) | |
f7deaa1a | 3150 | { |
3151 | /* | |
7a14d768 MS |
3152 | * Grab the lease-duration for the browse data; anything less then 1 |
3153 | * second or more than 1 week gets the default BrowseTimeout... | |
f7deaa1a | 3154 | */ |
3155 | ||
7a14d768 MS |
3156 | i = atoi(lease_duration); |
3157 | if (i < 1 || i > 604800) | |
3158 | i = BrowseTimeout; | |
3159 | ||
3160 | p->browse_expire = p->browse_time + i; | |
3161 | } | |
3162 | else | |
3163 | p->browse_expire = p->browse_time + BrowseTimeout; | |
f7deaa1a | 3164 | |
7a14d768 MS |
3165 | if (type & CUPS_PRINTER_REJECTING) |
3166 | { | |
3167 | type &= ~CUPS_PRINTER_REJECTING; | |
f7deaa1a | 3168 | |
7a14d768 | 3169 | if (p->accepting) |
f7deaa1a | 3170 | { |
7a14d768 MS |
3171 | update = 1; |
3172 | p->accepting = 0; | |
f7deaa1a | 3173 | } |
7a14d768 MS |
3174 | } |
3175 | else if (!p->accepting) | |
3176 | { | |
3177 | update = 1; | |
3178 | p->accepting = 1; | |
3179 | } | |
f7deaa1a | 3180 | |
7a14d768 MS |
3181 | if (p->type != type) |
3182 | { | |
3183 | p->type = type; | |
3184 | update = 1; | |
3185 | } | |
080811b1 | 3186 | |
7a14d768 MS |
3187 | if (location && (!p->location || strcmp(p->location, location))) |
3188 | { | |
3189 | cupsdSetString(&p->location, location); | |
3190 | update = 1; | |
3191 | } | |
080811b1 | 3192 | |
7a14d768 MS |
3193 | if (info && (!p->info || strcmp(p->info, info))) |
3194 | { | |
3195 | cupsdSetString(&p->info, info); | |
3196 | update = 1; | |
080811b1 | 3197 | |
7a14d768 MS |
3198 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE); |
3199 | } | |
f7deaa1a | 3200 | |
7a14d768 MS |
3201 | if (!make_model || !make_model[0]) |
3202 | { | |
1f0275e3 | 3203 | if (is_class) |
7a14d768 MS |
3204 | snprintf(local_make_model, sizeof(local_make_model), |
3205 | "Remote Class on %s", host); | |
3206 | else | |
3207 | snprintf(local_make_model, sizeof(local_make_model), | |
3208 | "Remote Printer on %s", host); | |
3209 | } | |
3210 | else | |
3211 | snprintf(local_make_model, sizeof(local_make_model), | |
3212 | "%s on %s", make_model, host); | |
f7deaa1a | 3213 | |
7a14d768 MS |
3214 | if (!p->make_model || strcmp(p->make_model, local_make_model)) |
3215 | { | |
3216 | cupsdSetString(&p->make_model, local_make_model); | |
3217 | update = 1; | |
3218 | } | |
080811b1 | 3219 | |
7a14d768 MS |
3220 | if (p->num_options) |
3221 | { | |
3222 | if (!update && !(type & CUPS_PRINTER_DELETE)) | |
3223 | { | |
3224 | /* | |
3225 | * See if we need to update the attributes... | |
3226 | */ | |
f7deaa1a | 3227 | |
7a14d768 MS |
3228 | if (p->num_options != num_attrs) |
3229 | update = 1; | |
3230 | else | |
3231 | { | |
3232 | for (i = 0; i < num_attrs; i ++) | |
3233 | if (strcmp(attrs[i].name, p->options[i].name) || | |
3234 | (!attrs[i].value != !p->options[i].value) || | |
3235 | (attrs[i].value && strcmp(attrs[i].value, p->options[i].value))) | |
3236 | { | |
3237 | update = 1; | |
3238 | break; | |
3239 | } | |
3240 | } | |
3241 | } | |
f7deaa1a | 3242 | |
3243 | /* | |
7a14d768 | 3244 | * Free the old options... |
f7deaa1a | 3245 | */ |
3246 | ||
7a14d768 MS |
3247 | cupsFreeOptions(p->num_options, p->options); |
3248 | } | |
f7deaa1a | 3249 | |
7a14d768 MS |
3250 | p->num_options = num_attrs; |
3251 | p->options = attrs; | |
f7deaa1a | 3252 | |
7a14d768 MS |
3253 | if (type & CUPS_PRINTER_DELETE) |
3254 | { | |
3255 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL, | |
3256 | "%s \'%s\' deleted by directory services.", | |
1f0275e3 | 3257 | is_class ? "Class" : "Printer", p->name); |
f7deaa1a | 3258 | |
7a14d768 MS |
3259 | cupsdExpireSubscriptions(p, NULL); |
3260 | ||
3261 | cupsdDeletePrinter(p, 1); | |
3262 | cupsdUpdateImplicitClasses(); | |
3263 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE); | |
f7deaa1a | 3264 | } |
7a14d768 | 3265 | else if (update) |
f7deaa1a | 3266 | { |
7a14d768 MS |
3267 | cupsdSetPrinterAttrs(p); |
3268 | cupsdUpdateImplicitClasses(); | |
3269 | } | |
f7deaa1a | 3270 | |
7a14d768 MS |
3271 | /* |
3272 | * See if we have a default printer... If not, make the first network | |
3273 | * default printer the default. | |
3274 | */ | |
f7deaa1a | 3275 | |
7a14d768 MS |
3276 | if (DefaultPrinter == NULL && Printers != NULL && UseNetworkDefault) |
3277 | { | |
3278 | /* | |
3279 | * Find the first network default printer and use it... | |
3280 | */ | |
f7deaa1a | 3281 | |
7a14d768 MS |
3282 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); |
3283 | p; | |
3284 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
3285 | if (p->type & CUPS_PRINTER_DEFAULT) | |
3286 | { | |
3287 | DefaultPrinter = p; | |
3288 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE); | |
3289 | break; | |
3290 | } | |
f7deaa1a | 3291 | } |
3292 | ||
7a14d768 MS |
3293 | /* |
3294 | * Do auto-classing if needed... | |
3295 | */ | |
f7deaa1a | 3296 | |
7a14d768 | 3297 | process_implicit_classes(); |
f7deaa1a | 3298 | } |
f7deaa1a | 3299 | |
3300 | ||
ef416fc2 | 3301 | /* |
e00b005a | 3302 | * 'process_implicit_classes()' - Create/update implicit classes as needed. |
ef416fc2 | 3303 | */ |
3304 | ||
e00b005a | 3305 | static void |
3dfe78b3 | 3306 | process_implicit_classes(void) |
ef416fc2 | 3307 | { |
e00b005a | 3308 | int i; /* Looping var */ |
3309 | int update; /* Update printer attributes? */ | |
3310 | char name[IPP_MAX_NAME], /* Name of printer */ | |
3311 | *hptr; /* Pointer into hostname */ | |
3312 | cupsd_printer_t *p, /* Printer information */ | |
3313 | *pclass, /* Printer class */ | |
3314 | *first; /* First printer in class */ | |
3315 | int offset, /* Offset of name */ | |
3316 | len; /* Length of name */ | |
ef416fc2 | 3317 | |
3318 | ||
e00b005a | 3319 | if (!ImplicitClasses || !Printers) |
3320 | return; | |
ef416fc2 | 3321 | |
e00b005a | 3322 | /* |
3323 | * Loop through all available printers and create classes as needed... | |
3324 | */ | |
3325 | ||
3326 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers), len = 0, offset = 0, | |
3327 | update = 0, pclass = NULL, first = NULL; | |
3328 | p != NULL; | |
3329 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
ef416fc2 | 3330 | { |
3331 | /* | |
e00b005a | 3332 | * Skip implicit classes... |
ef416fc2 | 3333 | */ |
3334 | ||
e00b005a | 3335 | if (p->type & CUPS_PRINTER_IMPLICIT) |
3336 | { | |
3337 | len = 0; | |
3338 | continue; | |
3339 | } | |
ef416fc2 | 3340 | |
e00b005a | 3341 | /* |
3342 | * If len == 0, get the length of this printer name up to the "@" | |
3343 | * sign (if any). | |
3344 | */ | |
ef416fc2 | 3345 | |
e00b005a | 3346 | cupsArraySave(Printers); |
ef416fc2 | 3347 | |
e00b005a | 3348 | if (len > 0 && |
3349 | !strncasecmp(p->name, name + offset, len) && | |
3350 | (p->name[len] == '\0' || p->name[len] == '@')) | |
3351 | { | |
3352 | /* | |
3353 | * We have more than one printer with the same name; see if | |
3354 | * we have a class, and if this printer is a member... | |
3355 | */ | |
ef416fc2 | 3356 | |
e00b005a | 3357 | if (pclass && strcasecmp(pclass->name, name)) |
3358 | { | |
3359 | if (update) | |
3360 | cupsdSetPrinterAttrs(pclass); | |
ef416fc2 | 3361 | |
e00b005a | 3362 | update = 0; |
3363 | pclass = NULL; | |
3364 | } | |
ef416fc2 | 3365 | |
e00b005a | 3366 | if (!pclass && (pclass = cupsdFindDest(name)) == NULL) |
3367 | { | |
3368 | /* | |
3369 | * Need to add the class... | |
3370 | */ | |
ef416fc2 | 3371 | |
e00b005a | 3372 | pclass = cupsdAddPrinter(name); |
3373 | cupsArrayAdd(ImplicitPrinters, pclass); | |
ef416fc2 | 3374 | |
e00b005a | 3375 | pclass->type |= CUPS_PRINTER_IMPLICIT; |
3376 | pclass->accepting = 1; | |
3377 | pclass->state = IPP_PRINTER_IDLE; | |
ef416fc2 | 3378 | |
e00b005a | 3379 | cupsdSetString(&pclass->location, p->location); |
3380 | cupsdSetString(&pclass->info, p->info); | |
ef416fc2 | 3381 | |
7594b224 | 3382 | cupsdSetString(&pclass->job_sheets[0], p->job_sheets[0]); |
3383 | cupsdSetString(&pclass->job_sheets[1], p->job_sheets[1]); | |
3384 | ||
e00b005a | 3385 | update = 1; |
ef416fc2 | 3386 | |
7a14d768 | 3387 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE); |
c277e2f8 | 3388 | |
89d46774 | 3389 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added implicit class \"%s\"...", |
e00b005a | 3390 | name); |
b423cd4c | 3391 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL, |
3392 | "Implicit class \'%s\' added by directory services.", | |
3393 | name); | |
e00b005a | 3394 | } |
ef416fc2 | 3395 | |
e00b005a | 3396 | if (first != NULL) |
3397 | { | |
3398 | for (i = 0; i < pclass->num_printers; i ++) | |
3399 | if (pclass->printers[i] == first) | |
3400 | break; | |
ef416fc2 | 3401 | |
e00b005a | 3402 | if (i >= pclass->num_printers) |
3403 | { | |
3404 | first->in_implicit_class = 1; | |
3405 | cupsdAddPrinterToClass(pclass, first); | |
3406 | } | |
ef416fc2 | 3407 | |
e00b005a | 3408 | first = NULL; |
3409 | } | |
ef416fc2 | 3410 | |
e00b005a | 3411 | for (i = 0; i < pclass->num_printers; i ++) |
3412 | if (pclass->printers[i] == p) | |
3413 | break; | |
ef416fc2 | 3414 | |
e00b005a | 3415 | if (i >= pclass->num_printers) |
3416 | { | |
3417 | p->in_implicit_class = 1; | |
3418 | cupsdAddPrinterToClass(pclass, p); | |
3419 | update = 1; | |
3420 | } | |
3421 | } | |
3422 | else | |
ef416fc2 | 3423 | { |
3424 | /* | |
e00b005a | 3425 | * First time around; just get name length and mark it as first |
3426 | * in the list... | |
ef416fc2 | 3427 | */ |
3428 | ||
e00b005a | 3429 | if ((hptr = strchr(p->name, '@')) != NULL) |
3430 | len = hptr - p->name; | |
3431 | else | |
3432 | len = strlen(p->name); | |
ef416fc2 | 3433 | |
e00b005a | 3434 | strncpy(name, p->name, len); |
3435 | name[len] = '\0'; | |
3436 | offset = 0; | |
ef416fc2 | 3437 | |
e00b005a | 3438 | if ((first = (hptr ? cupsdFindDest(name) : p)) != NULL && |
3439 | !(first->type & CUPS_PRINTER_IMPLICIT)) | |
3440 | { | |
3441 | /* | |
3442 | * Can't use same name as a local printer; add "Any" to the | |
3443 | * front of the name, unless we have explicitly disabled | |
3444 | * the "ImplicitAnyClasses"... | |
3445 | */ | |
ef416fc2 | 3446 | |
e00b005a | 3447 | if (ImplicitAnyClasses && len < (sizeof(name) - 4)) |
3448 | { | |
3449 | /* | |
3450 | * Add "Any" to the class name... | |
3451 | */ | |
3452 | ||
3453 | strcpy(name, "Any"); | |
3454 | strncpy(name + 3, p->name, len); | |
3455 | name[len + 3] = '\0'; | |
3456 | offset = 3; | |
3457 | } | |
3458 | else | |
3459 | { | |
3460 | /* | |
3461 | * Don't create an implicit class if we have a local printer | |
3462 | * with the same name... | |
3463 | */ | |
3464 | ||
3465 | len = 0; | |
3466 | cupsArrayRestore(Printers); | |
3467 | continue; | |
3468 | } | |
3469 | } | |
3470 | ||
3471 | first = p; | |
ef416fc2 | 3472 | } |
3473 | ||
e00b005a | 3474 | cupsArrayRestore(Printers); |
3475 | } | |
ef416fc2 | 3476 | |
e00b005a | 3477 | /* |
3478 | * Update the last printer class as needed... | |
3479 | */ | |
ef416fc2 | 3480 | |
e00b005a | 3481 | if (pclass && update) |
3482 | cupsdSetPrinterAttrs(pclass); | |
ef416fc2 | 3483 | } |
3484 | ||
3485 | ||
e1d6a774 | 3486 | /* |
3487 | * 'send_cups_browse()' - Send new browsing information using the CUPS | |
7594b224 | 3488 | * protocol. |
e1d6a774 | 3489 | */ |
3490 | ||
3491 | static void | |
3492 | send_cups_browse(cupsd_printer_t *p) /* I - Printer to send */ | |
3493 | { | |
3494 | int i; /* Looping var */ | |
3495 | cups_ptype_t type; /* Printer type */ | |
3496 | cupsd_dirsvc_addr_t *b; /* Browse address */ | |
3497 | int bytes; /* Length of packet */ | |
3498 | char packet[1453], /* Browse data packet */ | |
3499 | uri[1024], /* Printer URI */ | |
3500 | location[1024], /* printer-location */ | |
3501 | info[1024], /* printer-info */ | |
3502 | make_model[1024]; | |
3503 | /* printer-make-and-model */ | |
3504 | cupsd_netif_t *iface; /* Network interface */ | |
3505 | ||
3506 | ||
3507 | /* | |
3508 | * Figure out the printer type value... | |
3509 | */ | |
3510 | ||
3511 | type = p->type | CUPS_PRINTER_REMOTE; | |
3512 | ||
3513 | if (!p->accepting) | |
3514 | type |= CUPS_PRINTER_REJECTING; | |
3515 | ||
3516 | if (p == DefaultPrinter) | |
3517 | type |= CUPS_PRINTER_DEFAULT; | |
3518 | ||
3519 | /* | |
3520 | * Remove quotes from printer-info, printer-location, and | |
3521 | * printer-make-and-model attributes... | |
3522 | */ | |
3523 | ||
3524 | dequote(location, p->location, sizeof(location)); | |
3525 | dequote(info, p->info, sizeof(info)); | |
d6ae789d | 3526 | |
3527 | if (p->make_model) | |
3528 | dequote(make_model, p->make_model, sizeof(make_model)); | |
3529 | else if (p->type & CUPS_PRINTER_CLASS) | |
3530 | { | |
3531 | if (p->num_printers > 0 && p->printers[0]->make_model) | |
3532 | strlcpy(make_model, p->printers[0]->make_model, sizeof(make_model)); | |
3533 | else | |
3534 | strlcpy(make_model, "Local Printer Class", sizeof(make_model)); | |
3535 | } | |
3536 | else if (p->raw) | |
3537 | strlcpy(make_model, "Local Raw Printer", sizeof(make_model)); | |
3538 | else | |
3539 | strlcpy(make_model, "Local System V Printer", sizeof(make_model)); | |
e1d6a774 | 3540 | |
3541 | /* | |
3542 | * Send a packet to each browse address... | |
3543 | */ | |
3544 | ||
3545 | for (i = NumBrowsers, b = Browsers; i > 0; i --, b ++) | |
3546 | if (b->iface[0]) | |
3547 | { | |
3548 | /* | |
3549 | * Send the browse packet to one or more interfaces... | |
3550 | */ | |
3551 | ||
3552 | if (!strcmp(b->iface, "*")) | |
3553 | { | |
3554 | /* | |
3555 | * Send to all local interfaces... | |
3556 | */ | |
3557 | ||
3558 | cupsdNetIFUpdate(); | |
3559 | ||
3560 | for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList); | |
3561 | iface; | |
3562 | iface = (cupsd_netif_t *)cupsArrayNext(NetIFList)) | |
3563 | { | |
3564 | /* | |
3565 | * Only send to local, IPv4 interfaces... | |
3566 | */ | |
3567 | ||
3568 | if (!iface->is_local || !iface->port || | |
3569 | iface->address.addr.sa_family != AF_INET) | |
3570 | continue; | |
3571 | ||
3572 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
3573 | iface->hostname, iface->port, | |
ed486911 | 3574 | (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s" : |
e1d6a774 | 3575 | "/printers/%s", |
3576 | p->name); | |
3577 | snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n", | |
3578 | type, p->state, uri, location, info, make_model, | |
3579 | p->browse_attrs ? p->browse_attrs : ""); | |
3580 | ||
3581 | bytes = strlen(packet); | |
3582 | ||
3583 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3584 | "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes, | |
3585 | iface->name, packet); | |
3586 | ||
3587 | iface->broadcast.ipv4.sin_port = htons(BrowsePort); | |
3588 | ||
3589 | sendto(BrowseSocket, packet, bytes, 0, | |
3590 | (struct sockaddr *)&(iface->broadcast), | |
d09495fa | 3591 | httpAddrLength(&(iface->broadcast))); |
e1d6a774 | 3592 | } |
3593 | } | |
3594 | else if ((iface = cupsdNetIFFind(b->iface)) != NULL) | |
3595 | { | |
3596 | /* | |
3597 | * Send to the named interface using the IPv4 address... | |
3598 | */ | |
3599 | ||
3600 | while (iface) | |
3601 | if (strcmp(b->iface, iface->name)) | |
3602 | { | |
3603 | iface = NULL; | |
3604 | break; | |
3605 | } | |
3606 | else if (iface->address.addr.sa_family == AF_INET && iface->port) | |
3607 | break; | |
3608 | else | |
3609 | iface = (cupsd_netif_t *)cupsArrayNext(NetIFList); | |
3610 | ||
3611 | if (iface) | |
3612 | { | |
3613 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
3614 | iface->hostname, iface->port, | |
b94498cf | 3615 | (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s" : |
e1d6a774 | 3616 | "/printers/%s", |
3617 | p->name); | |
3618 | snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n", | |
3619 | type, p->state, uri, location, info, make_model, | |
3620 | p->browse_attrs ? p->browse_attrs : ""); | |
3621 | ||
3622 | bytes = strlen(packet); | |
3623 | ||
3624 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3625 | "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes, | |
3626 | iface->name, packet); | |
3627 | ||
3628 | iface->broadcast.ipv4.sin_port = htons(BrowsePort); | |
3629 | ||
3630 | sendto(BrowseSocket, packet, bytes, 0, | |
3631 | (struct sockaddr *)&(iface->broadcast), | |
d09495fa | 3632 | httpAddrLength(&(iface->broadcast))); |
e1d6a774 | 3633 | } |
3634 | } | |
3635 | } | |
3636 | else | |
3637 | { | |
3638 | /* | |
3639 | * Send the browse packet to the indicated address using | |
3640 | * the default server name... | |
3641 | */ | |
3642 | ||
3643 | snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n", | |
3644 | type, p->state, p->uri, location, info, make_model, | |
3645 | p->browse_attrs ? p->browse_attrs : ""); | |
3646 | ||
3647 | bytes = strlen(packet); | |
3648 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3649 | "cupsdSendBrowseList: (%d bytes) %s", bytes, packet); | |
3650 | ||
3651 | if (sendto(BrowseSocket, packet, bytes, 0, | |
3652 | (struct sockaddr *)&(b->to), | |
d09495fa | 3653 | httpAddrLength(&(b->to))) <= 0) |
e1d6a774 | 3654 | { |
3655 | /* | |
3656 | * Unable to send browse packet, so remove this address from the | |
3657 | * list... | |
3658 | */ | |
3659 | ||
3660 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3661 | "cupsdSendBrowseList: sendto failed for browser " | |
3662 | "%d - %s.", | |
3663 | (int)(b - Browsers + 1), strerror(errno)); | |
3664 | ||
3665 | if (i > 1) | |
3666 | memmove(b, b + 1, (i - 1) * sizeof(cupsd_dirsvc_addr_t)); | |
3667 | ||
3668 | b --; | |
3669 | NumBrowsers --; | |
3670 | } | |
3671 | } | |
3672 | } | |
3673 | ||
3674 | ||
749b1e90 MS |
3675 | #ifdef HAVE_LDAP |
3676 | /* | |
3677 | * 'ldap_search_rec()' - LDAP Search with reconnect | |
3678 | */ | |
3679 | ||
3680 | static int | |
3681 | ldap_search_rec(LDAP *ld, /* I - LDAP handler */ | |
3682 | char *base, /* I - Base dn */ | |
3683 | int scope, /* I - LDAP search scope */ | |
3684 | char *filter, /* I - Filter string */ | |
3685 | char *attrs[], /* I - Requested attributes */ | |
3686 | int attrsonly, /* I - Return only attributes? */ | |
3687 | LDAPMessage **res) /* I - LDAP handler */ | |
3688 | { | |
3689 | int rc; /* Return code */ | |
3690 | ||
3691 | ||
3692 | # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 | |
3693 | rc = ldap_search_ext_s(ld, base, scope, filter, attrs, attrsonly, NULL, NULL, | |
3694 | NULL, LDAP_NO_LIMIT, res); | |
3695 | # else | |
3696 | rc = ldap_search_s(ld, base, scope, filter, attrs, attrsonly, res); | |
3697 | # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */ | |
3698 | ||
3699 | /* | |
3700 | * If we have a connection problem try again... | |
3701 | */ | |
3702 | ||
3703 | if (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR) | |
3704 | { | |
3705 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3706 | "LDAP search failed with status %d: %s", | |
3707 | rc, ldap_err2string(rc)); | |
3708 | cupsdLogMessage(CUPSD_LOG_INFO, | |
3709 | "We try the LDAP search once again after reconnecting to " | |
3710 | "the server"); | |
3711 | ldap_freeres(*res); | |
3712 | ldap_reconnect(); | |
3713 | ||
3714 | # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 | |
3715 | rc = ldap_search_ext_s(ld, base, scope, filter, attrs, attrsonly, NULL, | |
3716 | NULL, NULL, LDAP_NO_LIMIT, res); | |
3717 | # else | |
3718 | rc = ldap_search_s(ld, base, scope, filter, attrs, attrsonly, res); | |
3719 | # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */ | |
3720 | } | |
3721 | ||
3722 | if (rc == LDAP_NO_SUCH_OBJECT) | |
3723 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
3724 | "ldap_search_rec: LDAP entry/object not found"); | |
3725 | else if (rc != LDAP_SUCCESS) | |
3726 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3727 | "ldap_search_rec: LDAP search failed with status %d: %s", | |
3728 | rc, ldap_err2string(rc)); | |
3729 | ||
3730 | if (rc != LDAP_SUCCESS) | |
3731 | ldap_freeres(*res); | |
3732 | ||
3733 | return (rc); | |
3734 | } | |
3735 | ||
3736 | ||
3737 | /* | |
3738 | * 'ldap_freeres()' - Free LDAPMessage | |
3739 | */ | |
3740 | ||
3741 | static void | |
3742 | ldap_freeres(LDAPMessage *entry) /* I - LDAP handler */ | |
3743 | { | |
3744 | int rc; /* Return value */ | |
3745 | ||
3746 | ||
3747 | rc = ldap_msgfree(entry); | |
3748 | if (rc == -1) | |
3749 | cupsdLogMessage(CUPSD_LOG_WARN, | |
3750 | "Can't free LDAPMessage!"); | |
3751 | else if (rc == 0) | |
3752 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3753 | "Freeing LDAPMessage was unnecessary"); | |
3754 | } | |
3755 | ||
3756 | ||
3757 | /* | |
3758 | * 'ldap_getval_char()' - Get first LDAP value and convert to string | |
3759 | */ | |
3760 | ||
3761 | static int | |
3762 | ldap_getval_firststring( | |
3763 | LDAP *ld, /* I - LDAP handler */ | |
3764 | LDAPMessage *entry, /* I - LDAP message or search result */ | |
3765 | char *attr, /* I - the wanted attribute */ | |
3766 | char *retval, /* O - String to return */ | |
3767 | unsigned long maxsize) /* I - Max string size */ | |
3768 | { | |
3769 | char *dn; /* LDAP DN */ | |
3770 | int rc = 0; /* Return code */ | |
3771 | # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 | |
3772 | struct berval **bval; /* LDAP value array */ | |
3773 | unsigned long size; /* String size */ | |
3774 | ||
3775 | ||
3776 | /* | |
3777 | * Get value from LDAPMessage... | |
3778 | */ | |
3779 | ||
3780 | if ((bval = ldap_get_values_len(ld, entry, attr)) == NULL) | |
3781 | { | |
3782 | rc = -1; | |
3783 | dn = ldap_get_dn(ld, entry); | |
3784 | cupsdLogMessage(CUPSD_LOG_WARN, | |
3785 | "Failed to get LDAP value %s for %s!", | |
3786 | attr, dn); | |
3787 | ldap_memfree(dn); | |
3788 | } | |
3789 | else | |
3790 | { | |
3791 | ||
3792 | /* | |
3793 | * Check size and copy value into our string... | |
3794 | */ | |
3795 | ||
3796 | size = maxsize; | |
3797 | if (size < bval[0]->bv_len) | |
3798 | { | |
3799 | rc = -1; | |
3800 | dn = ldap_get_dn(ld, entry); | |
3801 | cupsdLogMessage(CUPSD_LOG_WARN, | |
3802 | "Attribute %s is too big! (dn: %s)", | |
3803 | attr, dn); | |
3804 | ldap_memfree(dn); | |
3805 | } | |
3806 | else | |
3807 | size = bval[0]->bv_len; | |
3808 | ||
3809 | strlcpy(retval, bval[0]->bv_val, size); | |
3810 | ldap_value_free_len(bval); | |
3811 | } | |
3812 | # else | |
3813 | char **value; /* LDAP value */ | |
3814 | ||
3815 | /* | |
3816 | * Get value from LDAPMessage... | |
3817 | */ | |
3818 | ||
3819 | if ((value = (char **)ldap_get_values(ld, entry, attr)) == NULL) | |
3820 | { | |
3821 | rc = -1; | |
3822 | dn = ldap_get_dn(ld, entry); | |
3823 | cupsdLogMessage(CUPSD_LOG_WARN, | |
3824 | "Failed to get LDAP value %s for %s!", | |
3825 | attr, dn); | |
3826 | ldap_memfree(dn); | |
3827 | } | |
3828 | else | |
3829 | { | |
3830 | strlcpy(retval, *value, maxsize); | |
3831 | ldap_value_free(value); | |
3832 | } | |
3833 | # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */ | |
3834 | ||
3835 | return (rc); | |
3836 | } | |
3837 | ||
3838 | ||
3839 | /* | |
3840 | * 'send_ldap_ou()' - Send LDAP ou registrations. | |
3841 | */ | |
3842 | ||
3843 | static void | |
3844 | send_ldap_ou(char *ou, /* I - Servername/ou to register */ | |
3845 | char *basedn, /* I - Our base dn */ | |
3846 | char *descstring) /* I - Description for ou */ | |
3847 | { | |
3848 | int i; /* Looping var... */ | |
3849 | LDAPMod mods[3]; /* The 3 attributes we will be adding */ | |
3850 | LDAPMod *pmods[4]; /* Pointers to the 3 attributes + NULL */ | |
3851 | LDAPMessage *res, /* Search result token */ | |
3852 | *e; /* Current entry from search */ | |
3853 | int rc; /* LDAP status */ | |
3854 | char dn[1024], /* DN of the organizational unit we are adding */ | |
3855 | *desc[2], /* Change records */ | |
3856 | *ou_value[2]; | |
3857 | char old_desc[1024]; /* Old description */ | |
3858 | static const char * const objectClass_values[] = | |
3859 | { /* The 2 objectClass's we use in */ | |
3860 | "top", /* our LDAP entries */ | |
3861 | "organizationalUnit", | |
3862 | NULL | |
3863 | }; | |
3864 | static const char * const ou_attrs[] =/* CUPS LDAP attributes */ | |
3865 | { | |
3866 | "description", | |
3867 | NULL | |
3868 | }; | |
3869 | ||
3870 | ||
3871 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_ou: %s", ou); | |
3872 | ||
3873 | /* | |
3874 | * Reconnect if LDAP Handle is invalid... | |
3875 | */ | |
3876 | ||
3877 | if (! BrowseLDAPHandle) | |
3878 | { | |
3879 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3880 | "send_ldap_ou: LDAP Handle is invalid. Try " | |
3881 | "reconnecting..."); | |
3882 | ldap_reconnect(); | |
3883 | return; | |
3884 | } | |
3885 | ||
3886 | /* | |
3887 | * Prepare ldap search... | |
3888 | */ | |
3889 | ||
3890 | snprintf(dn, sizeof(dn), "ou=%s, %s", ou, basedn); | |
3891 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_ou: dn=\"%s\"", dn); | |
3892 | ||
3893 | ou_value[0] = ou; | |
3894 | ou_value[1] = NULL; | |
3895 | desc[0] = descstring; | |
3896 | desc[1] = NULL; | |
3897 | ||
3898 | mods[0].mod_type = "ou"; | |
3899 | mods[0].mod_values = ou_value; | |
3900 | mods[1].mod_type = "description"; | |
3901 | mods[1].mod_values = desc; | |
3902 | mods[2].mod_type = "objectClass"; | |
3903 | mods[2].mod_values = (char **)objectClass_values; | |
3904 | ||
3905 | rc = ldap_search_rec(BrowseLDAPHandle, dn, LDAP_SCOPE_BASE, NULL, | |
3906 | (char **)ou_attrs, 0, &res); | |
3907 | ||
3908 | /* | |
3909 | * If ldap search was not successfull then exit function... | |
3910 | */ | |
3911 | ||
3912 | if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT) | |
3913 | return; | |
3914 | ||
3915 | /* | |
3916 | * Check if we need to insert or update the LDAP entry... | |
3917 | */ | |
3918 | ||
3919 | if (ldap_count_entries(BrowseLDAPHandle, res) > 0 && | |
3920 | rc != LDAP_NO_SUCH_OBJECT) | |
3921 | { | |
3922 | /* | |
3923 | * Printserver has already been registered, check if | |
3924 | * modification is required... | |
3925 | */ | |
3926 | ||
3927 | e = ldap_first_entry(BrowseLDAPHandle, res); | |
3928 | ||
3929 | /* | |
3930 | * Get the required values from this entry... | |
3931 | */ | |
3932 | ||
3933 | if (ldap_getval_firststring(BrowseLDAPHandle, e, "description", old_desc, | |
3934 | sizeof(old_desc)) == -1) | |
3935 | old_desc[0] = '\0'; | |
3936 | ||
3937 | /* | |
3938 | * Check if modification is required... | |
3939 | */ | |
3940 | ||
3941 | if ( strcmp(desc[0], old_desc) == 0 ) | |
3942 | { | |
3943 | /* | |
3944 | * LDAP entry for the printer exists. | |
3945 | * Printer has already been registered, | |
3946 | * no modifications required... | |
3947 | */ | |
3948 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3949 | "send_ldap_ou: No updates required for %s", ou); | |
3950 | } | |
3951 | else | |
3952 | { | |
3953 | ||
3954 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3955 | "send_ldap_ou: Replace entry for %s", ou); | |
3956 | ||
3957 | for (i = 0; i < 3; i ++) | |
3958 | { | |
3959 | pmods[i] = mods + i; | |
3960 | pmods[i]->mod_op = LDAP_MOD_REPLACE; | |
3961 | } | |
3962 | pmods[i] = NULL; | |
3963 | ||
3964 | # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 | |
3965 | if ((rc = ldap_modify_ext_s(BrowseLDAPHandle, dn, pmods, NULL, | |
3966 | NULL)) != LDAP_SUCCESS) | |
3967 | # else | |
3968 | if ((rc = ldap_modify_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS) | |
3969 | # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */ | |
3970 | { | |
3971 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3972 | "LDAP modify for %s failed with status %d: %s", | |
3973 | ou, rc, ldap_err2string(rc)); | |
3974 | if ( LDAP_SERVER_DOWN == rc ) | |
3975 | ldap_reconnect(); | |
3976 | } | |
3977 | } | |
3978 | } | |
3979 | else | |
3980 | { | |
3981 | /* | |
3982 | * Printserver has never been registered, | |
3983 | * add registration... | |
3984 | */ | |
3985 | ||
3986 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3987 | "send_ldap_ou: Add entry for %s", ou); | |
3988 | ||
3989 | for (i = 0; i < 3; i ++) | |
3990 | { | |
3991 | pmods[i] = mods + i; | |
3992 | pmods[i]->mod_op = LDAP_MOD_ADD; | |
3993 | } | |
3994 | pmods[i] = NULL; | |
3995 | ||
3996 | # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 | |
3997 | if ((rc = ldap_add_ext_s(BrowseLDAPHandle, dn, pmods, NULL, | |
3998 | NULL)) != LDAP_SUCCESS) | |
3999 | # else | |
4000 | if ((rc = ldap_add_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS) | |
4001 | # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */ | |
4002 | { | |
4003 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4004 | "LDAP add for %s failed with status %d: %s", | |
4005 | ou, rc, ldap_err2string(rc)); | |
4006 | if ( LDAP_SERVER_DOWN == rc ) | |
4007 | ldap_reconnect(); | |
4008 | } | |
4009 | } | |
4010 | ||
4011 | ldap_freeres(res); | |
4012 | } | |
4013 | ||
4014 | ||
e1d6a774 | 4015 | /* |
4016 | * 'send_ldap_browse()' - Send LDAP printer registrations. | |
4017 | */ | |
4018 | ||
4019 | static void | |
4020 | send_ldap_browse(cupsd_printer_t *p) /* I - Printer to register */ | |
4021 | { | |
4022 | int i; /* Looping var... */ | |
4023 | LDAPMod mods[7]; /* The 7 attributes we will be adding */ | |
4024 | LDAPMod *pmods[8]; /* Pointers to the 7 attributes + NULL */ | |
749b1e90 MS |
4025 | LDAPMessage *res, /* Search result token */ |
4026 | *e; /* Current entry from search */ | |
e1d6a774 | 4027 | char *cn_value[2], /* Change records */ |
4028 | *uri[2], | |
4029 | *info[2], | |
4030 | *location[2], | |
4031 | *make_model[2], | |
4032 | *type[2], | |
4033 | typestring[255], /* String to hold printer-type */ | |
e1d6a774 | 4034 | dn[1024]; /* DN of the printer we are adding */ |
4035 | int rc; /* LDAP status */ | |
749b1e90 MS |
4036 | char old_uri[HTTP_MAX_URI], /* Printer URI */ |
4037 | old_location[1024], /* Printer location */ | |
4038 | old_info[1024], /* Printer information */ | |
4039 | old_make_model[1024], /* Printer make and model */ | |
4040 | old_type_string[30]; /* Temporary type number */ | |
4041 | int old_type; /* Printer type */ | |
e1d6a774 | 4042 | static const char * const objectClass_values[] = |
4043 | { /* The 3 objectClass's we use in */ | |
4044 | "top", /* our LDAP entries */ | |
4045 | "device", | |
4046 | "cupsPrinter", | |
4047 | NULL | |
4048 | }; | |
4049 | ||
749b1e90 | 4050 | |
75bd9771 | 4051 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: %s", p->name); |
e1d6a774 | 4052 | |
749b1e90 MS |
4053 | /* |
4054 | * Exit function if LDAP updates has been disabled... | |
4055 | */ | |
4056 | ||
4057 | if (!BrowseLDAPUpdate) | |
4058 | { | |
4059 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
4060 | "send_ldap_browse: Updates temporary disabled; " | |
4061 | "skipping..."); | |
4062 | return; | |
4063 | } | |
4064 | ||
4065 | /* | |
4066 | * Reconnect if LDAP Handle is invalid... | |
4067 | */ | |
4068 | ||
4069 | if (!BrowseLDAPHandle) | |
4070 | { | |
4071 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
4072 | "send_ldap_browse: LDAP Handle is invalid. Try " | |
4073 | "reconnecting..."); | |
4074 | ldap_reconnect(); | |
4075 | return; | |
4076 | } | |
4077 | ||
e1d6a774 | 4078 | /* |
4079 | * Everything in ldap is ** so we fudge around it... | |
4080 | */ | |
4081 | ||
4082 | sprintf(typestring, "%u", p->type); | |
4083 | ||
ed486911 | 4084 | cn_value[0] = p->name; |
e1d6a774 | 4085 | cn_value[1] = NULL; |
ed486911 | 4086 | info[0] = p->info ? p->info : "Unknown"; |
e1d6a774 | 4087 | info[1] = NULL; |
ed486911 | 4088 | location[0] = p->location ? p->location : "Unknown"; |
e1d6a774 | 4089 | location[1] = NULL; |
ed486911 | 4090 | make_model[0] = p->make_model ? p->make_model : "Unknown"; |
e1d6a774 | 4091 | make_model[1] = NULL; |
4092 | type[0] = typestring; | |
4093 | type[1] = NULL; | |
4094 | uri[0] = p->uri; | |
4095 | uri[1] = NULL; | |
4096 | ||
749b1e90 MS |
4097 | /* |
4098 | * Get ldap entry for printer ... | |
4099 | */ | |
e1d6a774 | 4100 | |
749b1e90 MS |
4101 | snprintf(dn, sizeof(dn), "cn=%s, ou=%s, %s", p->name, ServerName, |
4102 | BrowseLDAPDN); | |
4103 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: dn=\"%s\"", dn); | |
4104 | ||
4105 | rc = ldap_search_rec(BrowseLDAPHandle, dn, LDAP_SCOPE_BASE, NULL, | |
4106 | (char **)ldap_attrs, 0, &res); | |
4107 | ||
4108 | /* | |
4109 | * If ldap search was not successfull then exit function | |
4110 | * and temporary disable LDAP updates... | |
4111 | */ | |
4112 | ||
4113 | if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT) | |
4114 | { | |
4115 | if (BrowseLDAPUpdate && | |
4116 | (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR)) | |
4117 | { | |
4118 | BrowseLDAPUpdate = FALSE; | |
4119 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4120 | "LDAP update temporary disabled"); | |
4121 | } | |
4122 | ||
4123 | return; | |
4124 | } | |
4125 | ||
4126 | /* | |
4127 | * Fill modification array... | |
4128 | */ | |
e1d6a774 | 4129 | |
749b1e90 | 4130 | mods[0].mod_type = "cn"; |
e1d6a774 | 4131 | mods[0].mod_values = cn_value; |
749b1e90 | 4132 | mods[1].mod_type = "printerDescription"; |
e1d6a774 | 4133 | mods[1].mod_values = info; |
749b1e90 | 4134 | mods[2].mod_type = "printerURI"; |
e1d6a774 | 4135 | mods[2].mod_values = uri; |
749b1e90 | 4136 | mods[3].mod_type = "printerLocation"; |
e1d6a774 | 4137 | mods[3].mod_values = location; |
749b1e90 | 4138 | mods[4].mod_type = "printerMakeAndModel"; |
e1d6a774 | 4139 | mods[4].mod_values = make_model; |
749b1e90 | 4140 | mods[5].mod_type = "printerType"; |
e1d6a774 | 4141 | mods[5].mod_values = type; |
749b1e90 | 4142 | mods[6].mod_type = "objectClass"; |
e1d6a774 | 4143 | mods[6].mod_values = (char **)objectClass_values; |
4144 | ||
749b1e90 MS |
4145 | /* |
4146 | * Check if we need to insert or update the LDAP entry... | |
4147 | */ | |
e1d6a774 | 4148 | |
749b1e90 MS |
4149 | if (ldap_count_entries(BrowseLDAPHandle, res) > 0 && |
4150 | rc != LDAP_NO_SUCH_OBJECT) | |
e1d6a774 | 4151 | { |
4152 | /* | |
749b1e90 MS |
4153 | * Printer has already been registered, check if |
4154 | * modification is required... | |
e1d6a774 | 4155 | */ |
4156 | ||
749b1e90 | 4157 | e = ldap_first_entry(BrowseLDAPHandle, res); |
e1d6a774 | 4158 | |
749b1e90 MS |
4159 | /* |
4160 | * Get the required values from this entry... | |
4161 | */ | |
4162 | ||
4163 | if (ldap_getval_firststring(BrowseLDAPHandle, e, "printerDescription", | |
4164 | old_info, sizeof(old_info)) == -1) | |
4165 | old_info[0] = '\0'; | |
4166 | ||
4167 | if (ldap_getval_firststring(BrowseLDAPHandle, e, "printerLocation", | |
4168 | old_location, sizeof(old_location)) == -1) | |
4169 | old_info[0] = '\0'; | |
4170 | ||
4171 | if (ldap_getval_firststring(BrowseLDAPHandle, e, "printerMakeAndModel", | |
4172 | old_make_model, sizeof(old_make_model)) == -1) | |
4173 | old_info[0] = '\0'; | |
4174 | ||
4175 | if (ldap_getval_firststring(BrowseLDAPHandle, e, "printerType", | |
4176 | old_type_string, sizeof(old_type_string)) == -1) | |
4177 | old_info[0] = '\0'; | |
4178 | ||
4179 | old_type = atoi(old_type_string); | |
4180 | ||
4181 | if (ldap_getval_firststring(BrowseLDAPHandle, e, "printerURI", old_uri, | |
4182 | sizeof(old_uri)) == -1) | |
4183 | old_info[0] = '\0'; | |
4184 | ||
4185 | /* | |
4186 | * Check if modification is required... | |
4187 | */ | |
4188 | ||
4189 | if (!strcmp(info[0], old_info) && !strcmp(uri[0], old_uri) && | |
4190 | !strcmp(location[0], old_location) && | |
4191 | !strcmp(make_model[0], old_make_model) && p->type == old_type) | |
e1d6a774 | 4192 | { |
749b1e90 MS |
4193 | /* |
4194 | * LDAP entry for the printer exists. Printer has already been registered, | |
4195 | * no modifications required... | |
4196 | */ | |
4197 | ||
4198 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
4199 | "send_ldap_browse: No updates required for %s", p->name); | |
e1d6a774 | 4200 | } |
749b1e90 MS |
4201 | else |
4202 | { | |
4203 | /* | |
4204 | * LDAP entry for the printer exists. Printer has already been registered, | |
4205 | * modify the current registration... | |
4206 | */ | |
e1d6a774 | 4207 | |
749b1e90 MS |
4208 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
4209 | "send_ldap_browse: Replace entry for %s", p->name); | |
4210 | ||
4211 | for (i = 0; i < 7; i ++) | |
4212 | { | |
4213 | pmods[i] = mods + i; | |
4214 | pmods[i]->mod_op = LDAP_MOD_REPLACE; | |
4215 | } | |
4216 | pmods[i] = NULL; | |
4217 | ||
4218 | # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 | |
4219 | if ((rc = ldap_modify_ext_s(BrowseLDAPHandle, dn, pmods, NULL, | |
4220 | NULL)) != LDAP_SUCCESS) | |
4221 | # else | |
4222 | if ((rc = ldap_modify_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS) | |
4223 | # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */ | |
4224 | { | |
4225 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4226 | "LDAP modify for %s failed with status %d: %s", | |
4227 | p->name, rc, ldap_err2string(rc)); | |
4228 | if (rc == LDAP_SERVER_DOWN) | |
4229 | ldap_reconnect(); | |
4230 | } | |
4231 | } | |
e1d6a774 | 4232 | } |
4233 | else | |
4234 | { | |
4235 | /* | |
749b1e90 MS |
4236 | * No LDAP entry exists for the printer. Printer has never been registered, |
4237 | * add the current registration... | |
e1d6a774 | 4238 | */ |
4239 | ||
749b1e90 MS |
4240 | send_ldap_ou(ServerName, BrowseLDAPDN, "CUPS Server"); |
4241 | ||
e1d6a774 | 4242 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
749b1e90 | 4243 | "send_ldap_browse: Add entry for %s", p->name); |
e1d6a774 | 4244 | |
4245 | for (i = 0; i < 7; i ++) | |
4246 | { | |
4247 | pmods[i] = mods + i; | |
ed486911 | 4248 | pmods[i]->mod_op = LDAP_MOD_ADD; |
e1d6a774 | 4249 | } |
4250 | pmods[i] = NULL; | |
4251 | ||
749b1e90 MS |
4252 | # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 |
4253 | if ((rc = ldap_add_ext_s(BrowseLDAPHandle, dn, pmods, NULL, | |
4254 | NULL)) != LDAP_SUCCESS) | |
4255 | # else | |
ed486911 | 4256 | if ((rc = ldap_add_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS) |
749b1e90 MS |
4257 | # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */ |
4258 | { | |
e1d6a774 | 4259 | cupsdLogMessage(CUPSD_LOG_ERROR, |
4260 | "LDAP add for %s failed with status %d: %s", | |
4261 | p->name, rc, ldap_err2string(rc)); | |
749b1e90 MS |
4262 | if (rc == LDAP_SERVER_DOWN) |
4263 | ldap_reconnect(); | |
4264 | } | |
e1d6a774 | 4265 | } |
749b1e90 MS |
4266 | |
4267 | ldap_freeres(res); | |
e1d6a774 | 4268 | } |
749b1e90 MS |
4269 | |
4270 | ||
4271 | /* | |
4272 | * 'ldap_dereg_printer()' - Delete printer from directory | |
4273 | */ | |
4274 | ||
4275 | static void | |
4276 | ldap_dereg_printer(cupsd_printer_t *p) /* I - Printer to deregister */ | |
4277 | { | |
4278 | char dn[1024]; /* DN of the printer */ | |
4279 | int rc; /* LDAP status */ | |
4280 | ||
4281 | ||
4282 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "ldap_dereg_printer: Remove entry for %s", | |
4283 | p->name); | |
4284 | ||
4285 | /* | |
4286 | * Reconnect if LDAP Handle is invalid... | |
4287 | */ | |
4288 | ||
4289 | if (!BrowseLDAPHandle) | |
4290 | { | |
4291 | ldap_reconnect(); | |
4292 | return; | |
4293 | } | |
4294 | ||
4295 | /* | |
4296 | * Get dn for printer and delete LDAP entry... | |
4297 | */ | |
4298 | ||
4299 | snprintf(dn, sizeof(dn), "cn=%s, ou=%s, %s", p->name, ServerName, | |
4300 | BrowseLDAPDN); | |
4301 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "ldap_dereg_printer: dn=\"%s\"", dn); | |
4302 | ||
4303 | # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 | |
4304 | if ((rc = ldap_delete_ext_s(BrowseLDAPHandle, dn, NULL, | |
4305 | NULL)) != LDAP_SUCCESS) | |
4306 | # else | |
4307 | if ((rc = ldap_delete_s(BrowseLDAPHandle, dn)) != LDAP_SUCCESS) | |
4308 | # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */ | |
4309 | { | |
4310 | cupsdLogMessage(CUPSD_LOG_WARN, | |
4311 | "LDAP delete for %s failed with status %d: %s", | |
4312 | p->name, rc, ldap_err2string(rc)); | |
4313 | ||
4314 | /* | |
4315 | * If we had a connection problem (connection timed out, etc.) | |
4316 | * we should reconnect and try again to delete the entry... | |
4317 | */ | |
4318 | ||
4319 | if (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR) | |
4320 | { | |
4321 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4322 | "Retry deleting LDAP entry for %s after a reconnect...", p->name); | |
4323 | ldap_reconnect(); | |
4324 | ||
4325 | # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 | |
4326 | if ((rc = ldap_delete_ext_s(BrowseLDAPHandle, dn, NULL, | |
4327 | NULL)) != LDAP_SUCCESS) | |
4328 | # else | |
4329 | if ((rc = ldap_delete_s(BrowseLDAPHandle, dn)) != LDAP_SUCCESS) | |
4330 | # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */ | |
4331 | cupsdLogMessage(CUPSD_LOG_WARN, | |
4332 | "LDAP delete for %s failed with status %d: %s", | |
4333 | p->name, rc, ldap_err2string(rc)); | |
4334 | } | |
4335 | } | |
4336 | } | |
4337 | ||
4338 | ||
4339 | static void | |
4340 | ldap_dereg_ou(char *ou, /* I - Organizational unit (servername) */ | |
4341 | char *basedn) /* I - Dase dn */ | |
4342 | { | |
4343 | char dn[1024]; /* DN of the printer */ | |
4344 | int rc; /* LDAP status */ | |
4345 | ||
4346 | ||
4347 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "ldap_dereg_ou: Remove entry for %s", ou); | |
4348 | ||
4349 | /* | |
4350 | * Reconnect if LDAP Handle is invalid... | |
4351 | */ | |
4352 | ||
4353 | if (!BrowseLDAPHandle) | |
4354 | { | |
4355 | ldap_reconnect(); | |
4356 | return; | |
4357 | } | |
4358 | ||
4359 | /* | |
4360 | * Get dn for printer and delete LDAP entry... | |
4361 | */ | |
4362 | ||
4363 | snprintf(dn, sizeof(dn), "ou=%s, %s", ou, basedn); | |
4364 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "ldap_dereg_ou: dn=\"%s\"", dn); | |
4365 | ||
4366 | # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 | |
4367 | if ((rc = ldap_delete_ext_s(BrowseLDAPHandle, dn, NULL, | |
4368 | NULL)) != LDAP_SUCCESS) | |
4369 | # else | |
4370 | if ((rc = ldap_delete_s(BrowseLDAPHandle, dn)) != LDAP_SUCCESS) | |
4371 | # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */ | |
4372 | { | |
4373 | cupsdLogMessage(CUPSD_LOG_WARN, | |
4374 | "LDAP delete for %s failed with status %d: %s", | |
4375 | ou, rc, ldap_err2string(rc)); | |
4376 | ||
4377 | /* | |
4378 | * If we had a connection problem (connection timed out, etc.) | |
4379 | * we should reconnect and try again to delete the entry... | |
4380 | */ | |
4381 | ||
4382 | if (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR) | |
4383 | { | |
4384 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4385 | "Retry deleting LDAP entry for %s after a reconnect...", ou); | |
4386 | ldap_reconnect(); | |
4387 | # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 | |
4388 | if ((rc = ldap_delete_ext_s(BrowseLDAPHandle, dn, NULL, | |
4389 | NULL)) != LDAP_SUCCESS) | |
4390 | # else | |
4391 | if ((rc = ldap_delete_s(BrowseLDAPHandle, dn)) != LDAP_SUCCESS) | |
4392 | # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */ | |
4393 | cupsdLogMessage(CUPSD_LOG_WARN, | |
4394 | "LDAP delete for %s failed with status %d: %s", | |
4395 | ou, rc, ldap_err2string(rc)); | |
4396 | } | |
4397 | ||
4398 | } | |
4399 | } | |
4400 | #endif /* HAVE_LDAP */ | |
e1d6a774 | 4401 | |
4402 | ||
4403 | #ifdef HAVE_LIBSLP | |
4404 | /* | |
4405 | * 'send_slp_browse()' - Register the specified printer with SLP. | |
4406 | */ | |
4407 | ||
4408 | static void | |
4409 | send_slp_browse(cupsd_printer_t *p) /* I - Printer to register */ | |
4410 | { | |
4411 | char srvurl[HTTP_MAX_URI], /* Printer service URI */ | |
4412 | attrs[8192], /* Printer attributes */ | |
4413 | finishings[1024], /* Finishings to support */ | |
4414 | make_model[IPP_MAX_NAME * 2], | |
4415 | /* Make and model, quoted */ | |
4416 | location[IPP_MAX_NAME * 2], | |
4417 | /* Location, quoted */ | |
4418 | info[IPP_MAX_NAME * 2], /* Info, quoted */ | |
4419 | *src, /* Pointer to original string */ | |
4420 | *dst; /* Pointer to destination string */ | |
4421 | ipp_attribute_t *authentication; /* uri-authentication-supported value */ | |
4422 | SLPError error; /* SLP error, if any */ | |
4423 | ||
4424 | ||
4425 | cupsdLogMessage(CUPSD_LOG_DEBUG, "send_slp_browse(%p = \"%s\")", p, | |
4426 | p->name); | |
4427 | ||
4428 | /* | |
4429 | * Make the SLP service URL that conforms to the IANA | |
4430 | * 'printer:' template. | |
4431 | */ | |
4432 | ||
4433 | snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri); | |
4434 | ||
4435 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "Service URL = \"%s\"", srvurl); | |
4436 | ||
4437 | /* | |
4438 | * Figure out the finishings string... | |
4439 | */ | |
4440 | ||
4441 | if (p->type & CUPS_PRINTER_STAPLE) | |
4442 | strcpy(finishings, "staple"); | |
4443 | else | |
4444 | finishings[0] = '\0'; | |
4445 | ||
4446 | if (p->type & CUPS_PRINTER_BIND) | |
4447 | { | |
4448 | if (finishings[0]) | |
4449 | strlcat(finishings, ",bind", sizeof(finishings)); | |
4450 | else | |
4451 | strcpy(finishings, "bind"); | |
4452 | } | |
4453 | ||
4454 | if (p->type & CUPS_PRINTER_PUNCH) | |
4455 | { | |
4456 | if (finishings[0]) | |
4457 | strlcat(finishings, ",punch", sizeof(finishings)); | |
4458 | else | |
4459 | strcpy(finishings, "punch"); | |
4460 | } | |
4461 | ||
4462 | if (p->type & CUPS_PRINTER_COVER) | |
4463 | { | |
4464 | if (finishings[0]) | |
4465 | strlcat(finishings, ",cover", sizeof(finishings)); | |
4466 | else | |
4467 | strcpy(finishings, "cover"); | |
4468 | } | |
4469 | ||
4470 | if (p->type & CUPS_PRINTER_SORT) | |
4471 | { | |
4472 | if (finishings[0]) | |
4473 | strlcat(finishings, ",sort", sizeof(finishings)); | |
4474 | else | |
4475 | strcpy(finishings, "sort"); | |
4476 | } | |
4477 | ||
4478 | if (!finishings[0]) | |
4479 | strcpy(finishings, "none"); | |
4480 | ||
4481 | /* | |
4482 | * Quote any commas in the make and model, location, and info strings... | |
4483 | */ | |
4484 | ||
4485 | for (src = p->make_model, dst = make_model; | |
4486 | src && *src && dst < (make_model + sizeof(make_model) - 2);) | |
4487 | { | |
4488 | if (*src == ',' || *src == '\\' || *src == ')') | |
4489 | *dst++ = '\\'; | |
4490 | ||
4491 | *dst++ = *src++; | |
4492 | } | |
4493 | ||
4494 | *dst = '\0'; | |
4495 | ||
4496 | if (!make_model[0]) | |
4497 | strcpy(make_model, "Unknown"); | |
4498 | ||
4499 | for (src = p->location, dst = location; | |
4500 | src && *src && dst < (location + sizeof(location) - 2);) | |
4501 | { | |
4502 | if (*src == ',' || *src == '\\' || *src == ')') | |
4503 | *dst++ = '\\'; | |
4504 | ||
4505 | *dst++ = *src++; | |
4506 | } | |
4507 | ||
4508 | *dst = '\0'; | |
4509 | ||
4510 | if (!location[0]) | |
4511 | strcpy(location, "Unknown"); | |
4512 | ||
4513 | for (src = p->info, dst = info; | |
4514 | src && *src && dst < (info + sizeof(info) - 2);) | |
4515 | { | |
4516 | if (*src == ',' || *src == '\\' || *src == ')') | |
4517 | *dst++ = '\\'; | |
4518 | ||
4519 | *dst++ = *src++; | |
4520 | } | |
4521 | ||
4522 | *dst = '\0'; | |
4523 | ||
4524 | if (!info[0]) | |
4525 | strcpy(info, "Unknown"); | |
4526 | ||
4527 | /* | |
4528 | * Get the authentication value... | |
4529 | */ | |
4530 | ||
4531 | authentication = ippFindAttribute(p->attrs, "uri-authentication-supported", | |
4532 | IPP_TAG_KEYWORD); | |
4533 | ||
4534 | /* | |
4535 | * Make the SLP attribute string list that conforms to | |
4536 | * the IANA 'printer:' template. | |
4537 | */ | |
4538 | ||
4539 | snprintf(attrs, sizeof(attrs), | |
4540 | "(printer-uri-supported=%s)," | |
4541 | "(uri-authentication-supported=%s>)," | |
4542 | #ifdef HAVE_SSL | |
4543 | "(uri-security-supported=tls>)," | |
4544 | #else | |
4545 | "(uri-security-supported=none>)," | |
4546 | #endif /* HAVE_SSL */ | |
4547 | "(printer-name=%s)," | |
4548 | "(printer-location=%s)," | |
4549 | "(printer-info=%s)," | |
4550 | "(printer-more-info=%s)," | |
4551 | "(printer-make-and-model=%s)," | |
4552 | "(printer-type=%d)," | |
4553 | "(charset-supported=utf-8)," | |
4554 | "(natural-language-configured=%s)," | |
4555 | "(natural-language-supported=de,en,es,fr,it)," | |
4556 | "(color-supported=%s)," | |
4557 | "(finishings-supported=%s)," | |
4558 | "(sides-supported=one-sided%s)," | |
4559 | "(multiple-document-jobs-supported=true)" | |
4560 | "(ipp-versions-supported=1.0,1.1)", | |
4561 | p->uri, authentication->values[0].string.text, p->name, location, | |
4562 | info, p->uri, make_model, p->type, DefaultLanguage, | |
4563 | p->type & CUPS_PRINTER_COLOR ? "true" : "false", | |
4564 | finishings, | |
4565 | p->type & CUPS_PRINTER_DUPLEX ? | |
4566 | ",two-sided-long-edge,two-sided-short-edge" : ""); | |
4567 | ||
4568 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "Attributes = \"%s\"", attrs); | |
4569 | ||
4570 | /* | |
4571 | * Register the printer with the SLP server... | |
4572 | */ | |
4573 | ||
4574 | error = SLPReg(BrowseSLPHandle, srvurl, BrowseTimeout, | |
4575 | SLP_CUPS_SRVTYPE, attrs, SLP_TRUE, slp_reg_callback, 0); | |
4576 | ||
4577 | if (error != SLP_OK) | |
4578 | cupsdLogMessage(CUPSD_LOG_ERROR, "SLPReg of \"%s\" failed with status %d!", p->name, | |
4579 | error); | |
4580 | } | |
4581 | ||
4582 | ||
ef416fc2 | 4583 | /* |
4584 | * 'slp_attr_callback()' - SLP attribute callback | |
4585 | */ | |
4586 | ||
4587 | static SLPBoolean /* O - SLP_TRUE for success */ | |
4588 | slp_attr_callback( | |
4589 | SLPHandle hslp, /* I - SLP handle */ | |
4590 | const char *attrlist, /* I - Attribute list */ | |
4591 | SLPError errcode, /* I - Parsing status for this attr */ | |
4592 | void *cookie) /* I - Current printer */ | |
4593 | { | |
b423cd4c | 4594 | char *tmp = 0; /* Temporary string */ |
ef416fc2 | 4595 | cupsd_printer_t *p = (cupsd_printer_t*)cookie; |
b423cd4c | 4596 | /* Current printer */ |
ef416fc2 | 4597 | |
4598 | ||
b423cd4c | 4599 | (void)hslp; /* anti-compiler-warning-code */ |
ef416fc2 | 4600 | |
4601 | /* | |
4602 | * Bail if there was an error | |
4603 | */ | |
4604 | ||
4605 | if (errcode != SLP_OK) | |
4606 | return (SLP_TRUE); | |
4607 | ||
4608 | /* | |
4609 | * Parse the attrlist to obtain things needed to build CUPS browse packet | |
4610 | */ | |
4611 | ||
4612 | memset(p, 0, sizeof(cupsd_printer_t)); | |
4613 | ||
ef416fc2 | 4614 | if (slp_get_attr(attrlist, "(printer-location=", &(p->location))) |
4615 | return (SLP_FALSE); | |
4616 | if (slp_get_attr(attrlist, "(printer-info=", &(p->info))) | |
4617 | return (SLP_FALSE); | |
4618 | if (slp_get_attr(attrlist, "(printer-make-and-model=", &(p->make_model))) | |
4619 | return (SLP_FALSE); | |
b423cd4c | 4620 | if (!slp_get_attr(attrlist, "(printer-type=", &tmp)) |
4621 | p->type = atoi(tmp); | |
4622 | else | |
4623 | p->type = CUPS_PRINTER_REMOTE; | |
ef416fc2 | 4624 | |
4625 | cupsdClearString(&tmp); | |
4626 | ||
4627 | return (SLP_TRUE); | |
4628 | } | |
4629 | ||
4630 | ||
4631 | /* | |
4632 | * 'slp_dereg_printer()' - SLPDereg() the specified printer | |
4633 | */ | |
4634 | ||
4635 | static void | |
4636 | slp_dereg_printer(cupsd_printer_t *p) /* I - Printer */ | |
4637 | { | |
4638 | char srvurl[HTTP_MAX_URI]; /* Printer service URI */ | |
4639 | ||
4640 | ||
4641 | cupsdLogMessage(CUPSD_LOG_DEBUG, "slp_dereg_printer: printer=\"%s\"", p->name); | |
4642 | ||
4643 | if (!(p->type & CUPS_PRINTER_REMOTE)) | |
4644 | { | |
4645 | /* | |
4646 | * Make the SLP service URL that conforms to the IANA | |
4647 | * 'printer:' template. | |
4648 | */ | |
4649 | ||
4650 | snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri); | |
4651 | ||
4652 | /* | |
4653 | * Deregister the printer... | |
4654 | */ | |
4655 | ||
4656 | SLPDereg(BrowseSLPHandle, srvurl, slp_reg_callback, 0); | |
4657 | } | |
4658 | } | |
4659 | ||
4660 | ||
4661 | /* | |
4662 | * 'slp_get_attr()' - Get an attribute from an SLP registration. | |
4663 | */ | |
4664 | ||
4665 | static int /* O - 0 on success */ | |
4666 | slp_get_attr(const char *attrlist, /* I - Attribute list string */ | |
4667 | const char *tag, /* I - Name of attribute */ | |
4668 | char **valbuf) /* O - Value */ | |
4669 | { | |
4670 | char *ptr1, /* Pointer into string */ | |
4671 | *ptr2; /* ... */ | |
4672 | ||
4673 | ||
4674 | cupsdClearString(valbuf); | |
4675 | ||
4676 | if ((ptr1 = strstr(attrlist, tag)) != NULL) | |
4677 | { | |
4678 | ptr1 += strlen(tag); | |
4679 | ||
4680 | if ((ptr2 = strchr(ptr1,')')) != NULL) | |
4681 | { | |
4682 | /* | |
4683 | * Copy the value... | |
4684 | */ | |
4685 | ||
4686 | *valbuf = calloc(ptr2 - ptr1 + 1, 1); | |
4687 | strncpy(*valbuf, ptr1, ptr2 - ptr1); | |
4688 | ||
4689 | /* | |
4690 | * Dequote the value... | |
4691 | */ | |
4692 | ||
4693 | for (ptr1 = *valbuf; *ptr1; ptr1 ++) | |
4694 | if (*ptr1 == '\\' && ptr1[1]) | |
4695 | _cups_strcpy(ptr1, ptr1 + 1); | |
4696 | ||
4697 | return (0); | |
4698 | } | |
4699 | } | |
4700 | ||
4701 | return (-1); | |
4702 | } | |
4703 | ||
4704 | ||
4705 | /* | |
4706 | * 'slp_reg_callback()' - Empty SLPRegReport. | |
4707 | */ | |
4708 | ||
4709 | static void | |
4710 | slp_reg_callback(SLPHandle hslp, /* I - SLP handle */ | |
4711 | SLPError errcode, /* I - Error code, if any */ | |
4712 | void *cookie) /* I - App data */ | |
4713 | { | |
4714 | (void)hslp; | |
4715 | (void)errcode; | |
4716 | (void)cookie; | |
4717 | ||
4718 | return; | |
4719 | } | |
4720 | ||
4721 | ||
4722 | /* | |
4723 | * 'slp_url_callback()' - SLP service url callback | |
4724 | */ | |
4725 | ||
4726 | static SLPBoolean /* O - TRUE = OK, FALSE = error */ | |
4727 | slp_url_callback( | |
4728 | SLPHandle hslp, /* I - SLP handle */ | |
4729 | const char *srvurl, /* I - URL of service */ | |
4730 | unsigned short lifetime, /* I - Life of service */ | |
4731 | SLPError errcode, /* I - Existing error code */ | |
4732 | void *cookie) /* I - Pointer to service list */ | |
4733 | { | |
4734 | slpsrvurl_t *s, /* New service entry */ | |
4735 | **head; /* Pointer to head of entry */ | |
4736 | ||
4737 | ||
4738 | /* | |
4739 | * Let the compiler know we won't be using these vars... | |
4740 | */ | |
4741 | ||
4742 | (void)hslp; | |
4743 | (void)lifetime; | |
4744 | ||
4745 | /* | |
4746 | * Bail if there was an error | |
4747 | */ | |
4748 | ||
4749 | if (errcode != SLP_OK) | |
4750 | return (SLP_TRUE); | |
4751 | ||
4752 | /* | |
4753 | * Grab the head of the list... | |
4754 | */ | |
4755 | ||
4756 | head = (slpsrvurl_t**)cookie; | |
4757 | ||
4758 | /* | |
4759 | * Allocate a *temporary* slpsrvurl_t to hold this entry. | |
4760 | */ | |
4761 | ||
4762 | if ((s = (slpsrvurl_t *)calloc(1, sizeof(slpsrvurl_t))) == NULL) | |
4763 | return (SLP_FALSE); | |
4764 | ||
4765 | /* | |
4766 | * Copy the SLP service URL... | |
4767 | */ | |
4768 | ||
4769 | strlcpy(s->url, srvurl, sizeof(s->url)); | |
4770 | ||
4771 | /* | |
4772 | * Link the SLP service URL into the head of the list | |
4773 | */ | |
4774 | ||
4775 | if (*head) | |
4776 | s->next = *head; | |
4777 | ||
4778 | *head = s; | |
4779 | ||
4780 | return (SLP_TRUE); | |
4781 | } | |
4782 | #endif /* HAVE_LIBSLP */ | |
4783 | ||
4784 | ||
4785 | /* | |
f899b121 | 4786 | * 'update_cups_browse()' - Update the browse lists using the CUPS protocol. |
4787 | */ | |
4788 | ||
4789 | static void | |
4790 | update_cups_browse(void) | |
4791 | { | |
4792 | int i; /* Looping var */ | |
4793 | int auth; /* Authorization status */ | |
4794 | int len; /* Length of name string */ | |
4795 | int bytes; /* Number of bytes left */ | |
4796 | char packet[1541], /* Broadcast packet */ | |
4797 | *pptr; /* Pointer into packet */ | |
4798 | socklen_t srclen; /* Length of source address */ | |
4799 | http_addr_t srcaddr; /* Source address */ | |
4800 | char srcname[1024]; /* Source hostname */ | |
4801 | unsigned address[4]; /* Source address */ | |
4802 | unsigned type; /* Printer type */ | |
4803 | unsigned state; /* Printer state */ | |
4804 | char uri[HTTP_MAX_URI], /* Printer URI */ | |
4805 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
4806 | resource[HTTP_MAX_URI], /* Resource portion of URI */ | |
4807 | info[IPP_MAX_NAME], /* Information string */ | |
4808 | location[IPP_MAX_NAME], /* Location string */ | |
4809 | make_model[IPP_MAX_NAME];/* Make and model string */ | |
4810 | int num_attrs; /* Number of attributes */ | |
4811 | cups_option_t *attrs; /* Attributes */ | |
4812 | ||
4813 | ||
4814 | /* | |
4815 | * Read a packet from the browse socket... | |
4816 | */ | |
4817 | ||
4818 | srclen = sizeof(srcaddr); | |
4819 | if ((bytes = recvfrom(BrowseSocket, packet, sizeof(packet) - 1, 0, | |
4820 | (struct sockaddr *)&srcaddr, &srclen)) < 0) | |
4821 | { | |
4822 | /* | |
4823 | * "Connection refused" is returned under Linux if the destination port | |
4824 | * or address is unreachable from a previous sendto(); check for the | |
4825 | * error here and ignore it for now... | |
4826 | */ | |
4827 | ||
4828 | if (errno != ECONNREFUSED && errno != EAGAIN) | |
4829 | { | |
4830 | cupsdLogMessage(CUPSD_LOG_ERROR, "Browse recv failed - %s.", | |
4831 | strerror(errno)); | |
7a14d768 MS |
4832 | cupsdLogMessage(CUPSD_LOG_ERROR, "CUPS browsing turned off."); |
4833 | ||
4834 | #ifdef WIN32 | |
4835 | closesocket(BrowseSocket); | |
4836 | #else | |
4837 | close(BrowseSocket); | |
4838 | #endif /* WIN32 */ | |
4839 | ||
4840 | cupsdRemoveSelect(BrowseSocket); | |
4841 | BrowseSocket = -1; | |
f899b121 | 4842 | |
7a14d768 MS |
4843 | BrowseLocalProtocols &= ~BROWSE_CUPS; |
4844 | BrowseRemoteProtocols &= ~BROWSE_CUPS; | |
f899b121 | 4845 | } |
4846 | ||
4847 | return; | |
4848 | } | |
4849 | ||
4850 | packet[bytes] = '\0'; | |
4851 | ||
4852 | /* | |
4853 | * If we're about to sleep, ignore incoming browse packets. | |
4854 | */ | |
4855 | ||
4856 | if (Sleeping) | |
4857 | return; | |
4858 | ||
4859 | /* | |
4860 | * Figure out where it came from... | |
4861 | */ | |
4862 | ||
4863 | #ifdef AF_INET6 | |
4864 | if (srcaddr.addr.sa_family == AF_INET6) | |
4865 | { | |
4866 | address[0] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[0]); | |
4867 | address[1] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[1]); | |
4868 | address[2] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[2]); | |
4869 | address[3] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[3]); | |
4870 | } | |
4871 | else | |
4872 | #endif /* AF_INET6 */ | |
4873 | { | |
4874 | address[0] = 0; | |
4875 | address[1] = 0; | |
4876 | address[2] = 0; | |
4877 | address[3] = ntohl(srcaddr.ipv4.sin_addr.s_addr); | |
4878 | } | |
4879 | ||
4880 | if (HostNameLookups) | |
4881 | httpAddrLookup(&srcaddr, srcname, sizeof(srcname)); | |
4882 | else | |
4883 | httpAddrString(&srcaddr, srcname, sizeof(srcname)); | |
4884 | ||
4885 | len = strlen(srcname); | |
4886 | ||
4887 | /* | |
4888 | * Do ACL stuff... | |
4889 | */ | |
4890 | ||
4891 | if (BrowseACL) | |
4892 | { | |
4893 | if (httpAddrLocalhost(&srcaddr) || !strcasecmp(srcname, "localhost")) | |
4894 | { | |
4895 | /* | |
4896 | * Access from localhost (127.0.0.1) is always allowed... | |
4897 | */ | |
4898 | ||
5bd77a73 | 4899 | auth = CUPSD_AUTH_ALLOW; |
f899b121 | 4900 | } |
4901 | else | |
4902 | { | |
4903 | /* | |
4904 | * Do authorization checks on the domain/address... | |
4905 | */ | |
4906 | ||
4907 | switch (BrowseACL->order_type) | |
4908 | { | |
4909 | default : | |
5bd77a73 | 4910 | auth = CUPSD_AUTH_DENY; /* anti-compiler-warning-code */ |
f899b121 | 4911 | break; |
4912 | ||
5bd77a73 MS |
4913 | case CUPSD_AUTH_ALLOW : /* Order Deny,Allow */ |
4914 | auth = CUPSD_AUTH_ALLOW; | |
f899b121 | 4915 | |
4916 | if (cupsdCheckAuth(address, srcname, len, | |
4917 | BrowseACL->num_deny, BrowseACL->deny)) | |
5bd77a73 | 4918 | auth = CUPSD_AUTH_DENY; |
f899b121 | 4919 | |
4920 | if (cupsdCheckAuth(address, srcname, len, | |
4921 | BrowseACL->num_allow, BrowseACL->allow)) | |
5bd77a73 | 4922 | auth = CUPSD_AUTH_ALLOW; |
f899b121 | 4923 | break; |
4924 | ||
5bd77a73 MS |
4925 | case CUPSD_AUTH_DENY : /* Order Allow,Deny */ |
4926 | auth = CUPSD_AUTH_DENY; | |
f899b121 | 4927 | |
4928 | if (cupsdCheckAuth(address, srcname, len, | |
4929 | BrowseACL->num_allow, BrowseACL->allow)) | |
5bd77a73 | 4930 | auth = CUPSD_AUTH_ALLOW; |
f899b121 | 4931 | |
4932 | if (cupsdCheckAuth(address, srcname, len, | |
4933 | BrowseACL->num_deny, BrowseACL->deny)) | |
5bd77a73 | 4934 | auth = CUPSD_AUTH_DENY; |
f899b121 | 4935 | break; |
4936 | } | |
4937 | } | |
4938 | } | |
4939 | else | |
5bd77a73 | 4940 | auth = CUPSD_AUTH_ALLOW; |
f899b121 | 4941 | |
5bd77a73 | 4942 | if (auth == CUPSD_AUTH_DENY) |
f899b121 | 4943 | { |
4944 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
4945 | "update_cups_browse: Refused %d bytes from %s", bytes, | |
4946 | srcname); | |
4947 | return; | |
4948 | } | |
4949 | ||
4950 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
4951 | "update_cups_browse: (%d bytes from %s) %s", bytes, | |
4952 | srcname, packet); | |
4953 | ||
4954 | /* | |
4955 | * Parse packet... | |
4956 | */ | |
4957 | ||
4958 | if (sscanf(packet, "%x%x%1023s", &type, &state, uri) < 3) | |
4959 | { | |
4960 | cupsdLogMessage(CUPSD_LOG_WARN, | |
4961 | "update_cups_browse: Garbled browse packet - %s", packet); | |
4962 | return; | |
4963 | } | |
4964 | ||
4965 | strcpy(location, "Location Unknown"); | |
4966 | strcpy(info, "No Information Available"); | |
4967 | make_model[0] = '\0'; | |
4968 | num_attrs = 0; | |
4969 | attrs = NULL; | |
4970 | ||
4971 | if ((pptr = strchr(packet, '\"')) != NULL) | |
4972 | { | |
4973 | /* | |
4974 | * Have extended information; can't use sscanf for it because not all | |
4975 | * sscanf's allow empty strings with %[^\"]... | |
4976 | */ | |
4977 | ||
4978 | for (i = 0, pptr ++; | |
4979 | i < (sizeof(location) - 1) && *pptr && *pptr != '\"'; | |
4980 | i ++, pptr ++) | |
4981 | location[i] = *pptr; | |
4982 | ||
4983 | if (i) | |
4984 | location[i] = '\0'; | |
4985 | ||
4986 | if (*pptr == '\"') | |
4987 | pptr ++; | |
4988 | ||
4989 | while (*pptr && isspace(*pptr & 255)) | |
4990 | pptr ++; | |
4991 | ||
4992 | if (*pptr == '\"') | |
4993 | { | |
4994 | for (i = 0, pptr ++; | |
4995 | i < (sizeof(info) - 1) && *pptr && *pptr != '\"'; | |
4996 | i ++, pptr ++) | |
4997 | info[i] = *pptr; | |
4998 | ||
4999 | info[i] = '\0'; | |
5000 | ||
5001 | if (*pptr == '\"') | |
5002 | pptr ++; | |
5003 | ||
5004 | while (*pptr && isspace(*pptr & 255)) | |
5005 | pptr ++; | |
5006 | ||
5007 | if (*pptr == '\"') | |
5008 | { | |
5009 | for (i = 0, pptr ++; | |
5010 | i < (sizeof(make_model) - 1) && *pptr && *pptr != '\"'; | |
5011 | i ++, pptr ++) | |
5012 | make_model[i] = *pptr; | |
5013 | ||
5014 | if (*pptr == '\"') | |
5015 | pptr ++; | |
5016 | ||
5017 | make_model[i] = '\0'; | |
5018 | ||
5019 | if (*pptr) | |
5020 | num_attrs = cupsParseOptions(pptr, num_attrs, &attrs); | |
5021 | } | |
5022 | } | |
5023 | } | |
5024 | ||
5025 | DEBUG_puts(packet); | |
5026 | DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n" | |
5027 | "location=\"%s\", info=\"%s\", make_model=\"%s\"\n", | |
5028 | type, state, uri, location, info, make_model)); | |
5029 | ||
5030 | /* | |
5031 | * Pull the URI apart to see if this is a local or remote printer... | |
5032 | */ | |
5033 | ||
5034 | if (is_local_queue(uri, host, sizeof(host), resource, sizeof(resource))) | |
5035 | { | |
5036 | cupsFreeOptions(num_attrs, attrs); | |
5037 | return; | |
5038 | } | |
5039 | ||
5040 | /* | |
5041 | * Do relaying... | |
5042 | */ | |
5043 | ||
5044 | for (i = 0; i < NumRelays; i ++) | |
5045 | if (cupsdCheckAuth(address, srcname, len, 1, &(Relays[i].from))) | |
5046 | if (sendto(BrowseSocket, packet, bytes, 0, | |
5047 | (struct sockaddr *)&(Relays[i].to), | |
5048 | httpAddrLength(&(Relays[i].to))) <= 0) | |
5049 | { | |
5050 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
5051 | "update_cups_browse: sendto failed for relay %d - %s.", | |
5052 | i + 1, strerror(errno)); | |
5053 | cupsFreeOptions(num_attrs, attrs); | |
5054 | return; | |
5055 | } | |
5056 | ||
5057 | /* | |
5058 | * Process the browse data... | |
5059 | */ | |
5060 | ||
5061 | process_browse_data(uri, host, resource, (cups_ptype_t)type, | |
5062 | (ipp_pstate_t)state, location, info, make_model, | |
5063 | num_attrs, attrs); | |
5064 | } | |
5065 | ||
5066 | ||
2e4ff8af MS |
5067 | /* |
5068 | * 'update_lpd()' - Update the LPD configuration as needed. | |
5069 | */ | |
5070 | ||
5071 | static void | |
5072 | update_lpd(int onoff) /* - 1 = turn on, 0 = turn off */ | |
5073 | { | |
5074 | if (!LPDConfigFile) | |
5075 | return; | |
5076 | ||
a603edef MS |
5077 | #ifdef __APPLE__ |
5078 | /* | |
5079 | * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf | |
5080 | * setting for backwards-compatibility. | |
5081 | */ | |
5082 | ||
5083 | if (onoff && !get_hostconfig("CUPS_LPD")) | |
5084 | onoff = 0; | |
5085 | #endif /* __APPLE__ */ | |
5086 | ||
2e4ff8af MS |
5087 | if (!strncmp(LPDConfigFile, "xinetd:///", 10)) |
5088 | { | |
5089 | /* | |
5090 | * Enable/disable LPD via the xinetd.d config file for cups-lpd... | |
5091 | */ | |
5092 | ||
5093 | char newfile[1024]; /* New cups-lpd.N file */ | |
5094 | cups_file_t *ofp, /* Original file pointer */ | |
5095 | *nfp; /* New file pointer */ | |
5096 | char line[1024]; /* Line from file */ | |
5097 | ||
5098 | ||
5099 | snprintf(newfile, sizeof(newfile), "%s.N", LPDConfigFile + 9); | |
5100 | ||
5101 | if ((ofp = cupsFileOpen(LPDConfigFile + 9, "r")) == NULL) | |
5102 | { | |
5103 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"%s\" - %s", | |
5104 | LPDConfigFile + 9, strerror(errno)); | |
5105 | return; | |
5106 | } | |
5107 | ||
5108 | if ((nfp = cupsFileOpen(newfile, "w")) == NULL) | |
5109 | { | |
5110 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create \"%s\" - %s", | |
5111 | newfile, strerror(errno)); | |
5112 | cupsFileClose(ofp); | |
5113 | return; | |
5114 | } | |
5115 | ||
5116 | /* | |
5117 | * Copy all of the lines from the cups-lpd file... | |
5118 | */ | |
5119 | ||
5120 | while (cupsFileGets(ofp, line, sizeof(line))) | |
5121 | { | |
5122 | if (line[0] == '{') | |
5123 | { | |
5124 | cupsFilePrintf(nfp, "%s\n", line); | |
5125 | snprintf(line, sizeof(line), "\tdisable = %s", | |
5126 | onoff ? "no" : "yes"); | |
5127 | } | |
568fa3fa MS |
5128 | else if (!strstr(line, "disable =")) |
5129 | cupsFilePrintf(nfp, "%s\n", line); | |
2e4ff8af MS |
5130 | } |
5131 | ||
5132 | cupsFileClose(nfp); | |
5133 | cupsFileClose(ofp); | |
5134 | rename(newfile, LPDConfigFile + 9); | |
5135 | } | |
568fa3fa | 5136 | #ifdef __APPLE__ |
2e4ff8af MS |
5137 | else if (!strncmp(LPDConfigFile, "launchd:///", 11)) |
5138 | { | |
5139 | /* | |
5140 | * Enable/disable LPD via the launchctl command... | |
5141 | */ | |
5142 | ||
5143 | char *argv[5], /* Arguments for command */ | |
5144 | *envp[MAX_ENV]; /* Environment for command */ | |
5145 | int pid; /* Process ID */ | |
5146 | ||
5147 | ||
5148 | cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); | |
5149 | argv[0] = (char *)"launchctl"; | |
5150 | argv[1] = (char *)(onoff ? "load" : "unload"); | |
5151 | argv[2] = (char *)"-w"; | |
5152 | argv[3] = LPDConfigFile + 10; | |
5153 | argv[4] = NULL; | |
5154 | ||
a4924f6c MS |
5155 | cupsdStartProcess("/bin/launchctl", argv, envp, -1, -1, -1, -1, -1, 1, |
5156 | NULL, &pid); | |
2e4ff8af | 5157 | } |
568fa3fa MS |
5158 | #endif /* __APPLE__ */ |
5159 | else | |
5160 | cupsdLogMessage(CUPSD_LOG_INFO, "Unknown LPDConfigFile scheme!"); | |
2e4ff8af MS |
5161 | } |
5162 | ||
5163 | ||
f899b121 | 5164 | /* |
5165 | * 'update_polling()' - Read status messages from the poll daemons. | |
5166 | */ | |
5167 | ||
5168 | static void | |
5169 | update_polling(void) | |
5170 | { | |
5171 | char *ptr, /* Pointer to end of line in buffer */ | |
5172 | message[1024]; /* Pointer to message text */ | |
5173 | int loglevel; /* Log level for message */ | |
5174 | ||
5175 | ||
5176 | while ((ptr = cupsdStatBufUpdate(PollStatusBuffer, &loglevel, | |
5177 | message, sizeof(message))) != NULL) | |
5178 | if (!strchr(PollStatusBuffer->buffer, '\n')) | |
5179 | break; | |
5180 | ||
5181 | if (ptr == NULL && !PollStatusBuffer->bufused) | |
5182 | { | |
5183 | /* | |
5184 | * All polling processes have died; stop polling... | |
5185 | */ | |
5186 | ||
5187 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
5188 | "update_polling: all polling processes have exited!"); | |
5189 | cupsdStopPolling(); | |
5190 | } | |
5191 | } | |
5192 | ||
5193 | ||
5194 | /* | |
2e4ff8af MS |
5195 | * 'update_smb()' - Update the SMB configuration as needed. |
5196 | */ | |
5197 | ||
5198 | static void | |
5199 | update_smb(int onoff) /* I - 1 = turn on, 0 = turn off */ | |
5200 | { | |
5201 | if (!SMBConfigFile) | |
5202 | return; | |
5203 | ||
5204 | if (!strncmp(SMBConfigFile, "samba:///", 9)) | |
5205 | { | |
5206 | /* | |
5207 | * Enable/disable SMB via the specified smb.conf config file... | |
5208 | */ | |
5209 | ||
5210 | char newfile[1024]; /* New smb.conf.N file */ | |
5211 | cups_file_t *ofp, /* Original file pointer */ | |
5212 | *nfp; /* New file pointer */ | |
5213 | char line[1024]; /* Line from file */ | |
5214 | int in_printers; /* In [printers] section? */ | |
5215 | ||
5216 | ||
5217 | snprintf(newfile, sizeof(newfile), "%s.N", SMBConfigFile + 8); | |
5218 | ||
5219 | if ((ofp = cupsFileOpen(SMBConfigFile + 8, "r")) == NULL) | |
5220 | { | |
5221 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"%s\" - %s", | |
5222 | SMBConfigFile + 8, strerror(errno)); | |
5223 | return; | |
5224 | } | |
5225 | ||
5226 | if ((nfp = cupsFileOpen(newfile, "w")) == NULL) | |
5227 | { | |
5228 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create \"%s\" - %s", | |
5229 | newfile, strerror(errno)); | |
5230 | cupsFileClose(ofp); | |
5231 | return; | |
5232 | } | |
5233 | ||
5234 | /* | |
5235 | * Copy all of the lines from the smb.conf file... | |
5236 | */ | |
5237 | ||
5238 | in_printers = 0; | |
5239 | ||
5240 | while (cupsFileGets(ofp, line, sizeof(line))) | |
5241 | { | |
5242 | if (in_printers && strstr(line, "printable =")) | |
5243 | snprintf(line, sizeof(line), " printable = %s", | |
5244 | onoff ? "yes" : "no"); | |
5245 | ||
5246 | cupsFilePrintf(nfp, "%s\n", line); | |
5247 | ||
5248 | if (line[0] == '[') | |
5249 | in_printers = !strcmp(line, "[printers]"); | |
5250 | } | |
5251 | ||
5252 | cupsFileClose(nfp); | |
5253 | cupsFileClose(ofp); | |
5254 | rename(newfile, SMBConfigFile + 8); | |
5255 | } | |
568fa3fa MS |
5256 | else |
5257 | cupsdLogMessage(CUPSD_LOG_INFO, "Unknown SMBConfigFile scheme!"); | |
2e4ff8af MS |
5258 | } |
5259 | ||
5260 | ||
5261 | /* | |
b19ccc9e | 5262 | * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $". |
ef416fc2 | 5263 | */ |