aboutsummaryrefslogtreecommitdiff
path: root/src/inetd-bi.c
blob: f86f3734e12c7814f2dfe7354092b9f639794c53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* This file is part of GNU Pies.
   Copyright (C) 2009 Sergey Poznyakoff

   GNU Pies is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Pies is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Pies.  If not, see <http://www.gnu.org/licenses/>. */

#include "pies.h"
#include <netdb.h>

#define INTBUFSIZE	8192

/* Echo protocol, RFC 862 */
void
echo_stream (int fd)
{
  int rc;
  char buffer[INTBUFSIZE];

  while ((rc = read (fd, buffer, sizeof buffer)) > 0
	 && write (fd, buffer, rc) > 0)
    ;
}

void
echo_dg (int fd)
{
  int rc;
  char buffer[INTBUFSIZE];
  struct sockaddr sa;
  socklen_t size = sizeof sa;
  
  rc = recvfrom (fd, buffer, sizeof buffer, 0, &sa, &size);
  if (rc < 0)
    return;
  sendto (fd, buffer, rc, 0, &sa, sizeof sa);
}

/* Discard protocol, RFC 863 */
void
discard_stream (int fd)
{
  int rc;
  char buffer[INTBUFSIZE];

  while (1)
    {
      while ((rc = read (fd, buffer, sizeof buffer)) > 0)
	;
      if (rc == 0 || errno != EINTR)
	break;
    }
}

void
discard_dg (int fd)
{
  char buffer[INTBUFSIZE];
  read (fd, buffer, sizeof buffer);
}


/* Time Protocol, RFC 868 */

/* Return a machine readable date and time as seconds since
   midnight, Jan 1, 1900. */

#define SEVENTY_YEARS ((unsigned long)25567 * 24 * 60 * 60)

unsigned long
time_since_1900 (void)
{
  struct timeval tv;

  if (gettimeofday (&tv, NULL) < 0)
    {
      logmsg (LOG_ERR, "gettimeofday: %s", strerror (errno));
      return 0;
    }
  return htonl ((long) (tv.tv_sec + SEVENTY_YEARS));
}

void
time_stream (int fd)
{
  unsigned long result = time_since_1900 ();
  write (fd, (char *) &result, sizeof result);
}

void
time_dg (int fd)
{
  unsigned long result;
  struct sockaddr sa;
  socklen_t size = sizeof sa;
  
  if (recvfrom (fd, (char *) &result, sizeof result, 0, &sa, &size) < 0)
    return;
  result = time_since_1900 ();
  sendto (fd, (char *) &result, sizeof result, 0, &sa, sizeof sa);
}

/* Daytime Protocol, RFC 867 */
void
daytime_stream (int fd)
{
  char buffer[27];
  time_t now;

  time (&now);
  sprintf (buffer, "%.24s\r\n", ctime (&now));
  write (fd, buffer, strlen (buffer));
}

void
daytime_dg (int fd)
{
  char buffer[27];
  time_t now;
  struct sockaddr sa;
  socklen_t size = sizeof sa;

  time (&now);

  if (recvfrom (fd, buffer, sizeof buffer, 0, &sa, &size) < 0)
    return;
  sprintf (buffer, "%.24s\r\n", ctime (&now));
  sendto (fd, buffer, strlen (buffer), 0, &sa, sizeof sa);
}

/* Character Generator Protocol, RFC 864 */

#define LINESIZ 72

static void
chargen_next_line (char *text)
{
  static int ch = 0;
  int i, c;
  
  do
    ch = (ch + 1) % 128;
  while (!c_isprint (ch));
  
  for (i = 0, c = ch; i < LINESIZ; )
    {
      if (c_isprint (c))
	text[i++] = c;
      c = (c + 1) % 128;
    }
}

void
chargen_stream (int fd)
{
  char text[LINESIZ + 2];

  text[LINESIZ] = '\r';
  text[LINESIZ + 1] = '\n';
  
  while (1)
    {
      chargen_next_line (text);
      if (write (fd, text, sizeof text) != sizeof text)
	break;
    }
}

void
chargen_dg (int fd)
{
  struct sockaddr sa;
  socklen_t size = sizeof sa;
  char text[LINESIZ + 2];

  if (recvfrom (fd, text, sizeof text, 0, &sa, &size) < 0)
    return;
  text[LINESIZ] = '\r';
  text[LINESIZ + 1] = '\n';
  chargen_next_line (text);
  sendto (fd, text, sizeof text, 0, &sa, sizeof sa);
}  


/* Quote of the Day Protocol, RFC 865 */

#define QOTD_MAX 512
#define QOTD_DEF "Quote of the Day\r\n"

static size_t
trnl (char *text, size_t size)
{
  size_t off = size;
  
  for (; off > 0; off--)
    if (text[off] == '\n')
      {
	if (text[off-1] == '\r')
	  off--;
	else
	  {
	    if (size == QOTD_MAX)
	      size--;
	    memmove (text + off + 1, text + off, size - off);
	    text[off] = '\r';
	    size++;
	  }
      }
  return size;
}

size_t
qotd_read (char *text)
{
  ssize_t rc;
  int fd = open (qotdfile, O_RDONLY);
  if (fd == -1)
    {
      logmsg (LOG_ERR, _("cannot open %s: %s"), qotdfile, strerror (errno));
      strncpy (text, QOTD_DEF, QOTD_MAX);
      rc = QOTD_MAX;
    }
  else
    {    
      rc = read (fd, text, QOTD_MAX);
      if (rc >= 0)
	rc = trnl (text, rc);
      else
	{
	  logmsg (LOG_ERR, _("error reading %s: %s"),
		  qotdfile, strerror (errno));
	  strncpy (text, QOTD_DEF, QOTD_MAX);
	  rc = QOTD_MAX;
	}
    }
  close (fd);
  return rc;
}

void
qotd_stream (int fd)
{
  char text[QOTD_MAX];
  size_t len = qotd_read (text);
  write (fd, text, len);
}

void
qotd_dg (int fd)
{
  char text[QOTD_MAX];
  struct sockaddr sa;
  socklen_t size = sizeof sa;
  size_t len;
  if (recvfrom (fd, text, sizeof text, 0, &sa, &size) < 0)
    return;
  len = qotd_read (text);
  sendto (fd, text, len, 0, &sa, sizeof sa);
}  


struct inetd_builtin inetd_builtin_tab[] = {
    /* Echo received data */
    {"echo",    SOCK_STREAM, 0, 0, echo_stream},
    {"echo",    SOCK_DGRAM,  1, 0, echo_dg},
    /* Internet /dev/null */
    {"discard", SOCK_STREAM, 0, 0, discard_stream},
    {"discard", SOCK_DGRAM,  1, 0, discard_dg},
    /* Return 32 bit time since 1900 */
    {"time",    SOCK_STREAM, 1, 0, time_stream},
    {"time",    SOCK_DGRAM,  1, 0, time_dg},
    /* Return human-readable time */
    {"daytime", SOCK_STREAM, 1, 0, daytime_stream},
    {"daytime", SOCK_DGRAM,  1, 0, daytime_dg},
    /* Character generator */
    {"chargen", SOCK_STREAM, 0, 0, chargen_stream},
    {"chargen", SOCK_DGRAM,  1, 0, chargen_dg},
    /* Quote of the Day */
    {"qotd",    SOCK_STREAM, 0, 0, qotd_stream},
    {"qotd",    SOCK_DGRAM,  1, 0, qotd_dg},
#if 0    
    {"tcpmux", SOCK_STREAM,  0, 0, tcpmux},
#endif
    {NULL, 0, 0, 0, NULL}
};

struct inetd_builtin *
inetd_builtin_lookup (const char *service, int socktype)
{
  struct inetd_builtin *bp;

  for (bp = inetd_builtin_tab; bp->service; bp++)
    if (bp->socktype == socktype && strcmp (bp->service, service) == 0)
      return bp;
  return NULL;
}

Return to:

Send suggestions and report system problems to the System administrator.