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