Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

p910nd.c 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Port 9100+n daemon
  3. * Accepts a connection from port 9100+n and copy stream to
  4. * /dev/lpn, where n = 0,1,2.
  5. *
  6. * Run standalone as: p910nd [0|1|2]
  7. *
  8. * Run under inetd as:
  9. * p910n stream tcp nowait root /usr/sbin/tcpd p910nd [0|1|2]
  10. * where p910n is an /etc/services entry for
  11. * port 9100, 9101 or 9102 as the case may be.
  12. * root can be replaced by any uid with rw permission on /dev/lpn
  13. *
  14. * Port 9100+n will then be passively opened
  15. * n defaults to 0
  16. *
  17. * Version 0.8
  18. * Allow specifying address to bind to
  19. *
  20. * Version 0.7
  21. * Bidirectional data transfer
  22. *
  23. * Version 0.6
  24. * Arne Bernin fixed some cast warnings, corrected the version number
  25. * and added a -v option to print the version.
  26. *
  27. * Version 0.5
  28. * -DUSE_LIBWRAP and -lwrap enables hosts_access (tcpwrappers) checking.
  29. *
  30. * Version 0.4
  31. * Ken Yap (ken_yap@users.sourceforge.net), April 2001
  32. * Placed under GPL.
  33. *
  34. * Added -f switch to specify device which overrides /dev/lpn.
  35. * But number is still required get distinct ports and locks.
  36. *
  37. * Added locking so that two invocations of the daemon under inetd
  38. * don't try to open the printer at the same time. This can happen
  39. * even if there is one host running clients because the previous
  40. * client can exit after it has sent all data but the printer has not
  41. * finished printing and inetd starts up a new daemon when the next
  42. * request comes in too soon.
  43. *
  44. * Various things could be Linux specific. I don't
  45. * think there is much demand for this program outside of PCs,
  46. * but if you port it to other distributions or platforms,
  47. * I'd be happy to receive your patches.
  48. */
  49. #include <unistd.h>
  50. #include <stdlib.h>
  51. #include <stdio.h>
  52. #include <getopt.h>
  53. #include <ctype.h>
  54. #include <string.h>
  55. #include <fcntl.h>
  56. #include <netdb.h>
  57. #include <syslog.h>
  58. #include <errno.h>
  59. #include <sys/types.h>
  60. #include <sys/time.h>
  61. #include <sys/resource.h>
  62. #include <sys/stat.h>
  63. #include <sys/socket.h>
  64. #include <netinet/in.h>
  65. #include <arpa/inet.h>
  66. #ifdef USE_LIBWRAP
  67. #include "tcpd.h"
  68. int allow_severity, deny_severity;
  69. extern int hosts_ctl(char *daemon, char *client_name,
  70. char *client_addr, char *client_user);
  71. #endif
  72. #define BASEPORT 9100
  73. #define PIDFILE "/var/run/p910%cd.pid"
  74. #ifdef LOCKFILE_DIR
  75. #define LOCKFILE LOCKFILE_DIR "/p910%cd"
  76. #else
  77. #define LOCKFILE "/var/lock/subsys/p910%cd"
  78. #endif
  79. #define PRINTERFILE "/dev/lp%c"
  80. #define LOGOPTS LOG_ERR
  81. static char *progname;
  82. static char version[] = "p910nd Version 0.8";
  83. static int lockfd = -1;
  84. static char *device = 0;
  85. static int bidir = 0;
  86. static char *bindaddr = 0;
  87. void usage(void)
  88. {
  89. fprintf(stderr, "Usage: %s [-f device] [-i bindaddr] [-bv] [0|1|2]\n", progname);
  90. exit(1);
  91. }
  92. void show_version (void)
  93. {
  94. fprintf(stdout, "%s \n", version);
  95. }
  96. FILE *open_printer(int lpnumber)
  97. {
  98. FILE *f;
  99. char lpname[sizeof(PRINTERFILE)];
  100. #ifdef TESTING
  101. (void)snprintf(lpname, sizeof(lpname), "/dev/tty");
  102. #else
  103. (void)snprintf(lpname, sizeof(lpname), PRINTERFILE, lpnumber);
  104. #endif
  105. if (device == 0)
  106. device = lpname;
  107. if ((f = fopen(device, bidir ? "w+" : "w")) == NULL)
  108. {
  109. syslog(LOGOPTS, "%s: %m\n", device);
  110. exit(1);
  111. }
  112. return (f);
  113. }
  114. int get_lock(int lpnumber)
  115. {
  116. char lockname[sizeof(LOCKFILE)];
  117. struct flock lplock;
  118. (void)snprintf(lockname, sizeof(lockname), LOCKFILE, lpnumber);
  119. if ((lockfd = open(lockname, O_CREAT|O_RDWR)) < 0)
  120. {
  121. syslog(LOGOPTS, "%s: %m\n", lockname);
  122. return (0);
  123. }
  124. memset(&lplock, 0, sizeof(lplock));
  125. lplock.l_type = F_WRLCK;
  126. lplock.l_pid = getpid();
  127. if (fcntl(lockfd, F_SETLKW, &lplock) < 0)
  128. {
  129. syslog(LOGOPTS, "%s: %m\n", lockname);
  130. return (0);
  131. }
  132. return (1);
  133. }
  134. void free_lock(void)
  135. {
  136. if (lockfd >= 0)
  137. (void)close(lockfd);
  138. }
  139. /* Copy network socket to FILE f until EOS */
  140. int copy_stream(int fd, FILE *f)
  141. {
  142. int nread;
  143. char buffer[8192];
  144. if (bidir) {
  145. FILE *nf;
  146. if ((nf = fdopen(fd, "w")) == NULL) {
  147. syslog(LOGOPTS, "fdopen: %m\n");
  148. }
  149. for (;;) {
  150. fd_set readfds;
  151. int result;
  152. int maxfd = fileno(f) > fd ? fileno(f) : fd;
  153. FD_ZERO(&readfds);
  154. FD_SET(fileno(f), &readfds);
  155. FD_SET(fd, &readfds);
  156. result = select(maxfd + 1, &readfds, 0, 0, 0);
  157. if (result < 0)
  158. return (result);
  159. if (result == 0)
  160. continue;
  161. if (FD_ISSET(fd, &readfds)) {
  162. nread = read(fd, buffer, sizeof(buffer));
  163. if (nread <= 0)
  164. break;
  165. (void)fwrite(buffer, sizeof(char), nread, f);
  166. }
  167. if (FD_ISSET(fileno(f), &readfds)) {
  168. nread = read(fileno(f), buffer, sizeof(buffer));
  169. if (nread > 0 && nf != NULL) {
  170. (void)fwrite(buffer, sizeof(char), nread, nf);
  171. (void)fflush(nf);
  172. }
  173. }
  174. }
  175. (void)fflush(f);
  176. (void)fclose(nf);
  177. return (0);
  178. } else {
  179. while ((nread = read(fd, buffer, sizeof(buffer))) > 0)
  180. (void)fwrite(buffer, sizeof(char), nread, f);
  181. (void)fflush(f);
  182. return (nread);
  183. }
  184. }
  185. void one_job(int lpnumber)
  186. {
  187. FILE *f;
  188. struct sockaddr_in client;
  189. socklen_t clientlen = sizeof(client);
  190. if (getpeername(0, (struct sockaddr*) &client, &clientlen) >= 0)
  191. syslog(LOGOPTS, "Connection from %s port %hu\n",
  192. inet_ntoa(client.sin_addr),
  193. ntohs(client.sin_port));
  194. if (get_lock(lpnumber) == 0)
  195. return;
  196. f = open_printer(lpnumber);
  197. if (copy_stream(0, f) < 0)
  198. syslog(LOGOPTS, "copy_stream: %m\n");
  199. fclose(f);
  200. free_lock();
  201. }
  202. void server(int lpnumber)
  203. {
  204. struct rlimit resourcelimit;
  205. #ifdef USE_GETPROTOBYNAME
  206. struct protoent *proto;
  207. #endif
  208. int netfd, fd, one = 1;
  209. socklen_t clientlen;
  210. struct sockaddr_in netaddr, client;
  211. char pidfilename[sizeof(PIDFILE)];
  212. FILE *f;
  213. int ipret;
  214. #ifndef TESTING
  215. switch (fork())
  216. {
  217. case -1:
  218. syslog(LOGOPTS, "fork: %m\n");
  219. exit (1);
  220. case 0: /* child */
  221. break;
  222. default: /* parent */
  223. exit(0);
  224. }
  225. /* Now in child process */
  226. resourcelimit.rlim_max = 0;
  227. if (getrlimit(RLIMIT_NOFILE, &resourcelimit) < 0)
  228. {
  229. syslog(LOGOPTS, "getrlimit: %m\n");
  230. exit(1);
  231. }
  232. for (fd = 0; fd < resourcelimit.rlim_max; ++fd)
  233. (void)close(fd);
  234. if (setsid() < 0)
  235. {
  236. syslog(LOGOPTS, "setsid: %m\n");
  237. exit(1);
  238. }
  239. (void)chdir("/");
  240. (void)umask(022);
  241. fd = open("/dev/null", O_RDWR); /* stdin */
  242. (void)dup(fd); /* stdout */
  243. (void)dup(fd); /* stderr */
  244. (void)snprintf(pidfilename, sizeof(pidfilename), PIDFILE, lpnumber);
  245. if ((f = fopen(pidfilename, "w")) == NULL)
  246. {
  247. syslog(LOGOPTS, "%s: %m\n", pidfilename);
  248. exit(1);
  249. }
  250. (void)fprintf(f, "%d\n", getpid());
  251. (void)fclose(f);
  252. if (get_lock(lpnumber) == 0)
  253. exit(1);
  254. #endif
  255. f = open_printer(lpnumber);
  256. #ifdef USE_GETPROTOBYNAME
  257. if ((proto = getprotobyname("tcp")) == NULL)
  258. {
  259. syslog(LOGOPTS, "Cannot find protocol for TCP!\n");
  260. exit(1);
  261. }
  262. if ((netfd = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)
  263. #else
  264. if ((netfd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0)
  265. #endif
  266. {
  267. syslog(LOGOPTS, "socket: %m\n");
  268. exit(1);
  269. }
  270. if (setsockopt(netfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
  271. {
  272. syslog(LOGOPTS, "setsocketopt: %m\n");
  273. exit(1);
  274. }
  275. netaddr.sin_port = htons(BASEPORT + lpnumber - '0');
  276. if (bindaddr == 0) {
  277. netaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  278. } else {
  279. ipret = inet_pton(AF_INET, bindaddr, &netaddr.sin_addr.s_addr);
  280. if (ipret < 0) {
  281. syslog(LOGOPTS, "inet_pton: %m\n");
  282. exit(1);
  283. } else if (ipret == 0) {
  284. syslog(LOGOPTS, "inet_pton: invalid bind IP address\n");
  285. exit(1);
  286. }
  287. }
  288. memset(netaddr.sin_zero, 0, sizeof(netaddr.sin_zero));
  289. if (bind(netfd, (struct sockaddr*) &netaddr, sizeof(netaddr)) < 0)
  290. {
  291. syslog(LOGOPTS, "bind: %m\n");
  292. exit(1);
  293. }
  294. if (listen(netfd, 5) < 0)
  295. {
  296. syslog(LOGOPTS, "listen: %m\n");
  297. exit(1);
  298. }
  299. clientlen = sizeof(client);
  300. memset(&client, 0, sizeof(client));
  301. while ((fd = accept(netfd, (struct sockaddr*) &client, &clientlen)) >= 0)
  302. {
  303. #ifdef USE_LIBWRAP
  304. if (hosts_ctl("p910nd", STRING_UNKNOWN,
  305. inet_ntoa(client.sin_addr), STRING_UNKNOWN) == 0) {
  306. syslog(LOGOPTS, "Connection from %s port %hd rejected\n",
  307. inet_ntoa(client.sin_addr),
  308. ntohs(client.sin_port));
  309. close(fd);
  310. continue;
  311. }
  312. #endif
  313. syslog(LOGOPTS, "Connection from %s port %hd accepted\n",
  314. inet_ntoa(client.sin_addr),
  315. ntohs(client.sin_port));
  316. /*write(fd, "Printing", 8);*/
  317. if (copy_stream(fd, f) < 0)
  318. syslog(LOGOPTS, "copy_stream: %m\n");
  319. (void)close(fd);
  320. }
  321. syslog(LOGOPTS, "accept: %m\n");
  322. free_lock();
  323. exit(1);
  324. }
  325. int is_standalone(void)
  326. {
  327. struct sockaddr_in bind_addr;
  328. socklen_t ba_len;
  329. /*
  330. * Check to see if a socket was passed to us from inetd.
  331. *
  332. * Use getsockname() to determine if descriptor 0 is indeed a socket
  333. * (and thus we are probably a child of inetd) or if it is instead
  334. * something else and we are running standalone.
  335. */
  336. ba_len = sizeof(bind_addr);
  337. if (getsockname(0, (struct sockaddr*) &bind_addr, &ba_len) == 0)
  338. return (0); /* under inetd */
  339. if (errno != ENOTSOCK) /* strange... */
  340. syslog(LOGOPTS, "getsockname: %m\n");
  341. return (1);
  342. }
  343. int main(int argc, char *argv[])
  344. {
  345. int c, lpnumber;
  346. char *p;
  347. if (argc <= 0) /* in case not provided in inetd.conf */
  348. progname = "p910nd";
  349. else
  350. {
  351. progname = argv[0];
  352. if ((p = strrchr(progname, '/')) != 0)
  353. progname = p + 1;
  354. }
  355. lpnumber = '0';
  356. while ((c = getopt(argc, argv, "bi:f:v")) != EOF)
  357. {
  358. switch (c)
  359. {
  360. case 'b':
  361. bidir = 1;
  362. break;
  363. case 'f':
  364. device = optarg;
  365. break;
  366. case 'i':
  367. bindaddr = optarg;
  368. break;
  369. case 'v':
  370. show_version();
  371. break;
  372. default:
  373. usage();
  374. break;
  375. }
  376. }
  377. argc -= optind;
  378. argv += optind;
  379. if (argc > 0)
  380. {
  381. if (isdigit(argv[0][0]))
  382. lpnumber = argv[0][0];
  383. }
  384. /* change the n in argv[0] to match the port so ps will show that */
  385. if ((p = strstr(progname, "p910n")) != NULL)
  386. p[4] = lpnumber;
  387. /* We used to pass (LOG_PERROR|LOG_PID|LOG_LPR|LOG_ERR) to syslog, but
  388. * syslog ignored the LOG_PID and LOG_PERROR option. I.e. the intention
  389. * was to add both options but the effect was to have neither.
  390. * I disagree with the intention to add PERROR. --Stef */
  391. openlog (p, LOG_PID, LOG_LPR);
  392. if (is_standalone())
  393. server(lpnumber);
  394. else
  395. one_job(lpnumber);
  396. return (0);
  397. }