aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-04-06 17:43:29 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2010-04-06 17:43:29 +0200
commitf6916dbed4233ef0e202d1b299cb5a2e9287a696 (patch)
treea761598c5f3d6e935239d8631e7f984c4cd490d6
parentfcad7681f8a835f5c7b2093da505f46865a89d25 (diff)
downloadbusybox-w32-f6916dbed4233ef0e202d1b299cb5a2e9287a696.tar.gz
busybox-w32-f6916dbed4233ef0e202d1b299cb5a2e9287a696.tar.bz2
busybox-w32-f6916dbed4233ef0e202d1b299cb5a2e9287a696.zip
telnetd: fill hostname field in utmp/wtmp records
function old new delta get_lsa - 109 +109 make_new_session 438 504 +66 get_peer_lsa - 10 +10 ftpd_main 2340 2267 -73 get_sock_lsa 101 10 -91 ------------------------------------------------------------------------------ (add/remove: 2/0 grow/shrink: 1/2 up/down: 185/-164) Total: 21 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--include/libbb.h2
-rw-r--r--libbb/xconnect.c16
-rw-r--r--networking/ftpd.c15
-rw-r--r--networking/telnetd.c21
4 files changed, 30 insertions, 24 deletions
diff --git a/include/libbb.h b/include/libbb.h
index f3121eb47..357571fd8 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -530,6 +530,8 @@ int create_and_connect_stream_or_die(const char *peer, int port) FAST_FUNC;
530int xconnect_stream(const len_and_sockaddr *lsa) FAST_FUNC; 530int xconnect_stream(const len_and_sockaddr *lsa) FAST_FUNC;
531/* Get local address of bound or accepted socket */ 531/* Get local address of bound or accepted socket */
532len_and_sockaddr *get_sock_lsa(int fd) FAST_FUNC RETURNS_MALLOC; 532len_and_sockaddr *get_sock_lsa(int fd) FAST_FUNC RETURNS_MALLOC;
533/* Get remote address of connected or accepted socket */
534len_and_sockaddr *get_peer_lsa(int fd) FAST_FUNC RETURNS_MALLOC;
533/* Return malloc'ed len_and_sockaddr with socket address of host:port 535/* Return malloc'ed len_and_sockaddr with socket address of host:port
534 * Currently will return IPv4 or IPv6 sockaddrs only 536 * Currently will return IPv4 or IPv6 sockaddrs only
535 * (depending on host), but in theory nothing prevents e.g. 537 * (depending on host), but in theory nothing prevents e.g.
diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index d8c8d02d5..c3ee633e4 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -47,25 +47,35 @@ int FAST_FUNC setsockopt_bindtodevice(int fd UNUSED_PARAM,
47} 47}
48#endif 48#endif
49 49
50len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd) 50static len_and_sockaddr* get_lsa(int fd, int (*get_name)(int fd, struct sockaddr *addr, socklen_t *addrlen))
51{ 51{
52 len_and_sockaddr lsa; 52 len_and_sockaddr lsa;
53 len_and_sockaddr *lsa_ptr; 53 len_and_sockaddr *lsa_ptr;
54 54
55 lsa.len = LSA_SIZEOF_SA; 55 lsa.len = LSA_SIZEOF_SA;
56 if (getsockname(fd, &lsa.u.sa, &lsa.len) != 0) 56 if (get_name(fd, &lsa.u.sa, &lsa.len) != 0)
57 return NULL; 57 return NULL;
58 58
59 lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len); 59 lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len);
60 if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */ 60 if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */
61 lsa_ptr->len = lsa.len; 61 lsa_ptr->len = lsa.len;
62 getsockname(fd, &lsa_ptr->u.sa, &lsa_ptr->len); 62 get_name(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
63 } else { 63 } else {
64 memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len); 64 memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
65 } 65 }
66 return lsa_ptr; 66 return lsa_ptr;
67} 67}
68 68
69len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
70{
71 return get_lsa(fd, getsockname);
72}
73
74len_and_sockaddr* FAST_FUNC get_peer_lsa(int fd)
75{
76 return get_lsa(fd, getpeername);
77}
78
69void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen) 79void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
70{ 80{
71 if (connect(s, s_addr, addrlen) < 0) { 81 if (connect(s, s_addr, addrlen) < 0) {
diff --git a/networking/ftpd.c b/networking/ftpd.c
index 375cc0ca5..9d43ea3a2 100644
--- a/networking/ftpd.c
+++ b/networking/ftpd.c
@@ -461,21 +461,6 @@ handle_epsv(void)
461 free(response); 461 free(response);
462} 462}
463 463
464/* libbb candidate */
465static
466len_and_sockaddr* get_peer_lsa(int fd)
467{
468 len_and_sockaddr *lsa;
469 socklen_t len = 0;
470
471 if (getpeername(fd, NULL, &len) != 0)
472 return NULL;
473 lsa = xzalloc(LSA_LEN_SIZE + len);
474 lsa->len = len;
475 getpeername(fd, &lsa->u.sa, &lsa->len);
476 return lsa;
477}
478
479static void 464static void
480handle_port(void) 465handle_port(void)
481{ 466{
diff --git a/networking/telnetd.c b/networking/telnetd.c
index b3e66eb4b..a8c86b62f 100644
--- a/networking/telnetd.c
+++ b/networking/telnetd.c
@@ -226,6 +226,9 @@ make_new_session(
226 IF_FEATURE_TELNETD_STANDALONE(int sock) 226 IF_FEATURE_TELNETD_STANDALONE(int sock)
227 IF_NOT_FEATURE_TELNETD_STANDALONE(void) 227 IF_NOT_FEATURE_TELNETD_STANDALONE(void)
228) { 228) {
229#if !ENABLE_FEATURE_TELNETD_STANDALONE
230 enum { sock = 0 );
231#endif
229 const char *login_argv[2]; 232 const char *login_argv[2];
230 struct termios termbuf; 233 struct termios termbuf;
231 int fd, pid; 234 int fd, pid;
@@ -243,9 +246,9 @@ make_new_session(
243 ndelay_on(fd); 246 ndelay_on(fd);
244 close_on_exec_on(fd); 247 close_on_exec_on(fd);
245 248
246#if ENABLE_FEATURE_TELNETD_STANDALONE
247 /* SO_KEEPALIVE by popular demand */ 249 /* SO_KEEPALIVE by popular demand */
248 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1)); 250 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
251#if ENABLE_FEATURE_TELNETD_STANDALONE
249 ts->sockfd_read = sock; 252 ts->sockfd_read = sock;
250 ndelay_on(sock); 253 ndelay_on(sock);
251 if (sock == 0) { /* We are called with fd 0 - we are in inetd mode */ 254 if (sock == 0) { /* We are called with fd 0 - we are in inetd mode */
@@ -256,8 +259,6 @@ make_new_session(
256 if (sock > G.maxfd) 259 if (sock > G.maxfd)
257 G.maxfd = sock; 260 G.maxfd = sock;
258#else 261#else
259 /* SO_KEEPALIVE by popular demand */
260 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
261 /* ts->sockfd_read = 0; - done by xzalloc */ 262 /* ts->sockfd_read = 0; - done by xzalloc */
262 ts->sockfd_write = 1; 263 ts->sockfd_write = 1;
263 ndelay_on(0); 264 ndelay_on(0);
@@ -313,6 +314,17 @@ make_new_session(
313 /* Restore default signal handling ASAP */ 314 /* Restore default signal handling ASAP */
314 bb_signals((1 << SIGCHLD) + (1 << SIGPIPE), SIG_DFL); 315 bb_signals((1 << SIGCHLD) + (1 << SIGPIPE), SIG_DFL);
315 316
317 if (ENABLE_FEATURE_UTMP) {
318 len_and_sockaddr *lsa = get_peer_lsa(sock);
319 char *hostname = NULL;
320 if (lsa) {
321 hostname = xmalloc_sockaddr2dotted(&lsa->u.sa);
322 free(lsa);
323 }
324 write_new_utmp(pid, LOGIN_PROCESS, tty_name, /*username:*/ "LOGIN", hostname);
325 free(hostname);
326 }
327
316 /* Make new session and process group */ 328 /* Make new session and process group */
317 setsid(); 329 setsid();
318 330
@@ -326,9 +338,6 @@ make_new_session(
326 pid = getpid(); 338 pid = getpid();
327 tcsetpgrp(0, pid); /* switch this tty's process group to us */ 339 tcsetpgrp(0, pid); /* switch this tty's process group to us */
328 340
329//TODO: fetch remote addr via getpeername (see ftpd.c)
330 write_new_utmp(pid, LOGIN_PROCESS, tty_name, /*username:*/ "LOGIN", /*hostname:*/ NULL);
331
332 /* The pseudo-terminal allocated to the client is configured to operate 341 /* The pseudo-terminal allocated to the client is configured to operate
333 * in cooked mode, and with XTABS CRMOD enabled (see tty(4)) */ 342 * in cooked mode, and with XTABS CRMOD enabled (see tty(4)) */
334 tcgetattr(0, &termbuf); 343 tcgetattr(0, &termbuf);