3 * Accepts a connection from port 9100+n and copy stream to
4 * /dev/lpn, where n = 0,1,2.
6 * Run standalone as: p910nd [0|1|2]
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
14 * Port 9100+n will then be passively opened
18 * Allow specifying address to bind to
21 * Bidirectional data transfer
24 * Arne Bernin fixed some cast warnings, corrected the version number
25 * and added a -v option to print the version.
28 * -DUSE_LIBWRAP and -lwrap enables hosts_access (tcpwrappers) checking.
31 * Ken Yap (ken_yap@users.sourceforge.net), April 2001
34 * Added -f switch to specify device which overrides /dev/lpn.
35 * But number is still required get distinct ports and locks.
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.
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.
60 #include <sys/types.h>
62 #include <sys/resource.h>
64 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
70 int allow_severity
, deny_severity
;
71 extern int hosts_ctl(char *daemon
, char *client_name
,
72 char *client_addr
, char *client_user
);
76 #define PIDFILE "/var/run/p910%cd.pid"
78 #define LOCKFILE LOCKFILE_DIR "/p910%cd"
80 #define LOCKFILE "/var/lock/subsys/p910%cd"
82 #define PRINTERFILE "/dev/lp%c"
83 #define LOGOPTS LOG_ERR
85 static char *progname
;
86 static char version
[] = "p910nd Version 0.8";
87 static int lockfd
= -1;
88 static char *device
= 0;
90 static char *bindaddr
= 0;
94 fprintf(stderr
, "Usage: %s [-f device] [-i bindaddr] [-bv] [0|1|2]\n", progname
);
98 void show_version (void)
100 fprintf(stdout
, "%s \n", version
);
103 FILE *open_printer(int lpnumber
)
106 char lpname
[sizeof(PRINTERFILE
)];
109 (void)snprintf(lpname
, sizeof(lpname
), "/dev/tty");
111 (void)snprintf(lpname
, sizeof(lpname
), PRINTERFILE
, lpnumber
);
115 if ((f
= fopen(device
, bidir
? "w+" : "w")) == NULL
)
117 syslog(LOGOPTS
, "%s: %m\n", device
);
123 int get_lock(int lpnumber
)
125 char lockname
[sizeof(LOCKFILE
)];
128 (void)snprintf(lockname
, sizeof(lockname
), LOCKFILE
, lpnumber
);
129 if ((lockfd
= open(lockname
, O_CREAT
|O_RDWR
)) < 0)
131 syslog(LOGOPTS
, "%s: %m\n", lockname
);
134 memset(&lplock
, 0, sizeof(lplock
));
135 lplock
.l_type
= F_WRLCK
;
136 lplock
.l_pid
= getpid();
137 if (fcntl(lockfd
, F_SETLKW
, &lplock
) < 0)
139 syslog(LOGOPTS
, "%s: %m\n", lockname
);
151 /* Copy network socket to FILE f until EOS */
152 int copy_stream(int fd
, FILE *f
)
160 if ((nf
= fdopen(fd
, "w")) == NULL
) {
161 syslog(LOGOPTS
, "fdopen: %m\n");
166 int maxfd
= fileno(f
) > fd
? fileno(f
) : fd
;
168 FD_SET(fileno(f
), &readfds
);
169 FD_SET(fd
, &readfds
);
170 result
= select(maxfd
+ 1, &readfds
, 0, 0, 0);
175 if (FD_ISSET(fd
, &readfds
)) {
176 nread
= read(fd
, buffer
, sizeof(buffer
));
179 (void)fwrite(buffer
, sizeof(char), nread
, f
);
181 if (FD_ISSET(fileno(f
), &readfds
)) {
182 nread
= read(fileno(f
), buffer
, sizeof(buffer
));
183 if (nread
> 0 && nf
!= NULL
) {
184 (void)fwrite(buffer
, sizeof(char), nread
, nf
);
193 while ((nread
= read(fd
, buffer
, sizeof(buffer
))) > 0)
194 (void)fwrite(buffer
, sizeof(char), nread
, f
);
200 void one_job(int lpnumber
)
203 struct sockaddr_in client
;
204 socklen_t clientlen
= sizeof(client
);
206 if (getpeername(0, (struct sockaddr
*) &client
, &clientlen
) >= 0)
207 syslog(LOGOPTS
, "Connection from %s port %hu\n",
208 inet_ntoa(client
.sin_addr
),
209 ntohs(client
.sin_port
));
210 if (get_lock(lpnumber
) == 0)
212 f
= open_printer(lpnumber
);
213 if (copy_stream(0, f
) < 0)
214 syslog(LOGOPTS
, "copy_stream: %m\n");
219 void server(int lpnumber
)
221 struct rlimit resourcelimit
;
222 #ifdef USE_GETPROTOBYNAME
223 struct protoent
*proto
;
225 int netfd
, fd
, one
= 1;
227 struct sockaddr_in netaddr
, client
;
228 char pidfilename
[sizeof(PIDFILE
)];
236 syslog(LOGOPTS
, "fork: %m\n");
240 default: /* parent */
243 /* Now in child process */
244 resourcelimit
.rlim_max
= 0;
245 if (getrlimit(RLIMIT_NOFILE
, &resourcelimit
) < 0)
247 syslog(LOGOPTS
, "getrlimit: %m\n");
250 for (fd
= 0; fd
< resourcelimit
.rlim_max
; ++fd
)
254 syslog(LOGOPTS
, "setsid: %m\n");
259 fd
= open("/dev/null", O_RDWR
); /* stdin */
260 (void)dup(fd
); /* stdout */
261 (void)dup(fd
); /* stderr */
262 (void)snprintf(pidfilename
, sizeof(pidfilename
), PIDFILE
, lpnumber
);
263 if ((f
= fopen(pidfilename
, "w")) == NULL
)
265 syslog(LOGOPTS
, "%s: %m\n", pidfilename
);
268 (void)fprintf(f
, "%d\n", getpid());
270 if (get_lock(lpnumber
) == 0)
273 f
= open_printer(lpnumber
);
274 #ifdef USE_GETPROTOBYNAME
275 if ((proto
= getprotobyname("tcp")) == NULL
)
277 syslog(LOGOPTS
, "Cannot find protocol for TCP!\n");
280 if ((netfd
= socket(AF_INET
, SOCK_STREAM
, proto
->p_proto
)) < 0)
282 if ((netfd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_IP
)) < 0)
285 syslog(LOGOPTS
, "socket: %m\n");
288 if (setsockopt(netfd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof(one
)) < 0)
290 syslog(LOGOPTS
, "setsocketopt: %m\n");
293 netaddr
.sin_port
= htons(BASEPORT
+ lpnumber
- '0');
295 netaddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
297 ipret
= inet_pton(AF_INET
, bindaddr
, &netaddr
.sin_addr
.s_addr
);
299 syslog(LOGOPTS
, "inet_pton: %m\n");
301 } else if (ipret
== 0) {
302 syslog(LOGOPTS
, "inet_pton: invalid bind IP address\n");
306 memset(netaddr
.sin_zero
, 0, sizeof(netaddr
.sin_zero
));
307 if (bind(netfd
, (struct sockaddr
*) &netaddr
, sizeof(netaddr
)) < 0)
309 syslog(LOGOPTS
, "bind: %m\n");
312 if (listen(netfd
, 5) < 0)
314 syslog(LOGOPTS
, "listen: %m\n");
317 clientlen
= sizeof(client
);
318 memset(&client
, 0, sizeof(client
));
319 while ((fd
= accept(netfd
, (struct sockaddr
*) &client
, &clientlen
)) >= 0)
322 if (hosts_ctl("p910nd", STRING_UNKNOWN
,
323 inet_ntoa(client
.sin_addr
), STRING_UNKNOWN
) == 0) {
324 syslog(LOGOPTS
, "Connection from %s port %hd rejected\n",
325 inet_ntoa(client
.sin_addr
),
326 ntohs(client
.sin_port
));
331 syslog(LOGOPTS
, "Connection from %s port %hd accepted\n",
332 inet_ntoa(client
.sin_addr
),
333 ntohs(client
.sin_port
));
334 /*write(fd, "Printing", 8);*/
335 if (copy_stream(fd
, f
) < 0)
336 syslog(LOGOPTS
, "copy_stream: %m\n");
339 syslog(LOGOPTS
, "accept: %m\n");
344 int is_standalone(void)
346 struct sockaddr_in bind_addr
;
350 * Check to see if a socket was passed to us from inetd.
352 * Use getsockname() to determine if descriptor 0 is indeed a socket
353 * (and thus we are probably a child of inetd) or if it is instead
354 * something else and we are running standalone.
356 ba_len
= sizeof(bind_addr
);
357 if (getsockname(0, (struct sockaddr
*) &bind_addr
, &ba_len
) == 0)
358 return (0); /* under inetd */
359 if (errno
!= ENOTSOCK
) /* strange... */
360 syslog(LOGOPTS
, "getsockname: %m\n");
364 int main(int argc
, char *argv
[])
369 if (argc
<= 0) /* in case not provided in inetd.conf */
374 if ((p
= strrchr(progname
, '/')) != 0)
378 while ((c
= getopt(argc
, argv
, "bi:f:v")) != EOF
)
403 if (isdigit(argv
[0][0]))
404 lpnumber
= argv
[0][0];
406 /* change the n in argv[0] to match the port so ps will show that */
407 if ((p
= strstr(progname
, "p910n")) != NULL
)
410 /* We used to pass (LOG_PERROR|LOG_PID|LOG_LPR|LOG_ERR) to syslog, but
411 * syslog ignored the LOG_PID and LOG_PERROR option. I.e. the intention
412 * was to add both options but the effect was to have neither.
413 * I disagree with the intention to add PERROR. --Stef */
414 openlog (p
, LOG_PID
, LOG_LPR
);