2 /* compile: gcc -Wall -o ud-fingerserv2 ud-fingerserv2.c */
9 #include <netinet/in.h>
11 #include <sys/types.h>
12 #include <sys/socket.h>
14 #define PROGNAME "ud-fingerserv"
15 #define VERSION "0.90"
16 #define PROGDATE "1999/12/10"
19 #define TIMEOUT 600 /* seconds */
20 #define PERROR(ctx) do { perror(ctx); exit(1); } while (0);
21 #define DEFAULTLOGFILE "/var/log/ud-fingerserv.log"
24 #define OPT_LOGSCR (1 << 1)
26 static FILE *g_logfs = NULL;
27 static char *g_logfn = NULL;
28 static int g_options = 0;
30 int processcxn(int, struct sockaddr_in *);
31 void logf(char *fmt, ...);
37 /* ********************************************************************** */
39 int processcxn(int s, struct sockaddr_in *rmtaddr)
41 printf("connected\n");
49 void logf(char *fmt, ...)
58 ts[strlen(ts)-1] = 0; /* remove stupid newline */
60 if (g_logfs == NULL) {
61 if (g_logfn == NULL) g_logfn = DEFAULTLOGFILE;
62 if ((g_logfs = fopen(g_logfn, "a")) == NULL && !(g_options & OPT_LOGSCR)) PERROR("logf");
65 vsnprintf(logline, sizeof(logline), fmt, ap);
67 fprintf(g_logfs, "[%s] " PROGNAME ": %s\n", ts, logline);
71 if (g_options & OPT_LOGSCR) printf("[%s] " PROGNAME ": %s\n", ts, logline);
76 if (g_logfs) fclose(g_logfs);
81 fprintf(stderr, "ud-fingerserv " VERSION " " PROGDATE "\n");
82 fprintf(stderr, "\t(c) 1999 Randolph Chung <tausq@debian.org>. Released under the GPL\n");
83 fprintf(stderr, "\tThe following options are recognized:\n");
84 fprintf(stderr, "\t\t-h : this help text\n");
85 fprintf(stderr, "\t\t-i : run in inetd mode; otherwise runs in standalone mode\n");
86 fprintf(stderr, "\t\t-v : logs messages to stdout in addition to log file\n");
87 fprintf(stderr, "\t\t-l <file> : use <file> as the log file, instead of " DEFAULTLOGFILE "\n");
91 int main(int argc, char *argv[])
95 struct sockaddr_in myaddr, rmtaddr;
96 socklen_t addrlen = sizeof(struct sockaddr_in);
100 while ((r = getopt(argc, argv, "hivl:")) > 0) {
102 case 'i': g_options |= OPT_INETD; break;
103 case 'v': g_options |= OPT_LOGSCR; break;
104 case 'l': g_logfn = strdup(optarg); break;
109 if (g_options & OPT_INETD) {
110 getsockname(fileno(stdin), &rmtaddr, &addrlen);
111 processcxn(fileno(stdin), &rmtaddr);
113 if ((ls = socket(AF_INET, SOCK_STREAM, 0)) < 0) PERROR("socket");
114 memset(&myaddr, 0, sizeof(myaddr));
115 myaddr.sin_family = AF_INET;
116 myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
117 myaddr.sin_port = htons(FINGERPORT);
118 if (bind(ls, &myaddr, sizeof(myaddr)) < 0) PERROR("bind");
119 if (listen(ls, SOMAXCONN) < 0) PERROR("listen");
120 logf("Waiting for connection");
121 while ((as = accept(ls, &rmtaddr, &addrlen))) {
122 if ((r = fork()) == 0) {
123 processcxn(as, &rmtaddr);
126 if (r < 0) PERROR("fork");