1999-07-22 21:11:40 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1983, 1991, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2002-06-21 11:52:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1999-07-22 21:11:40 +00:00
|
|
|
#include <sys/filio.h>
|
|
|
|
#include <sys/ioccom.h>
|
1999-07-22 14:11:26 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/ucred.h>
|
|
|
|
#include <sys/uio.h>
|
1999-07-22 21:11:40 +00:00
|
|
|
#include <sys/utsname.h>
|
1999-07-22 14:11:26 +00:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2000-11-25 04:13:05 +00:00
|
|
|
#include <fcntl.h>
|
1999-07-22 14:11:26 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <signal.h>
|
1999-07-22 21:11:40 +00:00
|
|
|
#include <stdlib.h>
|
1999-07-22 14:11:26 +00:00
|
|
|
#include <string.h>
|
1999-07-23 15:26:42 +00:00
|
|
|
#include <sysexits.h>
|
|
|
|
#include <syslog.h>
|
1999-07-22 14:11:26 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "inetd.h"
|
|
|
|
|
2002-06-21 11:42:37 +00:00
|
|
|
void chargen_dg(int, struct servtab *);
|
|
|
|
void chargen_stream(int, struct servtab *);
|
|
|
|
void daytime_dg(int, struct servtab *);
|
|
|
|
void daytime_stream(int, struct servtab *);
|
|
|
|
void discard_dg(int, struct servtab *);
|
|
|
|
void discard_stream(int, struct servtab *);
|
|
|
|
void echo_dg(int, struct servtab *);
|
|
|
|
void echo_stream(int, struct servtab *);
|
|
|
|
static int getline(int, char *, int);
|
|
|
|
void iderror(int, int, int, const char *);
|
|
|
|
void ident_stream(int, struct servtab *);
|
|
|
|
void initring(void);
|
2006-04-17 18:35:58 +00:00
|
|
|
uint32_t machtime(void);
|
2002-06-21 11:42:37 +00:00
|
|
|
void machtime_dg(int, struct servtab *);
|
|
|
|
void machtime_stream(int, struct servtab *);
|
1999-07-22 14:11:26 +00:00
|
|
|
|
|
|
|
char ring[128];
|
|
|
|
char *endring;
|
|
|
|
|
|
|
|
|
|
|
|
struct biltin biltins[] = {
|
|
|
|
/* Echo received data */
|
|
|
|
{ "echo", SOCK_STREAM, 1, -1, echo_stream },
|
|
|
|
{ "echo", SOCK_DGRAM, 0, 1, echo_dg },
|
|
|
|
|
|
|
|
/* Internet /dev/null */
|
|
|
|
{ "discard", SOCK_STREAM, 1, -1, discard_stream },
|
|
|
|
{ "discard", SOCK_DGRAM, 0, 1, discard_dg },
|
|
|
|
|
2001-06-04 11:47:08 +00:00
|
|
|
/* Return 32 bit time since 1900 */
|
1999-07-22 14:11:26 +00:00
|
|
|
{ "time", SOCK_STREAM, 0, -1, machtime_stream },
|
|
|
|
{ "time", SOCK_DGRAM, 0, 1, machtime_dg },
|
|
|
|
|
|
|
|
/* Return human-readable time */
|
|
|
|
{ "daytime", SOCK_STREAM, 0, -1, daytime_stream },
|
|
|
|
{ "daytime", SOCK_DGRAM, 0, 1, daytime_dg },
|
|
|
|
|
|
|
|
/* Familiar character generator */
|
|
|
|
{ "chargen", SOCK_STREAM, 1, -1, chargen_stream },
|
|
|
|
{ "chargen", SOCK_DGRAM, 0, 1, chargen_dg },
|
|
|
|
|
2001-06-24 09:20:07 +00:00
|
|
|
{ "tcpmux", SOCK_STREAM, 1, -1, (bi_fn_t *)tcpmux },
|
1999-07-22 14:11:26 +00:00
|
|
|
|
|
|
|
{ "auth", SOCK_STREAM, 1, -1, ident_stream },
|
|
|
|
|
2001-06-24 09:20:07 +00:00
|
|
|
{ NULL, 0, 0, 0, NULL }
|
1999-07-22 14:11:26 +00:00
|
|
|
};
|
|
|
|
|
1999-07-24 13:02:09 +00:00
|
|
|
/*
|
|
|
|
* RFC864 Character Generator Protocol. Generates character data without
|
|
|
|
* any regard for input.
|
|
|
|
*/
|
|
|
|
|
1999-07-22 14:11:26 +00:00
|
|
|
void
|
2002-06-21 11:18:42 +00:00
|
|
|
initring(void)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
endring = ring;
|
|
|
|
|
|
|
|
for (i = 0; i <= 128; ++i)
|
|
|
|
if (isprint(i))
|
|
|
|
*endring++ = i;
|
|
|
|
}
|
|
|
|
|
2002-06-21 11:24:21 +00:00
|
|
|
/* Character generator */
|
1999-07-22 14:11:26 +00:00
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2002-06-21 11:24:21 +00:00
|
|
|
chargen_dg(int s, struct servtab *sep)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
2000-01-25 14:52:10 +00:00
|
|
|
struct sockaddr_storage ss;
|
1999-07-22 14:11:26 +00:00
|
|
|
static char *rs;
|
2000-03-09 15:07:38 +00:00
|
|
|
int len;
|
|
|
|
socklen_t size;
|
1999-07-22 14:11:26 +00:00
|
|
|
char text[LINESIZ+2];
|
|
|
|
|
|
|
|
if (endring == 0) {
|
|
|
|
initring();
|
|
|
|
rs = ring;
|
|
|
|
}
|
|
|
|
|
2000-01-25 14:52:10 +00:00
|
|
|
size = sizeof(ss);
|
1999-07-22 14:11:26 +00:00
|
|
|
if (recvfrom(s, text, sizeof(text), 0,
|
2000-01-25 14:52:10 +00:00
|
|
|
(struct sockaddr *)&ss, &size) < 0)
|
1999-07-22 14:11:26 +00:00
|
|
|
return;
|
|
|
|
|
2000-01-25 14:52:10 +00:00
|
|
|
if (check_loop((struct sockaddr *)&ss, sep))
|
1999-07-22 14:11:26 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if ((len = endring - rs) >= LINESIZ)
|
|
|
|
memmove(text, rs, LINESIZ);
|
|
|
|
else {
|
|
|
|
memmove(text, rs, len);
|
|
|
|
memmove(text + len, ring, LINESIZ - len);
|
|
|
|
}
|
|
|
|
if (++rs == endring)
|
|
|
|
rs = ring;
|
|
|
|
text[LINESIZ] = '\r';
|
|
|
|
text[LINESIZ + 1] = '\n';
|
2000-03-09 15:07:38 +00:00
|
|
|
(void) sendto(s, text, sizeof(text), 0, (struct sockaddr *)&ss, size);
|
1999-07-22 14:11:26 +00:00
|
|
|
}
|
|
|
|
|
2002-06-21 11:24:21 +00:00
|
|
|
/* Character generator */
|
1999-07-22 14:11:26 +00:00
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2002-06-21 11:24:21 +00:00
|
|
|
chargen_stream(int s, struct servtab *sep)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char *rs, text[LINESIZ+2];
|
|
|
|
|
|
|
|
inetd_setproctitle(sep->se_service, s);
|
|
|
|
|
|
|
|
if (!endring) {
|
|
|
|
initring();
|
|
|
|
rs = ring;
|
|
|
|
}
|
|
|
|
|
|
|
|
text[LINESIZ] = '\r';
|
|
|
|
text[LINESIZ + 1] = '\n';
|
|
|
|
for (rs = ring;;) {
|
|
|
|
if ((len = endring - rs) >= LINESIZ)
|
|
|
|
memmove(text, rs, LINESIZ);
|
|
|
|
else {
|
|
|
|
memmove(text, rs, len);
|
|
|
|
memmove(text + len, ring, LINESIZ - len);
|
|
|
|
}
|
|
|
|
if (++rs == endring)
|
|
|
|
rs = ring;
|
|
|
|
if (write(s, text, sizeof(text)) != sizeof(text))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1999-07-24 13:02:09 +00:00
|
|
|
/*
|
|
|
|
* RFC867 Daytime Protocol. Sends the current date and time as an ascii
|
|
|
|
* character string without any regard for input.
|
|
|
|
*/
|
|
|
|
|
2002-06-21 11:24:21 +00:00
|
|
|
/* Return human-readable time of day */
|
1999-07-22 14:11:26 +00:00
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2002-06-21 11:24:21 +00:00
|
|
|
daytime_dg(int s, struct servtab *sep)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
|
|
|
char buffer[256];
|
2001-06-24 09:20:07 +00:00
|
|
|
time_t now;
|
2000-01-25 14:52:10 +00:00
|
|
|
struct sockaddr_storage ss;
|
2000-03-09 15:07:38 +00:00
|
|
|
socklen_t size;
|
1999-07-22 14:11:26 +00:00
|
|
|
|
2001-06-24 09:20:07 +00:00
|
|
|
now = time((time_t *) 0);
|
1999-07-22 14:11:26 +00:00
|
|
|
|
2000-01-25 14:52:10 +00:00
|
|
|
size = sizeof(ss);
|
1999-07-22 14:11:26 +00:00
|
|
|
if (recvfrom(s, buffer, sizeof(buffer), 0,
|
2000-01-25 14:52:10 +00:00
|
|
|
(struct sockaddr *)&ss, &size) < 0)
|
1999-07-22 14:11:26 +00:00
|
|
|
return;
|
|
|
|
|
2000-01-25 14:52:10 +00:00
|
|
|
if (check_loop((struct sockaddr *)&ss, sep))
|
1999-07-22 14:11:26 +00:00
|
|
|
return;
|
|
|
|
|
2001-06-24 09:20:07 +00:00
|
|
|
(void) sprintf(buffer, "%.24s\r\n", ctime(&now));
|
1999-07-22 14:11:26 +00:00
|
|
|
(void) sendto(s, buffer, strlen(buffer), 0,
|
2000-03-09 15:07:38 +00:00
|
|
|
(struct sockaddr *)&ss, size);
|
1999-07-22 14:11:26 +00:00
|
|
|
}
|
|
|
|
|
2002-06-21 11:24:21 +00:00
|
|
|
/* Return human-readable time of day */
|
1999-07-22 14:11:26 +00:00
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2002-06-21 11:24:21 +00:00
|
|
|
daytime_stream(int s, struct servtab *sep __unused)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
|
|
|
char buffer[256];
|
2001-06-24 09:20:07 +00:00
|
|
|
time_t now;
|
1999-07-22 14:11:26 +00:00
|
|
|
|
2001-06-24 09:20:07 +00:00
|
|
|
now = time((time_t *) 0);
|
1999-07-22 14:11:26 +00:00
|
|
|
|
2001-06-24 09:20:07 +00:00
|
|
|
(void) sprintf(buffer, "%.24s\r\n", ctime(&now));
|
2000-03-28 09:45:19 +00:00
|
|
|
(void) send(s, buffer, strlen(buffer), MSG_EOF);
|
1999-07-22 14:11:26 +00:00
|
|
|
}
|
|
|
|
|
1999-07-24 13:02:09 +00:00
|
|
|
/*
|
|
|
|
* RFC863 Discard Protocol. Any data received is thrown away and no response
|
|
|
|
* is sent.
|
|
|
|
*/
|
|
|
|
|
2002-06-21 11:24:21 +00:00
|
|
|
/* Discard service -- ignore data */
|
1999-07-22 14:11:26 +00:00
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2002-06-21 11:40:03 +00:00
|
|
|
discard_dg(int s, struct servtab *sep __unused)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
|
|
|
char buffer[BUFSIZE];
|
|
|
|
|
|
|
|
(void) read(s, buffer, sizeof(buffer));
|
|
|
|
}
|
|
|
|
|
2002-06-21 11:24:21 +00:00
|
|
|
/* Discard service -- ignore data */
|
1999-07-22 14:11:26 +00:00
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2002-06-21 11:24:21 +00:00
|
|
|
discard_stream(int s, struct servtab *sep)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char buffer[BUFSIZE];
|
|
|
|
|
|
|
|
inetd_setproctitle(sep->se_service, s);
|
|
|
|
while (1) {
|
|
|
|
while ((ret = read(s, buffer, sizeof(buffer))) > 0)
|
|
|
|
;
|
|
|
|
if (ret == 0 || errno != EINTR)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1999-07-24 13:02:09 +00:00
|
|
|
/*
|
|
|
|
* RFC862 Echo Protocol. Any data received is sent back to the sender as
|
|
|
|
* received.
|
|
|
|
*/
|
|
|
|
|
2002-06-21 11:24:21 +00:00
|
|
|
/* Echo service -- echo data back */
|
1999-07-22 14:11:26 +00:00
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2002-06-21 11:24:21 +00:00
|
|
|
echo_dg(int s, struct servtab *sep)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
2001-05-26 14:40:39 +00:00
|
|
|
char buffer[65536]; /* Should be sizeof(max datagram). */
|
2000-03-09 15:07:38 +00:00
|
|
|
int i;
|
|
|
|
socklen_t size;
|
2000-01-25 14:52:10 +00:00
|
|
|
struct sockaddr_storage ss;
|
1999-07-22 14:11:26 +00:00
|
|
|
|
2000-01-25 14:52:10 +00:00
|
|
|
size = sizeof(ss);
|
1999-07-22 14:11:26 +00:00
|
|
|
if ((i = recvfrom(s, buffer, sizeof(buffer), 0,
|
2000-01-25 14:52:10 +00:00
|
|
|
(struct sockaddr *)&ss, &size)) < 0)
|
1999-07-22 14:11:26 +00:00
|
|
|
return;
|
|
|
|
|
2000-01-25 14:52:10 +00:00
|
|
|
if (check_loop((struct sockaddr *)&ss, sep))
|
1999-07-22 14:11:26 +00:00
|
|
|
return;
|
|
|
|
|
2000-03-09 15:07:38 +00:00
|
|
|
(void) sendto(s, buffer, i, 0, (struct sockaddr *)&ss, size);
|
1999-07-22 14:11:26 +00:00
|
|
|
}
|
|
|
|
|
2002-06-21 11:24:21 +00:00
|
|
|
/* Echo service -- echo data back */
|
1999-07-22 14:11:26 +00:00
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2002-06-21 11:24:21 +00:00
|
|
|
echo_stream(int s, struct servtab *sep)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
|
|
|
char buffer[BUFSIZE];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
inetd_setproctitle(sep->se_service, s);
|
|
|
|
while ((i = read(s, buffer, sizeof(buffer))) > 0 &&
|
|
|
|
write(s, buffer, i) > 0)
|
|
|
|
;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1999-07-24 13:02:09 +00:00
|
|
|
/*
|
|
|
|
* RFC1413 Identification Protocol. Given a TCP port number pair, return a
|
|
|
|
* character string which identifies the owner of that connection on the
|
1999-07-24 17:06:05 +00:00
|
|
|
* server's system. Extended to allow for ~/.fakeid support and ~/.noident
|
|
|
|
* support.
|
|
|
|
*/
|
|
|
|
|
2001-06-04 11:43:29 +00:00
|
|
|
/* RFC 1413 says the following are the only errors you can return. */
|
|
|
|
#define ID_INVALID "INVALID-PORT" /* Port number improperly specified. */
|
|
|
|
#define ID_NOUSER "NO-USER" /* Port not in use/not identifable. */
|
|
|
|
#define ID_HIDDEN "HIDDEN-USER" /* Hiden at user's request. */
|
|
|
|
#define ID_UNKNOWN "UNKNOWN-ERROR" /* Everything else. */
|
|
|
|
|
2002-06-21 11:24:21 +00:00
|
|
|
/* Generic ident_stream error-sending func */
|
1999-07-22 14:11:26 +00:00
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2002-06-21 11:24:21 +00:00
|
|
|
iderror(int lport, int fport, int s, const char *er)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
1999-07-22 21:11:40 +00:00
|
|
|
char *p;
|
|
|
|
|
2001-06-04 11:43:29 +00:00
|
|
|
asprintf(&p, "%d , %d : ERROR : %s\r\n", lport, fport, er);
|
1999-07-23 15:26:42 +00:00
|
|
|
if (p == NULL) {
|
1999-07-24 17:06:05 +00:00
|
|
|
syslog(LOG_ERR, "asprintf: %m");
|
1999-07-23 15:26:42 +00:00
|
|
|
exit(EX_OSERR);
|
|
|
|
}
|
2000-03-28 09:45:19 +00:00
|
|
|
send(s, p, strlen(p), MSG_EOF);
|
1999-07-22 21:11:40 +00:00
|
|
|
free(p);
|
1999-07-22 14:11:26 +00:00
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2002-06-21 11:24:21 +00:00
|
|
|
/* Ident service (AKA "auth") */
|
1999-07-22 14:11:26 +00:00
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2002-06-21 11:24:21 +00:00
|
|
|
ident_stream(int s, struct servtab *sep)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
1999-07-25 23:15:03 +00:00
|
|
|
struct utsname un;
|
|
|
|
struct stat sb;
|
2001-06-24 09:20:07 +00:00
|
|
|
struct sockaddr_in sin4[2];
|
2000-03-11 11:28:08 +00:00
|
|
|
#ifdef INET6
|
2000-01-25 14:52:10 +00:00
|
|
|
struct sockaddr_in6 sin6[2];
|
2000-03-11 11:28:08 +00:00
|
|
|
#endif
|
2000-01-25 14:52:10 +00:00
|
|
|
struct sockaddr_storage ss[2];
|
2001-02-18 13:30:20 +00:00
|
|
|
struct xucred uc;
|
1999-07-22 21:11:40 +00:00
|
|
|
struct timeval tv = {
|
|
|
|
10,
|
|
|
|
0
|
2000-07-12 20:49:06 +00:00
|
|
|
}, to;
|
2000-01-19 22:03:12 +00:00
|
|
|
struct passwd *pw = NULL;
|
1999-07-22 21:11:40 +00:00
|
|
|
fd_set fdset;
|
2001-06-04 11:43:29 +00:00
|
|
|
char buf[BUFSIZE], *p, **av, *osname = NULL, e;
|
|
|
|
char idbuf[MAXLOGNAME] = ""; /* Big enough to hold uid in decimal. */
|
2000-05-30 22:51:05 +00:00
|
|
|
socklen_t socklen;
|
|
|
|
ssize_t ssize;
|
2000-07-12 20:49:06 +00:00
|
|
|
size_t size, bufsiz;
|
2001-06-04 11:43:29 +00:00
|
|
|
int c, fflag = 0, nflag = 0, rflag = 0, argc = 0;
|
|
|
|
int gflag = 0, iflag = 0, Fflag = 0, getcredfail = 0, onreadlen;
|
1999-07-22 14:11:26 +00:00
|
|
|
u_short lport, fport;
|
|
|
|
|
|
|
|
inetd_setproctitle(sep->se_service, s);
|
1999-07-26 07:57:35 +00:00
|
|
|
/*
|
|
|
|
* Reset getopt() since we are a fork() but not an exec() from
|
|
|
|
* a parent which used getopt() already.
|
|
|
|
*/
|
1999-07-22 14:11:26 +00:00
|
|
|
optind = 1;
|
|
|
|
optreset = 1;
|
1999-07-26 07:57:35 +00:00
|
|
|
/*
|
|
|
|
* Take the internal argument vector and count it out to make an
|
|
|
|
* argument count for getopt. This can be used for any internal
|
|
|
|
* service to read arguments and use getopt() easily.
|
|
|
|
*/
|
1999-07-22 14:11:26 +00:00
|
|
|
for (av = sep->se_argv; *av; av++)
|
|
|
|
argc++;
|
|
|
|
if (argc) {
|
1999-07-24 16:24:03 +00:00
|
|
|
int sec, usec;
|
2000-01-19 22:03:12 +00:00
|
|
|
size_t i;
|
2001-06-24 09:20:07 +00:00
|
|
|
u_int32_t rnd32;
|
1999-07-24 16:24:03 +00:00
|
|
|
|
2001-06-04 11:43:29 +00:00
|
|
|
while ((c = getopt(argc, sep->se_argv, "d:fFgino:rt:")) != -1)
|
1999-07-22 14:11:26 +00:00
|
|
|
switch (c) {
|
2000-01-19 22:03:12 +00:00
|
|
|
case 'd':
|
2001-06-04 11:43:29 +00:00
|
|
|
if (!gflag)
|
|
|
|
strlcpy(idbuf, optarg, sizeof(idbuf));
|
2000-01-19 22:03:12 +00:00
|
|
|
break;
|
1999-07-22 14:11:26 +00:00
|
|
|
case 'f':
|
|
|
|
fflag = 1;
|
|
|
|
break;
|
2000-12-05 13:56:01 +00:00
|
|
|
case 'F':
|
|
|
|
fflag = 1;
|
|
|
|
Fflag=1;
|
|
|
|
break;
|
2000-01-19 22:03:12 +00:00
|
|
|
case 'g':
|
|
|
|
gflag = 1;
|
2001-06-24 09:20:07 +00:00
|
|
|
rnd32 = 0; /* Shush, compiler. */
|
2000-01-20 01:49:41 +00:00
|
|
|
/*
|
2001-06-24 09:20:07 +00:00
|
|
|
* The number of bits in "rnd32" divided
|
2000-01-20 01:49:41 +00:00
|
|
|
* by the number of bits needed per iteration
|
|
|
|
* gives a more optimal way to reload the
|
|
|
|
* random number only when necessary.
|
|
|
|
*
|
2002-07-22 13:58:58 +00:00
|
|
|
* 32 bits from arc4random corresponds to
|
2001-06-04 11:43:29 +00:00
|
|
|
* about 6 base-36 digits, so we reseed evey 6.
|
2000-01-20 01:49:41 +00:00
|
|
|
*/
|
2001-06-04 11:43:29 +00:00
|
|
|
for (i = 0; i < sizeof(idbuf) - 1; i++) {
|
|
|
|
static const char *const base36 =
|
2000-01-20 01:49:41 +00:00
|
|
|
"0123456789"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz";
|
2001-06-04 11:43:29 +00:00
|
|
|
if (i % 6 == 0)
|
2001-06-24 09:20:07 +00:00
|
|
|
rnd32 = arc4random();
|
|
|
|
idbuf[i] = base36[rnd32 % 36];
|
|
|
|
rnd32 /= 36;
|
2000-01-19 22:03:12 +00:00
|
|
|
}
|
2001-06-04 11:43:29 +00:00
|
|
|
idbuf[i] = '\0';
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
iflag = 1;
|
2000-01-19 22:03:12 +00:00
|
|
|
break;
|
1999-07-24 17:06:05 +00:00
|
|
|
case 'n':
|
|
|
|
nflag = 1;
|
1999-07-22 14:11:26 +00:00
|
|
|
break;
|
1999-07-22 21:11:40 +00:00
|
|
|
case 'o':
|
|
|
|
osname = optarg;
|
|
|
|
break;
|
1999-07-24 17:06:05 +00:00
|
|
|
case 'r':
|
|
|
|
rflag = 1;
|
|
|
|
break;
|
1999-07-24 12:35:50 +00:00
|
|
|
case 't':
|
1999-07-23 15:26:42 +00:00
|
|
|
switch (sscanf(optarg, "%d.%d", &sec, &usec)) {
|
|
|
|
case 2:
|
|
|
|
tv.tv_usec = usec;
|
2001-06-04 11:43:29 +00:00
|
|
|
/* FALLTHROUGH */
|
1999-07-23 15:26:42 +00:00
|
|
|
case 1:
|
|
|
|
tv.tv_sec = sec;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (debug)
|
|
|
|
warnx("bad -t argument");
|
|
|
|
break;
|
|
|
|
}
|
1999-07-24 16:24:03 +00:00
|
|
|
break;
|
1999-07-22 14:11:26 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-07-22 21:11:40 +00:00
|
|
|
if (osname == NULL) {
|
1999-07-23 15:49:14 +00:00
|
|
|
if (uname(&un) == -1)
|
2001-06-04 11:43:29 +00:00
|
|
|
iderror(0, 0, s, ID_UNKNOWN);
|
1999-07-22 21:11:40 +00:00
|
|
|
osname = un.sysname;
|
|
|
|
}
|
2001-06-04 11:43:29 +00:00
|
|
|
|
1999-07-26 07:57:35 +00:00
|
|
|
/*
|
|
|
|
* We're going to prepare for and execute reception of a
|
|
|
|
* packet of data from the user. The data is in the format
|
|
|
|
* "local_port , foreign_port\r\n" (with local being the
|
|
|
|
* server's port and foreign being the client's.)
|
|
|
|
*/
|
2000-07-12 20:49:06 +00:00
|
|
|
gettimeofday(&to, NULL);
|
|
|
|
to.tv_sec += tv.tv_sec;
|
2000-12-02 21:18:11 +00:00
|
|
|
to.tv_usec += tv.tv_usec;
|
|
|
|
if (to.tv_usec >= 1000000) {
|
2000-07-12 20:49:06 +00:00
|
|
|
to.tv_usec -= 1000000;
|
|
|
|
to.tv_sec++;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
bufsiz = sizeof(buf) - 1;
|
1999-07-22 21:11:40 +00:00
|
|
|
FD_ZERO(&fdset);
|
2001-03-28 13:41:19 +00:00
|
|
|
while (bufsiz > 0) {
|
2000-07-12 20:49:06 +00:00
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
tv.tv_sec = to.tv_sec - tv.tv_sec;
|
|
|
|
tv.tv_usec = to.tv_usec - tv.tv_usec;
|
|
|
|
if (tv.tv_usec < 0) {
|
|
|
|
tv.tv_usec += 1000000;
|
|
|
|
tv.tv_sec--;
|
|
|
|
}
|
|
|
|
if (tv.tv_sec < 0)
|
|
|
|
break;
|
|
|
|
FD_SET(s, &fdset);
|
|
|
|
if (select(s + 1, &fdset, NULL, NULL, &tv) == -1)
|
2001-06-04 11:43:29 +00:00
|
|
|
iderror(0, 0, s, ID_UNKNOWN);
|
2000-07-12 20:49:06 +00:00
|
|
|
if (ioctl(s, FIONREAD, &onreadlen) == -1)
|
2001-06-04 11:43:29 +00:00
|
|
|
iderror(0, 0, s, ID_UNKNOWN);
|
2001-06-24 09:20:07 +00:00
|
|
|
if ((size_t)onreadlen > bufsiz)
|
2000-07-12 20:49:06 +00:00
|
|
|
onreadlen = bufsiz;
|
|
|
|
ssize = read(s, &buf[size], (size_t)onreadlen);
|
|
|
|
if (ssize == -1)
|
2001-06-04 11:43:29 +00:00
|
|
|
iderror(0, 0, s, ID_UNKNOWN);
|
2000-10-02 12:04:17 +00:00
|
|
|
else if (ssize == 0)
|
|
|
|
break;
|
2000-07-12 20:49:06 +00:00
|
|
|
bufsiz -= ssize;
|
|
|
|
size += ssize;
|
2001-03-28 13:41:19 +00:00
|
|
|
if (memchr(&buf[size - ssize], '\n', ssize) != NULL)
|
|
|
|
break;
|
2000-07-12 20:49:06 +00:00
|
|
|
}
|
|
|
|
buf[size] = '\0';
|
|
|
|
/* Read two characters, and check for a delimiting character */
|
|
|
|
if (sscanf(buf, "%hu , %hu%c", &lport, &fport, &e) != 3 || isdigit(e))
|
2001-06-04 11:43:29 +00:00
|
|
|
iderror(0, 0, s, ID_INVALID);
|
|
|
|
|
|
|
|
/* Send garbage? */
|
|
|
|
if (gflag)
|
2000-01-19 22:03:12 +00:00
|
|
|
goto printit;
|
2001-06-04 11:43:29 +00:00
|
|
|
|
2000-03-28 01:10:35 +00:00
|
|
|
/*
|
|
|
|
* If not "real" (-r), send a HIDDEN-USER error for everything.
|
|
|
|
* If -d is used to set a fallback username, this is used to
|
|
|
|
* override it, and the fallback is returned instead.
|
|
|
|
*/
|
|
|
|
if (!rflag) {
|
2001-06-04 11:43:29 +00:00
|
|
|
if (*idbuf == '\0')
|
|
|
|
iderror(lport, fport, s, ID_HIDDEN);
|
|
|
|
goto printit;
|
2000-03-28 01:10:35 +00:00
|
|
|
}
|
2001-06-04 11:43:29 +00:00
|
|
|
|
1999-07-26 07:57:35 +00:00
|
|
|
/*
|
|
|
|
* We take the input and construct an array of two sockaddr_ins
|
|
|
|
* which contain the local address information and foreign
|
|
|
|
* address information, respectively, used to look up the
|
|
|
|
* credentials for the socket (which are returned by the
|
2001-06-04 11:43:29 +00:00
|
|
|
* sysctl "net.inet.tcp.getcred" when we call it.)
|
1999-07-26 07:57:35 +00:00
|
|
|
*/
|
2001-06-04 11:43:29 +00:00
|
|
|
socklen = sizeof(ss[0]);
|
|
|
|
if (getsockname(s, (struct sockaddr *)&ss[0], &socklen) == -1)
|
|
|
|
iderror(lport, fport, s, ID_UNKNOWN);
|
|
|
|
socklen = sizeof(ss[1]);
|
|
|
|
if (getpeername(s, (struct sockaddr *)&ss[1], &socklen) == -1)
|
|
|
|
iderror(lport, fport, s, ID_UNKNOWN);
|
2000-01-25 14:52:10 +00:00
|
|
|
if (ss[0].ss_family != ss[1].ss_family)
|
2001-06-04 11:43:29 +00:00
|
|
|
iderror(lport, fport, s, ID_UNKNOWN);
|
2000-05-30 22:51:05 +00:00
|
|
|
size = sizeof(uc);
|
2000-01-25 14:52:10 +00:00
|
|
|
switch (ss[0].ss_family) {
|
|
|
|
case AF_INET:
|
2001-06-24 09:20:07 +00:00
|
|
|
sin4[0] = *(struct sockaddr_in *)&ss[0];
|
|
|
|
sin4[0].sin_port = htons(lport);
|
|
|
|
sin4[1] = *(struct sockaddr_in *)&ss[1];
|
|
|
|
sin4[1].sin_port = htons(fport);
|
|
|
|
if (sysctlbyname("net.inet.tcp.getcred", &uc, &size, sin4,
|
|
|
|
sizeof(sin4)) == -1)
|
2000-12-02 21:18:11 +00:00
|
|
|
getcredfail = errno;
|
2000-01-25 14:52:10 +00:00
|
|
|
break;
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
sin6[0] = *(struct sockaddr_in6 *)&ss[0];
|
|
|
|
sin6[0].sin6_port = htons(lport);
|
|
|
|
sin6[1] = *(struct sockaddr_in6 *)&ss[1];
|
|
|
|
sin6[1].sin6_port = htons(fport);
|
2000-05-30 22:51:05 +00:00
|
|
|
if (sysctlbyname("net.inet6.tcp6.getcred", &uc, &size, sin6,
|
2000-01-25 14:52:10 +00:00
|
|
|
sizeof(sin6)) == -1)
|
2000-12-02 21:18:11 +00:00
|
|
|
getcredfail = errno;
|
2000-01-25 14:52:10 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default: /* should not reach here */
|
2000-12-02 21:18:11 +00:00
|
|
|
getcredfail = EAFNOSUPPORT;
|
2000-01-25 14:52:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-02-27 04:45:37 +00:00
|
|
|
if (getcredfail != 0 || uc.cr_version != XUCRED_VERSION) {
|
2001-06-04 11:43:29 +00:00
|
|
|
if (*idbuf == '\0')
|
|
|
|
iderror(lport, fport, s,
|
|
|
|
getcredfail == ENOENT ? ID_NOUSER : ID_UNKNOWN);
|
|
|
|
goto printit;
|
2000-01-19 22:03:12 +00:00
|
|
|
}
|
2001-06-04 11:43:29 +00:00
|
|
|
|
|
|
|
/* Look up the pw to get the username and home directory*/
|
|
|
|
errno = 0;
|
|
|
|
pw = getpwuid(uc.cr_uid);
|
|
|
|
if (pw == NULL)
|
|
|
|
iderror(lport, fport, s, errno == 0 ? ID_NOUSER : ID_UNKNOWN);
|
|
|
|
|
|
|
|
if (iflag)
|
|
|
|
snprintf(idbuf, sizeof(idbuf), "%u", (unsigned)pw->pw_uid);
|
|
|
|
else
|
|
|
|
strlcpy(idbuf, pw->pw_name, sizeof(idbuf));
|
|
|
|
|
1999-07-26 07:57:35 +00:00
|
|
|
/*
|
|
|
|
* If enabled, we check for a file named ".noident" in the user's
|
|
|
|
* home directory. If found, we return HIDDEN-USER.
|
|
|
|
*/
|
2001-06-04 11:43:29 +00:00
|
|
|
if (nflag) {
|
1999-07-25 23:15:03 +00:00
|
|
|
if (asprintf(&p, "%s/.noident", pw->pw_dir) == -1)
|
2001-06-04 11:43:29 +00:00
|
|
|
iderror(lport, fport, s, ID_UNKNOWN);
|
1999-07-25 23:15:03 +00:00
|
|
|
if (lstat(p, &sb) == 0) {
|
|
|
|
free(p);
|
2001-06-04 11:43:29 +00:00
|
|
|
iderror(lport, fport, s, ID_HIDDEN);
|
1999-07-24 17:06:05 +00:00
|
|
|
}
|
1999-07-25 23:15:03 +00:00
|
|
|
free(p);
|
1999-07-24 17:06:05 +00:00
|
|
|
}
|
2001-06-04 11:43:29 +00:00
|
|
|
|
1999-07-26 07:57:35 +00:00
|
|
|
/*
|
|
|
|
* Here, if enabled, we read a user's ".fakeid" file in their
|
|
|
|
* home directory. It consists of a line containing the name
|
|
|
|
* they want.
|
|
|
|
*/
|
2001-06-04 11:43:29 +00:00
|
|
|
if (fflag) {
|
2000-11-25 04:13:05 +00:00
|
|
|
int fakeid_fd;
|
1999-07-23 15:49:14 +00:00
|
|
|
|
1999-07-26 07:57:35 +00:00
|
|
|
/*
|
|
|
|
* Here we set ourself to effectively be the user, so we don't
|
|
|
|
* open any files we have no permission to open, especially
|
|
|
|
* symbolic links to sensitive root-owned files or devices.
|
|
|
|
*/
|
2000-11-25 04:13:05 +00:00
|
|
|
if (initgroups(pw->pw_name, pw->pw_gid) == -1)
|
2001-06-04 11:43:29 +00:00
|
|
|
iderror(lport, fport, s, ID_UNKNOWN);
|
|
|
|
if (seteuid(pw->pw_uid) == -1)
|
|
|
|
iderror(lport, fport, s, ID_UNKNOWN);
|
1999-07-26 07:57:35 +00:00
|
|
|
/*
|
2000-12-02 21:18:11 +00:00
|
|
|
* We can't stat() here since that would be a race
|
|
|
|
* condition.
|
1999-07-26 07:57:35 +00:00
|
|
|
* Therefore, we open the file we have permissions to open
|
|
|
|
* and if it's not a regular file, we close it and end up
|
|
|
|
* returning the user's real username.
|
|
|
|
*/
|
2001-06-04 11:43:29 +00:00
|
|
|
if (asprintf(&p, "%s/.fakeid", pw->pw_dir) == -1)
|
|
|
|
iderror(lport, fport, s, ID_UNKNOWN);
|
2000-11-25 04:13:05 +00:00
|
|
|
fakeid_fd = open(p, O_RDONLY | O_NONBLOCK);
|
1999-07-25 23:15:03 +00:00
|
|
|
free(p);
|
2001-06-04 11:43:29 +00:00
|
|
|
if (fakeid_fd == -1 || fstat(fakeid_fd, &sb) == -1 ||
|
|
|
|
!S_ISREG(sb.st_mode))
|
|
|
|
goto fakeid_fail;
|
|
|
|
|
|
|
|
if ((ssize = read(fakeid_fd, buf, sizeof(buf) - 1)) < 0)
|
|
|
|
goto fakeid_fail;
|
|
|
|
buf[ssize] = '\0';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Usually, the file will have the desired identity
|
|
|
|
* in the form "identity\n". Allow for leading white
|
|
|
|
* space and trailing white space/end of line.
|
|
|
|
*/
|
|
|
|
p = buf;
|
|
|
|
p += strspn(p, " \t");
|
|
|
|
p[strcspn(p, " \t\r\n")] = '\0';
|
|
|
|
if (strlen(p) > MAXLOGNAME - 1) /* Too long (including nul)? */
|
|
|
|
p[MAXLOGNAME - 1] = '\0';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the name is a zero-length string or matches it
|
|
|
|
* the id or name of another user (unless permitted by -F)
|
|
|
|
* then it is invalid.
|
|
|
|
*/
|
|
|
|
if (*p == '\0')
|
|
|
|
goto fakeid_fail;
|
|
|
|
if (!Fflag) {
|
|
|
|
if (iflag) {
|
|
|
|
if (p[strspn(p, "0123456789")] == '\0' &&
|
|
|
|
getpwuid(atoi(p)) != NULL)
|
|
|
|
goto fakeid_fail;
|
|
|
|
} else {
|
|
|
|
if (getpwnam(p) != NULL)
|
|
|
|
goto fakeid_fail;
|
2000-11-25 04:13:05 +00:00
|
|
|
}
|
2001-06-04 11:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
strlcpy(idbuf, p, sizeof(idbuf));
|
|
|
|
|
|
|
|
fakeid_fail:
|
|
|
|
if (fakeid_fd != -1)
|
2000-11-25 04:13:05 +00:00
|
|
|
close(fakeid_fd);
|
2001-06-04 11:43:29 +00:00
|
|
|
}
|
|
|
|
|
1999-07-22 14:11:26 +00:00
|
|
|
printit:
|
1999-07-26 07:57:35 +00:00
|
|
|
/* Finally, we make and send the reply. */
|
1999-07-22 21:11:40 +00:00
|
|
|
if (asprintf(&p, "%d , %d : USERID : %s : %s\r\n", lport, fport, osname,
|
2001-06-04 11:43:29 +00:00
|
|
|
idbuf) == -1) {
|
1999-07-24 17:06:05 +00:00
|
|
|
syslog(LOG_ERR, "asprintf: %m");
|
1999-07-24 12:35:50 +00:00
|
|
|
exit(EX_OSERR);
|
|
|
|
}
|
2000-03-28 09:45:19 +00:00
|
|
|
send(s, p, strlen(p), MSG_EOF);
|
1999-07-22 21:11:40 +00:00
|
|
|
free(p);
|
1999-07-22 14:11:26 +00:00
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-07-24 13:02:09 +00:00
|
|
|
* RFC738 Time Server.
|
1999-07-22 14:11:26 +00:00
|
|
|
* Return a machine readable date and time, in the form of the
|
|
|
|
* number of seconds since midnight, Jan 1, 1900. Since gettimeofday
|
|
|
|
* returns the number of seconds since midnight, Jan 1, 1970,
|
|
|
|
* we must add 2208988800 seconds to this figure to make up for
|
|
|
|
* some seventy years Bell Labs was asleep.
|
|
|
|
*/
|
|
|
|
|
2006-04-17 18:35:58 +00:00
|
|
|
uint32_t
|
2002-06-21 11:18:42 +00:00
|
|
|
machtime(void)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
if (gettimeofday(&tv, (struct timezone *)NULL) < 0) {
|
|
|
|
if (debug)
|
|
|
|
warnx("unable to get time of day");
|
|
|
|
return (0L);
|
|
|
|
}
|
2006-04-17 18:35:58 +00:00
|
|
|
#define OFFSET ((uint32_t)25567 * 24*60*60)
|
|
|
|
return (htonl((uint32_t)(tv.tv_sec + OFFSET)));
|
1999-07-22 14:11:26 +00:00
|
|
|
#undef OFFSET
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2002-06-21 11:18:42 +00:00
|
|
|
machtime_dg(int s, struct servtab *sep)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
2006-04-17 18:35:58 +00:00
|
|
|
uint32_t result;
|
2000-01-25 14:52:10 +00:00
|
|
|
struct sockaddr_storage ss;
|
2000-03-09 15:07:38 +00:00
|
|
|
socklen_t size;
|
1999-07-22 14:11:26 +00:00
|
|
|
|
2000-01-25 14:52:10 +00:00
|
|
|
size = sizeof(ss);
|
1999-07-22 14:11:26 +00:00
|
|
|
if (recvfrom(s, (char *)&result, sizeof(result), 0,
|
2000-01-25 14:52:10 +00:00
|
|
|
(struct sockaddr *)&ss, &size) < 0)
|
1999-07-22 14:11:26 +00:00
|
|
|
return;
|
|
|
|
|
2000-01-25 14:52:10 +00:00
|
|
|
if (check_loop((struct sockaddr *)&ss, sep))
|
1999-07-22 14:11:26 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
result = machtime();
|
|
|
|
(void) sendto(s, (char *) &result, sizeof(result), 0,
|
2000-03-09 15:07:38 +00:00
|
|
|
(struct sockaddr *)&ss, size);
|
1999-07-22 14:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2002-06-21 11:40:03 +00:00
|
|
|
machtime_stream(int s, struct servtab *sep __unused)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
2006-04-17 18:35:58 +00:00
|
|
|
uint32_t result;
|
1999-07-22 14:11:26 +00:00
|
|
|
|
|
|
|
result = machtime();
|
2000-03-28 09:45:19 +00:00
|
|
|
(void) send(s, (char *) &result, sizeof(result), MSG_EOF);
|
1999-07-22 14:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-07-24 13:02:09 +00:00
|
|
|
* RFC1078 TCP Port Service Multiplexer (TCPMUX). Service connections to
|
|
|
|
* services based on the service name sent.
|
|
|
|
*
|
1999-07-22 14:11:26 +00:00
|
|
|
* Based on TCPMUX.C by Mark K. Lottor November 1988
|
|
|
|
* sri-nic::ps:<mkl>tcpmux.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_SERV_LEN (256+2) /* 2 bytes for \r\n */
|
|
|
|
#define strwrite(fd, buf) (void) write(fd, buf, sizeof(buf)-1)
|
|
|
|
|
|
|
|
static int /* # of characters upto \r,\n or \0 */
|
2002-06-21 11:18:42 +00:00
|
|
|
getline(int fd, char *buf, int len)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
|
|
|
int count = 0, n;
|
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_handler = SIG_DFL;
|
|
|
|
sigaction(SIGALRM, &sa, (struct sigaction *)0);
|
|
|
|
do {
|
|
|
|
alarm(10);
|
|
|
|
n = read(fd, buf, len-count);
|
|
|
|
alarm(0);
|
|
|
|
if (n == 0)
|
|
|
|
return (count);
|
|
|
|
if (n < 0)
|
|
|
|
return (-1);
|
|
|
|
while (--n >= 0) {
|
|
|
|
if (*buf == '\r' || *buf == '\n' || *buf == '\0')
|
|
|
|
return (count);
|
|
|
|
count++;
|
|
|
|
buf++;
|
|
|
|
}
|
|
|
|
} while (count < len);
|
|
|
|
return (count);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct servtab *
|
2002-06-21 11:18:42 +00:00
|
|
|
tcpmux(int s)
|
1999-07-22 14:11:26 +00:00
|
|
|
{
|
|
|
|
struct servtab *sep;
|
|
|
|
char service[MAX_SERV_LEN+1];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* Get requested service name */
|
|
|
|
if ((len = getline(s, service, MAX_SERV_LEN)) < 0) {
|
|
|
|
strwrite(s, "-Error reading service name\r\n");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
service[len] = '\0';
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
warnx("tcpmux: someone wants %s", service);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Help is a required command, and lists available services,
|
|
|
|
* one per line.
|
|
|
|
*/
|
|
|
|
if (!strcasecmp(service, "help")) {
|
|
|
|
for (sep = servtab; sep; sep = sep->se_next) {
|
|
|
|
if (!ISMUX(sep))
|
|
|
|
continue;
|
|
|
|
(void)write(s,sep->se_service,strlen(sep->se_service));
|
|
|
|
strwrite(s, "\r\n");
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try matching a service in inetd.conf with the request */
|
|
|
|
for (sep = servtab; sep; sep = sep->se_next) {
|
|
|
|
if (!ISMUX(sep))
|
|
|
|
continue;
|
|
|
|
if (!strcasecmp(service, sep->se_service)) {
|
|
|
|
if (ISMUXPLUS(sep)) {
|
|
|
|
strwrite(s, "+Go\r\n");
|
|
|
|
}
|
|
|
|
return (sep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strwrite(s, "-Service not available\r\n");
|
|
|
|
return (NULL);
|
|
|
|
}
|