-
Notifications
You must be signed in to change notification settings - Fork 0
/
listner.c
271 lines (226 loc) · 6.72 KB
/
listner.c
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
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <md5.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#define IDENT_PORT 113
#define NOBODY 32767
char *pname;
const int STARS_SHINE = 1;
static void usage()
{
fprintf(stderr, "usage: %s [ -m message] [-s system] [-t timeout]\n", pname);
fprintf(stderr, "\
\t-m\tSpecify ident message (username). If -m is not specified, a\n\
\t\treadable random username will be generated for each response.\n\
\t-s\tSpecify system name, default: \"UNIX\".\n\
\t-t\tSpecify connection timeout in seconds, default: 15.\n\n");
exit(1);
}
int main(int argc, char *argv[])
{
int serv_sock, client_sock, cli_len;
struct sockaddr_in cli_addr, serv_addr;
fd_set infds, testfds;
int nfds = 0, minfds = 0, maxfds = 0, nready;
struct timeval tv = { 15, 0 };
char inbuf[BUFSIZ], outbuf[BUFSIZ * 2], *inptr;
size_t inlen;
int ch;
extern char *optarg;
extern int optind;
int daemonize = 1;
char * sysname = "UNIX";
int randomUser = 1;
char * message[64];
char digest[64] = "This is not really a random name, but plays one on TV.";
int msgLen = strlen(digest);
time_t now;
MD5_CTX ctx;
pname = argv[0];
while ((ch = getopt(argc, argv, "dm:s:t:")) != -1) {
switch (ch) {
case 'd': /* Undocumented -d flag for debugging */
daemonize = 0;
break;
case 'm':
strncpy((char *) message, optarg, 64);
randomUser = 0;
break;
case 's':
sysname = optarg;
break;
case 't':
tv.tv_sec = atoi(optarg);
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
openlog(pname, LOG_AUTH, LOG_CONS);
/*
* * Create a TCP socket and bind it to the ident port.
* */
serv_sock = socket(AF_INET, SOCK_STREAM, 0);
if (serv_sock < 0) {
syslog(LOG_ERR, "creating server socket: %m");
return -1;
}
setsockopt(serv_sock, SOL_SOCKET, SO_REUSEADDR, &serv_sock, sizeof serv_sock);
bzero((char *) &serv_addr, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(IDENT_PORT);
if (bind(serv_sock, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) {
syslog(LOG_ERR, "binding server socket to ident port: %m");
return -1;
}
listen(serv_sock, 5);
/*
* * Create a pid file for use in stopping this thing.
* */
if ((client_sock = open("/var/run/liedentd.pid", O_WRONLY | O_CREAT, 0644)) < 0) {
syslog(LOG_ERR, "creating pid file /var/run/liedentd.pid: %m");
return -2;
}
/*
* * Drop our root privileges so we are not an attack vector.
* */
if (daemonize) {
setuid(NOBODY);
chdir("/tmp");
daemon(1, 0);
}
/*
* * Finish writing the pid file now, after the daemon call, so the
* * pid will be correct.
* */
asprintf(&optarg, "%d\n", getpid());
write(client_sock, optarg, strlen(optarg));
free(optarg);
close(client_sock);
/*
* * Setup for selecting I/O.
* */
FD_ZERO(&infds);
FD_SET(serv_sock, &infds);
maxfds = serv_sock;
while (STARS_SHINE){
testfds = infds;
/*
* * Wait for something to happen, or for timeout.
* */
if ((nready = select(maxfds + 1, &testfds, NULL, NULL, &tv)) == 0) {
/*
* * The select timed out. Close/kill any outstanding connections
* * and try again. If we haven't received any connections yet,
* * minfds will be zero and we can skip this.
* */
for (client_sock = minfds; client_sock && client_sock <= maxfds; client_sock++) {
/*
* * Don't zap the listen socket! Ordinarily this shouldn't
* * happen, since serv_sock is created before any potential
* * version of client_sock, but careful is better than
* * crashed.
* */
if (client_sock == serv_sock)
continue;
if (FD_ISSET(client_sock, &infds))
FD_CLR(client_sock, &infds);
close(client_sock);
}
}
continue;
} else if (nready > 0) {
/*
* * We have nready sockets ready for processing. First check to
* * see if we have a connection attempt to fulfill.
* */
if (FD_ISSET(serv_sock, &testfds)) {
/*
* * Accept the connection and add to the current set.
* */
cli_len = sizeof (cli_addr);
bzero((char *) &cli_addr, sizeof (cli_addr));
client_sock = accept(serv_sock, (struct sockaddr *) &cli_addr, &cli_len);
if (client_sock < 0) {
syslog(LOG_WARNING, "accepting connection on server socket: %m");
continue;
}
FD_SET(client_sock, &infds);
maxfds = MAX(maxfds, client_sock);
minfds = (minfds == 0) ? client_sock : MIN(minfds, client_sock);
nready--;
}
/*
* * Roll through the remaining sockets in the fdset, answering each
* * that has a request available.
* */
for (client_sock = minfds; nready && client_sock <= maxfds; client_sock++) {
if (FD_ISSET(client_sock, &testfds)) {
if ((inlen = read(client_sock, inbuf, BUFSIZ - 1)) < 0){
syslog(LOG_WARNING, "reading from client: %m");
} else {
inbuf[inlen] = '\0';
inptr = strchr(inbuf, '\r');
if (inptr == NULL){
syslog(LOG_WARNING, "invalid client request: %s", inbuf);
} else {
*inptr = '\0';
/*
* Randomize the "name" string unless the user specified one.
*/
if (randomUser){
MD5Init(&ctx);
MD5Update(&ctx, digest, msgLen);
MD5End(&ctx, (char *) message);
/*
* Seed the next "random" name. This just sort of relies
* on message being preallocated to at least sizeof time_t
* + MD5_DIGEST_LENGTH (or 20 bytes) in length.
*/
now = time(0);
bcopy(&now, digest, sizeof now);
msgLen = strlen((char *) message);
bcopy(message, digest + sizeof now, msgLen);
msgLen += sizeof now;
}
snprintf(outbuf, 2 * BUFSIZ, "%s : USERID : %s : %s\r\n", inbuf, sysname, message);
if (write(client_sock, outbuf, strlen(outbuf)) < strlen(outbuf))
{
syslog(LOG_WARNING, "writing response to client: %m");
}
}
}
close(client_sock);
FD_CLR(client_sock, &infds);
nready--;
}
}
}
else
{
/*
* * The select returned an error. I wonder what that means?
* */
syslog(LOG_ERR, "select failed: %m");
}
}
/*
* * Not normally reached, but those stars may go out some day...
* */
syslog(LOG_CRIT, "exiting");
closelog();
close(serv_sock);
return 1;
}