1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56#include "libbb.h"
57#include "xregex.h"
58#include <syslog.h>
59
60#include <sys/un.h>
61#include <sys/sysmacros.h>
62
63
64#define IDE0_MAJOR 3
65#define IDE1_MAJOR 22
66#define IDE2_MAJOR 33
67#define IDE3_MAJOR 34
68#define IDE4_MAJOR 56
69#define IDE5_MAJOR 57
70#define IDE6_MAJOR 88
71#define IDE7_MAJOR 89
72#define IDE8_MAJOR 90
73#define IDE9_MAJOR 91
74
75
76
77#define DEVFSD_PROTOCOL_REVISION_KERNEL 5
78#define DEVFSD_IOCTL_BASE 'd'
79
80#define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
81#define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
82#define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
83#define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
84#define DEVFSD_NOTIFY_REGISTERED 0
85#define DEVFSD_NOTIFY_UNREGISTERED 1
86#define DEVFSD_NOTIFY_ASYNC_OPEN 2
87#define DEVFSD_NOTIFY_CLOSE 3
88#define DEVFSD_NOTIFY_LOOKUP 4
89#define DEVFSD_NOTIFY_CHANGE 5
90#define DEVFSD_NOTIFY_CREATE 6
91#define DEVFSD_NOTIFY_DELETE 7
92#define DEVFS_PATHLEN 1024
93
94
95struct devfsd_notify_struct {
96
97 unsigned int type;
98 unsigned int mode;
99 unsigned int major;
100 unsigned int minor;
101 unsigned int uid;
102 unsigned int gid;
103 unsigned int overrun_count;
104 unsigned int namelen;
105
106 char devname[DEVFS_PATHLEN];
107};
108
109#define BUFFER_SIZE 16384
110#define DEVFSD_VERSION "1.3.25"
111#define CONFIG_FILE "/etc/devfsd.conf"
112#define MODPROBE "/sbin/modprobe"
113#define MODPROBE_SWITCH_1 "-k"
114#define MODPROBE_SWITCH_2 "-C"
115#define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
116#define MAX_ARGS (6 + 1)
117#define MAX_SUBEXPR 10
118#define STRING_LENGTH 255
119
120
121#define UID 0
122#define GID 1
123
124
125# define DIE 1
126# define NO_DIE 0
127
128
129#define RESTORE 0
130#define SERVICE 1
131#define READ_CONFIG 2
132
133
134#define DEVFSD_PROTOCOL_REVISION_DAEMON 5
135
136
137#if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
138#error protocol version mismatch. Update your kernel headers
139#endif
140
141#define AC_PERMISSIONS 0
142#define AC_MODLOAD 1
143#define AC_EXECUTE 2
144#define AC_MFUNCTION 3
145#define AC_CFUNCTION 4
146#define AC_COPY 5
147#define AC_IGNORE 6
148#define AC_MKOLDCOMPAT 7
149#define AC_MKNEWCOMPAT 8
150#define AC_RMOLDCOMPAT 9
151#define AC_RMNEWCOMPAT 10
152#define AC_RESTORE 11
153
154struct permissions_type {
155 mode_t mode;
156 uid_t uid;
157 gid_t gid;
158};
159
160struct execute_type {
161 char *argv[MAX_ARGS + 1];
162};
163
164struct copy_type {
165 const char *source;
166 const char *destination;
167};
168
169struct action_type {
170 unsigned int what;
171 unsigned int when;
172};
173
174struct config_entry_struct {
175 struct action_type action;
176 regex_t preg;
177 union
178 {
179 struct permissions_type permissions;
180 struct execute_type execute;
181 struct copy_type copy;
182 }
183 u;
184 struct config_entry_struct *next;
185};
186
187struct get_variable_info {
188 const struct devfsd_notify_struct *info;
189 const char *devname;
190 char devpath[STRING_LENGTH];
191};
192
193static void dir_operation(int , const char * , int, unsigned long*);
194static void service(struct stat statbuf, char *path);
195static int st_expr_expand(char *, unsigned, const char *, const char *(*)(const char *, void *), void *);
196static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
197static int mksymlink(const char *oldpath, const char *newpath);
198static void read_config_file(char *path, int optional, unsigned long *event_mask);
199static void process_config_line(const char *, unsigned long *);
200static int do_servicing(int, unsigned long);
201static void service_name(const struct devfsd_notify_struct *);
202static void action_permissions(const struct devfsd_notify_struct *, const struct config_entry_struct *);
203static void action_execute(const struct devfsd_notify_struct *, const struct config_entry_struct *,
204 const regmatch_t *, unsigned);
205static void action_modload(const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
206static void action_copy(const struct devfsd_notify_struct *, const struct config_entry_struct *,
207 const regmatch_t *, unsigned);
208static void action_compat(const struct devfsd_notify_struct *, unsigned);
209static void free_config(void);
210static void restore(char *spath, struct stat source_stat, int rootlen);
211static int copy_inode(const char *, const struct stat *, mode_t, const char *, const struct stat *);
212static mode_t get_mode(const char *);
213static void signal_handler(int);
214static const char *get_variable(const char *, void *);
215static int make_dir_tree(const char *);
216static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
217 const char *, const regmatch_t *, unsigned);
218static void expand_regexp(char *, size_t, const char *, const char *, const regmatch_t *, unsigned);
219static const char *expand_variable( char *, unsigned, unsigned *, const char *,
220 const char *(*)(const char *, void *), void *);
221static const char *get_variable_v2(const char *, const char *(*)(const char *, void *), void *);
222static char get_old_ide_name(unsigned , unsigned);
223static char *write_old_sd_name(char *, unsigned, unsigned, const char *);
224
225
226static int get_uid_gid(int flag, const char *string);
227static void safe_memcpy(char * dest, const char * src, int len);
228static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr);
229static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr);
230
231
232static struct config_entry_struct *first_config = NULL;
233static struct config_entry_struct *last_config = NULL;
234static char *mount_point = NULL;
235static volatile int caught_signal = FALSE;
236static volatile int caught_sighup = FALSE;
237static struct initial_symlink_struct {
238 const char *dest;
239 const char *name;
240} initial_symlinks[] = {
241 {"/proc/self/fd", "fd"},
242 {"fd/0", "stdin"},
243 {"fd/1", "stdout"},
244 {"fd/2", "stderr"},
245 {NULL, NULL},
246};
247
248static struct event_type {
249 unsigned int type;
250 const char *config_name;
251} event_types[] = {
252 {DEVFSD_NOTIFY_REGISTERED, "REGISTER"},
253 {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
254 {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"},
255 {DEVFSD_NOTIFY_CLOSE, "CLOSE"},
256 {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"},
257 {DEVFSD_NOTIFY_CHANGE, "CHANGE"},
258 {DEVFSD_NOTIFY_CREATE, "CREATE"},
259 {DEVFSD_NOTIFY_DELETE, "DELETE"},
260 {0xffffffff, NULL}
261};
262
263
264
265static const char bb_msg_proto_rev[] ALIGN1 = "protocol revision";
266static const char bb_msg_bad_config[] ALIGN1 = "bad %s config file: %s";
267static const char bb_msg_small_buffer[] ALIGN1 = "buffer too small";
268static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found";
269
270
271#if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG
272#define info_logger(p, fmt, args...) bb_info_msg(fmt, ## args)
273#define msg_logger(p, fmt, args...) bb_error_msg(fmt, ## args)
274#define msg_logger_and_die(p, fmt, args...) bb_error_msg_and_die(fmt, ## args)
275#define error_logger(p, fmt, args...) bb_perror_msg(fmt, ## args)
276#define error_logger_and_die(p, fmt, args...) bb_perror_msg_and_die(fmt, ## args)
277#else
278#define info_logger(p, fmt, args...)
279#define msg_logger(p, fmt, args...)
280#define msg_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
281#define error_logger(p, fmt, args...)
282#define error_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
283#endif
284
285static void safe_memcpy(char *dest, const char *src, int len)
286{
287 memcpy(dest , src, len);
288 dest[len] = '\0';
289}
290
291static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr)
292{
293 if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c')
294 return 2 + addendum;
295 if (d[n - 2] == 'c' && d[n - 1] == 'd')
296 return 3 + addendum;
297 if (ptr[0] == 'p' && ptr[1] == 'a' && ptr[2] == 'r' && ptr[3] == 't')
298 return 4 + addendum;
299 if (ptr[n - 2] == 'm' && ptr[n - 1] == 't')
300 return 5 + addendum;
301 return 0;
302}
303
304static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr)
305{
306 if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') {
307 if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n'
308 && d[n - 4] == 'e' && d[n - 3] == 'r' && d[n - 2] == 'i' && d[n - 1] == 'c'
309 )
310 return 1;
311 return scan_dev_name_common(d, n, 0, ptr);
312 }
313 if (d[0] == 'i' && d[1] == 'd' && d[2] == 'e' && d[3] == '/'
314 && d[4] == 'h' && d[5] == 'o' && d[6] == 's' && d[7] == 't'
315 )
316 return scan_dev_name_common(d, n, 4, ptr);
317 if (d[0] == 's' && d[1] == 'b' && d[2] == 'p' && d[3] == '/')
318 return 10;
319 if (d[0] == 'v' && d[1] == 'c' && d[2] == 'c' && d[3] == '/')
320 return 11;
321 if (d[0] == 'p' && d[1] == 't' && d[2] == 'y' && d[3] == '/')
322 return 12;
323 return 0;
324}
325
326
327
328int devfsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
329int devfsd_main(int argc, char **argv)
330{
331 int print_version = FALSE;
332 int do_daemon = TRUE;
333 int no_polling = FALSE;
334 int do_scan;
335 int fd, proto_rev, count;
336 unsigned long event_mask = 0;
337 struct sigaction new_action;
338 struct initial_symlink_struct *curr;
339
340 if (argc < 2)
341 bb_show_usage();
342
343 for (count = 2; count < argc; ++count) {
344 if (argv[count][0] == '-') {
345 if (argv[count][1] == 'v' && !argv[count][2])
346 print_version = TRUE;
347 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'f'
348 && argv[count][2] == 'g' && !argv[count][3])
349 do_daemon = FALSE;
350 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'n'
351 && argv[count][2] == 'p' && !argv[count][3])
352 no_polling = TRUE;
353 else
354 bb_show_usage();
355 }
356 }
357
358 mount_point = bb_simplify_path(argv[1]);
359
360 xchdir(mount_point);
361
362 fd = xopen(".devfsd", O_RDONLY);
363 close_on_exec_on(fd);
364 xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev);
365
366
367 for (curr = initial_symlinks; curr->dest != NULL; ++curr)
368 symlink(curr->dest, curr->name);
369
370
371
372 if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) {
373 printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
374 applet_name, DEVFSD_VERSION, bb_msg_proto_rev,
375 DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev);
376 if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
377 bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev);
378 exit(EXIT_SUCCESS);
379 }
380
381 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
382
383
384 sigemptyset(&new_action.sa_mask);
385 new_action.sa_flags = 0;
386 new_action.sa_handler = signal_handler;
387 sigaction_set(SIGHUP, &new_action);
388 sigaction_set(SIGUSR1, &new_action);
389
390 printf("%s v%s started for %s\n", applet_name, DEVFSD_VERSION, mount_point);
391
392
393 umask(0);
394 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
395
396 dir_operation(SERVICE, mount_point, 0, NULL);
397
398 if (ENABLE_DEVFSD_FG_NP && no_polling)
399 exit(EXIT_SUCCESS);
400
401 if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG)
402 logmode = LOGMODE_BOTH;
403 else if (do_daemon == TRUE)
404 logmode = LOGMODE_SYSLOG;
405
406
407
408
409 if (do_daemon) {
410
411 xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
412 bb_daemonize_or_rexec(0, argv);
413 } else if (ENABLE_DEVFSD_FG_NP) {
414 setpgid(0, 0);
415 }
416
417 while (TRUE) {
418 do_scan = do_servicing(fd, event_mask);
419
420 free_config();
421 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
422 if (do_scan)
423 dir_operation(SERVICE, mount_point, 0, NULL);
424 }
425 if (ENABLE_FEATURE_CLEAN_UP) free(mount_point);
426}
427
428
429
430
431static void read_config_file(char *path, int optional, unsigned long *event_mask)
432
433
434
435
436
437
438
439{
440 struct stat statbuf;
441 FILE *fp;
442 char buf[STRING_LENGTH];
443 char *line = NULL;
444 char *p;
445
446 if (stat(path, &statbuf) == 0) {
447
448
449
450 if (S_ISDIR(statbuf.st_mode)) {
451 p = bb_simplify_path(path);
452 dir_operation(READ_CONFIG, p, 0, event_mask);
453 free(p);
454 return;
455 }
456 fp = fopen_for_read(path);
457 if (fp != NULL) {
458 while (fgets(buf, STRING_LENGTH, fp) != NULL) {
459
460 line = buf;
461 line = skip_whitespace(line);
462 if (line[0] == '\0' || line[0] == '#')
463 continue;
464 process_config_line(line, event_mask);
465 }
466 fclose(fp);
467 } else {
468 goto read_config_file_err;
469 }
470 } else {
471read_config_file_err:
472 if (optional == 0 && errno == ENOENT)
473 error_logger_and_die(LOG_ERR, "read config file: %s", path);
474 }
475}
476
477static void process_config_line(const char *line, unsigned long *event_mask)
478
479
480
481
482
483{
484 int num_args, count;
485 struct config_entry_struct *new;
486 char p[MAX_ARGS][STRING_LENGTH];
487 char when[STRING_LENGTH], what[STRING_LENGTH];
488 char name[STRING_LENGTH];
489 const char *msg = "";
490 char *ptr;
491 int i;
492
493
494 static const char options[] ALIGN1 =
495 "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0"
496 "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0"
497 "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0"
498 "RMOLDCOMPAT\0""RMNEWCOMPAT\0";
499
500 for (count = 0; count < MAX_ARGS; ++count)
501 p[count][0] = '\0';
502 num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s",
503 when, name, what,
504 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
505
506 i = index_in_strings(options, when);
507
508
509 if (i == 0) {
510 free_config();
511 *event_mask = 0;
512 return;
513 }
514
515 if (num_args < 2)
516 goto process_config_line_err;
517
518
519 if (i == 1 || i == 2) {
520 st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL);
521 info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name);
522 read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask);
523 return;
524 }
525
526 if (i == 3) {
527 dir_operation(RESTORE, name, strlen(name),NULL);
528 return;
529 }
530 if (num_args < 3)
531 goto process_config_line_err;
532
533 new = xzalloc(sizeof *new);
534
535 for (count = 0; event_types[count].config_name != NULL; ++count) {
536 if (strcasecmp(when, event_types[count].config_name) != 0)
537 continue;
538 new->action.when = event_types[count].type;
539 break;
540 }
541 if (event_types[count].config_name == NULL) {
542 msg = "WHEN in";
543 goto process_config_line_err;
544 }
545
546 i = index_in_strings(options, what);
547
548 switch (i) {
549 case 4:
550 new->action.what = AC_PERMISSIONS;
551
552 ptr = strchr(p[0], '.');
553 if (ptr == NULL) {
554 msg = "UID.GID";
555 goto process_config_line_err;
556 }
557
558 *ptr++ = '\0';
559 new->u.permissions.uid = get_uid_gid(UID, p[0]);
560 new->u.permissions.gid = get_uid_gid(GID, ptr);
561
562 new->u.permissions.mode = get_mode(p[1]);
563 break;
564 case 5:
565
566
567
568 if (ENABLE_DEVFSD_MODLOAD)
569 new->action.what = AC_MODLOAD;
570 break;
571 case 6:
572 new->action.what = AC_EXECUTE;
573 num_args -= 3;
574
575 for (count = 0; count < num_args; ++count)
576 new->u.execute.argv[count] = xstrdup(p[count]);
577
578 new->u.execute.argv[num_args] = NULL;
579 break;
580 case 7:
581 new->action.what = AC_COPY;
582 num_args -= 3;
583 if (num_args != 2)
584 goto process_config_line_err;
585
586 new->u.copy.source = xstrdup(p[0]);
587 new->u.copy.destination = xstrdup(p[1]);
588 break;
589 case 8:
590
591 case 9:
592
593 case 10:
594
595 case 11:
596
597 case 12:
598
599
600
601
602
603 new->action.what = i - 2;
604 break;
605 default:
606 msg = "WHAT in";
607 goto process_config_line_err;
608
609 }
610
611 xregcomp(&new->preg, name, REG_EXTENDED);
612
613 *event_mask |= 1 << new->action.when;
614 new->next = NULL;
615 if (first_config == NULL)
616 first_config = new;
617 else
618 last_config->next = new;
619 last_config = new;
620 return;
621
622 process_config_line_err:
623 msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
624}
625
626static int do_servicing(int fd, unsigned long event_mask)
627
628
629
630
631
632{
633 ssize_t bytes;
634 struct devfsd_notify_struct info;
635
636
637 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask);
638 while (!caught_signal) {
639 errno = 0;
640 bytes = read(fd, (char *) &info, sizeof info);
641 if (caught_signal)
642 break;
643 if (errno == EINTR)
644 continue;
645 if (bytes < 1)
646 break;
647 service_name(&info);
648 }
649 if (caught_signal) {
650 int c_sighup = caught_sighup;
651
652 caught_signal = FALSE;
653 caught_sighup = FALSE;
654 return c_sighup;
655 }
656 msg_logger_and_die(LOG_ERR, "read error on control file");
657}
658
659static void service_name(const struct devfsd_notify_struct *info)
660
661
662
663
664{
665 unsigned int n;
666 regmatch_t mbuf[MAX_SUBEXPR];
667 struct config_entry_struct *entry;
668
669 if (ENABLE_DEBUG && info->overrun_count > 0)
670 msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
671
672
673 if (info->type == DEVFSD_NOTIFY_LOOKUP
674 && ((info->devname[0] == 'l' && info->devname[1] == 'o'
675 && info->devname[2] == 'g' && !info->devname[3])
676 || (info->devname[0] == 'i' && info->devname[1] == 'n'
677 && info->devname[2] == 'i' && info->devname[3] == 't'
678 && info->devname[4] == 'c' && info->devname[5] == 't'
679 && info->devname[6] == 'l' && !info->devname[7]))
680 )
681 return;
682
683 for (entry = first_config; entry != NULL; entry = entry->next) {
684
685 if (info->type != entry->action.when
686 || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0)
687 continue;
688 for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
689 ;
690
691 switch (entry->action.what) {
692 case AC_PERMISSIONS:
693 action_permissions(info, entry);
694 break;
695 case AC_MODLOAD:
696 if (ENABLE_DEVFSD_MODLOAD)
697 action_modload(info, entry);
698 break;
699 case AC_EXECUTE:
700 action_execute(info, entry, mbuf, n);
701 break;
702 case AC_COPY:
703 action_copy(info, entry, mbuf, n);
704 break;
705 case AC_IGNORE:
706 return;
707
708 case AC_MKOLDCOMPAT:
709 case AC_MKNEWCOMPAT:
710 case AC_RMOLDCOMPAT:
711 case AC_RMNEWCOMPAT:
712 action_compat(info, entry->action.what);
713 break;
714 default:
715 msg_logger_and_die(LOG_ERR, "Unknown action");
716 }
717 }
718}
719
720static void action_permissions(const struct devfsd_notify_struct *info,
721 const struct config_entry_struct *entry)
722
723
724
725
726
727{
728 struct stat statbuf;
729
730 if (stat(info->devname, &statbuf) != 0
731 || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0
732 || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0
733 )
734 error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname);
735}
736
737static void action_modload(const struct devfsd_notify_struct *info,
738 const struct config_entry_struct *entry UNUSED_PARAM)
739
740
741
742
743
744{
745 char *argv[6];
746
747 argv[0] = (char*)MODPROBE;
748 argv[1] = (char*)MODPROBE_SWITCH_1;
749 argv[2] = (char*)MODPROBE_SWITCH_2;
750 argv[3] = (char*)CONFIG_MODULES_DEVFS;
751 argv[4] = concat_path_file("/dev", info->devname);
752 argv[5] = NULL;
753
754 spawn_and_wait(argv);
755 free(argv[4]);
756}
757
758static void action_execute(const struct devfsd_notify_struct *info,
759 const struct config_entry_struct *entry,
760 const regmatch_t *regexpr, unsigned int numexpr)
761
762
763
764
765
766
767
768
769{
770 unsigned int count;
771 struct get_variable_info gv_info;
772 char *argv[MAX_ARGS + 1];
773 char largv[MAX_ARGS + 1][STRING_LENGTH];
774
775 gv_info.info = info;
776 gv_info.devname = info->devname;
777 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
778 for (count = 0; entry->u.execute.argv[count] != NULL; ++count) {
779 expand_expression(largv[count], STRING_LENGTH,
780 entry->u.execute.argv[count],
781 get_variable, &gv_info,
782 gv_info.devname, regexpr, numexpr);
783 argv[count] = largv[count];
784 }
785 argv[count] = NULL;
786 spawn_and_wait(argv);
787}
788
789
790static void action_copy(const struct devfsd_notify_struct *info,
791 const struct config_entry_struct *entry,
792 const regmatch_t *regexpr, unsigned int numexpr)
793
794
795
796
797
798
799
800
801{
802 mode_t new_mode;
803 struct get_variable_info gv_info;
804 struct stat source_stat, dest_stat;
805 char source[STRING_LENGTH], destination[STRING_LENGTH];
806 int ret = 0;
807
808 dest_stat.st_mode = 0;
809
810 if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode))
811 return;
812 gv_info.info = info;
813 gv_info.devname = info->devname;
814
815 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
816 expand_expression(source, STRING_LENGTH, entry->u.copy.source,
817 get_variable, &gv_info, gv_info.devname,
818 regexpr, numexpr);
819
820 expand_expression(destination, STRING_LENGTH, entry->u.copy.destination,
821 get_variable, &gv_info, gv_info.devname,
822 regexpr, numexpr);
823
824 if (!make_dir_tree(destination) || lstat(source, &source_stat) != 0)
825 return;
826 lstat(destination, &dest_stat);
827 new_mode = source_stat.st_mode & ~S_ISVTX;
828 if (info->type == DEVFSD_NOTIFY_CREATE)
829 new_mode |= S_ISVTX;
830 else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX))
831 new_mode |= S_ISVTX;
832 ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat);
833 if (ENABLE_DEBUG && ret && (errno != EEXIST))
834 error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination);
835}
836
837static void action_compat(const struct devfsd_notify_struct *info, unsigned int action)
838
839
840
841
842
843{
844 int ret;
845 const char *compat_name = NULL;
846 const char *dest_name = info->devname;
847 const char *ptr;
848 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
849 int mode, host, bus, target, lun;
850 unsigned int i;
851 char rewind_;
852
853 static const char *const fmt[] = {
854 NULL ,
855 "sg/c%db%dt%du%d",
856 "sd/c%db%dt%du%d",
857 "sr/c%db%dt%du%d",
858 "sd/c%db%dt%du%dp%d",
859 "st/c%db%dt%du%dm%d%c",
860 "ide/hd/c%db%dt%du%d",
861 "ide/cd/c%db%dt%du%d",
862 "ide/hd/c%db%dt%du%dp%d",
863 "ide/mt/c%db%dt%du%d%s",
864 NULL
865 };
866
867
868 switch (action) {
869 case AC_MKOLDCOMPAT:
870 case AC_RMOLDCOMPAT:
871 compat_name = get_old_name(info->devname, info->namelen, compat_buf, info->major, info->minor);
872 break;
873 case AC_MKNEWCOMPAT:
874 case AC_RMNEWCOMPAT:
875 ptr = bb_basename(info->devname);
876 i = scan_dev_name(info->devname, info->namelen, ptr);
877
878
879 if (i == 0 || i > 9)
880 return;
881
882 sscanf(info->devname + ((i < 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
883 snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname + (( i > 5) ? 4 : 0));
884 dest_name = dest_buf;
885 compat_name = compat_buf;
886
887
888
889 if (i == 1 || i == 2 || i == 3 || i == 6 || i ==7)
890 sprintf(compat_buf, fmt[i], host, bus, target, lun);
891
892
893 if (i == 4 || i == 8)
894 sprintf(compat_buf, fmt[i], host, bus, target, lun, atoi(ptr + 4));
895
896
897 if (i == 5) {
898 rewind_ = info->devname[info->namelen - 1];
899 if (rewind_ != 'n')
900 rewind_ = '\0';
901 mode=0;
902 if (ptr[2] == 'l' || ptr[2] == 'm')
903 mode = ptr[2] - 107;
904 if (ptr[2] == 'a')
905 mode = 3;
906 sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_);
907 }
908
909
910 if (i == 9)
911 snprintf(compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
912
913 }
914
915 if (compat_name == NULL)
916 return;
917
918
919 switch (action) {
920 case AC_MKOLDCOMPAT:
921 case AC_MKNEWCOMPAT:
922 mksymlink(dest_name, compat_name);
923 break;
924 case AC_RMOLDCOMPAT:
925 case AC_RMNEWCOMPAT:
926 ret = unlink(compat_name);
927 if (ENABLE_DEBUG && ret)
928 error_logger(LOG_ERR, "unlink: %s", compat_name);
929 break;
930
931 }
932}
933
934static void restore(char *spath, struct stat source_stat, int rootlen)
935{
936 char *dpath;
937 struct stat dest_stat;
938
939 dest_stat.st_mode = 0;
940 dpath = concat_path_file(mount_point, spath + rootlen);
941 lstat(dpath, &dest_stat);
942 free(dpath);
943 if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX))
944 copy_inode(dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX), spath, &source_stat);
945
946 if (S_ISDIR(source_stat.st_mode))
947 dir_operation(RESTORE, spath, rootlen, NULL);
948}
949
950
951static int copy_inode(const char *destpath, const struct stat *dest_stat,
952 mode_t new_mode,
953 const char *sourcepath, const struct stat *source_stat)
954
955
956
957
958
959
960
961
962{
963 int source_len, dest_len;
964 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
965 int fd, val;
966 struct sockaddr_un un_addr;
967 char symlink_val[STRING_LENGTH];
968
969 if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) {
970
971 if (S_ISLNK(source_stat->st_mode)) {
972 source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1);
973 if ((source_len < 0)
974 || (dest_len = readlink(destpath, dest_link, STRING_LENGTH - 1)) < 0
975 )
976 return FALSE;
977 source_link[source_len] = '\0';
978 dest_link[dest_len] = '\0';
979 if ((source_len != dest_len) || (strcmp(source_link, dest_link) != 0)) {
980 unlink(destpath);
981 symlink(source_link, destpath);
982 }
983 return TRUE;
984 }
985 chmod(destpath, new_mode & ~S_IFMT);
986 chown(destpath, source_stat->st_uid, source_stat->st_gid);
987 return TRUE;
988 }
989
990 unlink(destpath);
991 switch (source_stat->st_mode & S_IFMT) {
992 case S_IFSOCK:
993 fd = socket(AF_UNIX, SOCK_STREAM, 0);
994 if (fd < 0)
995 break;
996 un_addr.sun_family = AF_UNIX;
997 snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath);
998 val = bind(fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr);
999 close(fd);
1000 if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0)
1001 break;
1002 goto do_chown;
1003 case S_IFLNK:
1004 val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1);
1005 if (val < 0)
1006 break;
1007 symlink_val[val] = '\0';
1008 if (symlink(symlink_val, destpath) == 0)
1009 return TRUE;
1010 break;
1011 case S_IFREG:
1012 fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT);
1013 if (fd < 0)
1014 break;
1015 close(fd);
1016 if (chmod(destpath, new_mode & ~S_IFMT) != 0)
1017 break;
1018 goto do_chown;
1019 case S_IFBLK:
1020 case S_IFCHR:
1021 case S_IFIFO:
1022 if (mknod(destpath, new_mode, source_stat->st_rdev) != 0)
1023 break;
1024 goto do_chown;
1025 case S_IFDIR:
1026 if (mkdir(destpath, new_mode & ~S_IFMT) != 0)
1027 break;
1028do_chown:
1029 if (chown(destpath, source_stat->st_uid, source_stat->st_gid) == 0)
1030 return TRUE;
1031
1032 }
1033 return FALSE;
1034}
1035
1036static void free_config(void)
1037
1038
1039
1040{
1041 struct config_entry_struct *c_entry;
1042 void *next;
1043
1044 for (c_entry = first_config; c_entry != NULL; c_entry = next) {
1045 unsigned int count;
1046
1047 next = c_entry->next;
1048 regfree(&c_entry->preg);
1049 if (c_entry->action.what == AC_EXECUTE) {
1050 for (count = 0; count < MAX_ARGS; ++count) {
1051 if (c_entry->u.execute.argv[count] == NULL)
1052 break;
1053 free(c_entry->u.execute.argv[count]);
1054 }
1055 }
1056 free(c_entry);
1057 }
1058 first_config = NULL;
1059 last_config = NULL;
1060}
1061
1062static int get_uid_gid(int flag, const char *string)
1063
1064
1065
1066
1067
1068{
1069 struct passwd *pw_ent;
1070 struct group *grp_ent;
1071 static const char *msg;
1072
1073 if (ENABLE_DEVFSD_VERBOSE)
1074 msg = "user";
1075
1076 if (isdigit(string[0]) ||((string[0] == '-') && isdigit(string[1])))
1077 return atoi(string);
1078
1079 if (flag == UID && (pw_ent = getpwnam(string)) != NULL)
1080 return pw_ent->pw_uid;
1081
1082 if (flag == GID && (grp_ent = getgrnam(string)) != NULL)
1083 return grp_ent->gr_gid;
1084 else if (ENABLE_DEVFSD_VERBOSE)
1085 msg = "group";
1086
1087 if (ENABLE_DEVFSD_VERBOSE)
1088 msg_logger(LOG_ERR, "unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
1089 return 0;
1090}
1091
1092static mode_t get_mode(const char *string)
1093
1094
1095
1096
1097{
1098 mode_t mode;
1099 int i;
1100
1101 if (isdigit(string[0]))
1102 return strtoul(string, NULL, 8);
1103 if (strlen(string) != 9)
1104 msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
1105
1106 mode = 0;
1107 i = S_IRUSR;
1108 while (i > 0) {
1109 if (string[0] == 'r' || string[0] == 'w' || string[0] == 'x')
1110 mode += i;
1111 i = i / 2;
1112 string++;
1113 }
1114 return mode;
1115}
1116
1117static void signal_handler(int sig)
1118{
1119 caught_signal = TRUE;
1120 if (sig == SIGHUP)
1121 caught_sighup = TRUE;
1122
1123 info_logger(LOG_INFO, "Caught signal %d", sig);
1124}
1125
1126static const char *get_variable(const char *variable, void *info)
1127{
1128 static char sbuf[sizeof(int)*3 + 2];
1129 static char *hostname;
1130
1131 struct get_variable_info *gv_info = info;
1132 const char *field_names[] = {
1133 "hostname", "mntpt", "devpath", "devname",
1134 "uid", "gid", "mode", hostname, mount_point,
1135 gv_info->devpath, gv_info->devname, NULL
1136 };
1137 int i;
1138
1139 if (!hostname)
1140 hostname = safe_gethostname();
1141
1142 i = index_in_str_array(field_names, variable);
1143
1144 if (i > 6 || i < 0 || (i > 1 && gv_info == NULL))
1145 return NULL;
1146 if (i >= 0 && i <= 3)
1147 return field_names[i + 7];
1148
1149 if (i == 4)
1150 sprintf(sbuf, "%u", gv_info->info->uid);
1151 else if (i == 5)
1152 sprintf(sbuf, "%u", gv_info->info->gid);
1153 else if (i == 6)
1154 sprintf(sbuf, "%o", gv_info->info->mode);
1155 return sbuf;
1156}
1157
1158static void service(struct stat statbuf, char *path)
1159{
1160 struct devfsd_notify_struct info;
1161
1162 memset(&info, 0, sizeof info);
1163 info.type = DEVFSD_NOTIFY_REGISTERED;
1164 info.mode = statbuf.st_mode;
1165 info.major = major(statbuf.st_rdev);
1166 info.minor = minor(statbuf.st_rdev);
1167 info.uid = statbuf.st_uid;
1168 info.gid = statbuf.st_gid;
1169 snprintf(info.devname, sizeof(info.devname), "%s", path + strlen(mount_point) + 1);
1170 info.namelen = strlen(info.devname);
1171 service_name(&info);
1172 if (S_ISDIR(statbuf.st_mode))
1173 dir_operation(SERVICE, path, 0, NULL);
1174}
1175
1176static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
1177
1178
1179
1180
1181
1182
1183
1184{
1185 struct stat statbuf;
1186 DIR *dp;
1187 struct dirent *de;
1188 char *path;
1189
1190 dp = warn_opendir(dir_name);
1191 if (dp == NULL)
1192 return;
1193
1194 while ((de = readdir(dp)) != NULL) {
1195
1196 if (de->d_name && DOT_OR_DOTDOT(de->d_name))
1197 continue;
1198 path = concat_path_file(dir_name, de->d_name);
1199 if (lstat(path, &statbuf) == 0) {
1200 switch (type) {
1201 case SERVICE:
1202 service(statbuf, path);
1203 break;
1204 case RESTORE:
1205 restore(path, statbuf, var);
1206 break;
1207 case READ_CONFIG:
1208 read_config_file(path, var, event_mask);
1209 break;
1210 }
1211 }
1212 free(path);
1213 }
1214 closedir(dp);
1215}
1216
1217static int mksymlink(const char *oldpath, const char *newpath)
1218
1219
1220
1221
1222
1223{
1224 if (!make_dir_tree(newpath))
1225 return -1;
1226
1227 if (symlink(oldpath, newpath) != 0) {
1228 if (errno != EEXIST)
1229 return -1;
1230 }
1231 return 0;
1232}
1233
1234
1235static int make_dir_tree(const char *path)
1236
1237
1238
1239
1240{
1241 if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1)
1242 return FALSE;
1243 return TRUE;
1244}
1245
1246static int expand_expression(char *output, unsigned int outsize,
1247 const char *input,
1248 const char *(*get_variable_func)(const char *variable, void *info),
1249 void *info,
1250 const char *devname,
1251 const regmatch_t *ex, unsigned int numexp)
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266{
1267 char temp[STRING_LENGTH];
1268
1269 if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info))
1270 return FALSE;
1271 expand_regexp(output, outsize, temp, devname, ex, numexp);
1272 return TRUE;
1273}
1274
1275static void expand_regexp(char *output, size_t outsize, const char *input,
1276 const char *devname,
1277 const regmatch_t *ex, unsigned int numex)
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292{
1293 const char last_exp = '0' - 1 + numex;
1294 int c = -1;
1295
1296
1297
1298 if (outsize)
1299 output[--outsize] = '\0';
1300
1301
1302
1303 while ((c != '\0') && (outsize != 0)) {
1304 c = *input;
1305 ++input;
1306 if (c == '\\') {
1307 c = *input;
1308 ++input;
1309 if (c != '\\') {
1310 if ((c >= '0') && (c <= last_exp)) {
1311 const regmatch_t *subexp = ex + (c - '0');
1312 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1313
1314
1315 if (sublen > outsize)
1316 sublen = outsize;
1317 strncpy(output, devname + subexp->rm_so, sublen);
1318 output += sublen;
1319 outsize -= sublen;
1320 }
1321 continue;
1322 }
1323 }
1324 *output = c;
1325 ++output;
1326 --outsize;
1327 }
1328}
1329
1330
1331
1332
1333struct translate_struct {
1334 const char *match;
1335 const char *format;
1336
1337};
1338
1339static struct translate_struct translate_table[] =
1340{
1341 {"sound/", NULL},
1342 {"printers/", "lp%s"},
1343 {"v4l/", NULL},
1344 {"parports/", "parport%s"},
1345 {"fb/", "fb%s"},
1346 {"netlink/", NULL},
1347 {"loop/", "loop%s"},
1348 {"floppy/", "fd%s"},
1349 {"rd/", "ram%s"},
1350 {"md/", "md%s"},
1351 {"vc/", "tty%s"},
1352 {"misc/", NULL},
1353 {"isdn/", NULL},
1354 {"pg/", "pg%s"},
1355 {"i2c/", "i2c-%s"},
1356 {"staliomem/", "staliomem%s"},
1357 {"tts/E", "ttyE%s"},
1358 {"cua/E", "cue%s"},
1359 {"tts/R", "ttyR%s"},
1360 {"cua/R", "cur%s"},
1361 {"ip2/", "ip2%s"},
1362 {"tts/F", "ttyF%s"},
1363 {"cua/F", "cuf%s"},
1364 {"tts/C", "ttyC%s"},
1365 {"cua/C", "cub%s"},
1366 {"tts/", "ttyS%s"},
1367 {"cua/", "cua%s"},
1368 {"input/js", "js%s"},
1369 {NULL, NULL}
1370};
1371
1372const char *get_old_name(const char *devname, unsigned int namelen,
1373 char *buffer, unsigned int major, unsigned int minor)
1374
1375
1376
1377
1378
1379
1380
1381
1382{
1383 const char *compat_name = NULL;
1384 const char *ptr;
1385 struct translate_struct *trans;
1386 unsigned int i;
1387 char mode;
1388 int indexx;
1389 const char *pty1;
1390 const char *pty2;
1391 size_t len;
1392
1393 static const char *const fmt[] = {
1394 NULL ,
1395 "sg%u",
1396 NULL,
1397 "sr%u",
1398 NULL,
1399 "nst%u%c",
1400 "hd%c" ,
1401 "hd%c" ,
1402 "hd%c%s",
1403 "%sht%d",
1404 "sbpcd%u",
1405 "vcs%s",
1406 "%cty%c%c",
1407 NULL
1408 };
1409
1410 for (trans = translate_table; trans->match != NULL; ++trans) {
1411 len = strlen(trans->match);
1412
1413 if (strncmp(devname, trans->match, len) == 0) {
1414 if (trans->format == NULL)
1415 return devname + len;
1416 sprintf(buffer, trans->format, devname + len);
1417 return buffer;
1418 }
1419 }
1420
1421 ptr = bb_basename(devname);
1422 i = scan_dev_name(devname, namelen, ptr);
1423
1424 if (i > 0 && i < 13)
1425 compat_name = buffer;
1426 else
1427 return NULL;
1428
1429
1430 if (i == 1 || i == 3 || i == 10)
1431 sprintf(buffer, fmt[i], minor);
1432
1433
1434 if (i == 2 || i == 4)
1435 compat_name = write_old_sd_name(buffer, major, minor, ((i == 2) ? "" : (ptr + 4)));
1436
1437
1438 if (i == 5) {
1439 mode = ptr[2];
1440 if (mode == 'n')
1441 mode = '\0';
1442 sprintf(buffer, fmt[i], minor & 0x1f, mode);
1443 if (devname[namelen - 1] != 'n')
1444 ++compat_name;
1445 }
1446
1447 if (i == 6 || i == 7 || i == 8)
1448
1449 sprintf(buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4);
1450
1451
1452 if (i == 9)
1453 sprintf(buffer, fmt[i], ptr + 2, minor & 0x7f);
1454
1455
1456 if (i == 11) {
1457 sprintf(buffer, fmt[i], devname + 4);
1458 if (buffer[3] == '0')
1459 buffer[3] = '\0';
1460 }
1461
1462 if (i == 12) {
1463 pty1 = "pqrstuvwxyzabcde";
1464 pty2 = "0123456789abcdef";
1465 indexx = atoi(devname + 5);
1466 sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
1467 }
1468 return compat_name;
1469}
1470
1471static char get_old_ide_name(unsigned int major, unsigned int minor)
1472
1473
1474
1475
1476
1477{
1478 char letter = 'y';
1479 char c = 'a';
1480 int i = IDE0_MAJOR;
1481
1482
1483 do {
1484 if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR
1485 || i == IDE3_MAJOR || i == IDE4_MAJOR || i == IDE5_MAJOR
1486 || i == IDE6_MAJOR || i == IDE7_MAJOR || i == IDE8_MAJOR
1487 || i == IDE9_MAJOR
1488 ) {
1489 if ((unsigned int)i == major) {
1490 letter = c;
1491 break;
1492 }
1493 c += 2;
1494 }
1495 i++;
1496 } while (i <= IDE9_MAJOR);
1497
1498 if (minor > 63)
1499 ++letter;
1500 return letter;
1501}
1502
1503static char *write_old_sd_name(char *buffer,
1504 unsigned int major, unsigned int minor,
1505 const char *part)
1506
1507
1508
1509
1510
1511
1512
1513{
1514 unsigned int disc_index;
1515
1516 if (major == 8) {
1517 sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part);
1518 return buffer;
1519 }
1520 if ((major > 64) && (major < 72)) {
1521 disc_index = ((major - 64) << 4) +(minor >> 4);
1522 if (disc_index < 26)
1523 sprintf(buffer, "sd%c%s", 'a' + disc_index, part);
1524 else
1525 sprintf(buffer, "sd%c%c%s", 'a' +(disc_index / 26) - 1, 'a' + disc_index % 26, part);
1526 return buffer;
1527 }
1528 return NULL;
1529}
1530
1531
1532
1533
1534
1535
1536int st_expr_expand(char *output, unsigned int length, const char *input,
1537 const char *(*get_variable_func)(const char *variable,
1538 void *info),
1539 void *info)
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550{
1551 char ch;
1552 unsigned int len;
1553 unsigned int out_pos = 0;
1554 const char *env;
1555 const char *ptr;
1556 struct passwd *pwent;
1557 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1558
1559 if (length > BUFFER_SIZE)
1560 length = BUFFER_SIZE;
1561 for (; TRUE; ++input) {
1562 switch (ch = *input) {
1563 case '$':
1564
1565 input = expand_variable(buffer, length, &out_pos, ++input, get_variable_func, info);
1566 if (input == NULL)
1567 return FALSE;
1568 break;
1569 case '~':
1570
1571 ch = input[1];
1572 if (isspace(ch) ||(ch == '/') ||(ch == '\0')) {
1573
1574 env = getenv("HOME");
1575 if (env == NULL) {
1576 info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
1577 return FALSE;
1578 }
1579 len = strlen(env);
1580 if (len + out_pos >= length)
1581 goto st_expr_expand_out;
1582 memcpy(buffer + out_pos, env, len + 1);
1583 out_pos += len;
1584 continue;
1585 }
1586
1587 for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
1588 ;
1589 len = ptr - input;
1590 if (len >= sizeof tmp)
1591 goto st_expr_expand_out;
1592 safe_memcpy(tmp, input, len);
1593 input = ptr - 1;
1594 pwent = getpwnam(tmp);
1595 if (pwent == NULL) {
1596 info_logger(LOG_INFO, "no pwent for: %s", tmp);
1597 return FALSE;
1598 }
1599 len = strlen(pwent->pw_dir);
1600 if (len + out_pos >= length)
1601 goto st_expr_expand_out;
1602 memcpy(buffer + out_pos, pwent->pw_dir, len + 1);
1603 out_pos += len;
1604 break;
1605 case '\0':
1606
1607 default:
1608 if (out_pos >= length)
1609 goto st_expr_expand_out;
1610 buffer[out_pos++] = ch;
1611 if (ch == '\0') {
1612 memcpy(output, buffer, out_pos);
1613 return TRUE;
1614 }
1615 break;
1616
1617 }
1618 }
1619 return FALSE;
1620st_expr_expand_out:
1621 info_logger(LOG_INFO, bb_msg_small_buffer);
1622 return FALSE;
1623}
1624
1625
1626
1627
1628static const char *expand_variable(char *buffer, unsigned int length,
1629 unsigned int *out_pos, const char *input,
1630 const char *(*func)(const char *variable,
1631 void *info),
1632 void *info)
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645{
1646 char ch;
1647 int len;
1648 unsigned int open_braces;
1649 const char *env, *ptr;
1650 char tmp[STRING_LENGTH];
1651
1652 ch = input[0];
1653 if (ch == '$') {
1654
1655 sprintf(tmp, "%d", (int) getpid());
1656 len = strlen(tmp);
1657 if (len + *out_pos >= length)
1658 goto expand_variable_out;
1659
1660 memcpy(buffer + *out_pos, tmp, len + 1);
1661 out_pos += len;
1662 return input;
1663 }
1664
1665 if (ch != '{') {
1666
1667 for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr)
1668 ;
1669 len = ptr - input;
1670 if ((size_t)len >= sizeof tmp)
1671 goto expand_variable_out;
1672
1673 safe_memcpy(tmp, input, len);
1674 input = ptr - 1;
1675 env = get_variable_v2(tmp, func, info);
1676 if (env == NULL) {
1677 info_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
1678 return NULL;
1679 }
1680 len = strlen(env);
1681 if (len + *out_pos >= length)
1682 goto expand_variable_out;
1683
1684 memcpy(buffer + *out_pos, env, len + 1);
1685 *out_pos += len;
1686 return input;
1687 }
1688
1689 ch = *++input;
1690 for (ptr = input; isalnum(ch) || (ch == '_'); ch = *++ptr)
1691 ;
1692 if (ch == '}') {
1693
1694 len = ptr - input;
1695 if ((size_t)len >= sizeof tmp)
1696 goto expand_variable_out;
1697
1698 safe_memcpy(tmp, input, len);
1699 ptr = expand_variable(buffer, length, out_pos, tmp, func, info);
1700 if (ptr == NULL)
1701 return NULL;
1702 return input + len;
1703 }
1704 if (ch != ':' || ptr[1] != '-') {
1705 info_logger(LOG_INFO, "illegal char in var name");
1706 return NULL;
1707 }
1708
1709 len = ptr - input;
1710 if ((size_t)len >= sizeof tmp)
1711 goto expand_variable_out;
1712
1713 safe_memcpy(tmp, input, len);
1714
1715 input = ptr;
1716
1717 ptr += 2;
1718 ch = ptr[0];
1719 for (open_braces = 1; open_braces > 0; ch = *++ptr) {
1720 switch (ch) {
1721 case '{':
1722 ++open_braces;
1723 break;
1724 case '}':
1725 --open_braces;
1726 break;
1727 case '\0':
1728 info_logger(LOG_INFO, "\"}\" not found in: %s", input);
1729 return NULL;
1730 default:
1731 break;
1732 }
1733 }
1734 --ptr;
1735
1736 env = get_variable_v2(tmp, func, info);
1737 if (env != NULL) {
1738
1739
1740 input = ptr;
1741 len = strlen(env);
1742 if (len + *out_pos >= length)
1743 goto expand_variable_out;
1744
1745 memcpy(buffer + *out_pos, env, len + 1);
1746 *out_pos += len;
1747 return input;
1748 }
1749
1750
1751 input += 2;
1752 len = ptr - input;
1753 if ((size_t)len >= sizeof tmp)
1754 goto expand_variable_out;
1755
1756 safe_memcpy(tmp, input, len);
1757 input = ptr;
1758 if (!st_expr_expand(tmp, STRING_LENGTH, tmp, func, info))
1759 return NULL;
1760 len = strlen(tmp);
1761 if (len + *out_pos >= length)
1762 goto expand_variable_out;
1763
1764 memcpy(buffer + *out_pos, tmp, len + 1);
1765 *out_pos += len;
1766 return input;
1767expand_variable_out:
1768 info_logger(LOG_INFO, bb_msg_small_buffer);
1769 return NULL;
1770}
1771
1772
1773static const char *get_variable_v2(const char *variable,
1774 const char *(*func)(const char *variable, void *info),
1775 void *info)
1776
1777
1778
1779
1780
1781
1782
1783{
1784 const char *value;
1785
1786 if (func != NULL) {
1787 value = (*func)(variable, info);
1788 if (value != NULL)
1789 return value;
1790 }
1791 return getenv(variable);
1792}
1793
1794
1795