irc-login.c 26 KB

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