irc-login.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #include "portab.h"
  12. /**
  13. * @file
  14. * Login and logout
  15. */
  16. #include "imp.h"
  17. #include <assert.h>
  18. #include <ctype.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <strings.h>
  22. #include "conn-func.h"
  23. #include "class.h"
  24. #include "conf.h"
  25. #include "channel.h"
  26. #include "log.h"
  27. #include "login.h"
  28. #include "messages.h"
  29. #include "parse.h"
  30. #include "irc.h"
  31. #include "irc-info.h"
  32. #include "irc-macros.h"
  33. #include "irc-write.h"
  34. #include "exp.h"
  35. #include "irc-login.h"
  36. static void Change_Nick PARAMS((CLIENT * Origin, CLIENT * Target, char *NewNick,
  37. bool InformClient));
  38. /**
  39. * Handler for the IRC "PASS" command.
  40. *
  41. * @param Client The client from which this command has been received.
  42. * @param Req Request structure with prefix and all parameters.
  43. * @return CONNECTED or DISCONNECTED.
  44. */
  45. GLOBAL bool
  46. IRC_PASS( CLIENT *Client, REQUEST *Req )
  47. {
  48. char *type, *orig_flags;
  49. int protohigh, protolow;
  50. assert( Client != NULL );
  51. assert( Req != NULL );
  52. /* Return an error if this is not a local client */
  53. if (Client_Conn(Client) <= NONE)
  54. return IRC_WriteErrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
  55. Client_ID(Client), Req->command);
  56. if (Client_Type(Client) == CLIENT_UNKNOWN && Req->argc == 1) {
  57. /* Not yet registered "unknown" connection, PASS with one
  58. * argument: either a regular client, service, or server
  59. * using the old RFC 1459 section 4.1.1 syntax. */
  60. LogDebug("Connection %d: got PASS command (RFC 1459) ...",
  61. Client_Conn(Client));
  62. } else if ((Client_Type(Client) == CLIENT_UNKNOWN ||
  63. Client_Type(Client) == CLIENT_UNKNOWNSERVER) &&
  64. (Req->argc == 3 || Req->argc == 4)) {
  65. /* Not yet registered "unknown" connection or outgoing server
  66. * link, PASS with three or four argument: server using the
  67. * RFC 2813 section 4.1.1 syntax. */
  68. LogDebug("Connection %d: got PASS command (RFC 2813, new server link) ...",
  69. Client_Conn(Client));
  70. } else if (Client_Type(Client) == CLIENT_UNKNOWN ||
  71. Client_Type(Client) == CLIENT_UNKNOWNSERVER) {
  72. /* Unregistered connection, but wrong number of arguments: */
  73. IRC_SetPenalty(Client, 2);
  74. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  75. Client_ID(Client), Req->command);
  76. } else {
  77. /* Registered connection, PASS command is not allowed! */
  78. return IRC_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
  79. Client_ID(Client));
  80. }
  81. Conn_SetPassword(Client_Conn(Client), Req->argv[0]);
  82. /* Protocol version */
  83. if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
  84. int c2, c4;
  85. c2 = Req->argv[1][2];
  86. c4 = Req->argv[1][4];
  87. Req->argv[1][4] = '\0';
  88. protolow = atoi(&Req->argv[1][2]);
  89. Req->argv[1][2] = '\0';
  90. protohigh = atoi(Req->argv[1]);
  91. Req->argv[1][2] = c2;
  92. Req->argv[1][4] = c4;
  93. Client_SetType(Client, CLIENT_GOTPASS_2813);
  94. } else {
  95. protohigh = protolow = 0;
  96. Client_SetType(Client, CLIENT_GOTPASS);
  97. }
  98. /* Protocol type, see doc/Protocol.txt */
  99. if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
  100. type = &Req->argv[1][4];
  101. else
  102. type = NULL;
  103. /* Protocol flags/options */
  104. if (Req->argc >= 4)
  105. orig_flags = Req->argv[3];
  106. else
  107. orig_flags = "";
  108. /* Implementation, version and IRC+ flags */
  109. if (Req->argc >= 3) {
  110. char *impl, *ptr, *serverver, *flags;
  111. impl = Req->argv[2];
  112. ptr = strchr(impl, '|');
  113. if (ptr)
  114. *ptr = '\0';
  115. if (type && strcmp(type, PROTOIRCPLUS) == 0) {
  116. /* The peer seems to be a server which supports the
  117. * IRC+ protocol (see doc/Protocol.txt). */
  118. serverver = ptr ? ptr + 1 : "?";
  119. flags = strchr(ptr ? serverver : impl, ':');
  120. if (flags) {
  121. *flags = '\0';
  122. flags++;
  123. } else
  124. flags = "";
  125. Log(LOG_INFO,
  126. "Peer on connection %d announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
  127. Client_Conn(Client), impl, serverver,
  128. protohigh, protolow, flags);
  129. } else {
  130. /* The peer seems to be a server supporting the
  131. * "original" IRC protocol (RFC 2813). */
  132. if (strchr(orig_flags, 'Z'))
  133. flags = "Z";
  134. else
  135. flags = "";
  136. Log(LOG_INFO,
  137. "Peer on connection %d announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
  138. Client_Conn(Client), impl,
  139. protohigh, protolow, flags);
  140. }
  141. Client_SetFlags(Client, flags);
  142. }
  143. return CONNECTED;
  144. } /* IRC_PASS */
  145. /**
  146. * Handler for the IRC "NICK" command.
  147. *
  148. * @param Client The client from which this command has been received.
  149. * @param Req Request structure with prefix and all parameters.
  150. * @return CONNECTED or DISCONNECTED.
  151. */
  152. GLOBAL bool
  153. IRC_NICK( CLIENT *Client, REQUEST *Req )
  154. {
  155. CLIENT *intr_c, *target, *c;
  156. char *nick, *user, *hostname, *modes, *info;
  157. int token, hops;
  158. assert( Client != NULL );
  159. assert( Req != NULL );
  160. /* Some IRC clients, for example BitchX, send the NICK and USER
  161. * commands in the wrong order ... */
  162. if(Client_Type(Client) == CLIENT_UNKNOWN
  163. || Client_Type(Client) == CLIENT_GOTPASS
  164. || Client_Type(Client) == CLIENT_GOTNICK
  165. #ifndef STRICT_RFC
  166. || Client_Type(Client) == CLIENT_GOTUSER
  167. #endif
  168. || Client_Type(Client) == CLIENT_USER
  169. || Client_Type(Client) == CLIENT_SERVICE
  170. || (Client_Type(Client) == CLIENT_SERVER && Req->argc == 1))
  171. {
  172. /* User registration or change of nickname */
  173. _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 1)
  174. /* Search "target" client */
  175. if (Client_Type(Client) == CLIENT_SERVER) {
  176. target = Client_Search(Req->prefix);
  177. if (!target)
  178. return IRC_WriteErrClient(Client,
  179. ERR_NOSUCHNICK_MSG,
  180. Client_ID(Client),
  181. Req->argv[0]);
  182. } else {
  183. /* Is this a restricted client? */
  184. if (Client_HasMode(Client, 'r'))
  185. return IRC_WriteErrClient(Client,
  186. ERR_RESTRICTED_MSG,
  187. Client_ID(Client));
  188. target = Client;
  189. }
  190. #ifndef STRICT_RFC
  191. /* If the clients tries to change to its own nickname we won't
  192. * do anything. This is how the original ircd behaves and some
  193. * clients (for example Snak) expect it to be like this.
  194. * But I doubt that this is "really the right thing" ... */
  195. if (strcmp(Client_ID(target), Req->argv[0]) == 0)
  196. return CONNECTED;
  197. #endif
  198. /* Check that the new nickname is available. Special case:
  199. * the client only changes from/to upper to lower case. */
  200. if (strcasecmp(Client_ID(target), Req->argv[0]) != 0) {
  201. if (!Client_CheckNick(target, Req->argv[0]))
  202. return CONNECTED;
  203. }
  204. if (Client_Type(target) != CLIENT_USER &&
  205. Client_Type(target) != CLIENT_SERVICE &&
  206. Client_Type(target) != CLIENT_SERVER) {
  207. /* New client */
  208. LogDebug("Connection %d: got valid NICK command ...",
  209. Client_Conn( Client ));
  210. /* Register new nickname of this client */
  211. Client_SetID( target, Req->argv[0] );
  212. #ifndef STRICT_RFC
  213. if (Conf_AuthPing) {
  214. #ifdef HAVE_ARC4RANDOM
  215. Conn_SetAuthPing(Client_Conn(Client), arc4random());
  216. #else
  217. Conn_SetAuthPing(Client_Conn(Client), rand());
  218. #endif
  219. IRC_WriteStrClient(Client, "PING :%ld",
  220. Conn_GetAuthPing(Client_Conn(Client)));
  221. LogDebug("Connection %d: sent AUTH PING %ld ...",
  222. Client_Conn(Client),
  223. Conn_GetAuthPing(Client_Conn(Client)));
  224. }
  225. #endif
  226. /* If we received a valid USER command already then
  227. * register the new client! */
  228. if( Client_Type( Client ) == CLIENT_GOTUSER )
  229. return Login_User( Client );
  230. else
  231. Client_SetType( Client, CLIENT_GOTNICK );
  232. } else {
  233. /* Nickname change */
  234. Change_Nick(Client, target, Req->argv[0],
  235. Client_Type(Client) == CLIENT_USER ? true : false);
  236. IRC_SetPenalty(target, 2);
  237. }
  238. return CONNECTED;
  239. } else if(Client_Type(Client) == CLIENT_SERVER ||
  240. Client_Type(Client) == CLIENT_SERVICE) {
  241. /* Server or service introduces new client */
  242. /* Bad number of parameters? */
  243. if (Req->argc != 2 && Req->argc != 7) {
  244. IRC_SetPenalty(Client, 2);
  245. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  246. Client_ID(Client), Req->command);
  247. }
  248. if (Req->argc >= 7) {
  249. /* RFC 2813 compatible syntax */
  250. nick = Req->argv[0];
  251. hops = atoi(Req->argv[1]);
  252. user = Req->argv[2];
  253. hostname = Req->argv[3];
  254. token = atoi(Req->argv[4]);
  255. modes = Req->argv[5] + 1;
  256. info = Req->argv[6];
  257. } else {
  258. /* RFC 1459 compatible syntax */
  259. nick = Req->argv[0];
  260. hops = 1;
  261. user = Req->argv[0];
  262. hostname = Client_ID(Client);
  263. token = atoi(Req->argv[1]);
  264. modes = "";
  265. info = Req->argv[0];
  266. }
  267. c = Client_Search(nick);
  268. if(c) {
  269. /*
  270. * the new nick is already present on this server:
  271. * the new and the old one have to be disconnected now.
  272. */
  273. Log(LOG_ERR,
  274. "Server %s introduces already registered nick \"%s\"!",
  275. Client_ID(Client), Req->argv[0]);
  276. return IRC_KillClient(Client, NULL, Req->argv[0],
  277. "Nick collision");
  278. }
  279. /* Find the Server this client is connected to */
  280. intr_c = Client_GetFromToken(Client, token);
  281. if (!intr_c) {
  282. Log(LOG_ERR,
  283. "Server %s introduces nick \"%s\" on unknown server!?",
  284. Client_ID(Client), Req->argv[0]);
  285. return IRC_KillClient(Client, NULL, Req->argv[0],
  286. "Unknown server");
  287. }
  288. c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
  289. token, modes, info, true);
  290. if (!c) {
  291. /* Out of memory, we need to disconnect client to keep
  292. * network state consistent! */
  293. Log(LOG_ALERT,
  294. "Can't create client structure! (on connection %d)",
  295. Client_Conn(Client));
  296. return IRC_KillClient(Client, NULL, Req->argv[0],
  297. "Server error");
  298. }
  299. /* RFC 2813: client is now fully registered, inform all the
  300. * other servers about the new user.
  301. * RFC 1459: announce the new client only after receiving the
  302. * USER command, first we need more information! */
  303. if (Req->argc < 7) {
  304. LogDebug("Client \"%s\" is being registered (RFC 1459) ...",
  305. Client_Mask(c));
  306. Client_SetType(c, CLIENT_GOTNICK);
  307. } else
  308. Client_Introduce(Client, c, CLIENT_USER);
  309. return CONNECTED;
  310. }
  311. else
  312. return IRC_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
  313. Client_ID(Client));
  314. } /* IRC_NICK */
  315. /**
  316. * Handler for the IRC "SVSNICK" command.
  317. *
  318. * @param Client The client from which this command has been received.
  319. * @param Req Request structure with prefix and all parameters.
  320. * @return CONNECTED or DISCONNECTED.
  321. */
  322. GLOBAL bool
  323. IRC_SVSNICK(CLIENT *Client, REQUEST *Req)
  324. {
  325. CLIENT *from, *target;
  326. assert(Client != NULL);
  327. assert(Req != NULL);
  328. /* Search the originator */
  329. from = Client_Search(Req->prefix);
  330. if (!from)
  331. from = Client;
  332. /* Search the target */
  333. target = Client_Search(Req->argv[0]);
  334. if (!target || Client_Type(target) != CLIENT_USER) {
  335. return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
  336. Client_ID(Client), Req->argv[0]);
  337. }
  338. if (Client_Conn(target) <= NONE) {
  339. /* We have to forward the message to the server handling
  340. * this user; this is required to make sure all servers
  341. * in the network do follow the nick name change! */
  342. return IRC_WriteStrClientPrefix(Client_NextHop(target), from,
  343. "SVSNICK %s %s",
  344. Req->argv[0], Req->argv[1]);
  345. }
  346. /* Make sure that the new nickname is valid */
  347. if (!Client_CheckNick(from, Req->argv[1]))
  348. return CONNECTED;
  349. Change_Nick(from, target, Req->argv[1], true);
  350. return CONNECTED;
  351. }
  352. /**
  353. * Handler for the IRC "USER" command.
  354. *
  355. * @param Client The client from which this command has been received.
  356. * @param Req Request structure with prefix and all parameters.
  357. * @return CONNECTED or DISCONNECTED.
  358. */
  359. GLOBAL bool
  360. IRC_USER(CLIENT * Client, REQUEST * Req)
  361. {
  362. CLIENT *c;
  363. char *ptr;
  364. assert(Client != NULL);
  365. assert(Req != NULL);
  366. if (Client_Type(Client) == CLIENT_GOTNICK ||
  367. #ifndef STRICT_RFC
  368. Client_Type(Client) == CLIENT_UNKNOWN ||
  369. #endif
  370. Client_Type(Client) == CLIENT_GOTPASS)
  371. {
  372. /* New connection */
  373. _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 4)
  374. /* User name: only alphanumeric characters and limited
  375. punctuation is allowed.*/
  376. ptr = Req->argv[0];
  377. while (*ptr) {
  378. if (!isalnum((int)*ptr) &&
  379. *ptr != '+' && *ptr != '-' && *ptr != '@' &&
  380. *ptr != '.' && *ptr != '_') {
  381. Conn_Close(Client_Conn(Client), NULL,
  382. "Invalid user name", true);
  383. return DISCONNECTED;
  384. }
  385. ptr++;
  386. }
  387. /* Save the received username for authentication, and use
  388. * it up to the first '@' as default user name (like ircd2.11,
  389. * bahamut, ircd-seven, ...), prefixed with '~', if needed: */
  390. Client_SetOrigUser(Client, Req->argv[0]);
  391. ptr = strchr(Req->argv[0], '@');
  392. if (ptr)
  393. *ptr = '\0';
  394. #ifdef IDENTAUTH
  395. ptr = Client_User(Client);
  396. if (!ptr || !*ptr || *ptr == '~')
  397. Client_SetUser(Client, Req->argv[0], false);
  398. #else
  399. Client_SetUser(Client, Req->argv[0], false);
  400. #endif
  401. /* "Real name" or user info text: Don't set it to the empty
  402. * string, the original ircd can't deal with such "real names"
  403. * (e. g. "USER user * * :") ... */
  404. if (*Req->argv[3])
  405. Client_SetInfo(Client, Req->argv[3]);
  406. else
  407. Client_SetInfo(Client, "-");
  408. LogDebug("Connection %d: got valid USER command ...",
  409. Client_Conn(Client));
  410. if (Client_Type(Client) == CLIENT_GOTNICK)
  411. return Login_User(Client);
  412. else
  413. Client_SetType(Client, CLIENT_GOTUSER);
  414. return CONNECTED;
  415. } else if (Client_Type(Client) == CLIENT_SERVER ||
  416. Client_Type(Client) == CLIENT_SERVICE) {
  417. /* Server/service updating an user */
  418. _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 4)
  419. c = Client_Search(Req->prefix);
  420. if (!c)
  421. return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
  422. Client_ID(Client),
  423. Req->prefix);
  424. Client_SetUser(c, Req->argv[0], true);
  425. Client_SetOrigUser(c, Req->argv[0]);
  426. Client_SetHostname(c, Req->argv[1]);
  427. Client_SetInfo(c, Req->argv[3]);
  428. LogDebug("Connection %d: got valid USER command for \"%s\".",
  429. Client_Conn(Client), Client_Mask(c));
  430. /* RFC 1459 style user registration?
  431. * Introduce client to network: */
  432. if (Client_Type(c) == CLIENT_GOTNICK)
  433. Client_Introduce(Client, c, CLIENT_USER);
  434. return CONNECTED;
  435. } else if (Client_Type(Client) == CLIENT_USER) {
  436. /* Already registered connection */
  437. return IRC_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
  438. Client_ID(Client));
  439. } else {
  440. /* Unexpected/invalid connection state? */
  441. return IRC_WriteErrClient(Client, ERR_NOTREGISTERED_MSG,
  442. Client_ID(Client));
  443. }
  444. } /* IRC_USER */
  445. /**
  446. * Handler for the IRC "SERVICE" command.
  447. *
  448. * At the moment ngIRCd doesn't support directly linked services, so this
  449. * function returns ERR_ERRONEUSNICKNAME when the SERVICE command has not been
  450. * received from a peer server.
  451. *
  452. * @param Client The client from which this command has been received.
  453. * @param Req Request structure with prefix and all parameters.
  454. * @return CONNECTED or DISCONNECTED.
  455. */
  456. GLOBAL bool
  457. IRC_SERVICE(CLIENT *Client, REQUEST *Req)
  458. {
  459. CLIENT *c, *intr_c;
  460. char *nick, *user, *host, *info, *modes, *ptr;
  461. int token, hops;
  462. assert(Client != NULL);
  463. assert(Req != NULL);
  464. if (Client_Type(Client) != CLIENT_GOTPASS &&
  465. Client_Type(Client) != CLIENT_SERVER)
  466. return IRC_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
  467. Client_ID(Client));
  468. if (Client_Type(Client) != CLIENT_SERVER)
  469. return IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
  470. Client_ID(Client), Req->argv[0]);
  471. nick = Req->argv[0];
  472. user = NULL; host = NULL;
  473. token = atoi(Req->argv[1]);
  474. hops = atoi(Req->argv[4]);
  475. info = Req->argv[5];
  476. /* Validate service name ("nickname") */
  477. c = Client_Search(nick);
  478. if(c) {
  479. /* Nickname collision: disconnect (KILL) both clients! */
  480. Log(LOG_ERR,
  481. "Server %s introduces already registered service \"%s\"!",
  482. Client_ID(Client), nick);
  483. return IRC_KillClient(Client, NULL, nick, "Nick collision");
  484. }
  485. /* Get the server to which the service is connected */
  486. intr_c = Client_GetFromToken(Client, token);
  487. if (! intr_c) {
  488. Log(LOG_ERR,
  489. "Server %s introduces service \"%s\" on unknown server!?",
  490. Client_ID(Client), nick);
  491. return IRC_KillClient(Client, NULL, nick, "Unknown server");
  492. }
  493. /* Get user and host name */
  494. ptr = strchr(nick, '@');
  495. if (ptr) {
  496. *ptr = '\0';
  497. host = ++ptr;
  498. }
  499. if (!host)
  500. host = Client_Hostname(intr_c);
  501. ptr = strchr(nick, '!');
  502. if (ptr) {
  503. *ptr = '\0';
  504. user = ++ptr;
  505. }
  506. if (!user)
  507. user = nick;
  508. /* According to RFC 2812/2813 parameter 4 <type> "is currently reserved
  509. * for future usage"; but we use it to transfer the modes and check
  510. * that the first character is a '+' sign and ignore it otherwise. */
  511. modes = (Req->argv[3][0] == '+') ? ++Req->argv[3] : "";
  512. c = Client_NewRemoteUser(intr_c, nick, hops, user, host,
  513. token, modes, info, true);
  514. if (! c) {
  515. /* Couldn't create client structure, so KILL the service to
  516. * keep network status consistent ... */
  517. Log(LOG_ALERT,
  518. "Can't create client structure! (on connection %d)",
  519. Client_Conn(Client));
  520. return IRC_KillClient(Client, NULL, nick, "Server error");
  521. }
  522. Client_Introduce(Client, c, CLIENT_SERVICE);
  523. return CONNECTED;
  524. } /* IRC_SERVICE */
  525. /**
  526. * Handler for the IRC "WEBIRC" command.
  527. *
  528. * @param Client The client from which this command has been received.
  529. * @param Req Request structure with prefix and all parameters.
  530. * @return CONNECTED or DISCONNECTED.
  531. */
  532. GLOBAL bool
  533. IRC_WEBIRC(CLIENT *Client, REQUEST *Req)
  534. {
  535. if (!Conf_WebircPwd[0] || strcmp(Req->argv[0], Conf_WebircPwd) != 0)
  536. return IRC_WriteErrClient(Client, ERR_PASSWDMISMATCH_MSG,
  537. Client_ID(Client));
  538. LogDebug("Connection %d: got valid WEBIRC command: user=%s, host=%s, ip=%s",
  539. Client_Conn(Client), Req->argv[1], Req->argv[2], Req->argv[3]);
  540. Client_SetUser(Client, Req->argv[1], true);
  541. Client_SetOrigUser(Client, Req->argv[1]);
  542. if (Conf_DNS)
  543. Client_SetHostname(Client, Req->argv[2]);
  544. else
  545. Client_SetHostname(Client, Req->argv[3]);
  546. Client_SetIPAText(Client, Req->argv[3]);
  547. return CONNECTED;
  548. } /* IRC_WEBIRC */
  549. /**
  550. * Handler for the IRC "QUIT" command.
  551. *
  552. * @param Client The client from which this command has been received.
  553. * @param Req Request structure with prefix and all parameters.
  554. * @return CONNECTED or DISCONNECTED.
  555. */
  556. GLOBAL bool
  557. IRC_QUIT( CLIENT *Client, REQUEST *Req )
  558. {
  559. CLIENT *target;
  560. char quitmsg[COMMAND_LEN];
  561. assert(Client != NULL);
  562. assert(Req != NULL);
  563. if (Req->argc == 1)
  564. strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
  565. if (Client_Type(Client) == CLIENT_SERVER) {
  566. /* Server */
  567. target = Client_Search(Req->prefix);
  568. if (!target) {
  569. Log(LOG_WARNING,
  570. "Got QUIT from %s for unknown client!?",
  571. Client_ID(Client));
  572. return CONNECTED;
  573. }
  574. if (target != Client) {
  575. Client_Destroy(target, "Got QUIT command",
  576. Req->argc == 1 ? quitmsg : NULL, true);
  577. return CONNECTED;
  578. } else {
  579. Conn_Close(Client_Conn(Client), "Got QUIT command",
  580. Req->argc == 1 ? quitmsg : NULL, true);
  581. return DISCONNECTED;
  582. }
  583. } else {
  584. if (Req->argc == 1 && quitmsg[0] != '\"') {
  585. /* " " to avoid confusion */
  586. strlcpy(quitmsg, "\"", sizeof quitmsg);
  587. strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
  588. strlcat(quitmsg, "\"", sizeof quitmsg );
  589. }
  590. /* User, Service, or not yet registered */
  591. Conn_Close(Client_Conn(Client), "Got QUIT command",
  592. Req->argc == 1 ? quitmsg : NULL, true);
  593. return DISCONNECTED;
  594. }
  595. } /* IRC_QUIT */
  596. #ifndef STRICT_RFC
  597. /**
  598. * Handler for HTTP command, e.g. GET and POST
  599. *
  600. * We handle these commands here to avoid the quite long timeout when
  601. * some user tries to access this IRC daemon using an web browser ...
  602. *
  603. * @param Client The client from which this command has been received.
  604. * @param Req Request structure with prefix and all parameters.
  605. * @return CONNECTED or DISCONNECTED.
  606. */
  607. GLOBAL bool
  608. IRC_QUIT_HTTP( CLIENT *Client, REQUEST *Req )
  609. {
  610. Req->argc = 1;
  611. Req->argv[0] = "Oops, HTTP request received? This is IRC!";
  612. return IRC_QUIT(Client, Req);
  613. } /* IRC_QUIT_HTTP */
  614. #endif
  615. /**
  616. * Handler for the IRC "PING" command.
  617. *
  618. * @param Client The client from which this command has been received.
  619. * @param Req Request structure with prefix and all parameters.
  620. * @return CONNECTED or DISCONNECTED.
  621. */
  622. GLOBAL bool
  623. IRC_PING(CLIENT *Client, REQUEST *Req)
  624. {
  625. CLIENT *target, *from;
  626. assert(Client != NULL);
  627. assert(Req != NULL);
  628. if (Req->argc < 1)
  629. return IRC_WriteErrClient(Client, ERR_NOORIGIN_MSG,
  630. Client_ID(Client));
  631. #ifdef STRICT_RFC
  632. /* Don't ignore additional arguments when in "strict" mode */
  633. _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
  634. #endif
  635. if (Req->argc > 1) {
  636. /* A target has been specified ... */
  637. target = Client_Search(Req->argv[1]);
  638. if (!target || Client_Type(target) != CLIENT_SERVER)
  639. return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
  640. Client_ID(Client), Req->argv[1]);
  641. if (target != Client_ThisServer()) {
  642. /* Ok, we have to forward the PING */
  643. if (Client_Type(Client) == CLIENT_SERVER)
  644. from = Client_Search(Req->prefix);
  645. else
  646. from = Client;
  647. if (!from)
  648. return IRC_WriteErrClient(Client,
  649. ERR_NOSUCHSERVER_MSG,
  650. Client_ID(Client), Req->prefix);
  651. return IRC_WriteStrClientPrefix(target, from,
  652. "PING %s :%s", Req->argv[0],
  653. Req->argv[1] );
  654. }
  655. }
  656. if (Client_Type(Client) == CLIENT_SERVER) {
  657. if (Req->prefix)
  658. from = Client_Search(Req->prefix);
  659. else
  660. from = Client;
  661. } else
  662. from = Client_ThisServer();
  663. if (!from)
  664. return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
  665. Client_ID(Client), Req->prefix);
  666. Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
  667. Client_Conn(Client));
  668. #ifdef STRICT_RFC
  669. return IRC_WriteStrClient(Client, "PONG %s :%s",
  670. Client_ID(from), Client_ID(Client));
  671. #else
  672. /* Some clients depend on the argument being returned in the PONG
  673. * reply (not mentioned in any RFC, though) */
  674. return IRC_WriteStrClient(Client, "PONG %s :%s",
  675. Client_ID(from), Req->argv[0]);
  676. #endif
  677. } /* IRC_PING */
  678. /**
  679. * Handler for the IRC "PONG" command.
  680. *
  681. * @param Client The client from which this command has been received.
  682. * @param Req Request structure with prefix and all parameters.
  683. * @return CONNECTED or DISCONNECTED.
  684. */
  685. GLOBAL bool
  686. IRC_PONG(CLIENT *Client, REQUEST *Req)
  687. {
  688. CLIENT *target, *from;
  689. CONN_ID conn;
  690. #ifndef STRICT_RFC
  691. long auth_ping;
  692. #endif
  693. char *s;
  694. assert(Client != NULL);
  695. assert(Req != NULL);
  696. /* Wrong number of arguments? */
  697. if (Req->argc < 1) {
  698. if (Client_Type(Client) == CLIENT_USER)
  699. return IRC_WriteErrClient(Client, ERR_NOORIGIN_MSG,
  700. Client_ID(Client));
  701. else
  702. return CONNECTED;
  703. }
  704. if (Client_Type(Client) == CLIENT_USER) {
  705. _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
  706. }
  707. /* Forward? */
  708. if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
  709. target = Client_Search(Req->argv[0]);
  710. if (!target)
  711. return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
  712. Client_ID(Client), Req->argv[0]);
  713. from = Client_Search(Req->prefix);
  714. if (target != Client_ThisServer() && target != from) {
  715. /* Ok, we have to forward the message. */
  716. if (!from)
  717. return IRC_WriteErrClient(Client,
  718. ERR_NOSUCHSERVER_MSG,
  719. Client_ID(Client), Req->prefix);
  720. if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
  721. s = Client_ID(from);
  722. else
  723. s = Req->argv[0];
  724. return IRC_WriteStrClientPrefix(target, from,
  725. "PONG %s :%s", s, Req->argv[1]);
  726. }
  727. }
  728. /* The connection timestamp has already been updated when the data has
  729. * been read from so socket, so we don't need to update it here. */
  730. conn = Client_Conn(Client);
  731. #ifndef STRICT_RFC
  732. /* Check authentication PING-PONG ... */
  733. auth_ping = Conn_GetAuthPing(conn);
  734. if (auth_ping) {
  735. LogDebug("AUTH PONG: waiting for token \"%ld\", got \"%s\" ...",
  736. auth_ping, Req->argv[0]);
  737. if (auth_ping == atoi(Req->argv[0])) {
  738. Conn_SetAuthPing(conn, 0);
  739. if (Client_Type(Client) == CLIENT_WAITAUTHPING)
  740. Login_User(Client);
  741. } else
  742. if (!IRC_WriteStrClient(Client,
  743. "To connect, type /QUOTE PONG %ld",
  744. auth_ping))
  745. return DISCONNECTED;
  746. }
  747. #endif
  748. if (Client_Type(Client) == CLIENT_SERVER && Conn_LastPing(conn) == 0) {
  749. Log(LOG_INFO,
  750. "Synchronization with \"%s\" done (connection %d): %ld second%s [%ld users, %ld channels].",
  751. Client_ID(Client), conn, time(NULL) - Conn_GetSignon(conn),
  752. time(NULL) - Conn_GetSignon(conn) == 1 ? "" : "s",
  753. Client_UserCount(), Channel_CountVisible(NULL));
  754. Conn_UpdatePing(conn);
  755. } else
  756. LogDebug("Connection %d: received PONG. Lag: %ld seconds.",
  757. conn, time(NULL) - Conn_LastPing(conn));
  758. return CONNECTED;
  759. } /* IRC_PONG */
  760. /**
  761. * Change the nickname of a client.
  762. *
  763. * @param Origin The client which caused the nickname change.
  764. * @param Target The client of which the nickname should be changed.
  765. * @param NewNick The new nickname.
  766. */
  767. static void
  768. Change_Nick(CLIENT *Origin, CLIENT *Target, char *NewNick, bool InformClient)
  769. {
  770. if (Client_Conn(Target) > NONE) {
  771. /* Local client */
  772. Log(LOG_INFO,
  773. "%s \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
  774. Client_TypeText(Target), Client_Mask(Target),
  775. Client_Conn(Target), Client_ID(Target), NewNick);
  776. Conn_UpdateIdle(Client_Conn(Target));
  777. } else {
  778. /* Remote client */
  779. LogDebug("%s \"%s\" changed nick: \"%s\" -> \"%s\".",
  780. Client_TypeText(Target),
  781. Client_Mask(Target), Client_ID(Target), NewNick);
  782. }
  783. /* Inform all servers and users (which have to know) of the new name */
  784. if (InformClient) {
  785. IRC_WriteStrClientPrefix(Target, Target, "NICK :%s", NewNick);
  786. IRC_WriteStrServersPrefix(NULL, Target, "NICK :%s", NewNick);
  787. } else
  788. IRC_WriteStrServersPrefix(Origin, Target, "NICK :%s", NewNick);
  789. IRC_WriteStrRelatedPrefix(Target, Target, false, "NICK :%s", NewNick);
  790. /* Register old nickname for WHOWAS queries */
  791. Client_RegisterWhowas(Target);
  792. /* Save new nickname */
  793. Client_SetID(Target, NewNick);
  794. }
  795. /* -eof- */