| 1 |
/* -*- Mode: C; c-file-style: "gnu" -*- |
| 2 |
* udev-configure-printer - a udev callout to configure print queues |
| 3 |
* Copyright (C) 2009, 2010, 2011, 2012, 2014 Red Hat, Inc. |
| 4 |
* Author: Tim Waugh <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
/* |
| 23 |
* The protocol for this program is: |
| 24 |
* |
| 25 |
* udev-configure-printer add {DEVADDR} |
| 26 |
* udev-configure-printer remove {DEVADDR} |
| 27 |
* |
| 28 |
* where DEVADDR is one of: |
| 29 |
* the USB address of the device in the form usb-$env{BUSNUM}-$env{DEVNUM} |
| 30 |
* the device path of the device (%p) |
| 31 |
* the bluetooth address of the device |
| 32 |
*/ |
| 33 |
|
| 34 |
#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE 1 |
| 35 |
|
| 36 |
#include <cups/cups.h> |
| 37 |
#include <cups/http.h> |
| 38 |
#include <errno.h> |
| 39 |
#include <fcntl.h> |
| 40 |
#include <libudev.h> |
| 41 |
#include <limits.h> |
| 42 |
#include <stdio.h> |
| 43 |
#include <stdlib.h> |
| 44 |
#include <string.h> |
| 45 |
#include <sys/types.h> |
| 46 |
#include <sys/stat.h> |
| 47 |
#include <sys/wait.h> |
| 48 |
#include <ctype.h> |
| 49 |
#include <syslog.h> |
| 50 |
#include <unistd.h> |
| 51 |
#include <libusb.h> |
| 52 |
#include <glib.h> |
| 53 |
#include <dirent.h> |
| 54 |
|
| 55 |
#define DISABLED_REASON "Unplugged or turned off" |
| 56 |
#define MATCH_ONLY_DISABLED 1 |
| 57 |
#define USB_URI_MAP "/var/run/udev-configure-printer/usb-uris" |
| 58 |
|
| 59 |
#if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5) |
| 60 |
#define HAVE_CUPS_1_6 1 |
| 61 |
#endif |
| 62 |
|
| 63 |
/* |
| 64 |
* CUPS 1.6 makes various structures private and |
| 65 |
* introduces these ippGet and ippSet functions |
| 66 |
* for all of the fields in these structures. |
| 67 |
* http://www.cups.org/str.php?L3928 |
| 68 |
* We define (same signatures) our own accessors when CUPS < 1.6. |
| 69 |
*/ |
| 70 |
#ifndef HAVE_CUPS_1_6 |
| 71 |
const char * |
| 72 |
ippGetName(ipp_attribute_t *attr) |
| 73 |
{ |
| 74 |
return (attr->name); |
| 75 |
} |
| 76 |
|
| 77 |
ipp_op_t |
| 78 |
ippGetOperation(ipp_t *ipp) |
| 79 |
{ |
| 80 |
return (ipp->request.op.operation_id); |
| 81 |
} |
| 82 |
|
| 83 |
ipp_status_t |
| 84 |
ippGetStatusCode(ipp_t *ipp) |
| 85 |
{ |
| 86 |
return (ipp->request.status.status_code); |
| 87 |
} |
| 88 |
|
| 89 |
ipp_tag_t |
| 90 |
ippGetGroupTag(ipp_attribute_t *attr) |
| 91 |
{ |
| 92 |
return (attr->group_tag); |
| 93 |
} |
| 94 |
|
| 95 |
ipp_tag_t |
| 96 |
ippGetValueTag(ipp_attribute_t *attr) |
| 97 |
{ |
| 98 |
return (attr->value_tag); |
| 99 |
} |
| 100 |
|
| 101 |
int |
| 102 |
ippGetInteger(ipp_attribute_t *attr, |
| 103 |
int element) |
| 104 |
{ |
| 105 |
return (attr->values[element].integer); |
| 106 |
} |
| 107 |
|
| 108 |
const char * |
| 109 |
ippGetString(ipp_attribute_t *attr, |
| 110 |
int element, |
| 111 |
const char **language) |
| 112 |
{ |
| 113 |
return (attr->values[element].string.text); |
| 114 |
} |
| 115 |
|
| 116 |
ipp_attribute_t * |
| 117 |
ippFirstAttribute(ipp_t *ipp) |
| 118 |
{ |
| 119 |
if (!ipp) |
| 120 |
return (NULL); |
| 121 |
return (ipp->current = ipp->attrs); |
| 122 |
} |
| 123 |
|
| 124 |
ipp_attribute_t * |
| 125 |
ippNextAttribute(ipp_t *ipp) |
| 126 |
{ |
| 127 |
if (!ipp || !ipp->current) |
| 128 |
return (NULL); |
| 129 |
return (ipp->current = ipp->current->next); |
| 130 |
} |
| 131 |
|
| 132 |
#endif |
| 133 |
|
| 134 |
struct device_uris |
| 135 |
{ |
| 136 |
size_t n_uris; |
| 137 |
char **uri; |
| 138 |
}; |
| 139 |
|
| 140 |
struct usb_uri_map_entry |
| 141 |
{ |
| 142 |
struct usb_uri_map_entry *next; |
| 143 |
|
| 144 |
/* The devpath of the ("usb","usb_device") device. */ |
| 145 |
char *devpath; |
| 146 |
|
| 147 |
/* List of matching device URIs. */ |
| 148 |
struct device_uris uris; |
| 149 |
}; |
| 150 |
|
| 151 |
struct usb_uri_map |
| 152 |
{ |
| 153 |
struct usb_uri_map_entry *entries; |
| 154 |
|
| 155 |
/* Open file descriptor for the map, or -1 if it has already been |
| 156 |
* written. */ |
| 157 |
int fd; |
| 158 |
}; |
| 159 |
|
| 160 |
struct device_id |
| 161 |
{ |
| 162 |
char *full_device_id; |
| 163 |
char *mfg; |
| 164 |
char *mdl; |
| 165 |
char *sern; |
| 166 |
}; |
| 167 |
|
| 168 |
/* Device URI schemes in decreasing order of preference. */ |
| 169 |
static const char *device_uri_types[] = |
| 170 |
{ |
| 171 |
"hp", |
| 172 |
"usb", |
| 173 |
}; |
| 174 |
|
| 175 |
static int |
| 176 |
device_uri_type (const char *uri) |
| 177 |
{ |
| 178 |
int slen = strcspn (uri, ":"); |
| 179 |
int i; |
| 180 |
int n = sizeof (device_uri_types) / sizeof (device_uri_types[0]); |
| 181 |
for (i = 0; i < n; i++) |
| 182 |
if (!strncmp (uri, device_uri_types[i], slen) && |
| 183 |
device_uri_types[i][slen] == '\0') |
| 184 |
break; |
| 185 |
|
| 186 |
return i; |
| 187 |
} |
| 188 |
|
| 189 |
static void |
| 190 |
add_device_uri (struct device_uris *uris, |
| 191 |
const char *uri) |
| 192 |
{ |
| 193 |
char *uri_copy = strdup (uri); |
| 194 |
if (!uri_copy) |
| 195 |
{ |
| 196 |
syslog (LOG_ERR, "out of memory"); |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
if (uris->n_uris == 0) |
| 201 |
{ |
| 202 |
uris->uri = malloc (sizeof (char *)); |
| 203 |
if (uris->uri) |
| 204 |
{ |
| 205 |
uris->n_uris = 1; |
| 206 |
uris->uri[0] = uri_copy; |
| 207 |
} |
| 208 |
else |
| 209 |
{ |
| 210 |
syslog (LOG_ERR, "out of memory"); |
| 211 |
free (uri_copy); |
| 212 |
return; |
| 213 |
} |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
char **old = uris->uri; |
| 218 |
if (++uris->n_uris < UINT_MAX / sizeof (char *)) |
| 219 |
{ |
| 220 |
uris->uri = realloc (uris->uri, |
| 221 |
sizeof (char *) * uris->n_uris); |
| 222 |
if (uris->uri) |
| 223 |
uris->uri[uris->n_uris - 1] = uri_copy; |
| 224 |
else |
| 225 |
{ |
| 226 |
uris->uri = old; |
| 227 |
uris->n_uris--; |
| 228 |
free (uri_copy); |
| 229 |
} |
| 230 |
} |
| 231 |
else |
| 232 |
{ |
| 233 |
uris->n_uris--; |
| 234 |
free (uri_copy); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
static void |
| 240 |
free_device_uris (struct device_uris *uris) |
| 241 |
{ |
| 242 |
size_t i; |
| 243 |
for (i = 0; i < uris->n_uris; i++) |
| 244 |
free (uris->uri[i]); |
| 245 |
free (uris->uri); |
| 246 |
} |
| 247 |
|
| 248 |
static void |
| 249 |
add_usb_uri_mapping (struct usb_uri_map **map, |
| 250 |
const char *devpath, |
| 251 |
const struct device_uris *uris) |
| 252 |
{ |
| 253 |
struct usb_uri_map_entry *entry, **prev; |
| 254 |
size_t i; |
| 255 |
prev = &(*map)->entries; |
| 256 |
while (*prev) |
| 257 |
prev = &((*prev)->next); |
| 258 |
|
| 259 |
entry = malloc (sizeof (struct usb_uri_map_entry)); |
| 260 |
if (!entry) |
| 261 |
{ |
| 262 |
syslog (LOG_ERR, "out of memory"); |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
entry->devpath = strdup (devpath); |
| 267 |
entry->uris.n_uris = uris->n_uris; |
| 268 |
entry->uris.uri = malloc (sizeof (char *) * uris->n_uris); |
| 269 |
for (i = 0; i < uris->n_uris; i++) |
| 270 |
entry->uris.uri[i] = strdup (uris->uri[i]); |
| 271 |
entry->next = NULL; |
| 272 |
*prev = entry; |
| 273 |
} |
| 274 |
|
| 275 |
static struct usb_uri_map * |
| 276 |
read_usb_uri_map (void) |
| 277 |
{ |
| 278 |
int fd = open (USB_URI_MAP, O_RDWR); |
| 279 |
struct usb_uri_map *map = NULL; |
| 280 |
struct flock lock; |
| 281 |
struct stat st; |
| 282 |
char *buf, *line; |
| 283 |
|
| 284 |
if (fd == -1) |
| 285 |
{ |
| 286 |
char dir[] = USB_URI_MAP; |
| 287 |
char *p = strrchr (dir, '/'); |
| 288 |
if (p) |
| 289 |
{ |
| 290 |
*p = '\0'; |
| 291 |
mkdir (dir, 0755); |
| 292 |
fd = open (USB_URI_MAP, O_RDWR | O_TRUNC | O_CREAT, 0644); |
| 293 |
if (fd == -1) |
| 294 |
{ |
| 295 |
syslog (LOG_ERR, "failed to create " USB_URI_MAP); |
| 296 |
exit (1); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
map = malloc (sizeof (struct usb_uri_map)); |
| 302 |
if (!map) |
| 303 |
{ |
| 304 |
syslog (LOG_ERR, "out of memory"); |
| 305 |
exit (1); |
| 306 |
} |
| 307 |
|
| 308 |
lock.l_type = F_WRLCK; |
| 309 |
lock.l_whence = SEEK_SET; |
| 310 |
lock.l_start = 0; |
| 311 |
lock.l_len = 0; |
| 312 |
if (fcntl (fd, F_SETLKW, &lock) == -1) |
| 313 |
{ |
| 314 |
syslog (LOG_ERR, "failed to lock " USB_URI_MAP); |
| 315 |
exit (1); |
| 316 |
} |
| 317 |
|
| 318 |
map->entries = NULL; |
| 319 |
map->fd = fd; |
| 320 |
if (fstat (fd, &st) == -1) |
| 321 |
{ |
| 322 |
syslog (LOG_ERR, "failed to fstat " USB_URI_MAP " (fd %d)", fd); |
| 323 |
exit (1); |
| 324 |
} |
| 325 |
|
| 326 |
/* Read the entire file into memory. */ |
| 327 |
buf = malloc (1 + (sizeof (char) * st.st_size)); |
| 328 |
if (!buf) |
| 329 |
{ |
| 330 |
syslog (LOG_ERR, "out of memory"); |
| 331 |
exit (1); |
| 332 |
} |
| 333 |
|
| 334 |
if (read (fd, buf, st.st_size) < 0) |
| 335 |
{ |
| 336 |
syslog (LOG_ERR, "failed to read " USB_URI_MAP); |
| 337 |
exit (1); |
| 338 |
} |
| 339 |
|
| 340 |
buf[st.st_size] = '\0'; |
| 341 |
line = buf; |
| 342 |
while (line) |
| 343 |
{ |
| 344 |
char *saveptr = NULL; |
| 345 |
const char *devpath, *uri; |
| 346 |
struct device_uris uris; |
| 347 |
char *nextline = strchr (line, '\n'); |
| 348 |
if (!nextline) |
| 349 |
break; |
| 350 |
|
| 351 |
*nextline++ = '\0'; |
| 352 |
if (nextline >= buf + st.st_size) |
| 353 |
nextline = NULL; |
| 354 |
|
| 355 |
devpath = strtok_r (line, "\t", &saveptr); |
| 356 |
uri = strtok_r (NULL, "\t", &saveptr); |
| 357 |
if (!devpath || !uri) |
| 358 |
{ |
| 359 |
syslog (LOG_DEBUG, "Incorrect line in " USB_URI_MAP ": %s", |
| 360 |
line); |
| 361 |
continue; |
| 362 |
} |
| 363 |
|
| 364 |
uris.n_uris = 1; |
| 365 |
uris.uri = malloc (sizeof (char *)); |
| 366 |
if (uris.uri == NULL) |
| 367 |
break; |
| 368 |
|
| 369 |
uris.uri[0] = strdup (uri); |
| 370 |
while ((uri = strtok_r (NULL, "\t", &saveptr)) != NULL) |
| 371 |
add_device_uri (&uris, uri); |
| 372 |
|
| 373 |
add_usb_uri_mapping (&map, devpath, &uris); |
| 374 |
|
| 375 |
line = nextline; |
| 376 |
} |
| 377 |
|
| 378 |
free (buf); |
| 379 |
return map; |
| 380 |
} |
| 381 |
|
| 382 |
static void |
| 383 |
write_usb_uri_map (struct usb_uri_map *map) |
| 384 |
{ |
| 385 |
struct usb_uri_map_entry *entry; |
| 386 |
int fd = map->fd; |
| 387 |
FILE *f; |
| 388 |
|
| 389 |
lseek (fd, SEEK_SET, 0); |
| 390 |
if (ftruncate (fd, 0) == -1) |
| 391 |
{ |
| 392 |
syslog (LOG_ERR, "failed to ftruncate " USB_URI_MAP " (fd %d, errno %d)", |
| 393 |
fd, errno); |
| 394 |
exit (1); |
| 395 |
} |
| 396 |
|
| 397 |
f = fdopen (fd, "w"); |
| 398 |
if (!f) |
| 399 |
{ |
| 400 |
syslog (LOG_ERR, "failed to fdopen " USB_URI_MAP " (fd %d, errno %d)", |
| 401 |
fd, errno); |
| 402 |
exit (1); |
| 403 |
} |
| 404 |
|
| 405 |
for (entry = map->entries; entry; entry = entry->next) |
| 406 |
{ |
| 407 |
size_t i; |
| 408 |
fprintf (f, "%s\t%s", entry->devpath, entry->uris.uri[0]); |
| 409 |
for (i = 1; i < entry->uris.n_uris; i++) |
| 410 |
{ |
| 411 |
if (fprintf (f, "\t%s", entry->uris.uri[i]) < 0) |
| 412 |
{ |
| 413 |
syslog (LOG_ERR, "failed to fprintf " USB_URI_MAP " (errno %d)", |
| 414 |
errno); |
| 415 |
exit (1); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
if (fwrite ("\n", 1, 1, f) < 1) |
| 420 |
{ |
| 421 |
syslog (LOG_ERR, "failed to fwrite " USB_URI_MAP " (errno %d)", |
| 422 |
errno); |
| 423 |
exit (1); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
if (fclose (f) == EOF) |
| 428 |
syslog (LOG_ERR, "error closing " USB_URI_MAP " (errno %d)", errno); |
| 429 |
|
| 430 |
map->fd = -1; |
| 431 |
} |
| 432 |
|
| 433 |
static void |
| 434 |
free_usb_uri_map (struct usb_uri_map *map) |
| 435 |
{ |
| 436 |
struct usb_uri_map_entry *entry, *next; |
| 437 |
for (entry = map->entries; entry; entry = next) |
| 438 |
{ |
| 439 |
next = entry->next; |
| 440 |
free (entry->devpath); |
| 441 |
free_device_uris (&entry->uris); |
| 442 |
free (entry); |
| 443 |
} |
| 444 |
|
| 445 |
if (map->fd != -1) |
| 446 |
close (map->fd); |
| 447 |
|
| 448 |
free (map); |
| 449 |
} |
| 450 |
|
| 451 |
static void |
| 452 |
clear_device_id (struct device_id *id) |
| 453 |
{ |
| 454 |
free (id->full_device_id); |
| 455 |
free (id->mfg); |
| 456 |
free (id->mdl); |
| 457 |
free (id->sern); |
| 458 |
} |
| 459 |
|
| 460 |
static void |
| 461 |
parse_device_id (const char *device_id, |
| 462 |
struct device_id *id) |
| 463 |
{ |
| 464 |
char *fieldname; |
| 465 |
char *start, *end; |
| 466 |
size_t len; |
| 467 |
|
| 468 |
len = strlen (device_id); |
| 469 |
if (len == 0) |
| 470 |
return; |
| 471 |
|
| 472 |
if (device_id[len - 1] == '\n') |
| 473 |
len--; |
| 474 |
|
| 475 |
id->full_device_id = malloc (len + 1); |
| 476 |
fieldname = malloc (len + 1); |
| 477 |
if (!id->full_device_id || !fieldname) |
| 478 |
{ |
| 479 |
syslog (LOG_ERR, "out of memory"); |
| 480 |
exit (1); |
| 481 |
} |
| 482 |
|
| 483 |
memcpy (id->full_device_id, device_id, len); |
| 484 |
id->full_device_id[len] = '\0'; |
| 485 |
fieldname[0] = '\0'; |
| 486 |
start = id->full_device_id; |
| 487 |
while (*start != '\0') |
| 488 |
{ |
| 489 |
/* New field. */ |
| 490 |
|
| 491 |
end = start; |
| 492 |
while (*end != '\0' && *end != ':') |
| 493 |
end++; |
| 494 |
|
| 495 |
if (*end == '\0') |
| 496 |
break; |
| 497 |
|
| 498 |
len = end - start; |
| 499 |
memcpy (fieldname, start, len); |
| 500 |
fieldname[len] = '\0'; |
| 501 |
|
| 502 |
start = end + 1; |
| 503 |
while (*end != '\0' && *end != ';') |
| 504 |
end++; |
| 505 |
|
| 506 |
len = end - start; |
| 507 |
|
| 508 |
if (!id->mfg && |
| 509 |
(!strncasecmp (fieldname, "MANUFACTURER", 12) || |
| 510 |
!strncasecmp (fieldname, "MFG", 3))) |
| 511 |
id->mfg = strndup (start, len); |
| 512 |
else if (!id->mdl && |
| 513 |
(!strncasecmp (fieldname, "MODEL", 5) || |
| 514 |
!strncasecmp (fieldname, "MDL", 3))) |
| 515 |
id->mdl = strndup (start, len); |
| 516 |
else if (!id->sern && |
| 517 |
(!strncasecmp (fieldname, "SERIALNUMBER", 12) || |
| 518 |
!strncasecmp (fieldname, "SERN", 4) || |
| 519 |
!strncasecmp (fieldname, "SN", 2))) |
| 520 |
id->sern = strndup (start, len); |
| 521 |
|
| 522 |
if (*end != '\0') |
| 523 |
start = end + 1; |
| 524 |
} |
| 525 |
|
| 526 |
free (fieldname); |
| 527 |
} |
| 528 |
|
| 529 |
int |
| 530 |
device_file_filter(const struct dirent *entry) |
| 531 |
{ |
| 532 |
return ((strstr(entry->d_name, "lp") != NULL) ? 1 : 0); |
| 533 |
} |
| 534 |
|
| 535 |
static char * |
| 536 |
get_ieee1284_id_from_child (struct udev *udev, struct udev_device *parent) |
| 537 |
{ |
| 538 |
struct udev_enumerate *udev_enum; |
| 539 |
struct udev_list_entry *item, *first = NULL; |
| 540 |
char *device_id = NULL; |
| 541 |
|
| 542 |
udev_enum = udev_enumerate_new (udev); |
| 543 |
if (!udev_enum) |
| 544 |
{ |
| 545 |
syslog (LOG_ERR, "udev_enumerate_new failed"); |
| 546 |
exit (1); |
| 547 |
} |
| 548 |
|
| 549 |
if (udev_enumerate_add_match_parent (udev_enum, parent) < 0) |
| 550 |
{ |
| 551 |
udev_enumerate_unref (udev_enum); |
| 552 |
syslog (LOG_ERR, "uname to add parent match"); |
| 553 |
exit (1); |
| 554 |
} |
| 555 |
|
| 556 |
if (udev_enumerate_scan_devices (udev_enum) < 0) |
| 557 |
{ |
| 558 |
udev_enumerate_unref (udev_enum); |
| 559 |
syslog (LOG_ERR, "udev_enumerate_scan_devices failed"); |
| 560 |
exit (1); |
| 561 |
} |
| 562 |
|
| 563 |
first = udev_enumerate_get_list_entry (udev_enum); |
| 564 |
udev_list_entry_foreach (item, first) |
| 565 |
{ |
| 566 |
const char *ieee1284_id = NULL; |
| 567 |
struct udev_device *dev; |
| 568 |
dev = udev_device_new_from_syspath (udev, |
| 569 |
udev_list_entry_get_name (item)); |
| 570 |
if (dev == NULL) |
| 571 |
continue; |
| 572 |
|
| 573 |
ieee1284_id = udev_device_get_sysattr_value (dev, "ieee1284_id"); |
| 574 |
if (ieee1284_id) |
| 575 |
device_id = g_strdup (ieee1284_id); |
| 576 |
|
| 577 |
udev_device_unref (dev); |
| 578 |
if (device_id) |
| 579 |
break; |
| 580 |
} |
| 581 |
|
| 582 |
udev_enumerate_unref (udev_enum); |
| 583 |
return device_id; |
| 584 |
} |
| 585 |
|
| 586 |
static char * |
| 587 |
get_ieee1284_id_using_libusb (struct udev_device *dev, |
| 588 |
const char *usbserial) |
| 589 |
{ |
| 590 |
const char *idVendorStr, *idProductStr; |
| 591 |
unsigned long idVendor, idProduct; |
| 592 |
char *end; |
| 593 |
int conf = 0, iface = 0, altset = 0, numdevs = 0, i, n, m; |
| 594 |
libusb_device **list; |
| 595 |
struct libusb_device *device; |
| 596 |
struct libusb_device_handle *handle = NULL; |
| 597 |
struct libusb_device_descriptor devdesc; |
| 598 |
struct libusb_config_descriptor *confptr = NULL; |
| 599 |
const struct libusb_interface *ifaceptr = NULL; |
| 600 |
const struct libusb_interface_descriptor *altptr = NULL; |
| 601 |
char libusbserial[1024]; |
| 602 |
char ieee1284_id[1024]; |
| 603 |
int got = 0; |
| 604 |
|
| 605 |
idVendorStr = udev_device_get_sysattr_value (dev, "idVendor"); |
| 606 |
idProductStr = udev_device_get_sysattr_value (dev, "idProduct"); |
| 607 |
|
| 608 |
if (!idVendorStr || !idProductStr) |
| 609 |
{ |
| 610 |
syslog (LOG_ERR, "Missing sysattr %s", |
| 611 |
idVendorStr ? |
| 612 |
(idProductStr ? "serial" : "idProduct") : "idVendor"); |
| 613 |
return NULL; |
| 614 |
} |
| 615 |
|
| 616 |
idVendor = strtoul (idVendorStr, &end, 16); |
| 617 |
if (end == idVendorStr) |
| 618 |
{ |
| 619 |
syslog (LOG_ERR, "Invalid idVendor: %s", idVendorStr); |
| 620 |
return NULL; |
| 621 |
} |
| 622 |
|
| 623 |
idProduct = strtoul (idProductStr, &end, 16); |
| 624 |
if (end == idProductStr) |
| 625 |
{ |
| 626 |
syslog (LOG_ERR, "Invalid idProduct: %s", idProductStr); |
| 627 |
return NULL; |
| 628 |
} |
| 629 |
|
| 630 |
syslog (LOG_DEBUG, "Device vendor/product is %04zX:%04zX", |
| 631 |
idVendor, idProduct); |
| 632 |
|
| 633 |
libusb_init(NULL); |
| 634 |
numdevs = libusb_get_device_list(NULL, &list); |
| 635 |
if (numdevs > 0) |
| 636 |
for (i = 0; i < numdevs; i++) |
| 637 |
{ |
| 638 |
device = list[i]; |
| 639 |
|
| 640 |
if (libusb_get_device_descriptor(device, &devdesc) < 0) |
| 641 |
continue; |
| 642 |
|
| 643 |
if (!devdesc.bNumConfigurations || !devdesc.idVendor || |
| 644 |
!devdesc.idProduct) |
| 645 |
continue; |
| 646 |
|
| 647 |
if (devdesc.idVendor != idVendor || devdesc.idProduct != idProduct) |
| 648 |
continue; |
| 649 |
|
| 650 |
for (conf = 0; conf < devdesc.bNumConfigurations; conf ++) |
| 651 |
{ |
| 652 |
if (libusb_get_config_descriptor(device, conf, &confptr) < 0) |
| 653 |
continue; |
| 654 |
for (iface = 0, ifaceptr = confptr->interface; |
| 655 |
iface < confptr->bNumInterfaces; |
| 656 |
iface ++, ifaceptr ++) |
| 657 |
{ |
| 658 |
for (altset = 0, altptr = ifaceptr->altsetting; |
| 659 |
altset < ifaceptr->num_altsetting; |
| 660 |
altset ++, altptr ++) |
| 661 |
{ |
| 662 |
if (altptr->bInterfaceClass != LIBUSB_CLASS_PRINTER || |
| 663 |
altptr->bInterfaceSubClass != 1) |
| 664 |
continue; |
| 665 |
|
| 666 |
if (libusb_open(device, &handle) < 0) |
| 667 |
{ |
| 668 |
syslog (LOG_DEBUG, "failed to open device"); |
| 669 |
continue; |
| 670 |
} |
| 671 |
|
| 672 |
if (usbserial[0] != '\0' && |
| 673 |
(libusb_get_string_descriptor_ascii(handle, |
| 674 |
devdesc.iSerialNumber, |
| 675 |
(unsigned char *)libusbserial, |
| 676 |
sizeof(libusbserial))) > 0 && |
| 677 |
strcmp(usbserial, libusbserial) != 0) |
| 678 |
{ |
| 679 |
libusb_close (handle); |
| 680 |
handle = NULL; |
| 681 |
continue; |
| 682 |
} |
| 683 |
|
| 684 |
n = altptr->bInterfaceNumber; |
| 685 |
if (libusb_claim_interface(handle, n) < 0) |
| 686 |
{ |
| 687 |
libusb_close (handle); |
| 688 |
handle = NULL; |
| 689 |
syslog (LOG_DEBUG, "failed to claim interface"); |
| 690 |
continue; |
| 691 |
} |
| 692 |
if (n != 0 && libusb_claim_interface(handle, 0) < 0) |
| 693 |
{ |
| 694 |
syslog (LOG_DEBUG, "failed to claim interface 0"); |
| 695 |
} |
| 696 |
|
| 697 |
m = altptr->bAlternateSetting; |
| 698 |
if (libusb_set_interface_alt_setting(handle, n, m) |
| 699 |
< 0) |
| 700 |
{ |
| 701 |
libusb_close (handle); |
| 702 |
handle = NULL; |
| 703 |
syslog (LOG_DEBUG, "failed set altinterface"); |
| 704 |
continue; |
| 705 |
} |
| 706 |
|
| 707 |
memset (ieee1284_id, '\0', sizeof (ieee1284_id)); |
| 708 |
if (libusb_control_transfer(handle, |
| 709 |
LIBUSB_REQUEST_TYPE_CLASS | |
| 710 |
LIBUSB_ENDPOINT_IN | |
| 711 |
LIBUSB_RECIPIENT_INTERFACE, |
| 712 |
0, conf, |
| 713 |
(n << 8) | m, |
| 714 |
(unsigned char *)ieee1284_id, |
| 715 |
sizeof (ieee1284_id), |
| 716 |
5000) < 0) |
| 717 |
{ |
| 718 |
libusb_close (handle); |
| 719 |
handle = NULL; |
| 720 |
syslog (LOG_ERR, "Failed to fetch Device ID"); |
| 721 |
continue; |
| 722 |
} |
| 723 |
|
| 724 |
got = 1; |
| 725 |
libusb_close (handle); |
| 726 |
break; |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
libusb_free_device_list(list, 1); |
| 733 |
libusb_exit(NULL); |
| 734 |
|
| 735 |
if (got) |
| 736 |
return g_strdup (ieee1284_id + 2); |
| 737 |
return NULL; |
| 738 |
} |
| 739 |
|
| 740 |
static char * |
| 741 |
device_id_from_devpath (struct udev *udev, const char *devpath, |
| 742 |
const struct usb_uri_map *map, |
| 743 |
struct device_id *id, |
| 744 |
char *usbserial, size_t usbseriallen, |
| 745 |
char *usblpdev, size_t usblpdevlen) |
| 746 |
{ |
| 747 |
struct usb_uri_map_entry *entry; |
| 748 |
struct udev_device *dev; |
| 749 |
const char *serial; |
| 750 |
size_t syslen, devpathlen; |
| 751 |
char *syspath, *devicefilepath; |
| 752 |
const char *device_id = NULL; |
| 753 |
char *usb_device_devpath; |
| 754 |
char *usblpdevpos, *dest; |
| 755 |
struct dirent **namelist; |
| 756 |
int num_names; |
| 757 |
|
| 758 |
syslen = strlen ("/sys"); |
| 759 |
devpathlen = strlen (devpath); |
| 760 |
syspath = malloc (syslen + devpathlen + 1); |
| 761 |
if (syspath == NULL) |
| 762 |
{ |
| 763 |
syslog (LOG_ERR, "out of memory"); |
| 764 |
exit (1); |
| 765 |
} |
| 766 |
memcpy (syspath, "/sys", syslen); |
| 767 |
memcpy (syspath + syslen, devpath, devpathlen); |
| 768 |
syspath[syslen + devpathlen] = '\0'; |
| 769 |
|
| 770 |
devicefilepath = malloc (syslen + devpathlen + 5); |
| 771 |
if (devicefilepath == NULL) |
| 772 |
{ |
| 773 |
syslog (LOG_ERR, "out of memory"); |
| 774 |
exit (1); |
| 775 |
} |
| 776 |
memcpy (devicefilepath, syspath, syslen + devpathlen); |
| 777 |
memcpy (devicefilepath + syslen + devpathlen, "/usb", 4); |
| 778 |
devicefilepath[syslen + devpathlen + 4] = '\0'; |
| 779 |
|
| 780 |
/* For devices under control of the usblp kernel module we read out the number |
| 781 |
* of the /dev/usb/lp* device file, as there can be queues set up with |
| 782 |
* non-standard CUPS backends based on the /dev/usb/lp* device file and |
| 783 |
* we want to avoid that an additional queue with a standard CUPS backend |
| 784 |
* gets set up. |
| 785 |
*/ |
| 786 |
num_names = scandir(devicefilepath, &namelist, device_file_filter, alphasort); |
| 787 |
if (num_names <= 0) |
| 788 |
num_names = scandir(syspath, &namelist, device_file_filter, alphasort); |
| 789 |
if (num_names > 0) |
| 790 |
{ |
| 791 |
usblpdevpos = strstr(namelist[0]->d_name, "lp"); |
| 792 |
if (usblpdevpos != NULL) |
| 793 |
{ |
| 794 |
usblpdevpos += 2; |
| 795 |
for (dest = usblpdev; |
| 796 |
(*usblpdevpos >= '0') && (*usblpdevpos <= '9') && |
| 797 |
(dest - usblpdev < usblpdevlen); |
| 798 |
usblpdevpos ++, dest ++) |
| 799 |
*dest = *usblpdevpos; |
| 800 |
*dest = '\0'; |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
dev = udev_device_new_from_syspath (udev, syspath); |
| 805 |
if (dev == NULL) |
| 806 |
{ |
| 807 |
udev_device_unref (dev); |
| 808 |
syslog (LOG_ERR, "unable to access %s", syspath); |
| 809 |
return NULL; |
| 810 |
} |
| 811 |
|
| 812 |
usb_device_devpath = strdup (udev_device_get_devpath (dev)); |
| 813 |
syslog (LOG_DEBUG, "device devpath is %s", usb_device_devpath); |
| 814 |
|
| 815 |
for (entry = map->entries; entry; entry = entry->next) |
| 816 |
if (!strcmp (entry->devpath, usb_device_devpath)) |
| 817 |
break; |
| 818 |
|
| 819 |
if (entry) |
| 820 |
{ |
| 821 |
/* The map already had an entry so has already been dealt |
| 822 |
* with. This can happen because there are two "add" |
| 823 |
* triggers: one for the usb_device device and the other for |
| 824 |
* the usblp device. We have most likely been triggered by |
| 825 |
* the usblp device, so the usb_device rule got there before |
| 826 |
* us and succeeded. |
| 827 |
* |
| 828 |
* Pretend we didn't find any device URIs that matched, and |
| 829 |
* exit. |
| 830 |
*/ |
| 831 |
syslog (LOG_DEBUG, "Device already handled"); |
| 832 |
free (usb_device_devpath); |
| 833 |
free (devicefilepath); |
| 834 |
return NULL; |
| 835 |
} |
| 836 |
|
| 837 |
serial = udev_device_get_sysattr_value (dev, "serial"); |
| 838 |
if (serial) |
| 839 |
{ |
| 840 |
strncpy (usbserial, serial, usbseriallen); |
| 841 |
usbserial[usbseriallen - 1] = '\0'; |
| 842 |
} |
| 843 |
else |
| 844 |
usbserial[0] = '\0'; |
| 845 |
|
| 846 |
device_id = get_ieee1284_id_from_child (udev, dev); |
| 847 |
if (!device_id) |
| 848 |
/* Use libusb to fetch the Device ID. */ |
| 849 |
device_id = get_ieee1284_id_using_libusb (dev, usbserial); |
| 850 |
|
| 851 |
if (device_id) |
| 852 |
parse_device_id (device_id, id); |
| 853 |
|
| 854 |
udev_device_unref (dev); |
| 855 |
free (devicefilepath); |
| 856 |
g_free(device_id); |
| 857 |
return usb_device_devpath; |
| 858 |
} |
| 859 |
|
| 860 |
static void |
| 861 |
device_id_from_bluetooth (const char *bdaddr, struct device_id *id) |
| 862 |
{ |
| 863 |
gint exit_status; |
| 864 |
char *device_id; |
| 865 |
gchar *argv[4]; |
| 866 |
|
| 867 |
argv[0] = g_strdup ("/usr/lib/cups/backend/bluetooth"); |
| 868 |
argv[1] = g_strdup ("--get-deviceid"); |
| 869 |
argv[2] = g_strdup (bdaddr); |
| 870 |
argv[3] = NULL; |
| 871 |
if (g_spawn_sync (NULL, argv, NULL, |
| 872 |
G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL, |
| 873 |
&device_id, NULL, &exit_status, NULL) == FALSE) { |
| 874 |
g_free (argv[0]); |
| 875 |
g_free (argv[1]); |
| 876 |
g_free (argv[2]); |
| 877 |
return; |
| 878 |
} |
| 879 |
g_free (argv[0]); |
| 880 |
g_free (argv[1]); |
| 881 |
g_free (argv[2]); |
| 882 |
|
| 883 |
if (WEXITSTATUS(exit_status) == 0) |
| 884 |
parse_device_id (device_id, id); |
| 885 |
|
| 886 |
g_free (device_id); |
| 887 |
} |
| 888 |
|
| 889 |
static char * |
| 890 |
devpath_from_usb_devaddr (struct udev *udev, const char *devaddr) |
| 891 |
{ |
| 892 |
char *devname_ending = g_strdup (devaddr); |
| 893 |
char *devname; |
| 894 |
const char *devpath; |
| 895 |
struct udev_enumerate *udev_enum; |
| 896 |
struct udev_list_entry *first = NULL; |
| 897 |
struct udev_device *device; |
| 898 |
|
| 899 |
g_strdelimit (devname_ending, "-", '/'); |
| 900 |
devname = g_strdup_printf("/dev/bus/%s", devname_ending); |
| 901 |
g_free (devname_ending); |
| 902 |
|
| 903 |
udev_enum = udev_enumerate_new (udev); |
| 904 |
if (udev_enum == NULL) |
| 905 |
{ |
| 906 |
syslog (LOG_ERR, "udev_enumerate_new failed"); |
| 907 |
exit (1); |
| 908 |
} |
| 909 |
|
| 910 |
if (udev_enumerate_add_match_property (udev_enum, "DEVNAME", devname) < 0) |
| 911 |
{ |
| 912 |
udev_enumerate_unref (udev_enum); |
| 913 |
syslog (LOG_ERR, "udev_enumerate_add_match_property failed"); |
| 914 |
exit (1); |
| 915 |
} |
| 916 |
|
| 917 |
if (udev_enumerate_scan_devices (udev_enum) < 0) |
| 918 |
{ |
| 919 |
udev_enumerate_unref (udev_enum); |
| 920 |
syslog (LOG_ERR, "udev_enumerate_scan_devices failed"); |
| 921 |
exit (1); |
| 922 |
} |
| 923 |
|
| 924 |
first = udev_enumerate_get_list_entry (udev_enum); |
| 925 |
if (first == NULL) |
| 926 |
{ |
| 927 |
udev_enumerate_unref (udev_enum); |
| 928 |
syslog (LOG_ERR, "no device named %s found", devname); |
| 929 |
exit (1); |
| 930 |
} |
| 931 |
|
| 932 |
device = udev_device_new_from_syspath (udev, |
| 933 |
udev_list_entry_get_name (first)); |
| 934 |
if (device == NULL) |
| 935 |
{ |
| 936 |
udev_enumerate_unref (udev_enum); |
| 937 |
syslog (LOG_ERR, "unable to examine device"); |
| 938 |
exit (1); |
| 939 |
} |
| 940 |
|
| 941 |
devpath = udev_device_get_devpath (device); |
| 942 |
udev_enumerate_unref (udev_enum); |
| 943 |
if (!devpath) |
| 944 |
{ |
| 945 |
syslog (LOG_ERR, "no devpath for device"); |
| 946 |
exit (1); |
| 947 |
} |
| 948 |
|
| 949 |
g_free (devname); |
| 950 |
return g_strdup (devpath); |
| 951 |
} |
| 952 |
|
| 953 |
static char * |
| 954 |
uri_from_bdaddr (const char *devpath) |
| 955 |
{ |
| 956 |
return g_strdup_printf("bluetooth://%c%c%c%c%c%c%c%c%c%c%c%c", |
| 957 |
devpath[0], devpath[1], |
| 958 |
devpath[3], devpath[4], |
| 959 |
devpath[6], devpath[7], |
| 960 |
devpath[9], devpath[10], |
| 961 |
devpath[12], devpath[13], |
| 962 |
devpath[15], devpath[16]); |
| 963 |
} |
| 964 |
|
| 965 |
static const char * |
| 966 |
no_password (const char *prompt) |
| 967 |
{ |
| 968 |
return ""; |
| 969 |
} |
| 970 |
|
| 971 |
static ipp_t * |
| 972 |
cupsDoRequestOrDie (http_t *http, |
| 973 |
ipp_t *request, |
| 974 |
const char *resource) |
| 975 |
{ |
| 976 |
ipp_t *answer = cupsDoRequest (http, request, resource); |
| 977 |
if (answer == NULL) |
| 978 |
{ |
| 979 |
syslog (LOG_ERR, "failed to send IPP request %d", |
| 980 |
ippGetOperation (request)); |
| 981 |
exit (1); |
| 982 |
} |
| 983 |
|
| 984 |
if (ippGetStatusCode (answer) > IPP_OK_CONFLICT) |
| 985 |
{ |
| 986 |
syslog (LOG_ERR, "IPP request %d failed (%d)", |
| 987 |
ippGetOperation (request), |
| 988 |
ippGetStatusCode (answer)); |
| 989 |
exit (1); |
| 990 |
} |
| 991 |
|
| 992 |
return answer; |
| 993 |
} |
| 994 |
|
| 995 |
static int |
| 996 |
find_matching_device_uris (struct device_id *id, |
| 997 |
const char *usbserial, |
| 998 |
struct device_uris *uris, |
| 999 |
const char *devpath, |
| 1000 |
struct usb_uri_map *map) |
| 1001 |
{ |
| 1002 |
http_t *cups; |
| 1003 |
ipp_t *request, *answer; |
| 1004 |
ipp_attribute_t *attr; |
| 1005 |
struct device_uris uris_noserial; |
| 1006 |
struct device_uris all_uris; |
| 1007 |
size_t i, n; |
| 1008 |
const char *exclude_schemes[] = { |
| 1009 |
"beh", |
| 1010 |
"cups-pdf", |
| 1011 |
"bluetooth", |
| 1012 |
"dnssd", |
| 1013 |
"http", |
| 1014 |
"https", |
| 1015 |
"ipp", |
| 1016 |
"lpd", |
| 1017 |
"ncp", |
| 1018 |
"parallel", |
| 1019 |
"scsi", |
| 1020 |
"smb", |
| 1021 |
"snmp", |
| 1022 |
"socket", |
| 1023 |
}; |
| 1024 |
|
| 1025 |
uris->n_uris = uris_noserial.n_uris = all_uris.n_uris = 0; |
| 1026 |
uris->uri = uris_noserial.uri = all_uris.uri = NULL; |
| 1027 |
|
| 1028 |
/* Leave the bus to settle (see e.g. bug #1206808). */ |
| 1029 |
sleep (5); |
| 1030 |
|
| 1031 |
cups = httpConnectEncrypt (cupsServer (), ippPort(), cupsEncryption ()); |
| 1032 |
if (cups == NULL) |
| 1033 |
{ |
| 1034 |
/* Don't bother retrying here. We've probably been run from |
| 1035 |
udev before the cups.socket systemd unit is running. We'll |
| 1036 |
get run again, as the systemd service |
| 1037 |
udev-configure-printer.service, after cups.socket. For more |
| 1038 |
information: |
| 1039 |
http://0pointer.de/blog/projects/socket-activation2.html |
| 1040 |
*/ |
| 1041 |
|
| 1042 |
syslog (LOG_DEBUG, "failed to connect to CUPS server; giving up"); |
| 1043 |
exit (1); |
| 1044 |
} |
| 1045 |
|
| 1046 |
request = ippNewRequest (CUPS_GET_DEVICES); |
| 1047 |
ippAddStrings (request, IPP_TAG_OPERATION, IPP_TAG_NAME, "exclude-schemes", |
| 1048 |
sizeof (exclude_schemes) / sizeof(exclude_schemes[0]), |
| 1049 |
NULL, exclude_schemes); |
| 1050 |
ippAddInteger (request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "timeout", |
| 1051 |
2); |
| 1052 |
|
| 1053 |
answer = cupsDoRequestOrDie (cups, request, "/"); |
| 1054 |
httpClose (cups); |
| 1055 |
|
| 1056 |
for (attr = ippFirstAttribute (answer); attr; attr = ippNextAttribute (answer)) |
| 1057 |
{ |
| 1058 |
const char *device_uri = NULL; |
| 1059 |
struct device_id this_id; |
| 1060 |
this_id.full_device_id = this_id.mfg = this_id.mdl = this_id.sern = NULL; |
| 1061 |
|
| 1062 |
while (attr && ippGetGroupTag (attr) != IPP_TAG_PRINTER) |
| 1063 |
attr = ippNextAttribute (answer); |
| 1064 |
|
| 1065 |
if (!attr) |
| 1066 |
break; |
| 1067 |
|
| 1068 |
for (; attr && ippGetGroupTag (attr) == IPP_TAG_PRINTER; attr = ippNextAttribute (answer)) |
| 1069 |
{ |
| 1070 |
if (ippGetValueTag (attr) == IPP_TAG_URI && |
| 1071 |
!strcmp (ippGetName (attr), "device-uri")) |
| 1072 |
device_uri = ippGetString (attr, 0, NULL); |
| 1073 |
else if (ippGetValueTag (attr) == IPP_TAG_TEXT && |
| 1074 |
!strcmp (ippGetName (attr), "device-id")) |
| 1075 |
parse_device_id (ippGetString (attr, 0, NULL), &this_id); |
| 1076 |
} |
| 1077 |
|
| 1078 |
/* Only use device schemes in our preference order for matching |
| 1079 |
* against the IEEE 1284 Device ID. */ |
| 1080 |
|
| 1081 |
for (i = 0; |
| 1082 |
device_uri && |
| 1083 |
i < sizeof (device_uri_types) / sizeof (device_uri_types[0]); |
| 1084 |
i++) |
| 1085 |
{ |
| 1086 |
size_t len = strlen (device_uri_types[i]); |
| 1087 |
if (!strncmp (device_uri_types[i], device_uri, len) && |
| 1088 |
device_uri[len] == ':') |
| 1089 |
break; |
| 1090 |
} |
| 1091 |
|
| 1092 |
if (device_uri) |
| 1093 |
add_device_uri (&all_uris, device_uri); |
| 1094 |
|
| 1095 |
if (i == sizeof (device_uri_types) / sizeof (device_uri_types[0])) |
| 1096 |
/* Not what we want to match against. Ignore this one. */ |
| 1097 |
device_uri = NULL; |
| 1098 |
|
| 1099 |
/* Now check the manufacturer and model names. */ |
| 1100 |
if (device_uri && this_id.mfg && this_id.mdl && |
| 1101 |
!strcasecmp (this_id.mfg, id->mfg) && |
| 1102 |
!strcasecmp (this_id.mdl, id->mdl)) |
| 1103 |
{ |
| 1104 |
/* We've checked everything except the serial numbers. This |
| 1105 |
* is more complicated. Some devices include a serial |
| 1106 |
* number (SERN) field in their IEEE 1284 Device ID. Others |
| 1107 |
* don't -- this was not a mandatory field in the |
| 1108 |
* specification. |
| 1109 |
* |
| 1110 |
* If the device includes SERN field in its, it must match |
| 1111 |
* what the device-id attribute has. |
| 1112 |
* |
| 1113 |
* Otherwise, the only means we have of knowing which device |
| 1114 |
* is meant is the USB serial number. |
| 1115 |
* |
| 1116 |
* CUPS backends may choose to insert the USB serial number |
| 1117 |
* into the SERN field when reporting a device-id attribute. |
| 1118 |
* HPLIP does this, and it seems not to stray too far from |
| 1119 |
* the intent of that field. We accommodate this. |
| 1120 |
* |
| 1121 |
* Alternatively, CUPS backends may include the USB serial |
| 1122 |
* number somewhere in their reported device-uri attributes. |
| 1123 |
* For instance, the CUPS 1.4 usb backend, when compiled |
| 1124 |
* with libusb support, gives device URIs containing the USB |
| 1125 |
* serial number for devices without a SERN field, like |
| 1126 |
* this: usb://HP/DESKJET%20990C?serial=US05M1D20CIJ |
| 1127 |
* |
| 1128 |
* To accommodate this we examine tokens between '?', '=' |
| 1129 |
* and '&' delimiters to check for USB serial number |
| 1130 |
* matches. |
| 1131 |
* |
| 1132 |
* CUPS 1.3, and CUPS 1.4 without libusb support, doesn't do this. |
| 1133 |
* As a result we also need to deal with devices that don't report a |
| 1134 |
* SERN field where the backends that don't add a SERN field from |
| 1135 |
* the USB serial number and also don't include the USB serial |
| 1136 |
* number in the URI. |
| 1137 |
*/ |
| 1138 |
|
| 1139 |
int match = 0; |
| 1140 |
if ((id->sern && this_id.sern && !strcmp (id->sern, this_id.sern))) |
| 1141 |
{ |
| 1142 |
syslog (LOG_DEBUG, "SERN fields match"); |
| 1143 |
match = 1; |
| 1144 |
} |
| 1145 |
|
| 1146 |
if (!match && usbserial[0] != '\0') |
| 1147 |
{ |
| 1148 |
if (!id->sern) |
| 1149 |
{ |
| 1150 |
if (this_id.sern && !strcmp (usbserial, this_id.sern)) |
| 1151 |
{ |
| 1152 |
syslog (LOG_DEBUG, |
| 1153 |
"SERN field matches USB serial number"); |
| 1154 |
match = 1; |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
if (!match) |
| 1159 |
{ |
| 1160 |
char *saveptr, *uri = strdup (device_uri); |
| 1161 |
const char *token; |
| 1162 |
const char *sep = "?=&/"; |
| 1163 |
for (token = strtok_r (uri, sep, &saveptr); |
| 1164 |
token; |
| 1165 |
token = strtok_r (NULL, sep, &saveptr)) |
| 1166 |
if (!strcmp (token, usbserial)) |
| 1167 |
{ |
| 1168 |
syslog (LOG_DEBUG, "URI contains USB serial number"); |
| 1169 |
match = 1; |
| 1170 |
break; |
| 1171 |
} |
| 1172 |
|
| 1173 |
free (uri); |
| 1174 |
} |
| 1175 |
} |
| 1176 |
|
| 1177 |
if (match) |
| 1178 |
{ |
| 1179 |
syslog (LOG_DEBUG, "URI match: %s", device_uri); |
| 1180 |
add_device_uri (uris, device_uri); |
| 1181 |
} |
| 1182 |
else if (!id->sern) |
| 1183 |
{ |
| 1184 |
syslog (LOG_DEBUG, "URI matches without serial number: %s", |
| 1185 |
device_uri); |
| 1186 |
add_device_uri (&uris_noserial, device_uri); |
| 1187 |
} |
| 1188 |
} |
| 1189 |
|
| 1190 |
if (!attr) |
| 1191 |
break; |
| 1192 |
} |
| 1193 |
|
| 1194 |
ippDelete (answer); |
| 1195 |
|
| 1196 |
/* Decide what to do about device URIs that did not match a serial |
| 1197 |
* number. The device had no SERN field, and the USB serial number |
| 1198 |
* was nowhere to be found from the device URI or device-id field. |
| 1199 |
* |
| 1200 |
* Device URIs with no reference to serial number can only each ever |
| 1201 |
* work when only one printer of that model is connected. |
| 1202 |
* Accordingly, it is safe to disable queues using such URIs, as we |
| 1203 |
* know the removed/added device is that lone printer. |
| 1204 |
* |
| 1205 |
* When adding queues it is best to avoid URIs that don't |
| 1206 |
* distinguish serial numbers. |
| 1207 |
* |
| 1208 |
* What we'll do, then, is concatenate the list of "non-serial" URIs |
| 1209 |
* onto the end of the list of "serial" URIs. |
| 1210 |
*/ |
| 1211 |
|
| 1212 |
if (uris->n_uris == 0 && uris_noserial.n_uris > 0) |
| 1213 |
{ |
| 1214 |
syslog (LOG_DEBUG, "No serial number URI matches so using those without"); |
| 1215 |
uris->n_uris = uris_noserial.n_uris; |
| 1216 |
uris->uri = uris_noserial.uri; |
| 1217 |
uris_noserial.n_uris = 0; |
| 1218 |
uris_noserial.uri = NULL; |
| 1219 |
} |
| 1220 |
else if (uris_noserial.n_uris > 0) |
| 1221 |
{ |
| 1222 |
char **old = uris->uri; |
| 1223 |
uris->uri = realloc (uris->uri, |
| 1224 |
sizeof (char *) * (uris->n_uris + |
| 1225 |
uris_noserial.n_uris)); |
| 1226 |
if (!uris->uri) |
| 1227 |
uris->uri = old; |
| 1228 |
else |
| 1229 |
{ |
| 1230 |
for (i = 0; i < uris_noserial.n_uris; i++) |
| 1231 |
uris->uri[uris->n_uris + i] = uris_noserial.uri[i]; |
| 1232 |
uris->n_uris += uris_noserial.n_uris; |
| 1233 |
} |
| 1234 |
|
| 1235 |
uris_noserial.n_uris = 0; |
| 1236 |
uris_noserial.uri = NULL; |
| 1237 |
} |
| 1238 |
|
| 1239 |
free_device_uris (&uris_noserial); |
| 1240 |
|
| 1241 |
/* Having decided which device URIs match based on IEEE 1284 Device |
| 1242 |
* ID, we now need to look for "paired" URIs for other functions of |
| 1243 |
* a multi-function device. This are the same except for the |
| 1244 |
* scheme. */ |
| 1245 |
|
| 1246 |
n = uris->n_uris; |
| 1247 |
for (i = 0; i < n; i++) |
| 1248 |
{ |
| 1249 |
size_t j; |
| 1250 |
char *me = uris->uri[i]; |
| 1251 |
char *my_rest = strchr (me, ':'); |
| 1252 |
size_t my_schemelen; |
| 1253 |
if (!my_rest) |
| 1254 |
continue; |
| 1255 |
|
| 1256 |
my_schemelen = my_rest - me; |
| 1257 |
for (j = 0; j < all_uris.n_uris; j++) |
| 1258 |
{ |
| 1259 |
char *twin = all_uris.uri[j]; |
| 1260 |
char *twin_rest = strchr (twin, ':'); |
| 1261 |
size_t twin_schemelen; |
| 1262 |
if (!twin_rest) |
| 1263 |
continue; |
| 1264 |
|
| 1265 |
twin_schemelen = twin_rest - twin; |
| 1266 |
if (my_schemelen == twin_schemelen && |
| 1267 |
!strncmp (me, twin, my_schemelen)) |
| 1268 |
/* This is the one we are looking for the twin of. */ |
| 1269 |
continue; |
| 1270 |
|
| 1271 |
if (!strcmp (my_rest, twin_rest)) |
| 1272 |
{ |
| 1273 |
syslog (LOG_DEBUG, "%s twinned with %s", me, twin); |
| 1274 |
add_device_uri (uris, twin); |
| 1275 |
} |
| 1276 |
} |
| 1277 |
} |
| 1278 |
|
| 1279 |
free_device_uris (&all_uris); |
| 1280 |
if (uris->n_uris > 0) |
| 1281 |
{ |
| 1282 |
add_usb_uri_mapping (&map, devpath, uris); |
| 1283 |
write_usb_uri_map (map); |
| 1284 |
} |
| 1285 |
|
| 1286 |
return uris->n_uris; |
| 1287 |
} |
| 1288 |
|
| 1289 |
char * |
| 1290 |
normalize_device_uri(const char *str_orig) |
| 1291 |
{ |
| 1292 |
int i, j; |
| 1293 |
int havespace = 0; |
| 1294 |
char *str = NULL; |
| 1295 |
char *cropped_str = NULL; |
| 1296 |
|
| 1297 |
if (str_orig == NULL) |
| 1298 |
return NULL; |
| 1299 |
|
| 1300 |
str = strdup(str_orig); |
| 1301 |
for (i = 0, j = 0; i < strlen(str); i++, j++) |
| 1302 |
{ |
| 1303 |
if (((str[i] >= 'A') && (str[i] <= 'Z')) || |
| 1304 |
((str[i] >= 'a') && (str[i] <= 'z')) || |
| 1305 |
((str[i] >= '0') && (str[i] <= '9'))) |
| 1306 |
{ |
| 1307 |
/* Letter or number, keep it */ |
| 1308 |
havespace = 0; |
| 1309 |
str[j] = tolower(str[i]); |
| 1310 |
} |
| 1311 |
else |
| 1312 |
{ |
| 1313 |
if ((str[i] == '%') && (i <= strlen(str)-3) && |
| 1314 |
(((str[i+1] >= 'A') && (str[i+1] <= 'F')) || |
| 1315 |
((str[i+1] >= 'a') && (str[i+1] <= 'f')) || |
| 1316 |
((str[i+1] >= '0') && (str[i+1] <= '9'))) && |
| 1317 |
(((str[i+2] >= 'A') && (str[i+2] <= 'F')) || |
| 1318 |
((str[i+2] >= 'a') && (str[i+2] <= 'f')) || |
| 1319 |
((str[i+2] >= '0') && (str[i+2] <= '9')))) |
| 1320 |
/* Hex-encoded special characters replace by a single space if the |
| 1321 |
last character is not already a space */ |
| 1322 |
i += 2; |
| 1323 |
if (havespace == 1) |
| 1324 |
j --; |
| 1325 |
else |
| 1326 |
{ |
| 1327 |
havespace = 1; |
| 1328 |
str[j] = ' '; |
| 1329 |
} |
| 1330 |
} |
| 1331 |
} |
| 1332 |
/* Add terminating zero */ |
| 1333 |
str[j] = '\0'; |
| 1334 |
/* Cut off trailing white space */ |
| 1335 |
while (str[strlen(str)-1] == ' ') |
| 1336 |
str[strlen(str)-1] = '\0'; |
| 1337 |
/* Cut off all before model name */ |
| 1338 |
while ((strstr(str, "hp ") == str) || |
| 1339 |
(strstr(str, "hewlett ") == str) || |
| 1340 |
(strstr(str, "packard ") == str) || |
| 1341 |
(strstr(str, "apollo ") == str) || |
| 1342 |
(strstr(str, "usb ") == str)) |
| 1343 |
{ |
| 1344 |
cropped_str = strdup(strchr(str, ' ') + 1); |
| 1345 |
free(str); |
| 1346 |
str = cropped_str; |
| 1347 |
} |
| 1348 |
|
| 1349 |
return str; |
| 1350 |
} |
| 1351 |
|
| 1352 |
/* Call a function for each queue with the given device-uri and printer-state. |
| 1353 |
* Returns the number of queues with a matching device-uri. */ |
| 1354 |
static size_t |
| 1355 |
for_each_matching_queue (struct device_uris *device_uris, |
| 1356 |
int flags, |
| 1357 |
void (*fn) (const char *, void *), |
| 1358 |
void *context, |
| 1359 |
char *usblpdev, size_t usblpdevlen) |
| 1360 |
{ |
| 1361 |
size_t matched = 0; |
| 1362 |
http_t *cups = httpConnectEncrypt (cupsServer (), ippPort (), |
| 1363 |
cupsEncryption ()); |
| 1364 |
ipp_t *request, *answer; |
| 1365 |
ipp_attribute_t *attr; |
| 1366 |
const char *attributes[] = { |
| 1367 |
"printer-uri-supported", |
| 1368 |
"device-uri", |
| 1369 |
"printer-state", |
| 1370 |
"printer-state-message", |
| 1371 |
}; |
| 1372 |
char usblpdevstr1[32] = "", usblpdevstr2[32] = ""; |
| 1373 |
int firstqueue = 1; |
| 1374 |
|
| 1375 |
if (cups == NULL) |
| 1376 |
return 0; |
| 1377 |
|
| 1378 |
request = ippNewRequest (CUPS_GET_PRINTERS); |
| 1379 |
ippAddStrings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, |
| 1380 |
"requested-attributes", |
| 1381 |
sizeof (attributes) / sizeof (attributes[0]), |
| 1382 |
NULL, attributes); |
| 1383 |
answer = cupsDoRequest (cups, request, "/"); |
| 1384 |
httpClose (cups); |
| 1385 |
if (answer == NULL) |
| 1386 |
{ |
| 1387 |
syslog (LOG_ERR, "failed to send CUPS-Get-Printers request"); |
| 1388 |
exit (1); |
| 1389 |
} |
| 1390 |
|
| 1391 |
if (ippGetStatusCode (answer) > IPP_OK_CONFLICT) |
| 1392 |
{ |
| 1393 |
if (ippGetStatusCode (answer) == IPP_NOT_FOUND) |
| 1394 |
{ |
| 1395 |
/* No printer queues configured. */ |
| 1396 |
ippDelete (answer); |
| 1397 |
return 0; |
| 1398 |
} |
| 1399 |
|
| 1400 |
syslog (LOG_ERR, "CUPS-Get-Printers request failed (%d)", |
| 1401 |
ippGetStatusCode (answer)); |
| 1402 |
exit (1); |
| 1403 |
} |
| 1404 |
|
| 1405 |
if (strlen(usblpdev) > 0) |
| 1406 |
{ |
| 1407 |
/* If one of these strings is contained in one of the existing queue's |
| 1408 |
device URIs, consider the printer as already configured. Some |
| 1409 |
non-standard CUPS backend use the (obsolete) reference to the |
| 1410 |
usblp device file. This avoids that in such a case a second queue |
| 1411 |
with a standard CUPS backend is auto-created. */ |
| 1412 |
snprintf(usblpdevstr1, sizeof(usblpdevstr1), "/usb/lp%s", |
| 1413 |
usblpdev); |
| 1414 |
snprintf(usblpdevstr2, sizeof(usblpdevstr2), "/usblp%s", |
| 1415 |
usblpdev); |
| 1416 |
} |
| 1417 |
|
| 1418 |
for (attr = ippFirstAttribute (answer); attr; attr = ippNextAttribute (answer)) |
| 1419 |
{ |
| 1420 |
const char *this_printer_uri = NULL; |
| 1421 |
const char *this_device_uri = NULL; |
| 1422 |
const char *printer_state_message = NULL; |
| 1423 |
int state = 0; |
| 1424 |
size_t i, l; |
| 1425 |
char *this_device_uri_n = NULL, *device_uri_n = NULL; |
| 1426 |
const char *ps1, *ps2, *pi1, *pi2; |
| 1427 |
|
| 1428 |
while (attr && ippGetGroupTag (attr) != IPP_TAG_PRINTER) |
| 1429 |
attr = ippNextAttribute (answer); |
| 1430 |
|
| 1431 |
if (!attr) |
| 1432 |
break; |
| 1433 |
|
| 1434 |
for (; attr && ippGetGroupTag (attr) == IPP_TAG_PRINTER; attr = ippNextAttribute (answer)) |
| 1435 |
{ |
| 1436 |
if (ippGetValueTag (attr) == IPP_TAG_URI) |
| 1437 |
{ |
| 1438 |
if (!strcmp (ippGetName (attr), "device-uri")) |
| 1439 |
this_device_uri = ippGetString (attr, 0, NULL); |
| 1440 |
else if (!strcmp (ippGetName (attr), "printer-uri-supported")) |
| 1441 |
this_printer_uri = ippGetString (attr, 0, NULL); |
| 1442 |
} |
| 1443 |
else if (ippGetValueTag (attr) == IPP_TAG_TEXT && |
| 1444 |
!strcmp (ippGetName (attr), "printer-state-message")) |
| 1445 |
printer_state_message = ippGetString (attr, 0, NULL); |
| 1446 |
else if (ippGetValueTag (attr) == IPP_TAG_ENUM && |
| 1447 |
!strcmp (ippGetName (attr), "printer-state")) |
| 1448 |
state = ippGetInteger (attr, 0); |
| 1449 |
} |
| 1450 |
|
| 1451 |
if (!this_device_uri) |
| 1452 |
/* CUPS didn't include a device-uri attribute in the response |
| 1453 |
for this printer (shouldn't happen). */ |
| 1454 |
goto skip; |
| 1455 |
|
| 1456 |
this_device_uri_n = normalize_device_uri(this_device_uri); |
| 1457 |
pi1 = strstr (this_device_uri, "interface="); |
| 1458 |
ps1 = strstr (this_device_uri, "serial="); |
| 1459 |
for (i = 0; i < device_uris->n_uris; i++) |
| 1460 |
{ |
| 1461 |
device_uri_n = normalize_device_uri(device_uris->uri[i]); |
| 1462 |
/* As for the same device different URIs can come out when the |
| 1463 |
device is accessed via the usblp kernel module or via low- |
| 1464 |
level USB (libusb) we cannot simply compare URIs, must |
| 1465 |
consider also URIs as equal if one has an "interface" |
| 1466 |
or "serial" attribute and the other not. If both have |
| 1467 |
the attribute it must naturally match. We check which attributes |
| 1468 |
are there and this way determine up to which length the two URIs |
| 1469 |
must match. Here we can assume that if a URI has an "interface" |
| 1470 |
attribute it has also a "serial" attribute, as this URI is |
| 1471 |
an URI obtained via libusb and these always have a "serial" |
| 1472 |
attribute. usblp-based URIs never have an "interface" |
| 1473 |
attribute.*/ |
| 1474 |
pi2 = strstr (device_uris->uri[i], "interface="); |
| 1475 |
ps2 = strstr (device_uris->uri[i], "serial="); |
| 1476 |
if (pi1 && !pi2) |
| 1477 |
l = strlen(device_uris->uri[i]); |
| 1478 |
else if (!pi1 && pi2) |
| 1479 |
l = strlen(this_device_uri); |
| 1480 |
else if (ps1 && !ps2) |
| 1481 |
l = strlen(device_uris->uri[i]); |
| 1482 |
else if (!ps1 && ps2) |
| 1483 |
l = strlen(this_device_uri); |
| 1484 |
else if (strlen(this_device_uri) > strlen(device_uris->uri[i])) |
| 1485 |
l = strlen(this_device_uri); |
| 1486 |
else |
| 1487 |
l = strlen(device_uris->uri[i]); |
| 1488 |
if (firstqueue == 1) |
| 1489 |
{ |
| 1490 |
syslog (LOG_DEBUG, "URI of detected printer: %s, normalized: %s", |
| 1491 |
device_uris->uri[i], device_uri_n); |
| 1492 |
if (i == 0 && strlen(usblpdev) > 0) |
| 1493 |
syslog (LOG_DEBUG, |
| 1494 |
"Consider also queues with \"%s\" or \"%s\" in their URIs as matching", |
| 1495 |
usblpdevstr1, usblpdevstr2); |
| 1496 |
} |
| 1497 |
if (i == 0) |
| 1498 |
syslog (LOG_DEBUG, "URI of print queue: %s, normalized: %s", |
| 1499 |
this_device_uri, this_device_uri_n); |
| 1500 |
if ((!strncmp (device_uris->uri[i], this_device_uri, l)) || |
| 1501 |
(strstr (device_uri_n, this_device_uri_n) == |
| 1502 |
device_uri_n) || |
| 1503 |
(strstr (this_device_uri_n, device_uri_n) == |
| 1504 |
this_device_uri_n) || |
| 1505 |
((strlen(usblpdev) > 0) && |
| 1506 |
((strstr (this_device_uri, usblpdevstr1) != NULL) || |
| 1507 |
(strstr (this_device_uri, usblpdevstr2) != NULL)))) |
| 1508 |
{ |
| 1509 |
matched++; |
| 1510 |
syslog (LOG_DEBUG, "Queue %s has matching device URI", |
| 1511 |
this_printer_uri); |
| 1512 |
if (((flags & MATCH_ONLY_DISABLED) && |
| 1513 |
state == IPP_PRINTER_STOPPED && |
| 1514 |
!strcmp (printer_state_message, DISABLED_REASON)) || |
| 1515 |
(flags & MATCH_ONLY_DISABLED) == 0) |
| 1516 |
{ |
| 1517 |
(*fn) (this_printer_uri, context); |
| 1518 |
break; |
| 1519 |
} |
| 1520 |
} |
| 1521 |
if (device_uri_n != NULL) |
| 1522 |
{ |
| 1523 |
free(device_uri_n); |
| 1524 |
device_uri_n = NULL; |
| 1525 |
} |
| 1526 |
} |
| 1527 |
|
| 1528 |
firstqueue = 0; |
| 1529 |
|
| 1530 |
skip: |
| 1531 |
if (this_device_uri_n != NULL) |
| 1532 |
{ |
| 1533 |
free(this_device_uri_n); |
| 1534 |
this_device_uri_n = NULL; |
| 1535 |
} |
| 1536 |
if (device_uri_n != NULL) |
| 1537 |
{ |
| 1538 |
free(device_uri_n); |
| 1539 |
device_uri_n = NULL; |
| 1540 |
} |
| 1541 |
if (!attr) |
| 1542 |
break; |
| 1543 |
} |
| 1544 |
|
| 1545 |
ippDelete (answer); |
| 1546 |
return matched; |
| 1547 |
} |
| 1548 |
|
| 1549 |
static void |
| 1550 |
enable_queue (const char *printer_uri, void *context) |
| 1551 |
{ |
| 1552 |
/* Enable it. */ |
| 1553 |
http_t *cups = httpConnectEncrypt (cupsServer (), ippPort (), |
| 1554 |
cupsEncryption ()); |
| 1555 |
ipp_t *request, *answer; |
| 1556 |
|
| 1557 |
if (cups == NULL) |
| 1558 |
return; |
| 1559 |
|
| 1560 |
request = ippNewRequest (IPP_RESUME_PRINTER); |
| 1561 |
ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI, |
| 1562 |
"printer-uri", NULL, printer_uri); |
| 1563 |
answer = cupsDoRequest (cups, request, "/admin/"); |
| 1564 |
if (!answer) |
| 1565 |
{ |
| 1566 |
syslog (LOG_ERR, "Failed to send IPP-Resume-Printer request"); |
| 1567 |
httpClose (cups); |
| 1568 |
return; |
| 1569 |
} |
| 1570 |
|
| 1571 |
if (ippGetStatusCode (answer) > IPP_OK_CONFLICT) |
| 1572 |
syslog (LOG_ERR, "IPP-Resume-Printer request failed"); |
| 1573 |
else |
| 1574 |
syslog (LOG_INFO, "Re-enabled printer %s", printer_uri); |
| 1575 |
|
| 1576 |
ippDelete (answer); |
| 1577 |
httpClose (cups); |
| 1578 |
} |
| 1579 |
|
| 1580 |
static gboolean |
| 1581 |
bluetooth_verify_address (const char *bdaddr) |
| 1582 |
{ |
| 1583 |
gboolean retval = TRUE; |
| 1584 |
char **elems; |
| 1585 |
guint i; |
| 1586 |
|
| 1587 |
g_return_val_if_fail (bdaddr != NULL, FALSE); |
| 1588 |
|
| 1589 |
if (strlen (bdaddr) != 17) |
| 1590 |
return FALSE; |
| 1591 |
|
| 1592 |
elems = g_strsplit (bdaddr, ":", -1); |
| 1593 |
if (elems == NULL) |
| 1594 |
return FALSE; |
| 1595 |
if (g_strv_length (elems) != 6) { |
| 1596 |
g_strfreev (elems); |
| 1597 |
return FALSE; |
| 1598 |
} |
| 1599 |
for (i = 0; i < 6; i++) { |
| 1600 |
if (strlen (elems[i]) != 2 || |
| 1601 |
g_ascii_isxdigit (elems[i][0]) == FALSE || |
| 1602 |
g_ascii_isxdigit (elems[i][1]) == FALSE) { |
| 1603 |
retval = FALSE; |
| 1604 |
break; |
| 1605 |
} |
| 1606 |
} |
| 1607 |
|
| 1608 |
g_strfreev (elems); |
| 1609 |
return retval; |
| 1610 |
} |
| 1611 |
|
| 1612 |
static int |
| 1613 |
do_add (const char *cmd, const char *devaddr) |
| 1614 |
{ |
| 1615 |
struct device_id id; |
| 1616 |
struct device_uris device_uris; |
| 1617 |
struct usb_uri_map *map; |
| 1618 |
struct udev *udev; |
| 1619 |
char *devpath = NULL; |
| 1620 |
char *usb_device_devpath = NULL; |
| 1621 |
char usbserial[256]; |
| 1622 |
char usblpdev[8] = ""; |
| 1623 |
gboolean is_bluetooth; |
| 1624 |
|
| 1625 |
syslog (LOG_DEBUG, "add %s", devaddr); |
| 1626 |
|
| 1627 |
is_bluetooth = bluetooth_verify_address (devaddr); |
| 1628 |
|
| 1629 |
id.full_device_id = id.mfg = id.mdl = id.sern = NULL; |
| 1630 |
map = read_usb_uri_map (); |
| 1631 |
if (is_bluetooth) { |
| 1632 |
usbserial[0] = '\0'; |
| 1633 |
device_id_from_bluetooth (devaddr, &id); |
| 1634 |
} else { |
| 1635 |
udev = udev_new (); |
| 1636 |
if (udev == NULL) |
| 1637 |
{ |
| 1638 |
syslog (LOG_ERR, "udev_new failed"); |
| 1639 |
free_usb_uri_map (map); |
| 1640 |
exit (1); |
| 1641 |
} |
| 1642 |
|
| 1643 |
if (!strncmp (devaddr, "usb-", 4)) |
| 1644 |
devpath = devpath_from_usb_devaddr (udev, devaddr); |
| 1645 |
else |
| 1646 |
devpath = g_strdup (devaddr); |
| 1647 |
|
| 1648 |
usb_device_devpath = device_id_from_devpath (udev, devpath, map, &id, |
| 1649 |
usbserial, sizeof (usbserial), |
| 1650 |
usblpdev, sizeof (usblpdev)); |
| 1651 |
g_free (devpath); |
| 1652 |
udev_unref (udev); |
| 1653 |
} |
| 1654 |
|
| 1655 |
if (!id.mfg || !id.mdl) |
| 1656 |
{ |
| 1657 |
clear_device_id (&id); |
| 1658 |
free_usb_uri_map (map); |
| 1659 |
free (usb_device_devpath); |
| 1660 |
return 1; |
| 1661 |
} |
| 1662 |
|
| 1663 |
syslog (LOG_DEBUG, "MFG:%s MDL:%s SERN:%s serial:%s", id.mfg, id.mdl, |
| 1664 |
id.sern ? id.sern : "-", usbserial[0] ? usbserial : "-"); |
| 1665 |
|
| 1666 |
if (!is_bluetooth) |
| 1667 |
{ |
| 1668 |
find_matching_device_uris (&id, usbserial, &device_uris, usb_device_devpath, |
| 1669 |
map); |
| 1670 |
free (usb_device_devpath); |
| 1671 |
} else { |
| 1672 |
char *device_uri; |
| 1673 |
|
| 1674 |
device_uri = uri_from_bdaddr (devaddr); |
| 1675 |
add_device_uri (&device_uris, device_uri); |
| 1676 |
g_free (device_uri); |
| 1677 |
} |
| 1678 |
|
| 1679 |
if (device_uris.n_uris == 0) |
| 1680 |
{ |
| 1681 |
syslog (LOG_ERR, "no corresponding CUPS device found"); |
| 1682 |
clear_device_id (&id); |
| 1683 |
free_usb_uri_map (map); |
| 1684 |
return 0; |
| 1685 |
} |
| 1686 |
|
| 1687 |
/* Re-enable any queues we'd previously disabled. */ |
| 1688 |
if (for_each_matching_queue (&device_uris, MATCH_ONLY_DISABLED, |
| 1689 |
enable_queue, NULL, |
| 1690 |
usblpdev, sizeof (usblpdev)) == 0) |
| 1691 |
{ |
| 1692 |
size_t i; |
| 1693 |
int type; |
| 1694 |
char argv0[PATH_MAX]; |
| 1695 |
char *p; |
| 1696 |
char **argv = malloc (sizeof (char *) * (3 + device_uris.n_uris)); |
| 1697 |
|
| 1698 |
/* No queue is configured for this device yet. |
| 1699 |
Decide on a URI to use. */ |
| 1700 |
type = device_uri_type (device_uris.uri[0]); |
| 1701 |
for (i = 1; i < device_uris.n_uris; i++) |
| 1702 |
{ |
| 1703 |
int new_type = device_uri_type (device_uris.uri[i]); |
| 1704 |
if (new_type < type) |
| 1705 |
{ |
| 1706 |
char *swap = device_uris.uri[0]; |
| 1707 |
device_uris.uri[0] = device_uris.uri[i]; |
| 1708 |
device_uris.uri[i] = swap; |
| 1709 |
type = new_type; |
| 1710 |
} |
| 1711 |
} |
| 1712 |
|
| 1713 |
argv[0] = argv0; |
| 1714 |
argv[1] = id.full_device_id; |
| 1715 |
for (i = 0; i < device_uris.n_uris; i++) |
| 1716 |
argv[i + 2] = device_uris.uri[i]; |
| 1717 |
argv[i + 2] = NULL; |
| 1718 |
|
| 1719 |
syslog (LOG_DEBUG, "About to add queue for %s", argv[2]); |
| 1720 |
strncpy (argv0, cmd, sizeof (argv0)); |
| 1721 |
argv0[sizeof (argv0) - 1] = '\0'; |
| 1722 |
p = strrchr (argv0, '/'); |
| 1723 |
if (p++ == NULL) |
| 1724 |
p = argv0; |
| 1725 |
|
| 1726 |
strncpy (p, "udev-add-printer", sizeof (argv0) - (p - argv0)); |
| 1727 |
argv0[sizeof (argv0) - 1] = '\0'; |
| 1728 |
|
| 1729 |
execv (argv0, argv); |
| 1730 |
syslog (LOG_ERR, "Failed to execute %s", argv0); |
| 1731 |
free (argv); |
| 1732 |
exit (1); |
| 1733 |
} |
| 1734 |
|
| 1735 |
clear_device_id (&id); |
| 1736 |
free_device_uris (&device_uris); |
| 1737 |
free_usb_uri_map (map); |
| 1738 |
return 0; |
| 1739 |
} |
| 1740 |
|
| 1741 |
static void |
| 1742 |
remove_queue (const char *printer_uri) |
| 1743 |
{ |
| 1744 |
/* Delete it. */ |
| 1745 |
http_t *cups = httpConnectEncrypt (cupsServer (), ippPort (), |
| 1746 |
cupsEncryption ()); |
| 1747 |
ipp_t *request, *answer; |
| 1748 |
|
| 1749 |
if (cups == NULL) |
| 1750 |
return; |
| 1751 |
|
| 1752 |
request = ippNewRequest (CUPS_DELETE_PRINTER); |
| 1753 |
ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI, |
| 1754 |
"printer-uri", NULL, printer_uri); |
| 1755 |
answer = cupsDoRequest (cups, request, "/admin/"); |
| 1756 |
if (!answer) |
| 1757 |
{ |
| 1758 |
syslog (LOG_ERR, "Failed to send IPP-Delete-Printer request"); |
| 1759 |
httpClose (cups); |
| 1760 |
return; |
| 1761 |
} |
| 1762 |
|
| 1763 |
if (ippGetStatusCode (answer) > IPP_OK_CONFLICT) |
| 1764 |
syslog (LOG_ERR, "IPP-Delete-Printer request failed"); |
| 1765 |
else |
| 1766 |
syslog (LOG_INFO, "Deleted printer %s as the corresponding device " |
| 1767 |
"was unpaired", printer_uri); |
| 1768 |
|
| 1769 |
ippDelete (answer); |
| 1770 |
httpClose (cups); |
| 1771 |
} |
| 1772 |
|
| 1773 |
static void |
| 1774 |
disable_queue (const char *printer_uri, void *context) |
| 1775 |
{ |
| 1776 |
/* Disable it. */ |
| 1777 |
http_t *cups = httpConnectEncrypt (cupsServer (), ippPort (), |
| 1778 |
cupsEncryption ()); |
| 1779 |
ipp_t *request, *answer; |
| 1780 |
|
| 1781 |
if (cups == NULL) |
| 1782 |
return; |
| 1783 |
|
| 1784 |
request = ippNewRequest (IPP_PAUSE_PRINTER); |
| 1785 |
ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI, |
| 1786 |
"printer-uri", NULL, printer_uri); |
| 1787 |
ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_TEXT, |
| 1788 |
"printer-state-message", NULL, DISABLED_REASON); |
| 1789 |
answer = cupsDoRequest (cups, request, "/admin/"); |
| 1790 |
if (!answer) |
| 1791 |
{ |
| 1792 |
syslog (LOG_ERR, "Failed to send IPP-Pause-Printer request"); |
| 1793 |
httpClose (cups); |
| 1794 |
return; |
| 1795 |
} |
| 1796 |
|
| 1797 |
if (ippGetStatusCode (answer) > IPP_OK_CONFLICT) |
| 1798 |
syslog (LOG_ERR, "IPP-Pause-Printer request failed"); |
| 1799 |
else |
| 1800 |
syslog (LOG_INFO, "Disabled printer %s as the corresponding device " |
| 1801 |
"was unplugged or turned off", printer_uri); |
| 1802 |
|
| 1803 |
ippDelete (answer); |
| 1804 |
httpClose (cups); |
| 1805 |
} |
| 1806 |
|
| 1807 |
/* |
| 1808 |
* 'device_exists()' - Checks if the device really exists within filesystem. |
| 1809 |
*/ |
| 1810 |
|
| 1811 |
static int /* 0 if not found, 1 if found*/ |
| 1812 |
device_exists( |
| 1813 |
char *device_path) /* I - string representing device path of USB device*/ |
| 1814 |
{ |
| 1815 |
char full_path[100]; |
| 1816 |
struct stat buffer; |
| 1817 |
|
| 1818 |
// create a full path |
| 1819 |
snprintf(full_path, sizeof(full_path), "%s%s", "/sys", device_path); |
| 1820 |
|
| 1821 |
// check the exist of this device |
| 1822 |
if (stat(full_path, &buffer) == 0) |
| 1823 |
return 1; |
| 1824 |
|
| 1825 |
return 0; |
| 1826 |
} |
| 1827 |
|
| 1828 |
static int |
| 1829 |
do_remove (const char *devaddr) |
| 1830 |
{ |
| 1831 |
struct usb_uri_map *map; |
| 1832 |
struct usb_uri_map_entry *entry, **prev; |
| 1833 |
struct device_uris *uris = NULL; |
| 1834 |
char usblpdev[8] = ""; |
| 1835 |
gchar *devpath = NULL; |
| 1836 |
syslog (LOG_DEBUG, "remove %s", devaddr); |
| 1837 |
|
| 1838 |
if (bluetooth_verify_address (devaddr)) |
| 1839 |
{ |
| 1840 |
char *device_uri; |
| 1841 |
|
| 1842 |
device_uri = uri_from_bdaddr (devaddr); |
| 1843 |
remove_queue (device_uri); |
| 1844 |
g_free (device_uri); |
| 1845 |
return 0; |
| 1846 |
} |
| 1847 |
|
| 1848 |
if (!strncmp (devaddr, "usb-", 4)) |
| 1849 |
{ |
| 1850 |
struct udev *udev = udev_new (); |
| 1851 |
if (udev == NULL) |
| 1852 |
{ |
| 1853 |
syslog (LOG_ERR, "udev_new failed"); |
| 1854 |
exit (1); |
| 1855 |
} |
| 1856 |
|
| 1857 |
devpath = devpath_from_usb_devaddr (udev, devaddr); |
| 1858 |
udev_unref (udev); |
| 1859 |
} |
| 1860 |
else |
| 1861 |
devpath = g_strdup (devaddr); |
| 1862 |
|
| 1863 |
map = read_usb_uri_map (); |
| 1864 |
prev = &map->entries; |
| 1865 |
for (entry = map->entries; entry; entry = entry->next) |
| 1866 |
{ |
| 1867 |
/* devpath is a string obtained from USB 'remove' event |
| 1868 |
* and it is usually longer or the same as the string |
| 1869 |
* from the main USB 'add' event, when the device is added. |
| 1870 |
* |
| 1871 |
* Both strings represent device paths in filesystem without '/sys': |
| 1872 |
* f.e.: |
| 1873 |
* /devices/pci0000:00/0000:00:14.0/usb3/3-2 |
| 1874 |
* |
| 1875 |
* When we are disabling the queue, we check if the string from 'add' |
| 1876 |
* event is a substring of string from 'remove' event and the actual |
| 1877 |
* device doesn't exist anymore. |
| 1878 |
*/ |
| 1879 |
if (strstr(devpath, entry->devpath) != NULL && !device_exists(entry->devpath)) |
| 1880 |
{ |
| 1881 |
uris = &entry->uris; |
| 1882 |
break; |
| 1883 |
} |
| 1884 |
|
| 1885 |
prev = &(entry->next); |
| 1886 |
} |
| 1887 |
|
| 1888 |
if (uris) |
| 1889 |
{ |
| 1890 |
/* Find the relevant queues and disable them if they are enabled. */ |
| 1891 |
for_each_matching_queue (uris, 0, disable_queue, NULL, |
| 1892 |
usblpdev, sizeof (usblpdev)); |
| 1893 |
*prev = entry->next; |
| 1894 |
write_usb_uri_map (map); |
| 1895 |
} |
| 1896 |
|
| 1897 |
free_usb_uri_map (map); |
| 1898 |
g_free (devpath); |
| 1899 |
return 0; |
| 1900 |
} |
| 1901 |
|
| 1902 |
int |
| 1903 |
main (int argc, char **argv) |
| 1904 |
{ |
| 1905 |
int add = 0; |
| 1906 |
int enumerate = 0; |
| 1907 |
|
| 1908 |
if (argc > 1) |
| 1909 |
{ |
| 1910 |
add = !strcmp (argv[1], "add"); |
| 1911 |
enumerate = !strcmp (argv[1], "enumerate"); |
| 1912 |
} |
| 1913 |
|
| 1914 |
if (!(argc == 3 && |
| 1915 |
(add || !strcmp (argv[1], "remove"))) && |
| 1916 |
!(argc == 2 && enumerate)) |
| 1917 |
{ |
| 1918 |
fprintf (stderr, |
| 1919 |
"Syntax: %s add {USB device path}\n" |
| 1920 |
" %s remove {USB device path}\n" |
| 1921 |
" %s enumerate\n", |
| 1922 |
argv[0], argv[0], argv[0]); |
| 1923 |
return 1; |
| 1924 |
} |
| 1925 |
|
| 1926 |
openlog ("udev-configure-printer", 0, LOG_LPR); |
| 1927 |
cupsSetPasswordCB (no_password); |
| 1928 |
if (add) |
| 1929 |
return do_add (argv[0], argv[2]); |
| 1930 |
if (enumerate) |
| 1931 |
return 0; // no-op |
| 1932 |
|
| 1933 |
return do_remove (argv[2]); |
| 1934 |
} |