irc-login.c 28 KB

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