irc-login.c 25 KB

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