irc-login.c 27 KB

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