irc-login.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
  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 <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <strings.h>
  22. #include <signal.h>
  23. #include <unistd.h>
  24. #include "ngircd.h"
  25. #include "conn-func.h"
  26. #include "conf.h"
  27. #include "channel.h"
  28. #include "io.h"
  29. #include "log.h"
  30. #include "messages.h"
  31. #include "pam.h"
  32. #include "parse.h"
  33. #include "irc.h"
  34. #include "irc-info.h"
  35. #include "irc-write.h"
  36. #include "exp.h"
  37. #include "irc-login.h"
  38. static bool Hello_User PARAMS(( CLIENT *Client ));
  39. static bool Hello_User_PostAuth PARAMS(( CLIENT *Client ));
  40. static void Kill_Nick PARAMS(( char *Nick, char *Reason ));
  41. static void Introduce_Client PARAMS((CLIENT *To, CLIENT *Client, int Type));
  42. static void Reject_Client PARAMS((CLIENT *Client));
  43. static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
  44. void *i));
  45. #ifdef PAM
  46. static void cb_Read_Auth_Result PARAMS((int r_fd, UNUSED short events));
  47. #endif
  48. /**
  49. * Handler for the IRC "PASS" command.
  50. *
  51. * See RFC 2813 section 4.1.1, and RFC 2812 section 3.1.1.
  52. *
  53. * @param Client The client from which this command has been received.
  54. * @param Req Request structure with prefix and all parameters.
  55. * @returns CONNECTED or DISCONNECTED.
  56. */
  57. GLOBAL bool
  58. IRC_PASS( CLIENT *Client, REQUEST *Req )
  59. {
  60. char *type, *orig_flags;
  61. int protohigh, protolow;
  62. assert( Client != NULL );
  63. assert( Req != NULL );
  64. /* Return an error if this is not a local client */
  65. if (Client_Conn(Client) <= NONE)
  66. return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
  67. Client_ID(Client), Req->command);
  68. if (Client_Type(Client) == CLIENT_UNKNOWN && Req->argc == 1) {
  69. /* Not yet registered "unknown" connection, PASS with one
  70. * argument: either a regular client, service, or server
  71. * using the old RFC 1459 section 4.1.1 syntax. */
  72. LogDebug("Connection %d: got PASS command (RFC 1459) ...",
  73. Client_Conn(Client));
  74. } else if ((Client_Type(Client) == CLIENT_UNKNOWN ||
  75. Client_Type(Client) == CLIENT_UNKNOWNSERVER) &&
  76. (Req->argc == 3 || Req->argc == 4)) {
  77. /* Not yet registered "unknown" connection or outgoing server
  78. * link, PASS with three or four argument: server using the
  79. * RFC 2813 section 4.1.1 syntax. */
  80. LogDebug("Connection %d: got PASS command (RFC 2813, new server link) ...",
  81. Client_Conn(Client));
  82. } else if (Client_Type(Client) == CLIENT_UNKNOWN ||
  83. Client_Type(Client) == CLIENT_UNKNOWNSERVER) {
  84. /* Unregistered connection, but wrong number of arguments: */
  85. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  86. Client_ID(Client), Req->command);
  87. } else {
  88. /* Registered connection, PASS command is not allowed! */
  89. return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
  90. Client_ID(Client));
  91. }
  92. Client_SetPassword(Client, Req->argv[0]);
  93. /* Protocol version */
  94. if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
  95. int c2, c4;
  96. c2 = Req->argv[1][2];
  97. c4 = Req->argv[1][4];
  98. Req->argv[1][4] = '\0';
  99. protolow = atoi(&Req->argv[1][2]);
  100. Req->argv[1][2] = '\0';
  101. protohigh = atoi(Req->argv[1]);
  102. Req->argv[1][2] = c2;
  103. Req->argv[1][4] = c4;
  104. Client_SetType(Client, CLIENT_GOTPASS_2813);
  105. } else {
  106. protohigh = protolow = 0;
  107. Client_SetType(Client, CLIENT_GOTPASS);
  108. }
  109. /* Protocol type, see doc/Protocol.txt */
  110. if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
  111. type = &Req->argv[1][4];
  112. else
  113. type = NULL;
  114. /* Protocol flags/options */
  115. if (Req->argc >= 4)
  116. orig_flags = Req->argv[3];
  117. else
  118. orig_flags = "";
  119. /* Implementation, version and IRC+ flags */
  120. if (Req->argc >= 3) {
  121. char *impl, *ptr, *serverver, *flags;
  122. impl = Req->argv[2];
  123. ptr = strchr(impl, '|');
  124. if (ptr)
  125. *ptr = '\0';
  126. if (type && strcmp(type, PROTOIRCPLUS) == 0) {
  127. /* The peer seems to be a server which supports the
  128. * IRC+ protocol (see doc/Protocol.txt). */
  129. serverver = ptr ? ptr + 1 : "?";
  130. flags = strchr(ptr ? serverver : impl, ':');
  131. if (flags) {
  132. *flags = '\0';
  133. flags++;
  134. } else
  135. flags = "";
  136. Log(LOG_INFO,
  137. "Peer on conenction %d announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
  138. Client_Conn(Client), impl, serverver,
  139. protohigh, protolow, flags);
  140. } else {
  141. /* The peer seems to be a server supporting the
  142. * "original" IRC protocol (RFC 2813). */
  143. if (strchr(orig_flags, 'Z'))
  144. flags = "Z";
  145. else
  146. flags = "";
  147. Log(LOG_INFO,
  148. "Peer on connection %d announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
  149. Client_Conn(Client), impl,
  150. protohigh, protolow, flags);
  151. }
  152. Client_SetFlags(Client, flags);
  153. }
  154. return CONNECTED;
  155. } /* IRC_PASS */
  156. /**
  157. * Handler for the IRC "NICK" command.
  158. *
  159. * See RFC 2812, 3.1.2 "Nick message", and RFC 2813, 4.1.3 "Nick".
  160. *
  161. * This function implements the IRC command "NICK" which is used to register
  162. * with the server, to change already registered nicknames and to introduce
  163. * new users which are connected to other servers.
  164. *
  165. * @param Client The client from which this command has been received.
  166. * @param Req Request structure with prefix and all parameters.
  167. * @returns CONNECTED or DISCONNECTED.
  168. */
  169. GLOBAL bool
  170. IRC_NICK( CLIENT *Client, REQUEST *Req )
  171. {
  172. CLIENT *intr_c, *target, *c;
  173. char *nick, *user, *hostname, *modes, *info;
  174. int token, hops;
  175. assert( Client != NULL );
  176. assert( Req != NULL );
  177. /* Some IRC clients, for example BitchX, send the NICK and USER
  178. * commands in the wrong order ... */
  179. if(Client_Type(Client) == CLIENT_UNKNOWN
  180. || Client_Type(Client) == CLIENT_GOTPASS
  181. || Client_Type(Client) == CLIENT_GOTNICK
  182. #ifndef STRICT_RFC
  183. || Client_Type(Client) == CLIENT_GOTUSER
  184. #endif
  185. || Client_Type(Client) == CLIENT_USER
  186. || Client_Type(Client) == CLIENT_SERVICE
  187. || (Client_Type(Client) == CLIENT_SERVER && Req->argc == 1))
  188. {
  189. /* User registration or change of nickname */
  190. /* Wrong number of arguments? */
  191. if( Req->argc != 1 )
  192. return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
  193. Client_ID( Client ),
  194. Req->command );
  195. /* Search "target" client */
  196. if( Client_Type( Client ) == CLIENT_SERVER )
  197. {
  198. target = Client_Search( Req->prefix );
  199. if( ! target )
  200. return IRC_WriteStrClient( Client,
  201. ERR_NOSUCHNICK_MSG,
  202. Client_ID( Client ),
  203. Req->argv[0] );
  204. }
  205. else
  206. {
  207. /* Is this a restricted client? */
  208. if( Client_HasMode( Client, 'r' ))
  209. return IRC_WriteStrClient( Client,
  210. ERR_RESTRICTED_MSG,
  211. Client_ID( Client ));
  212. target = Client;
  213. }
  214. #ifndef STRICT_RFC
  215. /* If the clients tries to change to its own nickname we won't
  216. * do anything. This is how the original ircd behaves and some
  217. * clients (for example Snak) expect it to be like this.
  218. * But I doubt that this is "really the right thing" ... */
  219. if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 )
  220. return CONNECTED;
  221. #endif
  222. /* Check that the new nickname is available. Special case:
  223. * the client only changes from/to upper to lower case. */
  224. if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
  225. {
  226. if( ! Client_CheckNick( target, Req->argv[0] ))
  227. return CONNECTED;
  228. }
  229. if (Client_Type(target) != CLIENT_USER &&
  230. Client_Type(target) != CLIENT_SERVICE &&
  231. Client_Type(target) != CLIENT_SERVER) {
  232. /* New client */
  233. LogDebug("Connection %d: got valid NICK command ...",
  234. Client_Conn( Client ));
  235. /* Register new nickname of this client */
  236. Client_SetID( target, Req->argv[0] );
  237. #ifndef STRICT_RFC
  238. if (Conf_AuthPing) {
  239. Conn_SetAuthPing(Client_Conn(Client), rand());
  240. IRC_WriteStrClient(Client, "PING :%ld",
  241. Conn_GetAuthPing(Client_Conn(Client)));
  242. LogDebug("Connection %d: sent AUTH PING %ld ...",
  243. Client_Conn(Client),
  244. Conn_GetAuthPing(Client_Conn(Client)));
  245. }
  246. #endif
  247. /* If we received a valid USER command already then
  248. * register the new client! */
  249. if( Client_Type( Client ) == CLIENT_GOTUSER )
  250. return Hello_User( Client );
  251. else
  252. Client_SetType( Client, CLIENT_GOTNICK );
  253. } else {
  254. /* Nickname change */
  255. if (Client_Conn(target) > NONE) {
  256. /* Local client */
  257. Log(LOG_INFO,
  258. "%s \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
  259. Client_TypeText(target), Client_Mask(target),
  260. Client_Conn(target), Client_ID(target),
  261. Req->argv[0]);
  262. Conn_UpdateIdle(Client_Conn(target));
  263. } else {
  264. /* Remote client */
  265. LogDebug("%s \"%s\" changed nick: \"%s\" -> \"%s\".",
  266. Client_TypeText(target),
  267. Client_Mask(target), Client_ID(target),
  268. Req->argv[0]);
  269. }
  270. /* Inform all users and servers (which have to know)
  271. * of this nickname change */
  272. if( Client_Type( Client ) == CLIENT_USER )
  273. IRC_WriteStrClientPrefix( Client, Client,
  274. "NICK :%s",
  275. Req->argv[0] );
  276. IRC_WriteStrServersPrefix( Client, target,
  277. "NICK :%s", Req->argv[0] );
  278. IRC_WriteStrRelatedPrefix( target, target, false,
  279. "NICK :%s", Req->argv[0] );
  280. /* Register old nickname for WHOWAS queries */
  281. Client_RegisterWhowas( target );
  282. /* Save new nickname */
  283. Client_SetID( target, Req->argv[0] );
  284. IRC_SetPenalty( target, 2 );
  285. }
  286. return CONNECTED;
  287. } else if(Client_Type(Client) == CLIENT_SERVER ||
  288. Client_Type(Client) == CLIENT_SERVICE) {
  289. /* Server or service introduces new client */
  290. /* Bad number of parameters? */
  291. if (Req->argc != 2 && Req->argc != 7)
  292. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  293. Client_ID(Client), Req->command);
  294. if (Req->argc >= 7) {
  295. /* RFC 2813 compatible syntax */
  296. nick = Req->argv[0];
  297. hops = atoi(Req->argv[1]);
  298. user = Req->argv[2];
  299. hostname = Req->argv[3];
  300. token = atoi(Req->argv[4]);
  301. modes = Req->argv[5] + 1;
  302. info = Req->argv[6];
  303. } else {
  304. /* RFC 1459 compatible syntax */
  305. nick = Req->argv[0];
  306. hops = 1;
  307. user = Req->argv[0];
  308. hostname = Client_ID(Client);
  309. token = atoi(Req->argv[1]);
  310. modes = "";
  311. info = Req->argv[0];
  312. }
  313. c = Client_Search(nick);
  314. if(c) {
  315. /*
  316. * the new nick is already present on this server:
  317. * the new and the old one have to be disconnected now.
  318. */
  319. Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
  320. Kill_Nick( Req->argv[0], "Nick collision" );
  321. return CONNECTED;
  322. }
  323. /* Find the Server this client is connected to */
  324. intr_c = Client_GetFromToken(Client, token);
  325. if( ! intr_c )
  326. {
  327. Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
  328. Kill_Nick( Req->argv[0], "Unknown server" );
  329. return CONNECTED;
  330. }
  331. c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
  332. token, modes, info, true);
  333. if( ! c )
  334. {
  335. /* out of memory, need to disconnect client to keep network state consistent */
  336. Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
  337. Kill_Nick( Req->argv[0], "Server error" );
  338. return CONNECTED;
  339. }
  340. /* RFC 2813: client is now fully registered, inform all the
  341. * other servers about the new user.
  342. * RFC 1459: announce the new client only after receiving the
  343. * USER command, first we need more information! */
  344. if (Req->argc < 7) {
  345. LogDebug("Client \"%s\" is being registered (RFC 1459) ...",
  346. Client_Mask(c));
  347. Client_SetType(c, CLIENT_GOTNICK);
  348. } else
  349. Introduce_Client(Client, c, CLIENT_USER);
  350. return CONNECTED;
  351. }
  352. else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
  353. } /* IRC_NICK */
  354. /**
  355. * Handler for the IRC "USER" command.
  356. *
  357. * See RFC 2812, 3.1.3 "User message".
  358. *
  359. * @param Client The client from which this command has been received.
  360. * @param Req Request structure with prefix and all parameters.
  361. * @returns CONNECTED or DISCONNECTED.
  362. */
  363. GLOBAL bool
  364. IRC_USER(CLIENT * Client, REQUEST * Req)
  365. {
  366. CLIENT *c;
  367. #ifdef IDENTAUTH
  368. char *ptr;
  369. #endif
  370. assert(Client != NULL);
  371. assert(Req != NULL);
  372. if (Client_Type(Client) == CLIENT_GOTNICK ||
  373. #ifndef STRICT_RFC
  374. Client_Type(Client) == CLIENT_UNKNOWN ||
  375. #endif
  376. Client_Type(Client) == CLIENT_GOTPASS)
  377. {
  378. /* New connection */
  379. if (Req->argc != 4)
  380. return IRC_WriteStrClient(Client,
  381. ERR_NEEDMOREPARAMS_MSG,
  382. Client_ID(Client),
  383. Req->command);
  384. /* User name */
  385. #ifdef IDENTAUTH
  386. ptr = Client_User(Client);
  387. if (!ptr || !*ptr || *ptr == '~')
  388. Client_SetUser(Client, Req->argv[0], false);
  389. #else
  390. Client_SetUser(Client, Req->argv[0], false);
  391. #endif
  392. Client_SetOrigUser(Client, Req->argv[0]);
  393. /* "Real name" or user info text: Don't set it to the empty
  394. * string, the original ircd can't deal with such "real names"
  395. * (e. g. "USER user * * :") ... */
  396. if (*Req->argv[3])
  397. Client_SetInfo(Client, Req->argv[3]);
  398. else
  399. Client_SetInfo(Client, "-");
  400. LogDebug("Connection %d: got valid USER command ...",
  401. Client_Conn(Client));
  402. if (Client_Type(Client) == CLIENT_GOTNICK)
  403. return Hello_User(Client);
  404. else
  405. Client_SetType(Client, CLIENT_GOTUSER);
  406. return CONNECTED;
  407. } else if (Client_Type(Client) == CLIENT_SERVER ||
  408. Client_Type(Client) == CLIENT_SERVICE) {
  409. /* Server/service updating an user */
  410. if (Req->argc != 4)
  411. return IRC_WriteStrClient(Client,
  412. ERR_NEEDMOREPARAMS_MSG,
  413. Client_ID(Client),
  414. Req->command);
  415. c = Client_Search(Req->prefix);
  416. if (!c)
  417. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
  418. Client_ID(Client),
  419. Req->prefix);
  420. Client_SetUser(c, Req->argv[0], true);
  421. Client_SetOrigUser(c, Req->argv[0]);
  422. Client_SetHostname(c, Req->argv[1]);
  423. Client_SetInfo(c, Req->argv[3]);
  424. LogDebug("Connection %d: got valid USER command for \"%s\".",
  425. Client_Conn(Client), Client_Mask(c));
  426. /* RFC 1459 style user registration?
  427. * Introduce client to network: */
  428. if (Client_Type(c) == CLIENT_GOTNICK)
  429. Introduce_Client(Client, c, CLIENT_USER);
  430. return CONNECTED;
  431. } else if (Client_Type(Client) == CLIENT_USER) {
  432. /* Already registered connection */
  433. return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
  434. Client_ID(Client));
  435. } else {
  436. /* Unexpected/invalid connection state? */
  437. return IRC_WriteStrClient(Client, ERR_NOTREGISTERED_MSG,
  438. Client_ID(Client));
  439. }
  440. } /* IRC_USER */
  441. /**
  442. * Handler for the IRC "SERVICE" command.
  443. *
  444. * This function implements IRC Services registration using the SERVICE command
  445. * defined in RFC 2812 3.1.6 and RFC 2813 4.1.4.
  446. *
  447. * At the moment ngIRCd doesn't support directly linked services, so this
  448. * function returns ERR_ERRONEUSNICKNAME when the SERVICE command has not been
  449. * received from a peer server.
  450. *
  451. * @param Client The client from which this command has been received.
  452. * @param Req Request structure with prefix and all parameters.
  453. * @returns CONNECTED or DISCONNECTED..
  454. */
  455. GLOBAL bool
  456. IRC_SERVICE(CLIENT *Client, REQUEST *Req)
  457. {
  458. CLIENT *c, *intr_c;
  459. char *nick, *user, *host, *info, *modes, *ptr;
  460. int token, hops;
  461. assert(Client != NULL);
  462. assert(Req != NULL);
  463. if (Client_Type(Client) != CLIENT_GOTPASS &&
  464. Client_Type(Client) != CLIENT_SERVER)
  465. return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
  466. Client_ID(Client));
  467. if (Req->argc != 6)
  468. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  469. Client_ID(Client), Req->command);
  470. if (Client_Type(Client) != CLIENT_SERVER)
  471. return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
  472. Client_ID(Client), Req->argv[0]);
  473. /* Bad number of parameters? */
  474. if (Req->argc != 6)
  475. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  476. Client_ID(Client), Req->command);
  477. nick = Req->argv[0];
  478. user = NULL; host = NULL;
  479. token = atoi(Req->argv[1]);
  480. hops = atoi(Req->argv[4]);
  481. info = Req->argv[5];
  482. /* Validate service name ("nick name") */
  483. c = Client_Search(nick);
  484. if(c) {
  485. /* Nick name collission: disconnect (KILL) both clients! */
  486. Log(LOG_ERR, "Server %s introduces already registered service \"%s\"!",
  487. Client_ID(Client), nick);
  488. Kill_Nick(nick, "Nick collision");
  489. return CONNECTED;
  490. }
  491. /* Get the server to which the service is connected */
  492. intr_c = Client_GetFromToken(Client, token);
  493. if (! intr_c) {
  494. Log(LOG_ERR, "Server %s introduces service \"%s\" on unknown server!?",
  495. Client_ID(Client), nick);
  496. Kill_Nick(nick, "Unknown server");
  497. return CONNECTED;
  498. }
  499. /* Get user and host name */
  500. ptr = strchr(nick, '@');
  501. if (ptr) {
  502. *ptr = '\0';
  503. host = ++ptr;
  504. }
  505. if (!host)
  506. host = Client_Hostname(intr_c);
  507. ptr = strchr(nick, '!');
  508. if (ptr) {
  509. *ptr = '\0';
  510. user = ++ptr;
  511. }
  512. if (!user)
  513. user = nick;
  514. /* According to RFC 2812/2813 parameter 4 <type> "is currently reserved
  515. * for future usage"; but we use it to transfer the modes and check
  516. * that the first character is a '+' sign and ignore it otherwise. */
  517. modes = (Req->argv[3][0] == '+') ? ++Req->argv[3] : "";
  518. c = Client_NewRemoteUser(intr_c, nick, hops, user, host,
  519. token, modes, info, true);
  520. if (! c) {
  521. /* Couldn't create client structure, so KILL the service to
  522. * keep network status consistent ... */
  523. Log(LOG_ALERT, "Can't create client structure! (on connection %d)",
  524. Client_Conn(Client));
  525. Kill_Nick(nick, "Server error");
  526. return CONNECTED;
  527. }
  528. Introduce_Client(Client, c, CLIENT_SERVICE);
  529. return CONNECTED;
  530. } /* IRC_SERVICE */
  531. /**
  532. * Handler for the IRC "WEBIRC" command.
  533. *
  534. * See doc/Protocol.txt, section II.4:
  535. * "Update webchat/proxy client information".
  536. *
  537. * @param Client The client from which this command has been received.
  538. * @param Req Request structure with prefix and all parameters.
  539. * @returns CONNECTED or DISCONNECTED.
  540. */
  541. GLOBAL bool
  542. IRC_WEBIRC(CLIENT *Client, REQUEST *Req)
  543. {
  544. /* Exactly 4 parameters are requited */
  545. if (Req->argc != 4)
  546. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  547. Client_ID(Client), Req->command);
  548. if (!Conf_WebircPwd[0] || strcmp(Req->argv[0], Conf_WebircPwd) != 0)
  549. return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
  550. Client_ID(Client));
  551. LogDebug("Connection %d: got valid WEBIRC command: user=%s, host=%s, ip=%s",
  552. Client_Conn(Client), Req->argv[1], Req->argv[2], Req->argv[3]);
  553. Client_SetUser(Client, Req->argv[1], true);
  554. Client_SetOrigUser(Client, Req->argv[1]);
  555. Client_SetHostname(Client, Req->argv[2]);
  556. return CONNECTED;
  557. } /* IRC_WEBIRC */
  558. /**
  559. * Handler for the IRC "QUIT" command.
  560. *
  561. * See RFC 2812, 3.1.7 "Quit", and RFC 2813, 4.1.5 "Quit".
  562. *
  563. * @param Client The client from which this command has been received.
  564. * @param Req Request structure with prefix and all parameters.
  565. * @returns CONNECTED or DISCONNECTED.
  566. */
  567. GLOBAL bool
  568. IRC_QUIT( CLIENT *Client, REQUEST *Req )
  569. {
  570. CLIENT *target;
  571. char quitmsg[LINE_LEN];
  572. assert( Client != NULL );
  573. assert( Req != NULL );
  574. /* Wrong number of arguments? */
  575. if( Req->argc > 1 )
  576. return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  577. if (Req->argc == 1)
  578. strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
  579. if ( Client_Type( Client ) == CLIENT_SERVER )
  580. {
  581. /* Server */
  582. target = Client_Search( Req->prefix );
  583. if( ! target )
  584. {
  585. Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
  586. return CONNECTED;
  587. }
  588. Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
  589. return CONNECTED;
  590. }
  591. else
  592. {
  593. if (Req->argc == 1 && quitmsg[0] != '\"') {
  594. /* " " to avoid confusion */
  595. strlcpy(quitmsg, "\"", sizeof quitmsg);
  596. strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
  597. strlcat(quitmsg, "\"", sizeof quitmsg );
  598. }
  599. /* User, Service, or not yet registered */
  600. Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
  601. return DISCONNECTED;
  602. }
  603. } /* IRC_QUIT */
  604. #ifndef STRICT_RFC
  605. /**
  606. * Handler for HTTP command, e.g. GET and POST
  607. *
  608. * We handle these commands here to avoid the quite long timeout when
  609. * some user tries to access this IRC daemon using an web browser ...
  610. *
  611. * @param Client The client from which this command has been received.
  612. * @param Req Request structure with prefix and all parameters.
  613. * @returns CONNECTED or DISCONNECTED.
  614. */
  615. GLOBAL bool
  616. IRC_QUIT_HTTP( CLIENT *Client, REQUEST *Req )
  617. {
  618. Req->argc = 1;
  619. Req->argv[0] = "Oops, HTTP request received? This is IRC!";
  620. return IRC_QUIT(Client, Req);
  621. } /* IRC_QUIT_HTTP */
  622. #endif
  623. /**
  624. * Handler for the IRC "PING" command.
  625. *
  626. * See RFC 2812, 3.7.2 "Ping message".
  627. *
  628. * @param Client The client from which this command has been received.
  629. * @param Req Request structure with prefix and all parameters.
  630. * @returns CONNECTED or DISCONNECTED.
  631. */
  632. GLOBAL bool
  633. IRC_PING(CLIENT *Client, REQUEST *Req)
  634. {
  635. CLIENT *target, *from;
  636. assert(Client != NULL);
  637. assert(Req != NULL);
  638. if (Req->argc < 1)
  639. return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
  640. Client_ID(Client));
  641. #ifdef STRICT_RFC
  642. /* Don't ignore additional arguments when in "strict" mode */
  643. if (Req->argc > 2)
  644. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  645. Client_ID(Client), Req->command);
  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_WriteStrClient(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. from = Client_Search(Req->prefix);
  657. else
  658. from = Client;
  659. if (!from)
  660. return IRC_WriteStrClient(Client,
  661. ERR_NOSUCHSERVER_MSG,
  662. Client_ID(Client), Req->prefix);
  663. return IRC_WriteStrClientPrefix(target, from,
  664. "PING %s :%s", Req->argv[0],
  665. Req->argv[1] );
  666. }
  667. }
  668. if (Client_Type(Client) == CLIENT_SERVER) {
  669. if (Req->prefix)
  670. from = Client_Search(Req->prefix);
  671. else
  672. from = Client;
  673. } else
  674. from = Client_ThisServer();
  675. if (!from)
  676. return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
  677. Client_ID(Client), Req->prefix);
  678. Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
  679. Client_Conn(Client));
  680. #ifdef STRICT_RFC
  681. return IRC_WriteStrClient(Client, "PONG %s :%s",
  682. Client_ID(from), Client_ID(Client));
  683. #else
  684. /* Some clients depend on the argument being returned in the PONG
  685. * reply (not mentioned in any RFC, though) */
  686. return IRC_WriteStrClient(Client, "PONG %s :%s",
  687. Client_ID(from), Req->argv[0]);
  688. #endif
  689. } /* IRC_PING */
  690. /**
  691. * Handler for the IRC "PONG" command.
  692. *
  693. * See RFC 2812, 3.7.3 "Pong message".
  694. *
  695. * @param Client The client from which this command has been received.
  696. * @param Req Request structure with prefix and all parameters.
  697. * @returns CONNECTED or DISCONNECTED.
  698. */
  699. GLOBAL bool
  700. IRC_PONG(CLIENT *Client, REQUEST *Req)
  701. {
  702. CLIENT *target, *from;
  703. CONN_ID conn;
  704. #ifndef STRICT_RFC
  705. long auth_ping;
  706. #endif
  707. char *s;
  708. assert(Client != NULL);
  709. assert(Req != NULL);
  710. /* Wrong number of arguments? */
  711. if (Req->argc < 1) {
  712. if (Client_Type(Client) == CLIENT_USER)
  713. return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
  714. Client_ID(Client));
  715. else
  716. return CONNECTED;
  717. }
  718. if (Req->argc > 2) {
  719. if (Client_Type(Client) == CLIENT_USER)
  720. return IRC_WriteStrClient(Client,
  721. ERR_NEEDMOREPARAMS_MSG,
  722. Client_ID(Client),
  723. Req->command);
  724. else
  725. return CONNECTED;
  726. }
  727. /* Forward? */
  728. if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
  729. target = Client_Search(Req->argv[0]);
  730. if (!target)
  731. return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
  732. Client_ID(Client), Req->argv[0]);
  733. from = Client_Search(Req->prefix);
  734. if (target != Client_ThisServer() && target != from) {
  735. /* Ok, we have to forward the message. */
  736. if (!from)
  737. return IRC_WriteStrClient(Client,
  738. ERR_NOSUCHSERVER_MSG,
  739. Client_ID(Client), Req->prefix);
  740. if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
  741. s = Client_ID(from);
  742. else
  743. s = Req->argv[0];
  744. return IRC_WriteStrClientPrefix(target, from,
  745. "PONG %s :%s", s, Req->argv[1]);
  746. }
  747. }
  748. /* The connection timestamp has already been updated when the data has
  749. * been read from so socket, so we don't need to update it here. */
  750. conn = Client_Conn(Client);
  751. #ifndef STRICT_RFC
  752. /* Check authentication PING-PONG ... */
  753. auth_ping = Conn_GetAuthPing(conn);
  754. if (auth_ping) {
  755. LogDebug("AUTH PONG: waiting for token \"%ld\", got \"%s\" ...",
  756. auth_ping, Req->argv[0]);
  757. if (auth_ping == atoi(Req->argv[0])) {
  758. Conn_SetAuthPing(conn, 0);
  759. if (Client_Type(Client) == CLIENT_WAITAUTHPING)
  760. Hello_User(Client);
  761. } else
  762. if (!IRC_WriteStrClient(Client,
  763. "To connect, type /QUOTE PONG %ld",
  764. auth_ping))
  765. return DISCONNECTED;
  766. }
  767. #endif
  768. #ifdef DEBUG
  769. if (conn > NONE)
  770. Log(LOG_DEBUG,
  771. "Connection %d: received PONG. Lag: %ld seconds.", conn,
  772. time(NULL) - Conn_LastPing(Client_Conn(Client)));
  773. else
  774. Log(LOG_DEBUG,
  775. "Connection %d: received PONG.", conn);
  776. #endif
  777. return CONNECTED;
  778. } /* IRC_PONG */
  779. /**
  780. * Initiate client registration.
  781. *
  782. * This function is called after the daemon received the required NICK and
  783. * USER commands of a new client. If the daemon is compiled with support for
  784. * PAM, the authentication sub-processs is forked; otherwise the global server
  785. * password is checked.
  786. *
  787. * @param Client The client logging in.
  788. * @returns CONNECTED or DISCONNECTED.
  789. */
  790. static bool
  791. Hello_User(CLIENT * Client)
  792. {
  793. #ifdef PAM
  794. int pipefd[2], result;
  795. pid_t pid;
  796. #endif
  797. CONN_ID conn;
  798. assert(Client != NULL);
  799. conn = Client_Conn(Client);
  800. #ifndef STRICT_RFC
  801. if (Conf_AuthPing) {
  802. /* Did we receive the "auth PONG" already? */
  803. if (Conn_GetAuthPing(conn)) {
  804. Client_SetType(Client, CLIENT_WAITAUTHPING);
  805. LogDebug("Connection %d: Waiting for AUTH PONG ...", conn);
  806. return CONNECTED;
  807. }
  808. }
  809. #endif
  810. #ifdef PAM
  811. if (!Conf_PAM) {
  812. /* Don't do any PAM authentication at all, instead emulate
  813. * the beahiour of the daemon compiled without PAM support:
  814. * because there can't be any "server password", all
  815. * passwords supplied are classified as "wrong". */
  816. if(Client_Password(Client)[0] == '\0')
  817. return Hello_User_PostAuth(Client);
  818. Reject_Client(Client);
  819. return DISCONNECTED;
  820. }
  821. /* Fork child process for PAM authentication; and make sure that the
  822. * process timeout is set higher than the login timeout! */
  823. pid = Proc_Fork(Conn_GetProcStat(conn), pipefd,
  824. cb_Read_Auth_Result, Conf_PongTimeout + 1);
  825. if (pid > 0) {
  826. LogDebug("Authenticator for connection %d created (PID %d).",
  827. conn, pid);
  828. return CONNECTED;
  829. } else {
  830. /* Sub process */
  831. Log_Init_Subprocess("Auth");
  832. result = PAM_Authenticate(Client);
  833. if (write(pipefd[1], &result, sizeof(result)) != sizeof(result))
  834. Log_Subprocess(LOG_ERR,
  835. "Failed to pipe result to parent!");
  836. Log_Exit_Subprocess("Auth");
  837. exit(0);
  838. }
  839. #else
  840. /* Check global server password ... */
  841. if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
  842. /* Bad password! */
  843. Reject_Client(Client);
  844. return DISCONNECTED;
  845. }
  846. return Hello_User_PostAuth(Client);
  847. #endif
  848. }
  849. #ifdef PAM
  850. /**
  851. * Read result of the authenticatior sub-process from pipe
  852. *
  853. * @param r_fd File descriptor of the pipe.
  854. * @param events (ignored IO specification)
  855. */
  856. static void
  857. cb_Read_Auth_Result(int r_fd, UNUSED short events)
  858. {
  859. CONN_ID conn;
  860. CLIENT *client;
  861. int result;
  862. size_t len;
  863. PROC_STAT *proc;
  864. LogDebug("Auth: Got callback on fd %d, events %d", r_fd, events);
  865. conn = Conn_GetFromProc(r_fd);
  866. if (conn == NONE) {
  867. /* Ops, none found? Probably the connection has already
  868. * been closed!? We'll ignore that ... */
  869. io_close(r_fd);
  870. LogDebug("Auth: Got callback for unknown connection!?");
  871. return;
  872. }
  873. proc = Conn_GetProcStat(conn);
  874. client = Conn_GetClient(conn);
  875. /* Read result from pipe */
  876. len = Proc_Read(proc, &result, sizeof(result));
  877. if (len == 0)
  878. return;
  879. if (len != sizeof(result)) {
  880. Log(LOG_CRIT, "Auth: Got malformed result!");
  881. Reject_Client(client);
  882. return;
  883. }
  884. if (result == true) {
  885. Client_SetUser(client, Client_OrigUser(client), true);
  886. (void)Hello_User_PostAuth(client);
  887. } else
  888. Reject_Client(client);
  889. }
  890. #endif
  891. /**
  892. * Reject a client because of wrong password.
  893. *
  894. * This function is called either when the global server password or a password
  895. * checked using PAM has been wrong.
  896. *
  897. * @param Client The client to reject.
  898. */
  899. static void
  900. Reject_Client(CLIENT *Client)
  901. {
  902. Log(LOG_ERR,
  903. "User \"%s\" rejected (connection %d): Access denied!",
  904. Client_Mask(Client), Client_Conn(Client));
  905. Conn_Close(Client_Conn(Client), NULL,
  906. "Access denied! Bad password?", true);
  907. }
  908. /**
  909. * Finish client registration.
  910. *
  911. * Introduce the new client to the network and send all "hello messages"
  912. * to it after authentication has been succeeded.
  913. *
  914. * @param Client The client logging in.
  915. * @returns CONNECTED or DISCONNECTED.
  916. */
  917. static bool
  918. Hello_User_PostAuth(CLIENT *Client)
  919. {
  920. Introduce_Client(NULL, Client, CLIENT_USER);
  921. if (!IRC_WriteStrClient
  922. (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
  923. return false;
  924. if (!IRC_WriteStrClient
  925. (Client, RPL_YOURHOST_MSG, Client_ID(Client),
  926. Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
  927. TARGET_VENDOR, TARGET_OS))
  928. return false;
  929. if (!IRC_WriteStrClient
  930. (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
  931. return false;
  932. if (!IRC_WriteStrClient
  933. (Client, RPL_MYINFO_MSG, Client_ID(Client),
  934. Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
  935. CHANMODES))
  936. return false;
  937. /* Features supported by this server (005 numeric, ISUPPORT),
  938. * see <http://www.irc.org/tech_docs/005.html> for details. */
  939. if (!IRC_Send_ISUPPORT(Client))
  940. return DISCONNECTED;
  941. if (!IRC_Send_LUSERS(Client))
  942. return DISCONNECTED;
  943. if (!IRC_Show_MOTD(Client))
  944. return DISCONNECTED;
  945. /* Suspend the client for a second ... */
  946. IRC_SetPenalty(Client, 1);
  947. return CONNECTED;
  948. }
  949. /**
  950. * Kill all users with a specific nick name in the network.
  951. *
  952. * @param Nick Nick name.
  953. * @param Reason Reason for the KILL.
  954. */
  955. static void
  956. Kill_Nick( char *Nick, char *Reason )
  957. {
  958. REQUEST r;
  959. assert( Nick != NULL );
  960. assert( Reason != NULL );
  961. r.prefix = (char *)Client_ThisServer( );
  962. r.argv[0] = Nick;
  963. r.argv[1] = Reason;
  964. r.argc = 2;
  965. Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
  966. IRC_KILL( Client_ThisServer( ), &r );
  967. } /* Kill_Nick */
  968. /**
  969. * Introduce a new user or service client in the network.
  970. *
  971. * @param From Remote server introducing the client or NULL (local).
  972. * @param Client New client.
  973. * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
  974. */
  975. static void
  976. Introduce_Client(CLIENT *From, CLIENT *Client, int Type)
  977. {
  978. /* Set client type (user or service) */
  979. Client_SetType(Client, Type);
  980. if (From) {
  981. if (Conf_IsService(Conf_GetServer(Client_Conn(From)),
  982. Client_ID(Client)))
  983. Client_SetType(Client, CLIENT_SERVICE);
  984. LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
  985. Client_TypeText(Client), Client_Mask(Client),
  986. Client_Modes(Client), Client_ID(From),
  987. Client_ID(Client_Introducer(Client)),
  988. Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
  989. } else {
  990. Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
  991. Client_TypeText(Client), Client_Mask(Client),
  992. Client_Conn(Client));
  993. Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
  994. Client_ID(Client), Client_User(Client),
  995. Client_Hostname(Client),
  996. Conn_IPA(Client_Conn(Client)),
  997. Client_TypeText(Client));
  998. }
  999. /* Inform other servers */
  1000. IRC_WriteStrServersPrefixFlag_CB(From,
  1001. From != NULL ? From : Client_ThisServer(),
  1002. '\0', cb_introduceClient, (void *)Client);
  1003. } /* Introduce_Client */
  1004. /**
  1005. * Introduce a new user or service client to a remote server.
  1006. *
  1007. * This function differentiates between RFC1459 and RFC2813 server links and
  1008. * generates the appropriate commands to register the new user or service.
  1009. *
  1010. * @param To The remote server to inform.
  1011. * @param Prefix Prefix for the generated commands.
  1012. * @param data CLIENT structure of the new client.
  1013. */
  1014. static void
  1015. cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
  1016. {
  1017. CLIENT *c = (CLIENT *)data;
  1018. CONN_ID conn;
  1019. char *modes, *user, *host;
  1020. modes = Client_Modes(c);
  1021. user = Client_User(c) ? Client_User(c) : "-";
  1022. host = Client_Hostname(c) ? Client_Hostname(c) : "-";
  1023. conn = Client_Conn(To);
  1024. if (Conn_Options(conn) & CONN_RFC1459) {
  1025. /* RFC 1459 mode: separate NICK and USER commands */
  1026. Conn_WriteStr(conn, "NICK %s :%d", Client_ID(c),
  1027. Client_Hops(c) + 1);
  1028. Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
  1029. Client_ID(c), user, host,
  1030. Client_ID(Client_Introducer(c)), Client_Info(c));
  1031. if (modes[0])
  1032. Conn_WriteStr(conn, ":%s MODE %s +%s",
  1033. Client_ID(c), Client_ID(c), modes);
  1034. } else {
  1035. /* RFC 2813 mode: one combined NICK or SERVICE command */
  1036. if (Client_Type(c) == CLIENT_SERVICE
  1037. && strchr(Client_Flags(To), 'S'))
  1038. IRC_WriteStrClientPrefix(To, Prefix,
  1039. "SERVICE %s %d * +%s %d :%s",
  1040. Client_Mask(c),
  1041. Client_MyToken(Client_Introducer(c)),
  1042. Client_Modes(c), Client_Hops(c) + 1,
  1043. Client_Info(c));
  1044. else
  1045. IRC_WriteStrClientPrefix(To, Prefix,
  1046. "NICK %s %d %s %s %d +%s :%s",
  1047. Client_ID(c), Client_Hops(c) + 1,
  1048. user, host,
  1049. Client_MyToken(Client_Introducer(c)),
  1050. modes, Client_Info(c));
  1051. }
  1052. } /* cb_introduceClient */
  1053. /* -eof- */