Update recyclix.c
[hband-ld-preload-libs.git] / src / cupspwd.c
blob413cacf3756c344b07dd8233fe393c082570e247
1 #define _GNU_SOURCE
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <dlfcn.h>
6 #include <string.h>
7 #include <err.h>
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
12 //typedef ssize_t (*_read_f_t_)(int fd, void *buf, size_t count);
13 //static _read_f_t_ _read_f_ = NULL;
15 ssize_t read(int fd, void *buf, size_t count)
17 int (*real_read)(int fd, void *buf, size_t count) = NULL;
18 char fdpath[21];
19 char *pwd;
20 struct stat st;
22 warnx("intercepted fd=%d", fd);
25 _read_f_ = (_read_f_t_)dlsym(RTLD_NEXT, "read");
26 warnx("-> %p", _read_f_);
27 if (_read_f_ == NULL)
28 errx(1, "dlsym: %s", dlerror());
30 if(real_read == NULL)
32 void *handle = dlopen("/lib/i386-linux-gnu/libc.so.6", RTLD_LAZY);
33 if (handle == NULL)
34 errx(1, "dlopen: %s", dlerror());
35 real_read = dlsym(handle, "read");
36 if (real_read == NULL)
37 errx(1, "dlopen: %s", dlerror());
40 snprintf(fdpath, 21, "/proc/self/fd/%d", fd);
41 if(lstat(fdpath, &st) == 0)
43 if(major(st.st_rdev) == 5 && minor(st.st_rdev) == 0)
45 pwd = getenv("CUPS_PASSWORD");
46 if(pwd != NULL)
48 if(count >= strlen(pwd))
50 sprintf(buf, "%s\n", pwd);
51 return strlen(pwd)+1;
53 else
55 warnx("Buffer is too small, %d, %d.", count, stren(pwd));
58 else
60 warnx("CUPS_PASSWORD is not set.");
64 else
66 warn("lstat: %s", fdpath);
68 return real_read(fd, buf, count);
69 // return _read_f_(fd, buf, count);
72 // Fordítás: gcc -s -x c cupspwd.c -fPIC -shared -o cupspwd.so
73 // Futtatás: CUPS_PASSWORD=xxx LD_PRELOAD=./cupspwd.so lpstat -l -t