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