1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 #include <sys/socket.h>
7 #include <netinet/tcp.h>
12 #include <sys/ioctl.h>
19 #include <arpa/inet.h>
20 #include <netinet/in.h>
24 #include <libcfs/portals_utils.h>
25 #include <portals/api-support.h>
26 #include <portals/lib-types.h>
27 #include <portals/socknal.h>
29 /* should get this from autoconf somehow */
31 #define PIDFILE_DIR "/var/run"
34 char progname[] = "acceptor";
35 char name_port[40]; /* for signal handler */
38 /* needed because libwrap declares these as externs */
39 int allow_severity = LOG_INFO;
40 int deny_severity = LOG_WARNING;
43 void usage(char *progname)
45 fprintf(stderr, "usage: %s [-N nal_id] [-p] [-l] port\n\n"
46 " -l\tKeep stdin/stdout open\n"
47 " -p\tAllow connections from non-privileged ports\n", progname);
51 void errlog(int level, const char *fmt, ...)
67 fprintf(out, "%s: ", name_port);
68 vfprintf(out, fmt, arg);
71 vsyslog(level, fmt, arg);
75 char *pidfile_name(char *name_port)
77 static char pidfile[1024];
79 snprintf(pidfile, sizeof(pidfile), "%s/%s.pid", PIDFILE_DIR, name_port);
84 void pidfile_create(char *name_port)
86 char *pidfile = pidfile_name(name_port);
89 if ((fp = fopen(pidfile, "w"))) {
90 fprintf(fp, "%d\n", getpid());
93 errlog(LOG_ERR, " error creating %s: %s\n",
94 pidfile, strerror(errno));
98 int pidfile_cleanup(char *name_port)
100 char *pidfile = pidfile_name(name_port);
103 rc = unlink(pidfile);
104 if (rc && errno != -ENOENT)
105 fprintf(stderr, "%s: error removing %s: %s\n",
106 progname, pidfile, strerror(errno));
111 int pidfile_exists(char *name_port)
113 char *pidfile = pidfile_name(name_port);
117 fpid = fopen(pidfile, "r+");
122 fprintf(stderr, "%s: error opening %s: %s.\n",
123 progname, pidfile, strerror(errno));
127 rc = fscanf(fpid, "%i", &pid);
130 fprintf(stderr,"%s: %s didn't contain a valid pid, removing.\n",
135 if (kill(pid, 0) == 0) {
136 fprintf(stderr, "%s: %s exists, acceptor pid %d running.\n",
137 progname, pidfile, pid);
141 fprintf(stderr, "%s: stale %s exists, pid %d doesn't, removing.\n",
142 progname, pidfile, pid);
144 pidfile_cleanup(name_port);
148 void handler(int sig)
150 pidfile_cleanup(name_port);
154 void show_connection(int fd, __u32 net_ip)
156 static long last_time;
157 static __u32 host_ip;
163 /* Don't show repeats for same host, it adds no value */
164 if (host_ip == ntohl(net_ip) && (now - last_time) < 5)
167 h = gethostbyaddr((char *)&net_ip, sizeof(net_ip), AF_INET);
169 host_ip = ntohl(net_ip);
172 snprintf(host, sizeof(host), "%d.%d.%d.%d",
173 (host_ip >> 24) & 0xff, (host_ip >> 16) & 0xff,
174 (host_ip >> 8) & 0xff, host_ip & 0xff);
176 snprintf(host, sizeof(host), "%s", h->h_name);
178 syslog(LOG_INFO, "accepted host: %s\n", host);
181 int main(int argc, char **argv)
183 int o, fd, rc, port, pfd;
184 struct sockaddr_in srvaddr;
189 int require_privports = 1;
191 while ((c = getopt (argc, argv, "N:lp")) != -1) {
194 if (sscanf(optarg, "%d", &nal) != 1 ||
195 nal < 0 || nal > NAL_MAX_NR)
202 require_privports = 0;
213 port = atol(argv[optind++]);
215 snprintf(name_port, sizeof(name_port) - 1, "%s-%d", progname, port);
216 if (pidfile_exists(name_port))
218 openlog(name_port, LOG_PID, LOG_DAEMON);
220 memset(&srvaddr, 0, sizeof(srvaddr));
221 srvaddr.sin_family = AF_INET;
222 srvaddr.sin_port = htons(port);
223 srvaddr.sin_addr.s_addr = INADDR_ANY;
225 fd = socket(PF_INET, SOCK_STREAM, 0);
228 errlog(LOG_ERR, "error opening socket: %s\n", strerror(errno));
233 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &o, sizeof(o))) {
235 errlog(LOG_ERR, "cannot set REUSEADDR socket opt: %s\n",
240 rc = bind(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
243 errlog(LOG_ERR, "error binding to socket: %s\n",
248 if (listen(fd, 127)) {
253 printf("listening on port %d\n", port);
255 pfd = open("/dev/portals", O_RDWR);
258 errlog(LOG_ERR, "opening portals device: %s\n",strerror(errno));
262 rc = daemon(0, noclose);
265 errlog(LOG_ERR, "error daemonizing: %s\n", strerror(errno));
269 signal(SIGHUP, SIG_IGN);
270 signal(SIGINT, handler);
271 signal(SIGQUIT, handler);
272 signal(SIGTERM, handler);
274 errlog(LOG_NOTICE, "started, listening on port %d\n", port);
275 pidfile_create(name_port);
278 struct sockaddr_in clntaddr;
279 int len = sizeof(clntaddr);
281 struct portal_ioctl_data data;
282 struct portals_cfg pcfg;
284 struct request_info request;
286 char addrstr[INET_ADDRSTRLEN];
288 cfd = accept(fd, (struct sockaddr *)&clntaddr, &len);
290 errlog(LOG_ERR, "error accepting connection: %s\n",
297 /* libwrap access control */
298 request_init(&request, RQ_DAEMON, "lustre", RQ_FILE, cfd, 0);
300 if (!hosts_access(&request)) {
301 inet_ntop(AF_INET, &clntaddr.sin_addr,
302 addrstr, INET_ADDRSTRLEN);
303 errlog(LOG_WARNING, "unauthorized access from %s:%hd\n",
304 addrstr, ntohs(clntaddr.sin_port));
310 if (require_privports &&
311 ntohs(clntaddr.sin_port) >= IPPORT_RESERVED) {
312 inet_ntop(AF_INET, &clntaddr.sin_addr,
313 addrstr, INET_ADDRSTRLEN);
315 "closing non-privileged connection from %s:%d\n",
316 addrstr, ntohs(clntaddr.sin_port));
319 perror ("close un-privileged client failed");
323 show_connection (cfd, clntaddr.sin_addr.s_addr);
325 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
328 pcfg.pcfg_misc = SOCKNAL_CONN_NONE; /* == incoming connection */
330 PORTAL_IOC_INIT(data);
331 data.ioc_pbuf1 = (char*)&pcfg;
332 data.ioc_plen1 = sizeof(pcfg);
334 if (ioctl(pfd, IOC_PORTAL_NAL_CMD, &data) < 0) {
336 "portals ioctl failed: %s\n", strerror(errno));
338 errlog(LOG_DEBUG, "client registered\n");
342 perror ("close failed");
346 pidfile_cleanup(name_port);