-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubot.c
351 lines (283 loc) · 7.47 KB
/
ubot.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
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
/* ubot - a very small and stupid irc bot
*
* Copyright (c) 2015 Joachim Nilsson <troglobit@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <arpa/inet.h> /* inet_pton() */
#include <errno.h>
#include <error.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h> /* close() */
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/tls1.h>
#include <openssl/err.h>
#define DBG(fmt, args...) if (debug) fprintf(stderr, fmt "\n", ##args)
#define BUFSZ 8192
#define SERVER "127.0.0.1"
#define PORT "6667"
#define CHANNEL "ubot"
#define NICK "ubot"
#define NAME "ubot rulez"
#define PARTMSG "ubot has left the building"
static SSL *ssl;
static SSL_CTX *ssl_ctx;
static int sd = -1;
static int debug = 0;
static int loop = 1;
static char *port = PORT;
static char *nick = NICK;
static char partmsg[42] = PARTMSG;
static char *channel = CHANNEL;
static char *pass = NULL;
extern char *__progname;
static int ssl_init(void)
{
SSL_library_init();
SSL_load_error_strings();
ssl_ctx = SSL_CTX_new(SSLv23_client_method());
if (!ssl_ctx)
return -1;
/* POODLE, only allow TLSv1.x or later */
SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
ssl = SSL_new(ssl_ctx);
if (!ssl)
return -1;
#ifdef SSL_MODE_SEND_FALLBACK_SCSV
SSL_set_mode(ssl, SSL_MODE_SEND_FALLBACK_SCSV);
#endif
return 0;
}
static int ssl_connect(int sd)
{
char buf[256];
X509 *cert;
SSL_set_fd(ssl, sd);
if (-1 == SSL_connect(ssl))
return -1;
DBG("SSL connection using %s", SSL_get_cipher(ssl));
/* Get server's certificate (note: beware of dynamic allocation) - opt */
cert = SSL_get_peer_certificate(ssl);
if (!cert)
return -1;
/* Logging some cert details. Please note: X509_NAME_oneline doesn't
work when giving NULL instead of a buffer. */
X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
DBG("SSL server cert subject: %s", buf);
X509_NAME_oneline(X509_get_issuer_name(cert), buf, sizeof(buf));
DBG("SSL server cert issuer: %s", buf);
/* We could do all sorts of certificate verification stuff here before
deallocating the certificate. */
X509_free(cert);
return 0;
}
static int do_connect(char *server, char *port)
{
int sd, ret;
struct addrinfo hints;
struct addrinfo *result, *rp;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
ret = getaddrinfo(server, port, &hints, &result);
if (ret)
return -1;
/* Iterate over all replys until we connect ... */
for (rp = result; rp; rp = rp->ai_next) {
sd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sd < 0)
continue;
if (!connect(sd, rp->ai_addr, rp->ai_addrlen))
break; /* Success */
close(sd);
}
if (result)
freeaddrinfo(result); /* No longer needed */
if (!rp) /* No address succeeded */
return -1;
if (ssl) {
ret = ssl_connect(sd);
if (ret < 0)
goto err;
}
return sd;
err:
close(sd);
return -1;
}
static char *chomp(char *str)
{
char *p;
if (!str || strlen(str) < 1) {
errno = EINVAL;
return NULL;
}
p = str + strlen(str) - 1;
while (*p == '\n' || *p == '\r')
*p-- = 0;
return str;
}
static int do_send(const char *fmt, ...)
{
int num;
char buf[256];
size_t len;
va_list ap;
va_start(ap, fmt);
len = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (ssl)
num = SSL_write(ssl, buf, len);
else
num = (int)write(sd, buf, len);
if (num <= 0)
return -1;
DBG(">> %s\n", chomp(buf));
return 0;
}
static ssize_t do_recv(char *buf, size_t len)
{
int num;
if (ssl)
num = SSL_read(ssl, buf, (int)len);
else
num = read(sd, buf, len);
if (num <= 0)
return -1;
buf[num] = 0;
chomp(buf);
DBG("<< %s", buf);
return 0;
}
static int bot(char *server, char *port)
{
char *buf;
char nickm[42];
sd = do_connect(server, port);
if (sd < 0)
error(1, errno, "Failed connecting to %s:%s", server, port);
buf = malloc(BUFSZ);
if (!buf)
goto exit;
if (pass)
do_send("PASS %s\r\n", pass);
do_send("NICK %s\r\n", nick);
do_send("USER %s 0 0 :%s\r\n", nick, NAME);
snprintf(nickm, sizeof(nickm), ":%s:", nick);
while (loop) {
if (!do_recv(buf, BUFSZ)) {
char *pos = strstr(buf, " ") + 1;
if (!pos)
continue;
if (strstr(buf, " 001 ")) /* Authenicated/registered */
do_send("JOIN #%s\r\n", channel);
if (!strncmp(buf, "PING ", 5)) /* Keepalive */
do_send("PONG %s\r\n", pos);
if (!strncmp(pos, "PRIVMSG ", 8) && strstr(pos, nickm)) {
pos = strchr(buf, '!');
if (pos)
*pos = 0;
do_send("PRIVMSG #%s %s: :Why hello there, stranger!\r\n", channel, &buf[1]);
}
}
}
free(buf);
exit:
return close(sd);
}
static int usage(int rc)
{
fprintf(stderr, "Usage: %s [OPTIONS] SERVER\n\n"
"Options:\n"
" -c, --channel=CHAN Join #CHAN on SERVER, default: " CHANNEL "\n"
" -d, --debug Enable debug messages\n"
" -h, --help This help text\n"
" -n, --nick=NICK Use NICK instead of default: " NICK "\n"
" --password=PWD Send PASS PWD to connect, use SSL!\n"
" -p, --port=PORT Connect to this port, default: 6667\n"
" -s, --ssl Connect using SSL/TLS\n"
" -v, --version Show version\n\n"
"Remember: The PWD will be visible in the ps listing for other users to see!\n", __progname);
return rc;
}
void sigint_cb(int signo __attribute__ ((unused)))
{
do_send("PART #%s :%s\r\n", channel, partmsg);
loop = 0;
}
int main(int argc, char *argv[])
{
int c;
char server[256] = SERVER;
struct option long_options[] = {
{ "channel", 1, NULL, 'c' },
{ "debug", 0, NULL, 'd' },
{ "help", 0, NULL, 'h' },
{ "nick", 1, NULL, 'n' },
{ "password", 1, NULL, 'l' },
{ "port", 1, NULL, 'p' },
{ "ssl", 0, NULL, 's' },
{ "version", 0, NULL, 'v' },
{ NULL, 0, NULL, 0 }
};
signal(SIGINT, sigint_cb);
signal(SIGTERM, sigint_cb);
while ((c = getopt_long(argc, argv, "c:dh?n:l:p:sv", long_options, NULL)) != EOF) {
switch(c) {
case 'c':
channel = strdup(optarg);
break;
case 'd':
debug = 1;
break;
case 'n':
nick = strdup(optarg);
break;
case 'l':
pass = strdup(optarg);
break;
case 'p':
port = strdup(optarg);
break;
case 'v':
return puts("v" VERSION) == EOF;
case 's':
if (ssl_init())
error(1, 0, "Failed creating SSL context, missing library?");
break;
case 'h':
case '?':
return usage(0);
}
}
if (optind < argc)
strncpy(server, argv[optind++], sizeof(server));
return bot(server, port);
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/