]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * "$Id: cups-deviced.c 5194 2006-02-27 20:57:07Z mike $" | |
3 | * | |
4 | * Device scanning mini-daemon for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2006 by Easy Software Products. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * main() - Scan for devices and return an IPP response. | |
27 | * add_dev() - Add a new device to the list. | |
28 | * compare_devs() - Compare device names for sorting. | |
29 | * sigalrm_handler() - Handle alarm signals for backends that get hung | |
30 | */ | |
31 | ||
32 | /* | |
33 | * Include necessary headers... | |
34 | */ | |
35 | ||
36 | #include "util.h" | |
37 | #include <cups/array.h> | |
38 | #include <cups/dir.h> | |
39 | ||
40 | #ifdef __hpux | |
41 | # define seteuid(uid) setresuid(-1, (uid), -1) | |
42 | #endif /* __hpux */ | |
43 | ||
44 | ||
45 | /* | |
46 | * Device information structure... | |
47 | */ | |
48 | ||
49 | typedef struct | |
50 | { | |
51 | char device_class[128], /* Device class */ | |
52 | device_make_and_model[128], /* Make and model, if known */ | |
53 | device_info[128], /* Device info/description */ | |
54 | device_uri[1024], /* Device URI */ | |
55 | device_id[1024]; /* 1284 Device ID */ | |
56 | } dev_info_t; | |
57 | ||
58 | ||
59 | /* | |
60 | * Local globals... | |
61 | */ | |
62 | ||
63 | static int alarm_tripped; /* Non-zero if alarm was tripped */ | |
64 | static cups_array_t *devs; /* Device info */ | |
65 | static int normal_user; /* Normal user ID */ | |
66 | ||
67 | ||
68 | /* | |
69 | * Local functions... | |
70 | */ | |
71 | ||
72 | static dev_info_t *add_dev(const char *device_class, | |
73 | const char *device_make_and_model, | |
74 | const char *device_info, | |
75 | const char *device_uri, | |
76 | const char *device_id); | |
77 | static int compare_devs(dev_info_t *p0, dev_info_t *p1); | |
78 | static void sigalrm_handler(int sig); | |
79 | ||
80 | ||
81 | /* | |
82 | * 'main()' - Scan for devices and return an IPP response. | |
83 | * | |
84 | * Usage: | |
85 | * | |
86 | * cups-deviced request_id limit options | |
87 | */ | |
88 | ||
89 | int /* O - Exit code */ | |
90 | main(int argc, /* I - Number of command-line args */ | |
91 | char *argv[]) /* I - Command-line arguments */ | |
92 | { | |
93 | const char *server_bin; /* CUPS_SERVERBIN environment variable */ | |
94 | char backends[1024]; /* Location of backends */ | |
95 | int request_id; /* Request ID */ | |
96 | int count; /* Number of devices from backend */ | |
97 | int compat; /* Compatibility device? */ | |
98 | FILE *fp; /* Pipe to device backend */ | |
99 | cups_dir_t *dir; /* Directory pointer */ | |
100 | cups_dentry_t *dent; /* Directory entry */ | |
101 | char filename[1024], /* Name of backend */ | |
102 | line[2048], /* Line from backend */ | |
103 | dclass[64], /* Device class */ | |
104 | uri[1024], /* Device URI */ | |
105 | info[128], /* Device info */ | |
106 | make_model[256], /* Make and model */ | |
107 | device_id[1024]; /* 1284 device ID */ | |
108 | int num_options; /* Number of options */ | |
109 | cups_option_t *options; /* Options */ | |
110 | const char *requested; /* requested-attributes option */ | |
111 | int send_class, /* Send device-class attribute? */ | |
112 | send_info, /* Send device-info attribute? */ | |
113 | send_make_and_model, /* Send device-make-and-model attribute? */ | |
114 | send_uri, /* Send device-uri attribute? */ | |
115 | send_id; /* Send device-id attribute? */ | |
116 | dev_info_t *dev; /* Current device */ | |
117 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
118 | struct sigaction action; /* Actions for POSIX signals */ | |
119 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
120 | ||
121 | ||
122 | setbuf(stderr, NULL); | |
123 | ||
124 | /* | |
125 | * Check the command-line... | |
126 | */ | |
127 | ||
128 | if (argc > 1) | |
129 | request_id = atoi(argv[1]); | |
130 | else | |
131 | request_id = 1; | |
132 | ||
133 | if (argc != 5) | |
134 | { | |
135 | fputs("Usage: cups-deviced request-id limit user-id options\n", stderr); | |
136 | ||
137 | return (1); | |
138 | } | |
139 | ||
140 | if (request_id < 1) | |
141 | { | |
142 | fprintf(stderr, "cups-deviced: Bad request ID %d!\n", request_id); | |
143 | ||
144 | return (1); | |
145 | } | |
146 | ||
147 | normal_user = atoi(argv[3]); | |
148 | if (normal_user <= 0) | |
149 | { | |
150 | fprintf(stderr, "cups-deviced: Bad user %d!\n", normal_user); | |
151 | ||
152 | return (1); | |
153 | } | |
154 | ||
155 | num_options = cupsParseOptions(argv[4], 0, &options); | |
156 | requested = cupsGetOption("requested-attributes", num_options, options); | |
157 | ||
158 | if (!requested || strstr(requested, "all")) | |
159 | { | |
160 | send_class = 1; | |
161 | send_info = 1; | |
162 | send_make_and_model = 1; | |
163 | send_uri = 1; | |
164 | send_id = 1; | |
165 | } | |
166 | else | |
167 | { | |
168 | send_class = strstr(requested, "device-class") != NULL; | |
169 | send_info = strstr(requested, "device-info") != NULL; | |
170 | send_make_and_model = strstr(requested, "device-make-and-model") != NULL; | |
171 | send_uri = strstr(requested, "device-uri") != NULL; | |
172 | send_id = strstr(requested, "device-id") != NULL; | |
173 | } | |
174 | ||
175 | /* | |
176 | * Try opening the backend directory... | |
177 | */ | |
178 | ||
179 | if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL) | |
180 | server_bin = CUPS_SERVERBIN; | |
181 | ||
182 | snprintf(backends, sizeof(backends), "%s/backend", server_bin); | |
183 | ||
184 | if ((dir = cupsDirOpen(backends)) == NULL) | |
185 | { | |
186 | fprintf(stderr, "ERROR: [cups-deviced] Unable to open backend directory " | |
187 | "\"%s\": %s", backends, strerror(errno)); | |
188 | ||
189 | return (1); | |
190 | } | |
191 | ||
192 | /* | |
193 | * Setup the devices array... | |
194 | */ | |
195 | ||
196 | devs = cupsArrayNew((cups_array_func_t)compare_devs, NULL); | |
197 | ||
198 | /* | |
199 | * Loop through all of the device backends... | |
200 | */ | |
201 | ||
202 | while ((dent = cupsDirRead(dir)) != NULL) | |
203 | { | |
204 | /* | |
205 | * Skip entries that are not executable files... | |
206 | */ | |
207 | ||
208 | if (!S_ISREG(dent->fileinfo.st_mode) || | |
209 | (dent->fileinfo.st_mode & (S_IRUSR | S_IXUSR)) != (S_IRUSR | S_IXUSR)) | |
210 | continue; | |
211 | ||
212 | /* | |
213 | * Change effective users depending on the backend permissions... | |
214 | */ | |
215 | ||
216 | if (!getuid()) | |
217 | { | |
218 | /* | |
219 | * Backends without permissions for normal users run as root, | |
220 | * all others run as the unprivileged user... | |
221 | */ | |
222 | ||
223 | if (!(dent->fileinfo.st_mode & (S_IRWXG | S_IRWXO))) | |
224 | seteuid(0); | |
225 | else | |
226 | seteuid(normal_user); | |
227 | } | |
228 | ||
229 | /* | |
230 | * Run the backend with no arguments and collect the output... | |
231 | */ | |
232 | ||
233 | snprintf(filename, sizeof(filename), "%s/%s", backends, dent->filename); | |
234 | if ((fp = popen(filename, "r")) != NULL) | |
235 | { | |
236 | /* | |
237 | * Set an alarm for the first read from the backend; this avoids | |
238 | * problems when a backend is hung getting device information. | |
239 | */ | |
240 | ||
241 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
242 | sigset(SIGALRM, sigalrm_handler); | |
243 | #elif defined(HAVE_SIGACTION) | |
244 | memset(&action, 0, sizeof(action)); | |
245 | ||
246 | sigemptyset(&action.sa_mask); | |
247 | sigaddset(&action.sa_mask, SIGALRM); | |
248 | action.sa_handler = sigalrm_handler; | |
249 | sigaction(SIGALRM, &action, NULL); | |
250 | #else | |
251 | signal(SIGALRM, sigalrm_handler); | |
252 | #endif /* HAVE_SIGSET */ | |
253 | ||
254 | alarm_tripped = 0; | |
255 | count = 0; | |
256 | compat = !strcmp(dent->filename, "smb"); | |
257 | ||
258 | alarm(30); | |
259 | ||
260 | while (fgets(line, sizeof(line), fp) != NULL) | |
261 | { | |
262 | /* | |
263 | * Reset the alarm clock... | |
264 | */ | |
265 | ||
266 | alarm(30); | |
267 | ||
268 | /* | |
269 | * Each line is of the form: | |
270 | * | |
271 | * class URI "make model" "name" ["1284 device ID"] | |
272 | */ | |
273 | ||
274 | device_id[0] = '\0'; | |
275 | ||
276 | if (!strncasecmp(line, "Usage", 5)) | |
277 | compat = 1; | |
278 | else if (sscanf(line, | |
279 | "%63s%1023s%*[ \t]\"%255[^\"]\"%*[ \t]\"%127[^\"]" | |
280 | "%*[ \t]\"%1023[^\"]", | |
281 | dclass, uri, make_model, info, device_id) < 4) | |
282 | { | |
283 | /* | |
284 | * Bad format; strip trailing newline and write an error message. | |
285 | */ | |
286 | ||
287 | if (line[strlen(line) - 1] == '\n') | |
288 | line[strlen(line) - 1] = '\0'; | |
289 | ||
290 | fprintf(stderr, "ERROR: [cups-deviced] Bad line from \"%s\": %s\n", | |
291 | dent->filename, line); | |
292 | compat = 1; | |
293 | break; | |
294 | } | |
295 | else | |
296 | { | |
297 | /* | |
298 | * Add the device to the array of available devices... | |
299 | */ | |
300 | ||
301 | dev = add_dev(dclass, make_model, info, uri, device_id); | |
302 | if (!dev) | |
303 | { | |
304 | cupsDirClose(dir); | |
305 | return (1); | |
306 | } | |
307 | ||
308 | fprintf(stderr, "DEBUG: [cups-deviced] Added device \"%s\"...\n", | |
309 | uri); | |
310 | count ++; | |
311 | } | |
312 | } | |
313 | ||
314 | /* | |
315 | * Turn the alarm clock off and close the pipe to the command... | |
316 | */ | |
317 | ||
318 | alarm(0); | |
319 | ||
320 | if (alarm_tripped) | |
321 | fprintf(stderr, "WARNING: [cups-deviced] Backend \"%s\" did not " | |
322 | "respond within 30 seconds!\n", dent->filename); | |
323 | ||
324 | pclose(fp); | |
325 | ||
326 | /* | |
327 | * Hack for backends that don't support the CUPS 1.1 calling convention: | |
328 | * add a network device with the method == backend name. | |
329 | */ | |
330 | ||
331 | if (count == 0 && compat) | |
332 | { | |
333 | snprintf(line, sizeof(line), "Unknown Network Device (%s)", | |
334 | dent->filename); | |
335 | ||
336 | dev = add_dev("network", line, "Unknown", dent->filename, ""); | |
337 | if (!dev) | |
338 | { | |
339 | cupsDirClose(dir); | |
340 | return (1); | |
341 | } | |
342 | ||
343 | fprintf(stderr, "DEBUG: [cups-deviced] Compatibility device " | |
344 | "\"%s\"...\n", dent->filename); | |
345 | } | |
346 | } | |
347 | else | |
348 | fprintf(stderr, "WARNING: [cups-deviced] Unable to execute \"%s\" " | |
349 | "backend: %s\n", dent->filename, strerror(errno)); | |
350 | } | |
351 | ||
352 | cupsDirClose(dir); | |
353 | ||
354 | /* | |
355 | * Switch back to root as needed... | |
356 | */ | |
357 | ||
358 | if (!getuid() && geteuid()) | |
359 | seteuid(0); | |
360 | ||
361 | /* | |
362 | * Output the list of devices... | |
363 | */ | |
364 | ||
365 | puts("Content-Type: application/ipp\n"); | |
366 | ||
367 | cupsdSendIPPHeader(IPP_OK, request_id); | |
368 | cupsdSendIPPGroup(IPP_TAG_OPERATION); | |
369 | cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8"); | |
370 | cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", "en-US"); | |
371 | ||
372 | if ((count = atoi(argv[2])) <= 0) | |
373 | count = cupsArrayCount(devs); | |
374 | ||
375 | if (count > cupsArrayCount(devs)) | |
376 | count = cupsArrayCount(devs); | |
377 | ||
378 | for (dev = (dev_info_t *)cupsArrayFirst(devs); | |
379 | count > 0; | |
380 | count --, dev = (dev_info_t *)cupsArrayNext(devs)) | |
381 | { | |
382 | /* | |
383 | * Add strings to attributes... | |
384 | */ | |
385 | ||
386 | cupsdSendIPPGroup(IPP_TAG_PRINTER); | |
387 | if (send_class) | |
388 | cupsdSendIPPString(IPP_TAG_KEYWORD, "device-class", dev->device_class); | |
389 | if (send_info) | |
390 | cupsdSendIPPString(IPP_TAG_TEXT, "device-info", dev->device_info); | |
391 | if (send_make_and_model) | |
392 | cupsdSendIPPString(IPP_TAG_TEXT, "device-make-and-model", | |
393 | dev->device_make_and_model); | |
394 | if (send_uri) | |
395 | cupsdSendIPPString(IPP_TAG_URI, "device-uri", dev->device_uri); | |
396 | if (send_id) | |
397 | cupsdSendIPPString(IPP_TAG_TEXT, "device-id", dev->device_id); | |
398 | } | |
399 | ||
400 | cupsdSendIPPTrailer(); | |
401 | ||
402 | /* | |
403 | * Free the devices array and return... | |
404 | */ | |
405 | ||
406 | for (dev = (dev_info_t *)cupsArrayFirst(devs); | |
407 | dev; | |
408 | dev = (dev_info_t *)cupsArrayNext(devs)) | |
409 | free(dev); | |
410 | ||
411 | cupsArrayDelete(devs); | |
412 | ||
413 | return (0); | |
414 | } | |
415 | ||
416 | ||
417 | /* | |
418 | * 'add_dev()' - Add a new device to the list. | |
419 | */ | |
420 | ||
421 | static dev_info_t * /* O - New device or NULL on error */ | |
422 | add_dev( | |
423 | const char *device_class, /* I - Device class */ | |
424 | const char *device_make_and_model, /* I - Device make and model */ | |
425 | const char *device_info, /* I - Device information */ | |
426 | const char *device_uri, /* I - Device URI */ | |
427 | const char *device_id) /* I - 1284 device ID */ | |
428 | { | |
429 | dev_info_t *dev; /* New device */ | |
430 | ||
431 | ||
432 | /* | |
433 | * Allocate memory for the device record... | |
434 | */ | |
435 | ||
436 | if ((dev = calloc(1, sizeof(dev_info_t))) == NULL) | |
437 | { | |
438 | fputs("ERROR: [cups-deviced] Ran out of memory allocating a device!\n", | |
439 | stderr); | |
440 | return (NULL); | |
441 | } | |
442 | ||
443 | /* | |
444 | * Copy the strings over... | |
445 | */ | |
446 | ||
447 | strlcpy(dev->device_class, device_class, sizeof(dev->device_class)); | |
448 | strlcpy(dev->device_make_and_model, device_make_and_model, | |
449 | sizeof(dev->device_make_and_model)); | |
450 | strlcpy(dev->device_info, device_info, sizeof(dev->device_info)); | |
451 | strlcpy(dev->device_uri, device_uri, sizeof(dev->device_uri)); | |
452 | strlcpy(dev->device_id, device_id, sizeof(dev->device_id)); | |
453 | ||
454 | /* | |
455 | * Add the device to the array and return... | |
456 | */ | |
457 | ||
458 | cupsArrayAdd(devs, dev); | |
459 | ||
460 | return (dev); | |
461 | } | |
462 | ||
463 | ||
464 | /* | |
465 | * 'compare_devs()' - Compare device names for sorting. | |
466 | */ | |
467 | ||
468 | static int /* O - Result of comparison */ | |
469 | compare_devs(dev_info_t *d0, /* I - First device */ | |
470 | dev_info_t *d1) /* I - Second device */ | |
471 | { | |
472 | int diff; /* Difference between strings */ | |
473 | ||
474 | ||
475 | /* | |
476 | * Sort devices by device-info, device-class, and device-uri... | |
477 | */ | |
478 | ||
479 | if ((diff = cupsdCompareNames(d0->device_info, d1->device_info)) != 0) | |
480 | return (diff); | |
481 | else if ((diff = strcasecmp(d0->device_class, d1->device_class)) != 0) | |
482 | return (diff); | |
483 | else | |
484 | return (strcasecmp(d0->device_uri, d1->device_uri)); | |
485 | } | |
486 | ||
487 | ||
488 | /* | |
489 | * 'sigalrm_handler()' - Handle alarm signals for backends that get hung | |
490 | * trying to list the available devices... | |
491 | */ | |
492 | ||
493 | static void | |
494 | sigalrm_handler(int sig) /* I - Signal number */ | |
495 | { | |
496 | (void)sig; /* remove compiler warnings... */ | |
497 | ||
498 | alarm_tripped = 1; | |
499 | } | |
500 | ||
501 | ||
502 | /* | |
503 | * End of "$Id: cups-deviced.c 5194 2006-02-27 20:57:07Z mike $". | |
504 | */ |