irc-login.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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 <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 "class.h"
  27. #include "conf.h"
  28. #include "channel.h"
  29. #include "io.h"
  30. #include "log.h"
  31. #include "messages.h"
  32. #include "pam.h"
  33. #include "parse.h"
  34. #include "irc.h"
  35. #include "irc-info.h"
  36. #include "irc-write.h"
  37. #include "exp.h"
  38. #include "irc-login.h"
  39. static bool Hello_User PARAMS(( CLIENT *Client ));
  40. static bool Hello_User_PostAuth PARAMS(( CLIENT *Client ));
  41. static void Kill_Nick PARAMS(( char *Nick, char *Reason ));
  42. static void Introduce_Client PARAMS((CLIENT *To, CLIENT *Client, int Type));
  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,
  577. Client_ID(Client), Req->command);
  578. if (Req->argc == 1)
  579. strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
  580. if (Client_Type(Client) == CLIENT_SERVER) {
  581. /* Server */
  582. target = Client_Search(Req->prefix);
  583. if (!target) {
  584. Log(LOG_WARNING,
  585. "Got QUIT from %s for unknown client!?",
  586. Client_ID(Client));
  587. return CONNECTED;
  588. }
  589. if (target != Client) {
  590. Client_Destroy(target, "Got QUIT command.",
  591. Req->argc == 1 ? quitmsg : NULL, true);
  592. return CONNECTED;
  593. } else {
  594. Conn_Close(Client_Conn(Client), "Got QUIT command.",
  595. Req->argc == 1 ? quitmsg : NULL, true);
  596. return DISCONNECTED;
  597. }
  598. } else {
  599. if (Req->argc == 1 && quitmsg[0] != '\"') {
  600. /* " " to avoid confusion */
  601. strlcpy(quitmsg, "\"", sizeof quitmsg);
  602. strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
  603. strlcat(quitmsg, "\"", sizeof quitmsg );
  604. }
  605. /* User, Service, or not yet registered */
  606. Conn_Close(Client_Conn(Client), "Got QUIT command.",
  607. Req->argc == 1 ? quitmsg : NULL, true);
  608. return DISCONNECTED;
  609. }
  610. } /* IRC_QUIT */
  611. #ifndef STRICT_RFC
  612. /**
  613. * Handler for HTTP command, e.g. GET and POST
  614. *
  615. * We handle these commands here to avoid the quite long timeout when
  616. * some user tries to access this IRC daemon using an web browser ...
  617. *
  618. * @param Client The client from which this command has been received.
  619. * @param Req Request structure with prefix and all parameters.
  620. * @returns CONNECTED or DISCONNECTED.
  621. */
  622. GLOBAL bool
  623. IRC_QUIT_HTTP( CLIENT *Client, REQUEST *Req )
  624. {
  625. Req->argc = 1;
  626. Req->argv[0] = "Oops, HTTP request received? This is IRC!";
  627. return IRC_QUIT(Client, Req);
  628. } /* IRC_QUIT_HTTP */
  629. #endif
  630. /**
  631. * Handler for the IRC "PING" command.
  632. *
  633. * See RFC 2812, 3.7.2 "Ping message".
  634. *
  635. * @param Client The client from which this command has been received.
  636. * @param Req Request structure with prefix and all parameters.
  637. * @returns CONNECTED or DISCONNECTED.
  638. */
  639. GLOBAL bool
  640. IRC_PING(CLIENT *Client, REQUEST *Req)
  641. {
  642. CLIENT *target, *from;
  643. assert(Client != NULL);
  644. assert(Req != NULL);
  645. if (Req->argc < 1)
  646. return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
  647. Client_ID(Client));
  648. #ifdef STRICT_RFC
  649. /* Don't ignore additional arguments when in "strict" mode */
  650. if (Req->argc > 2)
  651. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  652. Client_ID(Client), Req->command);
  653. #endif
  654. if (Req->argc > 1) {
  655. /* A target has been specified ... */
  656. target = Client_Search(Req->argv[1]);
  657. if (!target || Client_Type(target) != CLIENT_SERVER)
  658. return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
  659. Client_ID(Client), Req->argv[1]);
  660. if (target != Client_ThisServer()) {
  661. /* Ok, we have to forward the PING */
  662. if (Client_Type(Client) == CLIENT_SERVER)
  663. from = Client_Search(Req->prefix);
  664. else
  665. from = Client;
  666. if (!from)
  667. return IRC_WriteStrClient(Client,
  668. ERR_NOSUCHSERVER_MSG,
  669. Client_ID(Client), Req->prefix);
  670. return IRC_WriteStrClientPrefix(target, from,
  671. "PING %s :%s", Req->argv[0],
  672. Req->argv[1] );
  673. }
  674. }
  675. if (Client_Type(Client) == CLIENT_SERVER) {
  676. if (Req->prefix)
  677. from = Client_Search(Req->prefix);
  678. else
  679. from = Client;
  680. } else
  681. from = Client_ThisServer();
  682. if (!from)
  683. return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
  684. Client_ID(Client), Req->prefix);
  685. Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
  686. Client_Conn(Client));
  687. #ifdef STRICT_RFC
  688. return IRC_WriteStrClient(Client, "PONG %s :%s",
  689. Client_ID(from), Client_ID(Client));
  690. #else
  691. /* Some clients depend on the argument being returned in the PONG
  692. * reply (not mentioned in any RFC, though) */
  693. return IRC_WriteStrClient(Client, "PONG %s :%s",
  694. Client_ID(from), Req->argv[0]);
  695. #endif
  696. } /* IRC_PING */
  697. /**
  698. * Handler for the IRC "PONG" command.
  699. *
  700. * See RFC 2812, 3.7.3 "Pong message".
  701. *
  702. * @param Client The client from which this command has been received.
  703. * @param Req Request structure with prefix and all parameters.
  704. * @returns CONNECTED or DISCONNECTED.
  705. */
  706. GLOBAL bool
  707. IRC_PONG(CLIENT *Client, REQUEST *Req)
  708. {
  709. CLIENT *target, *from;
  710. CONN_ID conn;
  711. #ifndef STRICT_RFC
  712. long auth_ping;
  713. #endif
  714. char *s;
  715. assert(Client != NULL);
  716. assert(Req != NULL);
  717. /* Wrong number of arguments? */
  718. if (Req->argc < 1) {
  719. if (Client_Type(Client) == CLIENT_USER)
  720. return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
  721. Client_ID(Client));
  722. else
  723. return CONNECTED;
  724. }
  725. if (Req->argc > 2) {
  726. if (Client_Type(Client) == CLIENT_USER)
  727. return IRC_WriteStrClient(Client,
  728. ERR_NEEDMOREPARAMS_MSG,
  729. Client_ID(Client),
  730. Req->command);
  731. else
  732. return CONNECTED;
  733. }
  734. /* Forward? */
  735. if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
  736. target = Client_Search(Req->argv[0]);
  737. if (!target)
  738. return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
  739. Client_ID(Client), Req->argv[0]);
  740. from = Client_Search(Req->prefix);
  741. if (target != Client_ThisServer() && target != from) {
  742. /* Ok, we have to forward the message. */
  743. if (!from)
  744. return IRC_WriteStrClient(Client,
  745. ERR_NOSUCHSERVER_MSG,
  746. Client_ID(Client), Req->prefix);
  747. if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
  748. s = Client_ID(from);
  749. else
  750. s = Req->argv[0];
  751. return IRC_WriteStrClientPrefix(target, from,
  752. "PONG %s :%s", s, Req->argv[1]);
  753. }
  754. }
  755. /* The connection timestamp has already been updated when the data has
  756. * been read from so socket, so we don't need to update it here. */
  757. conn = Client_Conn(Client);
  758. #ifndef STRICT_RFC
  759. /* Check authentication PING-PONG ... */
  760. auth_ping = Conn_GetAuthPing(conn);
  761. if (auth_ping) {
  762. LogDebug("AUTH PONG: waiting for token \"%ld\", got \"%s\" ...",
  763. auth_ping, Req->argv[0]);
  764. if (auth_ping == atoi(Req->argv[0])) {
  765. Conn_SetAuthPing(conn, 0);
  766. if (Client_Type(Client) == CLIENT_WAITAUTHPING)
  767. Hello_User(Client);
  768. } else
  769. if (!IRC_WriteStrClient(Client,
  770. "To connect, type /QUOTE PONG %ld",
  771. auth_ping))
  772. return DISCONNECTED;
  773. }
  774. #endif
  775. if (Client_Type(Client) == CLIENT_SERVER && Conn_LastPing(conn) == 0) {
  776. Log(LOG_INFO,
  777. "Synchronization with \"%s\" done (connection %d): %ld seconds [%ld users, %ld channels]",
  778. Client_ID(Client), conn, time(NULL) - Conn_GetSignon(conn),
  779. Client_UserCount(), Channel_CountVisible(NULL));
  780. Conn_UpdatePing(conn);
  781. } else
  782. LogDebug("Connection %d: received PONG. Lag: %ld seconds.",
  783. conn, time(NULL) - Conn_LastPing(conn));
  784. return CONNECTED;
  785. } /* IRC_PONG */
  786. /**
  787. * Initiate client registration.
  788. *
  789. * This function is called after the daemon received the required NICK and
  790. * USER commands of a new client. If the daemon is compiled with support for
  791. * PAM, the authentication sub-processs is forked; otherwise the global server
  792. * password is checked.
  793. *
  794. * @param Client The client logging in.
  795. * @returns CONNECTED or DISCONNECTED.
  796. */
  797. static bool
  798. Hello_User(CLIENT * Client)
  799. {
  800. #ifdef PAM
  801. int pipefd[2], result;
  802. pid_t pid;
  803. #endif
  804. CONN_ID conn;
  805. assert(Client != NULL);
  806. conn = Client_Conn(Client);
  807. #ifndef STRICT_RFC
  808. if (Conf_AuthPing) {
  809. /* Did we receive the "auth PONG" already? */
  810. if (Conn_GetAuthPing(conn)) {
  811. Client_SetType(Client, CLIENT_WAITAUTHPING);
  812. LogDebug("Connection %d: Waiting for AUTH PONG ...", conn);
  813. return CONNECTED;
  814. }
  815. }
  816. #endif
  817. #ifdef PAM
  818. if (!Conf_PAM) {
  819. /* Don't do any PAM authentication at all, instead emulate
  820. * the beahiour of the daemon compiled without PAM support:
  821. * because there can't be any "server password", all
  822. * passwords supplied are classified as "wrong". */
  823. if(Client_Password(Client)[0] == '\0')
  824. return Hello_User_PostAuth(Client);
  825. Client_Reject(Client, "Non-empty password", false);
  826. return DISCONNECTED;
  827. }
  828. if (Conf_PAMIsOptional && strcmp(Client_Password(Client), "") == 0) {
  829. /* Clients are not required to send a password and to be PAM-
  830. * authenticated at all. If not, they won't become "identified"
  831. * and keep the "~" in their supplied user name.
  832. * Therefore it is sensible to either set Conf_PAMisOptional or
  833. * to enable IDENT lookups -- not both. */
  834. return Hello_User_PostAuth(Client);
  835. }
  836. /* Fork child process for PAM authentication; and make sure that the
  837. * process timeout is set higher than the login timeout! */
  838. pid = Proc_Fork(Conn_GetProcStat(conn), pipefd,
  839. cb_Read_Auth_Result, Conf_PongTimeout + 1);
  840. if (pid > 0) {
  841. LogDebug("Authenticator for connection %d created (PID %d).",
  842. conn, pid);
  843. return CONNECTED;
  844. } else {
  845. /* Sub process */
  846. Log_Init_Subprocess("Auth");
  847. Conn_CloseAllSockets(NONE);
  848. result = PAM_Authenticate(Client);
  849. if (write(pipefd[1], &result, sizeof(result)) != sizeof(result))
  850. Log_Subprocess(LOG_ERR,
  851. "Failed to pipe result to parent!");
  852. Log_Exit_Subprocess("Auth");
  853. exit(0);
  854. }
  855. #else
  856. /* Check global server password ... */
  857. if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
  858. /* Bad password! */
  859. Client_Reject(Client, "Bad server password", false);
  860. return DISCONNECTED;
  861. }
  862. return Hello_User_PostAuth(Client);
  863. #endif
  864. }
  865. #ifdef PAM
  866. /**
  867. * Read result of the authenticatior sub-process from pipe
  868. *
  869. * @param r_fd File descriptor of the pipe.
  870. * @param events (ignored IO specification)
  871. */
  872. static void
  873. cb_Read_Auth_Result(int r_fd, UNUSED short events)
  874. {
  875. CONN_ID conn;
  876. CLIENT *client;
  877. int result;
  878. size_t len;
  879. PROC_STAT *proc;
  880. LogDebug("Auth: Got callback on fd %d, events %d", r_fd, events);
  881. conn = Conn_GetFromProc(r_fd);
  882. if (conn == NONE) {
  883. /* Ops, none found? Probably the connection has already
  884. * been closed!? We'll ignore that ... */
  885. io_close(r_fd);
  886. LogDebug("Auth: Got callback for unknown connection!?");
  887. return;
  888. }
  889. proc = Conn_GetProcStat(conn);
  890. client = Conn_GetClient(conn);
  891. /* Read result from pipe */
  892. len = Proc_Read(proc, &result, sizeof(result));
  893. Proc_Close(proc);
  894. if (len == 0)
  895. return;
  896. if (len != sizeof(result)) {
  897. Log(LOG_CRIT, "Auth: Got malformed result!");
  898. Client_Reject(client, "Internal error", false);
  899. return;
  900. }
  901. if (result == true) {
  902. Client_SetUser(client, Client_OrigUser(client), true);
  903. (void)Hello_User_PostAuth(client);
  904. } else
  905. Client_Reject(client, "Bad password", false);
  906. }
  907. #endif
  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. assert(Client != NULL);
  921. if (Class_HandleServerBans(Client) != CONNECTED)
  922. return DISCONNECTED;
  923. Introduce_Client(NULL, Client, CLIENT_USER);
  924. if (!IRC_WriteStrClient
  925. (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
  926. return false;
  927. if (!IRC_WriteStrClient
  928. (Client, RPL_YOURHOST_MSG, Client_ID(Client),
  929. Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
  930. TARGET_VENDOR, TARGET_OS))
  931. return false;
  932. if (!IRC_WriteStrClient
  933. (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
  934. return false;
  935. if (!IRC_WriteStrClient
  936. (Client, RPL_MYINFO_MSG, Client_ID(Client),
  937. Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
  938. CHANMODES))
  939. return false;
  940. /* Features supported by this server (005 numeric, ISUPPORT),
  941. * see <http://www.irc.org/tech_docs/005.html> for details. */
  942. if (!IRC_Send_ISUPPORT(Client))
  943. return DISCONNECTED;
  944. if (!IRC_Send_LUSERS(Client))
  945. return DISCONNECTED;
  946. if (!IRC_Show_MOTD(Client))
  947. return DISCONNECTED;
  948. /* Suspend the client for a second ... */
  949. IRC_SetPenalty(Client, 1);
  950. return CONNECTED;
  951. }
  952. /**
  953. * Kill all users with a specific nick name in the network.
  954. *
  955. * @param Nick Nick name.
  956. * @param Reason Reason for the KILL.
  957. */
  958. static void
  959. Kill_Nick(char *Nick, char *Reason)
  960. {
  961. REQUEST r;
  962. assert (Nick != NULL);
  963. assert (Reason != NULL);
  964. r.prefix = NULL;
  965. r.argv[0] = Nick;
  966. r.argv[1] = Reason;
  967. r.argc = 2;
  968. Log(LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s",
  969. Nick, Reason);
  970. IRC_KILL(Client_ThisServer(), &r);
  971. } /* Kill_Nick */
  972. /**
  973. * Introduce a new user or service client in the network.
  974. *
  975. * @param From Remote server introducing the client or NULL (local).
  976. * @param Client New client.
  977. * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
  978. */
  979. static void
  980. Introduce_Client(CLIENT *From, CLIENT *Client, int Type)
  981. {
  982. /* Set client type (user or service) */
  983. Client_SetType(Client, Type);
  984. if (From) {
  985. if (Conf_IsService(Conf_GetServer(Client_Conn(From)),
  986. Client_ID(Client)))
  987. Client_SetType(Client, CLIENT_SERVICE);
  988. LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
  989. Client_TypeText(Client), Client_Mask(Client),
  990. Client_Modes(Client), Client_ID(From),
  991. Client_ID(Client_Introducer(Client)),
  992. Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
  993. } else {
  994. Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
  995. Client_TypeText(Client), Client_Mask(Client),
  996. Client_Conn(Client));
  997. Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
  998. Client_ID(Client), Client_User(Client),
  999. Client_Hostname(Client),
  1000. Conn_IPA(Client_Conn(Client)),
  1001. Client_TypeText(Client));
  1002. }
  1003. /* Inform other servers */
  1004. IRC_WriteStrServersPrefixFlag_CB(From,
  1005. From != NULL ? From : Client_ThisServer(),
  1006. '\0', cb_introduceClient, (void *)Client);
  1007. } /* Introduce_Client */
  1008. /**
  1009. * Introduce a new user or service client to a remote server.
  1010. *
  1011. * This function differentiates between RFC1459 and RFC2813 server links and
  1012. * generates the appropriate commands to register the new user or service.
  1013. *
  1014. * @param To The remote server to inform.
  1015. * @param Prefix Prefix for the generated commands.
  1016. * @param data CLIENT structure of the new client.
  1017. */
  1018. static void
  1019. cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
  1020. {
  1021. CLIENT *c = (CLIENT *)data;
  1022. CONN_ID conn;
  1023. char *modes, *user, *host;
  1024. modes = Client_Modes(c);
  1025. user = Client_User(c) ? Client_User(c) : "-";
  1026. host = Client_Hostname(c) ? Client_Hostname(c) : "-";
  1027. conn = Client_Conn(To);
  1028. if (Conn_Options(conn) & CONN_RFC1459) {
  1029. /* RFC 1459 mode: separate NICK and USER commands */
  1030. Conn_WriteStr(conn, "NICK %s :%d", Client_ID(c),
  1031. Client_Hops(c) + 1);
  1032. Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
  1033. Client_ID(c), user, host,
  1034. Client_ID(Client_Introducer(c)), Client_Info(c));
  1035. if (modes[0])
  1036. Conn_WriteStr(conn, ":%s MODE %s +%s",
  1037. Client_ID(c), Client_ID(c), modes);
  1038. } else {
  1039. /* RFC 2813 mode: one combined NICK or SERVICE command */
  1040. if (Client_Type(c) == CLIENT_SERVICE
  1041. && strchr(Client_Flags(To), 'S'))
  1042. IRC_WriteStrClientPrefix(To, Prefix,
  1043. "SERVICE %s %d * +%s %d :%s",
  1044. Client_Mask(c),
  1045. Client_MyToken(Client_Introducer(c)),
  1046. Client_Modes(c), Client_Hops(c) + 1,
  1047. Client_Info(c));
  1048. else
  1049. IRC_WriteStrClientPrefix(To, Prefix,
  1050. "NICK %s %d %s %s %d +%s :%s",
  1051. Client_ID(c), Client_Hops(c) + 1,
  1052. user, host,
  1053. Client_MyToken(Client_Introducer(c)),
  1054. modes, Client_Info(c));
  1055. }
  1056. } /* cb_introduceClient */
  1057. /* -eof- */