aboutsummaryrefslogtreecommitdiff
path: root/src/inetd-bi.c
blob: b771936045a66b4126f5552f7d5512ec47af7a62 (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* 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 */
static void
echo_stream (int fd, struct component const *comp)
{
  int rc;
  char buffer[INTBUFSIZE];

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

static void
echo_dg (int fd, struct component const *comp)
{
  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 */
static void
discard_stream (int fd, struct component const *comp)
{
  int rc;
  char buffer[INTBUFSIZE];

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

static void
discard_dg (int fd, struct component const *comp)
{
  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)

static 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));
}

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

static void
time_dg (int fd, struct component const *comp)
{
  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 */
static void
daytime_stream (int fd, struct component const *comp)
{
  char buffer[27];
  time_t now;

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

static void
daytime_dg (int fd, struct component const *comp)
{
  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;
    }
}

static void
chargen_stream (int fd, struct component const *comp)
{
  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;
    }
}

static void
chargen_dg (int fd, struct component const *comp)
{
  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;
}

static 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;
}

static void
qotd_stream (int fd, struct component const *comp)
{
  char text[QOTD_MAX];
  size_t len = qotd_read (text);
  write (fd, text, len);
}

static void
qotd_dg (int fd, struct component const *comp)
{
  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);
}  


/* TCPMUX service, RFC 1078 */
#define MAX_SERV_LEN    (256+2)

static int
fd_getline (int fd, char *buf, int len)
{
  int count = 0, n;

  do
    {
      n = read (fd, buf, len - count);
      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;
}

static int
tcpmux_help (struct component *comp, void *data)
{
  int *pfd = data;

  if (!(comp->flags & CF_DISABLED) && ISCF_TCPMUX (comp->flags))
    {
      fd_report (*pfd, comp->service);
      fd_report (*pfd, "\r\n");
    }
  return 0;
}

static void
tcpmux (int fd, struct component const *comp)
{
  char service[MAX_SERV_LEN + 1];
  size_t len;
  struct component *srv_comp;
  union pies_sockaddr_storage sa;
  socklen_t salen = sizeof (sa);
  int rc;
  
  /* Read service name */
  if ((len = fd_getline (fd, service, MAX_SERV_LEN)) < 0)
    {
      fd_report (fd, "-Error reading service name\r\n");
      return;
    }
  service[len] = 0;

  debug (2, ("tcpmux: someone wants %s", service));

  if (!strcasecmp (service, "help"))
    {
      progman_iterate_comp (tcpmux_help, &fd);
      return;
    }

  srv_comp = progman_lookup_tcpmux (service, comp->tag);
  if (!srv_comp)
    {
      fd_report (fd, "-Service not available\r\n");
      return;
    }

  rc = getpeername (fd, (struct sockaddr *) &sa, &salen);
  if (rc)
    logmsg (LOG_ERR, _("%s: cannot get peer name: %s"),
	    comp->tag, strerror (errno));
  
  if (comp->acl)
    {
      if (rc)
	{
	  fd_report (fd, "-Service not available\r\n");
	  return;
	}
      
      if (check_acl (comp->acl, (struct sockaddr *) &sa, salen))
	{
	  fd_report (fd, "-Service not available\r\n");
	  return;
	}
    }
  /* FIXME: What about max-instances, etc.? */
  
  if (srv_comp->flags & CF_TCPMUXPLUS)
    fd_report (fd, "+Go\r\n");
  
  progman_run_comp (srv_comp, fd, &sa, salen);
}


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},
    /* TCPMUX */
    {"tcpmux",  SOCK_STREAM, 0, 0, tcpmux},
    {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.