irc-login.c 25 KB

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