File: portmsg.c

package info (click to toggle)
pennmush 1.8.0p4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 8,068 kB
  • ctags: 6,319
  • sloc: ansic: 68,221; sh: 7,056; perl: 1,150; makefile: 284
file content (287 lines) | stat: -rw-r--r-- 7,416 bytes parent folder | download | duplicates (2)
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
/*
 *    portmsg - generate a message on a port, then close connection
 *
 *      Usage:  portmsg file port
 *
 *              When a telnet client connects to the specified port, the
 *              text from the file will be echoed to the user.  After a
 *              short delay the connection will close.
 *
 * Derived from ftpd by Klaas @ {RUD, LPSwat}, original ftpd copyright
 * message follows:
 *
 * Copyright (c) 1985, 1988, 1990 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the University of
 *      California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
 *
 * This version extensively modified by Javelin (Alan Schwartz)
 * to conform to PennMUSH autoconfiguration standards.
 */

/* If you have multiple IP addresses and want to bind to only one,
 * uncomment this:
 */
/*#define SINGLE_IP_ADDR "your.address.goes.here" */

#include "config.h"
#include <stdio.h>
#ifdef I_UNISTD
#include <unistd.h>
#endif
#include <limits.h>
#ifdef I_SYS_TYPES
#include <sys/types.h>
#endif
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <errno.h>
#ifdef I_SYS_ERRNO
#include <sys/errno.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/param.h>
#include <signal.h>
#ifdef I_SYS_WAIT
#include <sys/wait.h>
#endif
#include <fcntl.h>

#include "conf.h"
#include "externs.h"
#include "mymalloc.h"
#include "confmagic.h"

/* What htons expects */
typedef unsigned short Port_t;

#ifdef HAS_WAITPID
#define WAIT_TYPE int
#else
#ifdef UNION_WAIT
#define WAIT_TYPE union wait
#else
#define WAIT_TYPE int
#endif
#endif

static Signal_t wait_on_child(int sig);
static Signal_t lostconn(int sig);
static int how_many_fds(void);

static Signal_t
wait_on_child(int sig __attribute__ ((__unused__)))
{
  WAIT_TYPE status;

#ifdef HAS_WAITPID
  while (waitpid(-1, &status, WNOHANG) > 0) ;
#else
  while (wait3(&status, WNOHANG, 0) > 0) ;
#endif

#ifndef SIGNALS_KEPT
  signal(SIGCLD, (Sigfunc) wait_on_child);
#endif
#ifndef VOIDSIG
  return 0;
#endif
}

Signal_t
lostconn(int sig __attribute__ ((__unused__)))
{
  exit(1);
}

int
main(int argc, char **argv)
{
  int msgfd, fd, n;
  struct stat statBuf;
  Port_t port;
  char *msg;
  int sockfd, newsockfd;
  int addrlen;
  int opt;
  struct sockaddr_in tcp_srv_addr;
  struct sockaddr_in their_addr;
  int num_fds;

  if (argc != 3) {
    fprintf(stderr, "Usage: portmsg file port\n");
    exit(1);
  }
  port = atoi(argv[2]);
  if (port == 0) {
    fprintf(stderr, "error: bad port number [%s]\n", argv[2]);
    exit(1);
  }
  if ((msgfd = open(argv[1], O_RDONLY)) < 0) {
    fprintf(stderr, "error: cannot open message file [%s]\n", argv[1]);
    exit(1);
  }
  /* read the message */
  fstat(msgfd, &statBuf);
  if (statBuf.st_size <= 0) {
    fprintf(stderr, "error: message file [%s] is empty\n", argv[1]);
    exit(1);
  }
  msg = (char *) malloc(statBuf.st_size);
  if (read(msgfd, msg, statBuf.st_size) != statBuf.st_size) {
    fprintf(stderr, "error: cannot read message file [%s]\n", argv[1]);
    exit(1);
  }
  num_fds = how_many_fds();

  /* become a daemon */
  switch (fork()) {
  case -1:
    fprintf(stderr, "error: can't fork\n");
    exit(1);
  case 0:
    break;
  default:
    exit(0);
  }
#ifdef HAS_SETPGRP
#ifdef USE_BSD_SETPGRP
  if (setpgrp(0, getpid()) == -1) {
#else
  if (setpgrp() == -1) {
#endif
    fprintf(stderr, "error: can't change process group\n");
    exit(1);
  }
#endif

#ifdef USE_TIOCNOTTY
  if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
    ioctl(fd, TIOCNOTTY, NULL);
    close(fd);
  }
#endif

  signal(SIGCLD, (void *) wait_on_child);
  memset((char *) &tcp_srv_addr, 0, sizeof(tcp_srv_addr));
  tcp_srv_addr.sin_family = AF_INET;
#ifdef SINGLE_IP_ADDR
  tcp_srv_addr.sin_addr.s_addr = inet_addr(MUSH_IP_ADDR);
#else
  tcp_srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
#endif
  tcp_srv_addr.sin_port = htons(port);

  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "can't create stream socket\n");
    exit(-1);
  }
  opt = 1;
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
		 (char *) &opt, sizeof(opt)) < 0) {
    perror("setsockopt");
    exit(1);
  }
  if (bind(sockfd, (struct sockaddr *) &tcp_srv_addr, sizeof(tcp_srv_addr)) < 0) {
    fprintf(stderr, "can't bind local address\n");
    exit(-1);
  }
  listen(sockfd, 5);

main_again:
  addrlen = sizeof(their_addr);
  newsockfd = accept(sockfd, (struct sockaddr *) &their_addr, &addrlen);
  if (newsockfd < 0) {
    if (errno == EINTR)
      goto main_again;
    fprintf(stderr, "accept error\n");
    exit(-1);
  }
  switch (fork()) {
  case -1:
    fprintf(stderr, "server can't fork\n");
    exit(-1);
  case 0:
    dup2(newsockfd, 0);
    dup2(newsockfd, 1);
    for (n = 3; n < num_fds; n++)
      close(n);
    break;
  default:
    close(newsockfd);
    goto main_again;
  }

  /* daemon child arrives here */
  signal(SIGPIPE, lostconn);
  signal(SIGCLD, SIG_IGN);

  fprintf(stdout, msg);
  fflush(stdout);
  sleep(5);
  exit(0);
}

static int
how_many_fds(void)
{
  /* Determine how many open file descriptors we're allowed
   * In order, we'll try:
   * 0. OPEN_MAX constant - POSIX.1 limits.h
   * 1. sysconf(_SC_OPEN_MAX) - POSIX.1
   * 2. getdtablesize - BSD
   * 3. NOFILE - in some sys/param.h
   * 4. _NFILE - in some stdio.h
   */
#ifdef OPEN_MAX
  static int open_max = OPEN_MAX;
#else
  static int open_max = 0;
#endif

  if (open_max)
    return open_max;

#ifdef HAS_SYSCONF
  errno = 0;
  if ((open_max = sysconf(_SC_OPEN_MAX)) < 0) {
    if (errno == 0)		/* Value was indeterminate */
      open_max = 0;
  }
  if (open_max)
    return open_max;
#endif
  /* Caching getdtablesize is dangerous, since it's affected by
   * getrlimit, so we don't.
   */
  open_max = 0;
  return getdtablesize();
}