IRC bouncer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

state.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* Copyright (C) 2019 C. McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #include <assert.h>
  17. #include <err.h>
  18. #include <stdbool.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <strings.h>
  23. #include <sysexits.h>
  24. #include "bounce.h"
  25. typedef void Handler(struct Message *msg);
  26. static void require(const struct Message *msg, bool origin, size_t len) {
  27. if (origin && !msg->origin) {
  28. errx(EX_PROTOCOL, "%s missing origin", msg->cmd);
  29. }
  30. for (size_t i = 0; i < len; ++i) {
  31. if (msg->params[i]) continue;
  32. errx(EX_PROTOCOL, "%s missing parameter %zu", msg->cmd, 1 + i);
  33. }
  34. }
  35. static char *plainBase64;
  36. void stateLogin(
  37. const char *pass, bool sasl, const char *plain,
  38. const char *nick, const char *user, const char *real
  39. ) {
  40. serverFormat("CAP LS\r\n");
  41. if (pass) serverFormat("PASS :%s\r\n", pass);
  42. if (sasl) {
  43. serverFormat("CAP REQ :%s\r\n", capList(CapSASL));
  44. if (plain) {
  45. byte buf[1 + strlen(plain)];
  46. buf[0] = 0;
  47. for (size_t i = 0; plain[i]; ++i) {
  48. buf[1 + i] = (plain[i] == ':' ? 0 : plain[i]);
  49. }
  50. plainBase64 = malloc(BASE64_SIZE(sizeof(buf)));
  51. if (!plainBase64) err(EX_OSERR, "malloc");
  52. base64(plainBase64, buf, sizeof(buf));
  53. }
  54. }
  55. serverFormat("NICK %s\r\n", nick);
  56. serverFormat("USER %s 0 * :%s\r\n", user, real);
  57. }
  58. static void handleCap(struct Message *msg) {
  59. require(msg, false, 3);
  60. enum Cap caps = capParse(msg->params[2]);
  61. if (!strcmp(msg->params[1], "LS")) {
  62. caps &= ~(CapSASL | CapServerTime | CapUnsupported);
  63. serverFormat("CAP REQ :%s\r\n", capList(caps));
  64. } else if (!strcmp(msg->params[1], "ACK")) {
  65. stateCaps |= caps;
  66. if (caps & CapSASL) {
  67. serverFormat(
  68. "AUTHENTICATE %s\r\n", (plainBase64 ? "PLAIN" : "EXTERNAL")
  69. );
  70. }
  71. if (!(stateCaps & CapSASL)) serverFormat("CAP END\r\n");
  72. } else if (!strcmp(msg->params[1], "NAK")) {
  73. errx(EX_CONFIG, "server does not support %s", msg->params[2]);
  74. }
  75. }
  76. static void handleAuthenticate(struct Message *msg) {
  77. (void)msg;
  78. if (plainBase64) {
  79. serverFormat("AUTHENTICATE %s\r\n", plainBase64);
  80. explicit_bzero(plainBase64, strlen(plainBase64));
  81. free(plainBase64);
  82. plainBase64 = NULL;
  83. } else {
  84. serverFormat("AUTHENTICATE +\r\n");
  85. }
  86. }
  87. static void handleReplyLoggedIn(struct Message *msg) {
  88. (void)msg;
  89. serverFormat("CAP END\r\n");
  90. }
  91. static void handleErrorSASLFail(struct Message *msg) {
  92. require(msg, false, 2);
  93. errx(EX_CONFIG, "%s", msg->params[1]);
  94. }
  95. static struct {
  96. char *nick;
  97. char *origin;
  98. } self;
  99. static struct {
  100. char *origin;
  101. char *welcome;
  102. char *yourHost;
  103. char *created;
  104. char *myInfo[5];
  105. } intro;
  106. const char *stateNick(void) {
  107. return (self.nick ? self.nick : "*");
  108. }
  109. const char *stateEcho(void) {
  110. return (self.origin ? self.origin : stateNick());
  111. }
  112. bool stateReady(void) {
  113. return self.nick
  114. && intro.origin
  115. && intro.welcome
  116. && intro.yourHost
  117. && intro.created
  118. && intro.myInfo[0];
  119. }
  120. static void set(char **field, const char *value) {
  121. if (*field) free(*field);
  122. *field = strdup(value);
  123. if (!*field) err(EX_OSERR, "strdup");
  124. }
  125. static void handleErrorNicknameInUse(struct Message *msg) {
  126. if (self.nick) return;
  127. require(msg, false, 2);
  128. serverFormat("NICK %s_\r\n", msg->params[1]);
  129. }
  130. static void handleReplyWelcome(struct Message *msg) {
  131. require(msg, true, 2);
  132. set(&intro.origin, msg->origin);
  133. set(&self.nick, msg->params[0]);
  134. set(&intro.welcome, msg->params[1]);
  135. }
  136. static void handleReplyYourHost(struct Message *msg) {
  137. require(msg, false, 2);
  138. set(&intro.yourHost, msg->params[1]);
  139. }
  140. static void handleReplyCreated(struct Message *msg) {
  141. require(msg, false, 2);
  142. set(&intro.created, msg->params[1]);
  143. }
  144. static void handleReplyMyInfo(struct Message *msg) {
  145. require(msg, false, 5);
  146. set(&intro.myInfo[0], msg->params[1]);
  147. set(&intro.myInfo[1], msg->params[2]);
  148. set(&intro.myInfo[2], msg->params[3]);
  149. set(&intro.myInfo[3], msg->params[4]);
  150. if (msg->params[5]) set(&intro.myInfo[4], msg->params[5]);
  151. }
  152. static struct {
  153. char **tokens;
  154. size_t cap, len;
  155. } support;
  156. static void supportAdd(const char *token) {
  157. if (support.len == support.cap) {
  158. support.cap = (support.cap ? support.cap * 2 : 8);
  159. support.tokens = realloc(support.tokens, sizeof(char *) * support.cap);
  160. if (!support.tokens) err(EX_OSERR, "realloc");
  161. }
  162. support.tokens[support.len] = strdup(token);
  163. if (!support.tokens[support.len]) err(EX_OSERR, "strdup");
  164. support.len++;
  165. }
  166. static void handleReplyISupport(struct Message *msg) {
  167. require(msg, false, 1);
  168. for (size_t i = 1; i < ParamCap; ++i) {
  169. if (!msg->params[i] || strchr(msg->params[i], ' ')) break;
  170. supportAdd(msg->params[i]);
  171. }
  172. }
  173. struct Channel {
  174. char *name;
  175. char *topic;
  176. };
  177. static struct {
  178. struct Channel *ptr;
  179. size_t cap, len;
  180. } chans;
  181. static void chanAdd(const char *name) {
  182. if (chans.len == chans.cap) {
  183. chans.cap = (chans.cap ? chans.cap * 2 : 8);
  184. chans.ptr = realloc(chans.ptr, sizeof(*chans.ptr) * chans.cap);
  185. if (!chans.ptr) err(EX_OSERR, "realloc");
  186. }
  187. struct Channel *chan = &chans.ptr[chans.len++];
  188. chan->name = strdup(name);
  189. if (!chan->name) err(EX_OSERR, "strdup");
  190. chan->topic = NULL;
  191. }
  192. static void chanTopic(const char *name, const char *topic) {
  193. for (size_t i = 0; i < chans.len; ++i) {
  194. if (strcmp(chans.ptr[i].name, name)) continue;
  195. set(&chans.ptr[i].topic, topic);
  196. break;
  197. }
  198. }
  199. static void chanRemove(const char *name) {
  200. for (size_t i = 0; i < chans.len; ++i) {
  201. if (strcmp(chans.ptr[i].name, name)) continue;
  202. free(chans.ptr[i].name);
  203. free(chans.ptr[i].topic);
  204. chans.ptr[i] = chans.ptr[--chans.len];
  205. break;
  206. }
  207. }
  208. static bool originSelf(const char *origin) {
  209. if (!self.nick) return false;
  210. size_t len = strlen(self.nick);
  211. if (strlen(origin) < len) return false;
  212. if (strncmp(origin, self.nick, len)) return false;
  213. if (origin[len] != '!') return false;
  214. if (!self.origin || strcmp(self.origin, origin)) {
  215. set(&self.origin, origin);
  216. }
  217. return true;
  218. }
  219. static void handleNick(struct Message *msg) {
  220. require(msg, true, 1);
  221. if (!originSelf(msg->origin)) return;
  222. set(&self.nick, msg->params[0]);
  223. char *rest = strchr(self.origin, '!');
  224. assert(rest);
  225. size_t size = strlen(self.nick) + strlen(rest) + 1;
  226. char *origin = malloc(size);
  227. if (!origin) err(EX_OSERR, "malloc");
  228. snprintf(origin, size, "%s%s", self.nick, rest);
  229. free(self.origin);
  230. self.origin = origin;
  231. }
  232. static void handleJoin(struct Message *msg) {
  233. require(msg, true, 1);
  234. if (originSelf(msg->origin)) chanAdd(msg->params[0]);
  235. }
  236. static void handlePart(struct Message *msg) {
  237. require(msg, true, 1);
  238. if (originSelf(msg->origin)) chanRemove(msg->params[0]);
  239. }
  240. static void handleKick(struct Message *msg) {
  241. require(msg, false, 2);
  242. if (self.nick && !strcmp(msg->params[1], self.nick)) {
  243. chanRemove(msg->params[0]);
  244. }
  245. }
  246. static void handleTopic(struct Message *msg) {
  247. require(msg, false, 2);
  248. chanTopic(msg->params[0], msg->params[1]);
  249. }
  250. static void handleReplyTopic(struct Message *msg) {
  251. require(msg, false, 3);
  252. chanTopic(msg->params[1], msg->params[2]);
  253. }
  254. static void handleError(struct Message *msg) {
  255. require(msg, false, 1);
  256. errx(EX_UNAVAILABLE, "%s", msg->params[0]);
  257. }
  258. static const struct {
  259. const char *cmd;
  260. Handler *fn;
  261. } Handlers[] = {
  262. { "001", handleReplyWelcome },
  263. { "002", handleReplyYourHost },
  264. { "003", handleReplyCreated },
  265. { "004", handleReplyMyInfo },
  266. { "005", handleReplyISupport },
  267. { "332", handleReplyTopic },
  268. { "433", handleErrorNicknameInUse },
  269. { "900", handleReplyLoggedIn },
  270. { "904", handleErrorSASLFail },
  271. { "905", handleErrorSASLFail },
  272. { "906", handleErrorSASLFail },
  273. { "AUTHENTICATE", handleAuthenticate },
  274. { "CAP", handleCap },
  275. { "ERROR", handleError },
  276. { "JOIN", handleJoin },
  277. { "KICK", handleKick },
  278. { "NICK", handleNick },
  279. { "PART", handlePart },
  280. { "TOPIC", handleTopic },
  281. };
  282. void stateParse(char *line) {
  283. struct Message msg = parse(line);
  284. if (!msg.cmd) return;
  285. for (size_t i = 0; i < ARRAY_LEN(Handlers); ++i) {
  286. if (strcmp(msg.cmd, Handlers[i].cmd)) continue;
  287. Handlers[i].fn(&msg);
  288. break;
  289. }
  290. }
  291. void stateSync(struct Client *client) {
  292. assert(stateReady());
  293. clientFormat(
  294. client,
  295. ":%s NOTICE %s :"
  296. "pounce is GPLv3 fwee softwawe ^w^ code is avaiwable fwom %s\r\n",
  297. ORIGIN, self.nick, SOURCE_URL
  298. );
  299. clientFormat(
  300. client,
  301. ":%s 001 %s :%s\r\n"
  302. ":%s 002 %s :%s\r\n"
  303. ":%s 003 %s :%s\r\n"
  304. ":%s 004 %s %s %s %s %s%s%s\r\n",
  305. intro.origin, self.nick, intro.welcome,
  306. intro.origin, self.nick, intro.yourHost,
  307. intro.origin, self.nick, intro.created,
  308. intro.origin, self.nick,
  309. intro.myInfo[0], intro.myInfo[1], intro.myInfo[2], intro.myInfo[3],
  310. (intro.myInfo[4] ? " " : ""), (intro.myInfo[4] ? intro.myInfo[4] : "")
  311. );
  312. size_t i;
  313. for (i = 0; support.len - i >= 13; i += 13) {
  314. clientFormat(
  315. client,
  316. ":%s 005 %s"
  317. " %s %s %s %s %s %s %s %s %s %s %s %s %s"
  318. " :are supported by this server\r\n",
  319. intro.origin, self.nick,
  320. support.tokens[i + 0], support.tokens[i + 1],
  321. support.tokens[i + 2], support.tokens[i + 3],
  322. support.tokens[i + 4], support.tokens[i + 5],
  323. support.tokens[i + 6], support.tokens[i + 7],
  324. support.tokens[i + 8], support.tokens[i + 9],
  325. support.tokens[i + 10], support.tokens[i + 11],
  326. support.tokens[i + 12]
  327. );
  328. }
  329. if (i < support.len) {
  330. clientFormat(client, ":%s 005 %s", intro.origin, self.nick);
  331. for (; i < support.len; ++i) {
  332. clientFormat(client, " %s", support.tokens[i]);
  333. }
  334. clientFormat(client, " :are supported by this server\r\n");
  335. }
  336. if (chans.len) assert(self.origin);
  337. for (size_t i = 0; i < chans.len; ++i) {
  338. const struct Channel *chan = &chans.ptr[i];
  339. clientFormat(client, ":%s JOIN %s\r\n", self.origin, chan->name);
  340. if (chan->topic) {
  341. clientFormat(
  342. client, ":%s 332 %s %s :%s\r\n",
  343. ORIGIN, self.nick, chan->name, chan->topic
  344. );
  345. }
  346. if (stateJoinNames) serverFormat("NAMES %s\r\n", chan->name);
  347. }
  348. }