summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--blabouncer.c67
1 files changed, 33 insertions, 34 deletions
diff --git a/blabouncer.c b/blabouncer.c
index 751143d..c3eed3b 100644
--- a/blabouncer.c
+++ b/blabouncer.c
@@ -10,7 +10,6 @@
// - Keep track of changing user nicks/modes
// - Should relay log do more than PRIVMSGs?
// - Make certificate paths configurable
-// - Rename "ssl" to "arr_ssl"
// - Implement TLS on real IRCd server side
// - Make TLS optional
// - Make cert/key filenames configurable
@@ -112,7 +111,7 @@ int arrindex(int arr_clients[], int clientfd) {
}
// Send whatever string to a specific client by providing the FD
-int sendtoclient(int fd, char *str, int arr_clients[], int arr_authed[], SSL **ssl) {
+int sendtoclient(int fd, char *str, int arr_clients[], int arr_authed[], SSL **arr_ssl) {
appendcrlf(str); // Do this just before sending so callers don't need to worry about it
// Find the client in the clients array and make sure they are authenticated
@@ -128,7 +127,7 @@ int sendtoclient(int fd, char *str, int arr_clients[], int arr_authed[], SSL **s
printf("sendtoclient(): sending \"%s\" (length %zd) to client with fd %d.\n", str, strlen(str), fd);
// if (send(fd, str, strlen(str), 0) == -1) {
- if (SSL_write(ssl[arrindex(arr_clients, fd)], str, strlen(str)) == -1) {
+ if (SSL_write(arr_ssl[arrindex(arr_clients, fd)], str, strlen(str)) == -1) {
perror("error: sendtoclient() send()\n");
return 0;
}
@@ -161,7 +160,7 @@ int disconnectclient(int fd, int arr_clients[], int arr_authed[]) {
// "except" is used to send to all clients _except_ the fd provided (except = 0 (EXCEPT_NONE) avoids this, i.e. sends to all)
// "except" is really the "sourcefd" and is also used as part of the authentication check - this is messy and they should perhaps be two separate arguments.
// TODO - is passing str_len useful if we're appendcrlfing and then using strlen(str) in the send? I guess not... (As long as we're always null terminated in the correct place.)
-int sendtoallclients(int *clientsockfd, int fdmax, int arr_clients[], char *str, int except, int arr_authed[], SSL **ssl) {
+int sendtoallclients(int *clientsockfd, int fdmax, int arr_clients[], char *str, int except, int arr_authed[], SSL **arr_ssl) {
char *sendertype;
@@ -208,7 +207,7 @@ int sendtoallclients(int *clientsockfd, int fdmax, int arr_clients[], char *str,
}
printf("sendtoallclients(): %s: sending '%s' to client with fd %d.\n", sendertype, str, i);
// if (send(i, str, strlen(str), 0) == -1) {
- if (SSL_write(ssl[arrindex(arr_clients, i)], str, strlen(str)) == -1) {
+ if (SSL_write(arr_ssl[arrindex(arr_clients, i)], str, strlen(str)) == -1) {
perror("error: sendtoallclients() send()\n");
}
}
@@ -437,7 +436,7 @@ int removechannel(struct channel *channels, char *name) {
// Return 1 if we processed something and expect the caller to not need to do anything more
// Return 0 if we didn't process it and the caller might want to do something
//int processircmessage(int *serversockfd, int *clientsockfd, char *str, int source) {
-int processircmessage(int *serversockfd, int *clientsockfd, char *str, int source, int fdmax, int arr_clients[], int sourcefd, struct ircdstrings *ircdstrings, struct channel *channels, int arr_authed[], SSL **ssl) {
+int processircmessage(int *serversockfd, int *clientsockfd, char *str, int source, int fdmax, int arr_clients[], int sourcefd, struct ircdstrings *ircdstrings, struct channel *channels, int arr_authed[], SSL **arr_ssl) {
// Track which space-separated token within this response we're on
int counter = 0;
@@ -546,7 +545,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
}
// And then send to all clients
- sendtoallclients(clientsockfd, fdmax, arr_clients, str, sourcefd, arr_authed, ssl);
+ sendtoallclients(clientsockfd, fdmax, arr_clients, str, sourcefd, arr_authed, arr_ssl);
return 1;
}
@@ -567,7 +566,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
}
// And then send to all clients
- sendtoallclients(clientsockfd, fdmax, arr_clients, str, sourcefd, arr_authed, ssl);
+ sendtoallclients(clientsockfd, fdmax, arr_clients, str, sourcefd, arr_authed, arr_ssl);
return 1;
}
@@ -634,7 +633,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
setchanneltopicwhotime(channels, tokens[2], prefixcopy, timenowstr);
// And then finally relay to all clients
- sendtoallclients(clientsockfd, fdmax, arr_clients, str, sourcefd, arr_authed, ssl);
+ sendtoallclients(clientsockfd, fdmax, arr_clients, str, sourcefd, arr_authed, arr_ssl);
return 1;
}
@@ -642,7 +641,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
if (strncmp(tokens[1], "PRIVMSG", strlen(tokens[1])) == 0) {
printf("Server PRIVMSG found and it is: %s with length %zd! Next token is '%s'. Relaying to all clients.\n", tokens[0], strlen(tokens[0]), tokens[2]);
- sendtoallclients(clientsockfd, fdmax, arr_clients, str, sourcefd, arr_authed, ssl);
+ sendtoallclients(clientsockfd, fdmax, arr_clients, str, sourcefd, arr_authed, arr_ssl);
// Write to relay log
writerelayline(str);
@@ -682,13 +681,13 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
// Send IRC greeting strings (001/RPL_WELCOME, 002/RPL_YOURHOST, 003/RPL_CREATED, 004/RPL_MYINFO) to client
snprintf(outgoingmsg, MAXDATASIZE, "%s", ircdstrings->greeting001);
- sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, ssl);
+ sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, arr_ssl);
snprintf(outgoingmsg, MAXDATASIZE, "%s", ircdstrings->greeting002);
- sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, ssl);
+ sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, arr_ssl);
snprintf(outgoingmsg, MAXDATASIZE, "%s", ircdstrings->greeting003);
- sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, ssl);
+ sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, arr_ssl);
snprintf(outgoingmsg, MAXDATASIZE, "%s", ircdstrings->greeting004);
- sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, ssl);
+ sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, arr_ssl);
// Get the channel count so we can enumerate over all channels.
// Storing separately so we can skip over blank channels.
@@ -709,7 +708,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
fprintf(stderr, "Error while preparing USER just connected, channel JOIN responses!\n");
exit(1);
}
- sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, ssl);
+ sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, arr_ssl);
// Send topic (or lack thereof) to client
// If there isn't one set (we guess this if topic timestamp is 0), send 331 RPL_NOTOPIC
@@ -720,7 +719,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
exit(1);
}
// ..and send it to the client
- sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, ssl);
+ sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, arr_ssl);
// If there is one set, send 332 RPL_TOPIC and 333 RPL_TOPICWHOTIME
} else {
// Prepare the topic message...
@@ -729,7 +728,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
exit(1);
}
// ..and send it to the client
- sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, ssl);
+ sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, arr_ssl);
// Next prepare the topic who/when message...
if (!snprintf(outgoingmsg, MAXDATASIZE, ":%s 333 %s %s %s %s", ircdstrings->ircdname, ircdstrings->ircnick, channels[i].name, channels[i].topicwho, channels[i].topicwhen)) {
@@ -737,7 +736,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
exit(1);
}
// ..and send it to the client
- sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, ssl);
+ sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, arr_ssl);
}
// Send list of names
@@ -757,7 +756,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
fprintf(stderr, "Error while preparing USER just connected, channel NAMES responses!\n");
exit(1);
}
- sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, ssl);
+ sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, arr_ssl);
}
// Once all names are sent, send the "end of /NAMES" 366 (RPL_ENDOFNAMES) message
@@ -765,7 +764,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
fprintf(stderr, "Error while preparing USER just connected, end of NAMES response!\n");
exit(1);
}
- sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, ssl);
+ sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, arr_ssl);
}
// Send the client however many relay lines have been requested
@@ -787,7 +786,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
exit(1);
}
printf("Sending relay line: '%s'.\n", outgoingmsg);
- sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, ssl);
+ sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, arr_ssl);
}
return 1;
@@ -802,7 +801,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
fprintf(stderr, "Error while preparing PONG response!\n");
exit(1);
}
- sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, ssl);
+ sendtoclient(sourcefd, outgoingmsg, arr_clients, arr_authed, arr_ssl);
// We processed something so return true
return 1;
@@ -835,7 +834,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
exit(1);
}
// Send to all except source client
- sendtoallclients(clientsockfd, fdmax, arr_clients, outgoingmsg, sourcefd, arr_authed, ssl);
+ sendtoallclients(clientsockfd, fdmax, arr_clients, outgoingmsg, sourcefd, arr_authed, arr_ssl);
// Write to relay log
writerelayline(outgoingmsg);
@@ -902,7 +901,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
//
// Return 0 if something went wrong
// Return 1 if everything OK
-int processrawstring(int *serversockfd, int *clientsockfd, char *str, int source, int fdmax, int arr_clients[], int sourcefd, struct ircdstrings *ircdstrings, struct channel *channels, int arr_authed[], SSL **ssl) {
+int processrawstring(int *serversockfd, int *clientsockfd, char *str, int source, int fdmax, int arr_clients[], int sourcefd, struct ircdstrings *ircdstrings, struct channel *channels, int arr_authed[], SSL **arr_ssl) {
// Copy to a temporary string so we still have the original in case it's not processed
char *strcopy = strdup(str);
@@ -957,7 +956,7 @@ int processrawstring(int *serversockfd, int *clientsockfd, char *str, int source
for (int i = 0; i < messagecount; i++) {
// Copy to a temporary string so we still have the original in case it's not processed
char *messagecopy = strdup(messages[i]);
- if (processircmessage(serversockfd, clientsockfd, messagecopy, source, fdmax, arr_clients, sourcefd, ircdstrings, channels, arr_authed, ssl)) {
+ if (processircmessage(serversockfd, clientsockfd, messagecopy, source, fdmax, arr_clients, sourcefd, ircdstrings, channels, arr_authed, arr_ssl)) {
printf("Message processed: \"%s\", NULLing...\n", messages[i]);
messages[i][0] = '\0';
}
@@ -973,7 +972,7 @@ int processrawstring(int *serversockfd, int *clientsockfd, char *str, int source
// Relay/send to all clients ("except" = 0 because this should send to all clients)
// TODO - Is this really going to send the original string if we have messed it with it in processrawstring() and friends!?
printf("bouncer-server: sending unprocessed server message \"%s\" to all clients, length %zd.\n", messages[i], strlen(messages[i]));
- sendtoallclients(clientsockfd, fdmax, arr_clients, messages[i], EXCEPT_NONE, arr_authed, ssl);
+ sendtoallclients(clientsockfd, fdmax, arr_clients, messages[i], EXCEPT_NONE, arr_authed, arr_ssl);
break;
case SOURCE_CLIENT: // If message(s) were from a real IRC client
// Send to server
@@ -982,7 +981,7 @@ int processrawstring(int *serversockfd, int *clientsockfd, char *str, int source
printf("bouncer-client: sending unprocessed client message \"%s\" to all other clients, length %zd.\n", messages[i], strlen(messages[i]));
// send the same thing to all *other* clients (all except for source fd)
- sendtoallclients(clientsockfd, fdmax, arr_clients, messages[i], sourcefd, arr_authed, ssl);
+ sendtoallclients(clientsockfd, fdmax, arr_clients, messages[i], sourcefd, arr_authed, arr_ssl);
break;
default:
fprintf(stderr, "Unexpected raw IRC string source for unprocessed message \"%s\", length %zd.!\n", messages[i], strlen(messages[i]));
@@ -1005,7 +1004,7 @@ void dochat(int *serversockfd, int *clientsockfd) {
int outgoingmsgrc; // Return code from getstdin() for outgoing message
int arr_clients[MAXCLIENTS]; // Array of all client FDs - 0 means not connected, greater than 0 means connected and the value is the fd number (so we know which ones to try to read)
int arr_authed[MAXCLIENTS]; // Array of client authentication statuses - 0 means not authenticated, 1 means authenticated. Element numbers match those of arr_clients.
- SSL *ssl[MAXCLIENTS]; // Array of OpenSSL structures
+ SSL *arr_ssl[MAXCLIENTS]; // Array of OpenSSL structures
int num_clients = 0; // Current number of clients
int fdmax; // highest numbered socket fd
@@ -1121,7 +1120,7 @@ void dochat(int *serversockfd, int *clientsockfd) {
// Try to process received string (which should contain one or more server responses/commands)
// TODO - What if there were two server respones/commands and only one didn't need relaying?
- if (!processrawstring(serversockfd, clientsockfd, serverbuf, SOURCE_SERVER, fdmax, arr_clients, EXCEPT_NONE, &ircdstrings, channels, arr_authed, ssl)) {
+ if (!processrawstring(serversockfd, clientsockfd, serverbuf, SOURCE_SERVER, fdmax, arr_clients, EXCEPT_NONE, &ircdstrings, channels, arr_authed, arr_ssl)) {
fprintf(stderr, "Error: bouncer-server failed to process raw string.\n");
exit(1);
}
@@ -1201,9 +1200,9 @@ void dochat(int *serversockfd, int *clientsockfd) {
// Ensure its authentication status is set to 0
arr_authed[j] = 0;
// Set as OpenSSL FD and SSL_accept it
- ssl[j] = SSL_new(ctx);
- SSL_set_fd(ssl[j], newfd);
- if (SSL_accept(ssl[j]) <= 0) {
+ arr_ssl[j] = SSL_new(ctx);
+ SSL_set_fd(arr_ssl[j], newfd);
+ if (SSL_accept(arr_ssl[j]) <= 0) {
printf("SSL_accept failed for fd %d.\n", j);
ERR_print_errors_fp(stderr);
} else {
@@ -1221,7 +1220,7 @@ void dochat(int *serversockfd, int *clientsockfd) {
printf("...previous connection!\n");
// handle data from a client
// if ((clientnumbytes = recv(i, clientbuf, sizeof clientbuf, 0)) <= 0) {
- if ((clientnumbytes = SSL_read(ssl[arrindex(arr_clients, i)], clientbuf, sizeof clientbuf)) <= 0) {
+ if ((clientnumbytes = SSL_read(arr_ssl[arrindex(arr_clients, i)], clientbuf, sizeof clientbuf)) <= 0) {
// got error or connection closed by client
if (clientnumbytes == 0) {
// connection closed
@@ -1247,7 +1246,7 @@ void dochat(int *serversockfd, int *clientsockfd) {
// Try to process received string (which should contain one or more client responses/commands)
// TODO - What if there were two server respones/commands and only one didn't need relaying?
- if (!processrawstring(serversockfd, clientsockfd, clientbuf, SOURCE_CLIENT, fdmax, arr_clients, i, &ircdstrings, channels, arr_authed, ssl)) {
+ if (!processrawstring(serversockfd, clientsockfd, clientbuf, SOURCE_CLIENT, fdmax, arr_clients, i, &ircdstrings, channels, arr_authed, arr_ssl)) {
fprintf(stderr, "Error: bouncer-client failed to process raw string.\n");
exit(1);
}