client.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2014 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. #define __client_c__
  12. #include "portab.h"
  13. /**
  14. * @file
  15. * Client management.
  16. */
  17. #include <assert.h>
  18. #include <unistd.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <strings.h>
  23. #include <time.h>
  24. #include <netdb.h>
  25. #include "conn.h"
  26. #include "ngircd.h"
  27. #include "channel.h"
  28. #include "conf.h"
  29. #include "conn-func.h"
  30. #include "hash.h"
  31. #include "irc-write.h"
  32. #include "log.h"
  33. #include "match.h"
  34. #include "messages.h"
  35. #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
  36. static CLIENT *This_Server, *My_Clients;
  37. static WHOWAS My_Whowas[MAX_WHOWAS];
  38. static int Last_Whowas = -1;
  39. static long Max_Users, My_Max_Users;
  40. static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
  41. static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
  42. static CLIENT *New_Client_Struct PARAMS(( void ));
  43. static void Generate_MyToken PARAMS(( CLIENT *Client ));
  44. static void Adjust_Counters PARAMS(( CLIENT *Client ));
  45. static void Free_Client PARAMS(( CLIENT **Client ));
  46. static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
  47. CLIENT *TopServer, int Type, const char *ID,
  48. const char *User, const char *Hostname, const char *Info,
  49. int Hops, int Token, const char *Modes,
  50. bool Idented));
  51. static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
  52. bool SendQuit));
  53. static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
  54. void *i));
  55. GLOBAL void
  56. Client_Init( void )
  57. {
  58. struct hostent *h;
  59. This_Server = New_Client_Struct( );
  60. if( ! This_Server )
  61. {
  62. Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
  63. Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
  64. exit( 1 );
  65. }
  66. /* Client structure for this server */
  67. This_Server->next = NULL;
  68. This_Server->type = CLIENT_SERVER;
  69. This_Server->conn_id = NONE;
  70. This_Server->introducer = This_Server;
  71. This_Server->mytoken = 1;
  72. This_Server->hops = 0;
  73. gethostname( This_Server->host, CLIENT_HOST_LEN );
  74. if (Conf_DNS) {
  75. h = gethostbyname( This_Server->host );
  76. if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
  77. }
  78. Client_SetID( This_Server, Conf_ServerName );
  79. Client_SetInfo( This_Server, Conf_ServerInfo );
  80. My_Clients = This_Server;
  81. memset( &My_Whowas, 0, sizeof( My_Whowas ));
  82. } /* Client_Init */
  83. GLOBAL void
  84. Client_Exit( void )
  85. {
  86. CLIENT *c, *next;
  87. int cnt;
  88. if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
  89. else Client_Destroy( This_Server, "Server going down.", NULL, false );
  90. cnt = 0;
  91. c = My_Clients;
  92. while(c) {
  93. cnt++;
  94. next = (CLIENT *)c->next;
  95. Free_Client(&c);
  96. c = next;
  97. }
  98. if (cnt)
  99. Log(LOG_INFO, "Freed %d client structure%s.",
  100. cnt, cnt == 1 ? "" : "s");
  101. } /* Client_Exit */
  102. GLOBAL CLIENT *
  103. Client_ThisServer( void )
  104. {
  105. return This_Server;
  106. } /* Client_ThisServer */
  107. /**
  108. * Initialize new local client; wrapper function for Init_New_Client().
  109. * @return New CLIENT structure.
  110. */
  111. GLOBAL CLIENT *
  112. Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
  113. {
  114. return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
  115. Hostname, NULL, 0, 0, NULL, Idented);
  116. } /* Client_NewLocal */
  117. /**
  118. * Initialize new remote server; wrapper function for Init_New_Client().
  119. * @return New CLIENT structure.
  120. */
  121. GLOBAL CLIENT *
  122. Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
  123. int Hops, int Token, const char *Info, bool Idented)
  124. {
  125. return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
  126. Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
  127. } /* Client_NewRemoteServer */
  128. /**
  129. * Initialize new remote client; wrapper function for Init_New_Client().
  130. * @return New CLIENT structure.
  131. */
  132. GLOBAL CLIENT *
  133. Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
  134. const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
  135. {
  136. return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
  137. User, Hostname, Info, Hops, Token, Modes, Idented);
  138. } /* Client_NewRemoteUser */
  139. /**
  140. * Initialize new client and set up the given parameters like client type,
  141. * user name, host name, introducing server etc. ...
  142. * @return New CLIENT structure.
  143. */
  144. static CLIENT *
  145. Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
  146. int Type, const char *ID, const char *User, const char *Hostname,
  147. const char *Info, int Hops, int Token, const char *Modes, bool Idented)
  148. {
  149. CLIENT *client;
  150. assert(Idx >= NONE);
  151. assert(Introducer != NULL);
  152. client = New_Client_Struct();
  153. if (!client)
  154. return NULL;
  155. client->starttime = time(NULL);
  156. client->conn_id = Idx;
  157. client->introducer = Introducer;
  158. client->topserver = TopServer;
  159. client->type = Type;
  160. if (ID)
  161. Client_SetID(client, ID);
  162. if (User) {
  163. Client_SetUser(client, User, Idented);
  164. Client_SetOrigUser(client, User);
  165. }
  166. if (Hostname)
  167. Client_SetHostname(client, Hostname);
  168. if (Info)
  169. Client_SetInfo(client, Info);
  170. client->hops = Hops;
  171. client->token = Token;
  172. if (Modes)
  173. Client_SetModes(client, Modes);
  174. if (Type == CLIENT_SERVER)
  175. Generate_MyToken(client);
  176. if (Client_HasMode(client, 'a'))
  177. client->away = strndup(DEFAULT_AWAY_MSG, CLIENT_AWAY_LEN - 1);
  178. client->next = (POINTER *)My_Clients;
  179. My_Clients = client;
  180. Adjust_Counters(client);
  181. return client;
  182. } /* Init_New_Client */
  183. GLOBAL void
  184. Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
  185. {
  186. /* remove a client */
  187. CLIENT *last, *c;
  188. char msg[COMMAND_LEN];
  189. const char *txt;
  190. assert( Client != NULL );
  191. txt = LogMsg ? LogMsg : FwdMsg;
  192. if (!txt)
  193. txt = "Reason unknown";
  194. /* netsplit message */
  195. if( Client->type == CLIENT_SERVER ) {
  196. strlcpy(msg, This_Server->id, sizeof (msg));
  197. strlcat(msg, " ", sizeof (msg));
  198. strlcat(msg, Client->id, sizeof (msg));
  199. }
  200. last = NULL;
  201. c = My_Clients;
  202. while( c )
  203. {
  204. if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
  205. {
  206. /*
  207. * The client that is about to be removed is a server,
  208. * the client we are checking right now is a child of that
  209. * server and thus has to be removed, too.
  210. *
  211. * Call Client_Destroy() recursively with the server as the
  212. * new "object to be removed". This starts the cycle again, until
  213. * all servers that are linked via the original server have been
  214. * removed.
  215. */
  216. Client_Destroy( c, NULL, msg, false );
  217. last = NULL;
  218. c = My_Clients;
  219. continue;
  220. }
  221. if( c == Client )
  222. {
  223. /* found the client: remove it */
  224. if( last ) last->next = c->next;
  225. else My_Clients = (CLIENT *)c->next;
  226. if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
  227. Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
  228. else if( c->type == CLIENT_SERVER )
  229. {
  230. if (c != This_Server) {
  231. if (c->conn_id != NONE)
  232. Log(LOG_NOTICE|LOG_snotice,
  233. "Server \"%s\" unregistered (connection %d): %s.",
  234. c->id, c->conn_id, txt);
  235. else
  236. Log(LOG_NOTICE|LOG_snotice,
  237. "Server \"%s\" unregistered: %s.",
  238. c->id, txt);
  239. }
  240. /* inform other servers */
  241. if( ! NGIRCd_SignalQuit )
  242. {
  243. if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
  244. else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
  245. }
  246. }
  247. else
  248. {
  249. if (c->conn_id != NONE) {
  250. if (c->id[0])
  251. Log(LOG_NOTICE,
  252. "Client \"%s\" unregistered (connection %d): %s.",
  253. c->id, c->conn_id, txt);
  254. else
  255. Log(LOG_NOTICE,
  256. "Client unregistered (connection %d): %s.",
  257. c->conn_id, txt);
  258. } else {
  259. Log(LOG_WARNING,
  260. "Unregistered unknown client \"%s\": %s",
  261. c->id[0] ? c->id : "(No Nick)", txt);
  262. }
  263. }
  264. Free_Client(&c);
  265. break;
  266. }
  267. last = c;
  268. c = (CLIENT *)c->next;
  269. }
  270. } /* Client_Destroy */
  271. /**
  272. * Set client hostname.
  273. *
  274. * If global hostname cloaking is in effect, don't set the real hostname
  275. * but the configured one.
  276. *
  277. * @param Client The client of which the hostname should be set.
  278. * @param Hostname The new hostname.
  279. */
  280. GLOBAL void
  281. Client_SetHostname( CLIENT *Client, const char *Hostname )
  282. {
  283. assert(Client != NULL);
  284. assert(Hostname != NULL);
  285. if (Conf_CloakHost[0]) {
  286. char cloak[GETID_LEN];
  287. strlcpy(cloak, Hostname, GETID_LEN);
  288. strlcat(cloak, Conf_CloakHostSalt, GETID_LEN);
  289. snprintf(cloak, GETID_LEN, Conf_CloakHost, Hash(cloak));
  290. LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
  291. Client_ID(Client), Client->host, cloak);
  292. strlcpy(Client->host, cloak, sizeof(Client->host));
  293. } else {
  294. LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
  295. Client_ID(Client), Client->host, Hostname);
  296. strlcpy(Client->host, Hostname, sizeof(Client->host));
  297. }
  298. } /* Client_SetHostname */
  299. /**
  300. * Set IP address to display for a client.
  301. *
  302. * @param Client The client.
  303. * @param IPAText Textual representation of the IP address or NULL to unset.
  304. */
  305. GLOBAL void
  306. Client_SetIPAText(CLIENT *Client, const char *IPAText)
  307. {
  308. assert(Client != NULL);
  309. if (Client->ipa_text)
  310. free(Client->ipa_text);
  311. if (*IPAText)
  312. Client->ipa_text = strndup(IPAText, CLIENT_HOST_LEN - 1);
  313. else
  314. Client->ipa_text = NULL;
  315. }
  316. GLOBAL void
  317. Client_SetID( CLIENT *Client, const char *ID )
  318. {
  319. assert( Client != NULL );
  320. assert( ID != NULL );
  321. strlcpy( Client->id, ID, sizeof( Client->id ));
  322. if (Conf_CloakUserToNick) {
  323. strlcpy( Client->user, ID, sizeof( Client->user ));
  324. strlcpy( Client->info, ID, sizeof( Client->info ));
  325. }
  326. /* Hash */
  327. Client->hash = Hash( Client->id );
  328. } /* Client_SetID */
  329. GLOBAL void
  330. Client_SetUser( CLIENT *Client, const char *User, bool Idented )
  331. {
  332. /* set clients username */
  333. assert( Client != NULL );
  334. assert( User != NULL );
  335. if (Conf_CloakUserToNick) {
  336. strlcpy(Client->user, Client->id, sizeof(Client->user));
  337. } else if (Idented) {
  338. strlcpy(Client->user, User, sizeof(Client->user));
  339. } else {
  340. Client->user[0] = '~';
  341. strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
  342. }
  343. } /* Client_SetUser */
  344. /**
  345. * Set "original" user name of a client.
  346. * This function saves the "original" user name, the user name specified by
  347. * the peer using the USER command, into the CLIENT structure. This user
  348. * name may be used for authentication, for example.
  349. * @param Client The client.
  350. * @param User User name to set.
  351. */
  352. GLOBAL void
  353. Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
  354. {
  355. assert(Client != NULL);
  356. assert(User != NULL);
  357. #if defined(PAM)
  358. strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
  359. #endif
  360. } /* Client_SetOrigUser */
  361. GLOBAL void
  362. Client_SetInfo( CLIENT *Client, const char *Info )
  363. {
  364. /* set client hostname */
  365. assert( Client != NULL );
  366. assert( Info != NULL );
  367. if (Conf_CloakUserToNick)
  368. strlcpy(Client->info, Client->id, sizeof(Client->info));
  369. else
  370. strlcpy(Client->info, Info, sizeof(Client->info));
  371. } /* Client_SetInfo */
  372. GLOBAL void
  373. Client_SetModes( CLIENT *Client, const char *Modes )
  374. {
  375. assert( Client != NULL );
  376. assert( Modes != NULL );
  377. strlcpy(Client->modes, Modes, sizeof( Client->modes ));
  378. } /* Client_SetModes */
  379. GLOBAL void
  380. Client_SetFlags( CLIENT *Client, const char *Flags )
  381. {
  382. assert( Client != NULL );
  383. assert( Flags != NULL );
  384. strlcpy(Client->flags, Flags, sizeof(Client->flags));
  385. } /* Client_SetFlags */
  386. GLOBAL void
  387. Client_SetAccountName(CLIENT *Client, const char *AccountName)
  388. {
  389. assert(Client != NULL);
  390. if (Client->account_name)
  391. free(Client->account_name);
  392. if (*AccountName)
  393. Client->account_name = strndup(AccountName,
  394. CLIENT_NICK_LEN - 1);
  395. else
  396. Client->account_name = NULL;
  397. }
  398. GLOBAL void
  399. Client_SetAway( CLIENT *Client, const char *Txt )
  400. {
  401. /* Set AWAY reason of client */
  402. assert( Client != NULL );
  403. assert( Txt != NULL );
  404. if (Client->away)
  405. free(Client->away);
  406. Client->away = strndup(Txt, CLIENT_AWAY_LEN - 1);
  407. LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
  408. Client_Mask(Client), Txt);
  409. } /* Client_SetAway */
  410. GLOBAL void
  411. Client_SetType( CLIENT *Client, int Type )
  412. {
  413. assert( Client != NULL );
  414. Client->type = Type;
  415. if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
  416. Adjust_Counters( Client );
  417. } /* Client_SetType */
  418. GLOBAL void
  419. Client_SetHops( CLIENT *Client, int Hops )
  420. {
  421. assert( Client != NULL );
  422. Client->hops = Hops;
  423. } /* Client_SetHops */
  424. GLOBAL void
  425. Client_SetToken( CLIENT *Client, int Token )
  426. {
  427. assert( Client != NULL );
  428. Client->token = Token;
  429. } /* Client_SetToken */
  430. GLOBAL void
  431. Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
  432. {
  433. assert( Client != NULL );
  434. assert( Introducer != NULL );
  435. Client->introducer = Introducer;
  436. } /* Client_SetIntroducer */
  437. GLOBAL bool
  438. Client_ModeAdd( CLIENT *Client, char Mode )
  439. {
  440. /* Set Mode.
  441. * If Client already had Mode, return false.
  442. * If the Mode was newly set, return true.
  443. */
  444. char x[2];
  445. assert( Client != NULL );
  446. x[0] = Mode; x[1] = '\0';
  447. if (!Client_HasMode(Client, x[0])) {
  448. strlcat( Client->modes, x, sizeof( Client->modes ));
  449. return true;
  450. }
  451. else return false;
  452. } /* Client_ModeAdd */
  453. GLOBAL bool
  454. Client_ModeDel( CLIENT *Client, char Mode )
  455. {
  456. /* Delete Mode.
  457. * If Mode was removed, return true.
  458. * If Client did not have Mode, return false.
  459. */
  460. char x[2], *p;
  461. assert( Client != NULL );
  462. x[0] = Mode; x[1] = '\0';
  463. p = strchr( Client->modes, x[0] );
  464. if( ! p ) return false;
  465. /* Client has Mode -> delete */
  466. while( *p )
  467. {
  468. *p = *(p + 1);
  469. p++;
  470. }
  471. return true;
  472. } /* Client_ModeDel */
  473. /**
  474. * Search CLIENT structure of a given nick name.
  475. *
  476. * @return Pointer to CLIENT structure or NULL if not found.
  477. */
  478. GLOBAL CLIENT *
  479. Client_Search( const char *Nick )
  480. {
  481. char search_id[CLIENT_ID_LEN], *ptr;
  482. CLIENT *c = NULL;
  483. UINT32 search_hash;
  484. assert( Nick != NULL );
  485. /* copy Nick and truncate hostmask if necessary */
  486. strlcpy( search_id, Nick, sizeof( search_id ));
  487. ptr = strchr( search_id, '!' );
  488. if( ptr ) *ptr = '\0';
  489. search_hash = Hash(search_id);
  490. c = My_Clients;
  491. while (c) {
  492. if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
  493. return c;
  494. c = (CLIENT *)c->next;
  495. }
  496. return NULL;
  497. }
  498. /**
  499. * Search first CLIENT structure matching a given mask of a server.
  500. *
  501. * The order of servers is arbitrary, but this function makes sure that the
  502. * local server is always returned if the mask matches it.
  503. *
  504. * @return Pointer to CLIENT structure or NULL if no server could be found.
  505. */
  506. GLOBAL CLIENT *
  507. Client_SearchServer(const char *Mask)
  508. {
  509. CLIENT *c;
  510. assert(Mask != NULL);
  511. /* First check if mask matches the local server */
  512. if (MatchCaseInsensitive(Mask, Client_ID(Client_ThisServer())))
  513. return Client_ThisServer();
  514. c = My_Clients;
  515. while (c) {
  516. if (Client_Type(c) == CLIENT_SERVER) {
  517. /* This is a server: check if Mask matches */
  518. if (MatchCaseInsensitive(Mask, c->id))
  519. return c;
  520. }
  521. c = (CLIENT *)c->next;
  522. }
  523. return NULL;
  524. }
  525. /**
  526. * Get client structure ("introducer") identfied by a server token.
  527. * @return CLIENT structure or NULL if none could be found.
  528. */
  529. GLOBAL CLIENT *
  530. Client_GetFromToken( CLIENT *Client, int Token )
  531. {
  532. CLIENT *c;
  533. assert( Client != NULL );
  534. if (!Token)
  535. return NULL;
  536. c = My_Clients;
  537. while (c) {
  538. if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
  539. (c->token == Token))
  540. return c;
  541. c = (CLIENT *)c->next;
  542. }
  543. return NULL;
  544. } /* Client_GetFromToken */
  545. GLOBAL int
  546. Client_Type( CLIENT *Client )
  547. {
  548. assert( Client != NULL );
  549. return Client->type;
  550. } /* Client_Type */
  551. GLOBAL CONN_ID
  552. Client_Conn( CLIENT *Client )
  553. {
  554. assert( Client != NULL );
  555. return Client->conn_id;
  556. } /* Client_Conn */
  557. GLOBAL char *
  558. Client_ID( CLIENT *Client )
  559. {
  560. assert( Client != NULL );
  561. #ifdef DEBUG
  562. if(Client->type == CLIENT_USER)
  563. assert(strlen(Client->id) < Conf_MaxNickLength);
  564. #endif
  565. if( Client->id[0] ) return Client->id;
  566. else return "*";
  567. } /* Client_ID */
  568. GLOBAL char *
  569. Client_Info( CLIENT *Client )
  570. {
  571. assert( Client != NULL );
  572. return Client->info;
  573. } /* Client_Info */
  574. GLOBAL char *
  575. Client_User( CLIENT *Client )
  576. {
  577. assert( Client != NULL );
  578. return Client->user[0] ? Client->user : "~";
  579. } /* Client_User */
  580. #ifdef PAM
  581. /**
  582. * Get the "original" user name as supplied by the USER command.
  583. * The user name as given by the client is used for authentication instead
  584. * of the one detected using IDENT requests.
  585. * @param Client The client.
  586. * @return Original user name.
  587. */
  588. GLOBAL char *
  589. Client_OrigUser(CLIENT *Client) {
  590. return Client->orig_user;
  591. } /* Client_OrigUser */
  592. #endif
  593. /**
  594. * Return the hostname of a client.
  595. * @param Client Pointer to client structure
  596. * @return Pointer to client hostname
  597. */
  598. GLOBAL char *
  599. Client_Hostname(CLIENT *Client)
  600. {
  601. assert (Client != NULL);
  602. return Client->host;
  603. }
  604. /**
  605. * Return the cloaked hostname of a client, if set.
  606. * @param Client Pointer to the client structure.
  607. * @return Pointer to the cloaked hostname or NULL if not set.
  608. */
  609. GLOBAL char *
  610. Client_HostnameCloaked(CLIENT *Client)
  611. {
  612. assert(Client != NULL);
  613. return Client->cloaked;
  614. }
  615. /**
  616. * Get (potentially cloaked) hostname of a client to display it to other users.
  617. *
  618. * If the client has not enabled cloaking, the real hostname is used.
  619. *
  620. * @param Client Pointer to client structure
  621. * @return Pointer to client hostname
  622. */
  623. GLOBAL char *
  624. Client_HostnameDisplayed(CLIENT *Client)
  625. {
  626. assert(Client != NULL);
  627. /* Client isn't cloaked at all, return real hostname: */
  628. if (!Client_HasMode(Client, 'x'))
  629. return Client_Hostname(Client);
  630. /* Use an already saved cloaked hostname, if there is one */
  631. if (Client->cloaked)
  632. return Client->cloaked;
  633. Client_UpdateCloakedHostname(Client, NULL, NULL);
  634. return Client->cloaked;
  635. }
  636. GLOBAL const char *
  637. Client_IPAText(CLIENT *Client)
  638. {
  639. assert(Client != NULL);
  640. /* Not a local client? */
  641. if (Client_Conn(Client) <= NONE)
  642. return "0.0.0.0";
  643. if (!Client->ipa_text)
  644. return Conn_GetIPAInfo(Client_Conn(Client));
  645. else
  646. return Client->ipa_text;
  647. }
  648. /**
  649. * Update (and generate, if necessary) the cloaked hostname of a client.
  650. *
  651. * The newly set cloaked hostname is announced in the network using METADATA
  652. * commands to peers that support this feature.
  653. *
  654. * @param Client The client of which the cloaked hostname should be updated.
  655. * @param Origin The originator of the hostname change, or NULL if this server.
  656. * @param Hostname The new cloaked hostname, or NULL if it should be generated.
  657. */
  658. GLOBAL void
  659. Client_UpdateCloakedHostname(CLIENT *Client, CLIENT *Origin,
  660. const char *Hostname)
  661. {
  662. char Cloak_Buffer[CLIENT_HOST_LEN];
  663. assert(Client != NULL);
  664. if (!Origin)
  665. Origin = Client_ThisServer();
  666. if (!Client->cloaked) {
  667. Client->cloaked = malloc(CLIENT_HOST_LEN);
  668. if (!Client->cloaked)
  669. return;
  670. }
  671. if (!Hostname) {
  672. /* Generate new cloaked hostname */
  673. if (*Conf_CloakHostModeX) {
  674. strlcpy(Cloak_Buffer, Client->host,
  675. sizeof(Cloak_Buffer));
  676. strlcat(Cloak_Buffer, Conf_CloakHostSalt,
  677. sizeof(Cloak_Buffer));
  678. snprintf(Client->cloaked, CLIENT_HOST_LEN,
  679. Conf_CloakHostModeX, Hash(Cloak_Buffer));
  680. } else
  681. strlcpy(Client->cloaked, Client_ID(Client->introducer),
  682. CLIENT_HOST_LEN);
  683. } else
  684. strlcpy(Client->cloaked, Hostname, CLIENT_HOST_LEN);
  685. LogDebug("Cloaked hostname of \"%s\" updated to \"%s\"",
  686. Client_ID(Client), Client->cloaked);
  687. /* Inform other servers in the network */
  688. IRC_WriteStrServersPrefixFlag(Client_NextHop(Origin), Origin, 'M',
  689. "METADATA %s cloakhost :%s",
  690. Client_ID(Client), Client->cloaked);
  691. }
  692. GLOBAL char *
  693. Client_Modes( CLIENT *Client )
  694. {
  695. assert( Client != NULL );
  696. return Client->modes;
  697. } /* Client_Modes */
  698. GLOBAL char *
  699. Client_Flags( CLIENT *Client )
  700. {
  701. assert( Client != NULL );
  702. return Client->flags;
  703. } /* Client_Flags */
  704. GLOBAL int
  705. Client_Hops( CLIENT *Client )
  706. {
  707. assert( Client != NULL );
  708. return Client->hops;
  709. } /* Client_Hops */
  710. GLOBAL int
  711. Client_Token( CLIENT *Client )
  712. {
  713. assert( Client != NULL );
  714. return Client->token;
  715. } /* Client_Token */
  716. GLOBAL int
  717. Client_MyToken( CLIENT *Client )
  718. {
  719. assert( Client != NULL );
  720. return Client->mytoken;
  721. } /* Client_MyToken */
  722. GLOBAL CLIENT *
  723. Client_NextHop( CLIENT *Client )
  724. {
  725. CLIENT *c;
  726. assert( Client != NULL );
  727. c = Client;
  728. while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
  729. c = c->introducer;
  730. return c;
  731. } /* Client_NextHop */
  732. /**
  733. * Return ID of a client: "client!user@host"
  734. * This client ID is used for IRC prefixes, for example.
  735. * Please note that this function uses a global static buffer, so you can't
  736. * nest invocations without overwriting earlier results!
  737. * @param Client Pointer to client structure
  738. * @return Pointer to global buffer containing the client ID
  739. */
  740. GLOBAL char *
  741. Client_Mask( CLIENT *Client )
  742. {
  743. static char Mask_Buffer[GETID_LEN];
  744. assert (Client != NULL);
  745. /* Servers: return name only, there is no "mask" */
  746. if (Client->type == CLIENT_SERVER)
  747. return Client->id;
  748. snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
  749. Client->id, Client->user, Client->host);
  750. return Mask_Buffer;
  751. } /* Client_Mask */
  752. /**
  753. * Return ID of a client with cloaked hostname: "client!user@server-name"
  754. *
  755. * This client ID is used for IRC prefixes, for example.
  756. * Please note that this function uses a global static buffer, so you can't
  757. * nest invocations without overwriting earlier results!
  758. * If the client has not enabled cloaking, the real hostname is used.
  759. *
  760. * @param Client Pointer to client structure
  761. * @return Pointer to global buffer containing the client ID
  762. */
  763. GLOBAL char *
  764. Client_MaskCloaked(CLIENT *Client)
  765. {
  766. static char Mask_Buffer[GETID_LEN];
  767. assert (Client != NULL);
  768. /* Is the client using cloaking at all? */
  769. if (!Client_HasMode(Client, 'x'))
  770. return Client_Mask(Client);
  771. snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
  772. Client_HostnameDisplayed(Client));
  773. return Mask_Buffer;
  774. } /* Client_MaskCloaked */
  775. GLOBAL CLIENT *
  776. Client_Introducer( CLIENT *Client )
  777. {
  778. assert( Client != NULL );
  779. return Client->introducer;
  780. } /* Client_Introducer */
  781. GLOBAL CLIENT *
  782. Client_TopServer( CLIENT *Client )
  783. {
  784. assert( Client != NULL );
  785. return Client->topserver;
  786. } /* Client_TopServer */
  787. GLOBAL bool
  788. Client_HasMode( CLIENT *Client, char Mode )
  789. {
  790. assert( Client != NULL );
  791. return strchr( Client->modes, Mode ) != NULL;
  792. } /* Client_HasMode */
  793. GLOBAL bool
  794. Client_HasFlag( CLIENT *Client, char Flag )
  795. {
  796. assert( Client != NULL );
  797. return strchr( Client->flags, Flag ) != NULL;
  798. } /* Client_HasFlag */
  799. GLOBAL char *
  800. Client_Away( CLIENT *Client )
  801. {
  802. assert( Client != NULL );
  803. return Client->away;
  804. } /* Client_Away */
  805. GLOBAL char *
  806. Client_AccountName(CLIENT *Client)
  807. {
  808. assert(Client != NULL);
  809. return Client->account_name;
  810. }
  811. /**
  812. * Make sure that a given nickname is valid.
  813. *
  814. * If the nickname is not valid for the given client, this function sends back
  815. * the appropriate error messages.
  816. *
  817. * @param Client Client that wants to change the nickname.
  818. * @param Nick New nickname.
  819. * @returns true if nickname is valid, false otherwise.
  820. */
  821. GLOBAL bool
  822. Client_CheckNick(CLIENT *Client, char *Nick)
  823. {
  824. assert(Client != NULL);
  825. assert(Nick != NULL);
  826. if (!Client_IsValidNick(Nick)) {
  827. if (strlen(Nick ) >= Conf_MaxNickLength)
  828. IRC_WriteErrClient(Client, ERR_NICKNAMETOOLONG_MSG,
  829. Client_ID(Client), Nick,
  830. Conf_MaxNickLength - 1);
  831. else
  832. IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
  833. Client_ID(Client), Nick);
  834. return false;
  835. }
  836. if (Client_Type(Client) != CLIENT_SERVER
  837. && Client_Type(Client) != CLIENT_SERVICE) {
  838. /* Make sure that this isn't a restricted/forbidden nickname */
  839. if (Conf_NickIsBlocked(Nick)) {
  840. IRC_WriteErrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
  841. Client_ID(Client), Nick);
  842. return false;
  843. }
  844. }
  845. /* Nickname already registered? */
  846. if (Client_Search(Nick)) {
  847. IRC_WriteErrClient(Client, ERR_NICKNAMEINUSE_MSG,
  848. Client_ID(Client), Nick);
  849. return false;
  850. }
  851. return true;
  852. } /* Client_CheckNick */
  853. GLOBAL bool
  854. Client_CheckID( CLIENT *Client, char *ID )
  855. {
  856. char str[COMMAND_LEN];
  857. CLIENT *c;
  858. assert( Client != NULL );
  859. assert( Client->conn_id > NONE );
  860. assert( ID != NULL );
  861. /* ID too long? */
  862. if (strlen(ID) > CLIENT_ID_LEN) {
  863. IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
  864. Client_ID(Client), ID);
  865. return false;
  866. }
  867. /* ID already in use? */
  868. c = My_Clients;
  869. while (c) {
  870. if (strcasecmp(c->id, ID) == 0) {
  871. snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
  872. if (c->conn_id != NONE)
  873. Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
  874. else
  875. Log(LOG_ERR, "%s (via network)!", str);
  876. Conn_Close(Client->conn_id, str, str, true);
  877. return false;
  878. }
  879. c = (CLIENT *)c->next;
  880. }
  881. return true;
  882. } /* Client_CheckID */
  883. GLOBAL CLIENT *
  884. Client_First( void )
  885. {
  886. return My_Clients;
  887. } /* Client_First */
  888. GLOBAL CLIENT *
  889. Client_Next( CLIENT *c )
  890. {
  891. assert( c != NULL );
  892. return (CLIENT *)c->next;
  893. } /* Client_Next */
  894. GLOBAL long
  895. Client_UserCount( void )
  896. {
  897. return Count( CLIENT_USER );
  898. } /* Client_UserCount */
  899. GLOBAL long
  900. Client_ServiceCount( void )
  901. {
  902. return Count( CLIENT_SERVICE );;
  903. } /* Client_ServiceCount */
  904. GLOBAL long
  905. Client_ServerCount( void )
  906. {
  907. return Count( CLIENT_SERVER );
  908. } /* Client_ServerCount */
  909. GLOBAL long
  910. Client_MyUserCount( void )
  911. {
  912. return MyCount( CLIENT_USER );
  913. } /* Client_MyUserCount */
  914. GLOBAL long
  915. Client_MyServiceCount( void )
  916. {
  917. return MyCount( CLIENT_SERVICE );
  918. } /* Client_MyServiceCount */
  919. GLOBAL unsigned long
  920. Client_MyServerCount( void )
  921. {
  922. CLIENT *c;
  923. unsigned long cnt = 0;
  924. c = My_Clients;
  925. while( c )
  926. {
  927. if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
  928. c = (CLIENT *)c->next;
  929. }
  930. return cnt;
  931. } /* Client_MyServerCount */
  932. GLOBAL unsigned long
  933. Client_OperCount( void )
  934. {
  935. CLIENT *c;
  936. unsigned long cnt = 0;
  937. c = My_Clients;
  938. while( c )
  939. {
  940. if (c && c->type == CLIENT_USER && Client_HasMode(c, 'o' ))
  941. cnt++;
  942. c = (CLIENT *)c->next;
  943. }
  944. return cnt;
  945. } /* Client_OperCount */
  946. GLOBAL unsigned long
  947. Client_UnknownCount( void )
  948. {
  949. CLIENT *c;
  950. unsigned long cnt = 0;
  951. c = My_Clients;
  952. while( c )
  953. {
  954. if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
  955. c = (CLIENT *)c->next;
  956. }
  957. return cnt;
  958. } /* Client_UnknownCount */
  959. GLOBAL long
  960. Client_MaxUserCount( void )
  961. {
  962. return Max_Users;
  963. } /* Client_MaxUserCount */
  964. GLOBAL long
  965. Client_MyMaxUserCount( void )
  966. {
  967. return My_Max_Users;
  968. } /* Client_MyMaxUserCount */
  969. /**
  970. * Check that a given nickname is valid.
  971. *
  972. * @param Nick the nickname to check.
  973. * @returns true if nickname is valid, false otherwise.
  974. */
  975. GLOBAL bool
  976. Client_IsValidNick(const char *Nick)
  977. {
  978. const char *ptr;
  979. static const char goodchars[] = ";0123456789-";
  980. assert (Nick != NULL);
  981. if (strchr(goodchars, Nick[0]))
  982. return false;
  983. if (strlen(Nick ) >= Conf_MaxNickLength)
  984. return false;
  985. ptr = Nick;
  986. while (*ptr) {
  987. if (*ptr < 'A' && !strchr(goodchars, *ptr ))
  988. return false;
  989. if (*ptr > '}')
  990. return false;
  991. ptr++;
  992. }
  993. return true;
  994. } /* Client_IsValidNick */
  995. /**
  996. * Return pointer to "My_Whowas" structure.
  997. */
  998. GLOBAL WHOWAS *
  999. Client_GetWhowas( void )
  1000. {
  1001. return My_Whowas;
  1002. } /* Client_GetWhowas */
  1003. /**
  1004. * Return the index of the last used WHOWAS entry.
  1005. */
  1006. GLOBAL int
  1007. Client_GetLastWhowasIndex( void )
  1008. {
  1009. return Last_Whowas;
  1010. } /* Client_GetLastWhowasIndex */
  1011. /**
  1012. * Get the start time of this client.
  1013. * The result is the start time in seconds since 1970-01-01, as reported
  1014. * by the C function time(NULL).
  1015. */
  1016. GLOBAL time_t
  1017. Client_StartTime(CLIENT *Client)
  1018. {
  1019. assert( Client != NULL );
  1020. return Client->starttime;
  1021. } /* Client_Uptime */
  1022. /**
  1023. * Reject a client when logging in.
  1024. *
  1025. * This function is called when a client isn't allowed to connect to this
  1026. * server. Possible reasons are bad server password, bad PAM password,
  1027. * or that the client is G/K-Line'd.
  1028. *
  1029. * After calling this function, the client isn't connected any more.
  1030. *
  1031. * @param Client The client to reject.
  1032. * @param Reason The reason why the client has been rejected.
  1033. * @param InformClient If true, send the exact reason to the client.
  1034. */
  1035. GLOBAL void
  1036. Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
  1037. {
  1038. char info[COMMAND_LEN];
  1039. assert(Client != NULL);
  1040. assert(Reason != NULL);
  1041. if (InformClient)
  1042. snprintf(info, sizeof(info), "Access denied: %s", Reason);
  1043. else
  1044. strcpy(info, "Access denied: Bad password?");
  1045. Log(LOG_ERR,
  1046. "User \"%s\" rejected (connection %d): %s!",
  1047. Client_Mask(Client), Client_Conn(Client), Reason);
  1048. Conn_Close(Client_Conn(Client), Reason, info, true);
  1049. }
  1050. /**
  1051. * Introduce a new user or service client in the network.
  1052. *
  1053. * @param From Remote server introducing the client or NULL (local).
  1054. * @param Client New client.
  1055. * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
  1056. */
  1057. GLOBAL void
  1058. Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
  1059. {
  1060. /* Set client type (user or service) */
  1061. Client_SetType(Client, Type);
  1062. if (From) {
  1063. if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
  1064. Client_ID(Client)))
  1065. Client_SetType(Client, CLIENT_SERVICE);
  1066. LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
  1067. Client_TypeText(Client), Client_Mask(Client),
  1068. Client_Modes(Client), Client_ID(From),
  1069. Client_ID(Client_Introducer(Client)),
  1070. Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
  1071. } else {
  1072. Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
  1073. Client_TypeText(Client), Client_Mask(Client),
  1074. Client_Conn(Client));
  1075. Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
  1076. Client_ID(Client), Client_User(Client),
  1077. Client_Hostname(Client),
  1078. Conn_IPA(Client_Conn(Client)),
  1079. Client_TypeText(Client));
  1080. }
  1081. /* Inform other servers */
  1082. IRC_WriteStrServersPrefixFlag_CB(From,
  1083. From != NULL ? From : Client_ThisServer(),
  1084. '\0', cb_introduceClient, (void *)Client);
  1085. } /* Client_Introduce */
  1086. static unsigned long
  1087. Count( CLIENT_TYPE Type )
  1088. {
  1089. CLIENT *c;
  1090. unsigned long cnt = 0;
  1091. c = My_Clients;
  1092. while( c )
  1093. {
  1094. if( c->type == Type ) cnt++;
  1095. c = (CLIENT *)c->next;
  1096. }
  1097. return cnt;
  1098. } /* Count */
  1099. static unsigned long
  1100. MyCount( CLIENT_TYPE Type )
  1101. {
  1102. CLIENT *c;
  1103. unsigned long cnt = 0;
  1104. c = My_Clients;
  1105. while( c )
  1106. {
  1107. if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
  1108. c = (CLIENT *)c->next;
  1109. }
  1110. return cnt;
  1111. } /* MyCount */
  1112. /**
  1113. * Allocate and initialize new CLIENT strcuture.
  1114. *
  1115. * @return Pointer to CLIENT structure or NULL on error.
  1116. */
  1117. static CLIENT *
  1118. New_Client_Struct( void )
  1119. {
  1120. CLIENT *c;
  1121. c = (CLIENT *)malloc( sizeof( CLIENT ));
  1122. if( ! c )
  1123. {
  1124. Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
  1125. return NULL;
  1126. }
  1127. memset( c, 0, sizeof ( CLIENT ));
  1128. c->type = CLIENT_UNKNOWN;
  1129. c->conn_id = NONE;
  1130. c->hops = -1;
  1131. c->token = -1;
  1132. c->mytoken = -1;
  1133. return c;
  1134. }
  1135. /**
  1136. * Free a CLIENT structure and its member variables.
  1137. */
  1138. static void
  1139. Free_Client(CLIENT **Client)
  1140. {
  1141. assert(Client != NULL);
  1142. assert(*Client != NULL);
  1143. if ((*Client)->account_name)
  1144. free((*Client)->account_name);
  1145. if ((*Client)->away)
  1146. free((*Client)->away);
  1147. if ((*Client)->cloaked)
  1148. free((*Client)->cloaked);
  1149. if ((*Client)->ipa_text)
  1150. free((*Client)->ipa_text);
  1151. free(*Client);
  1152. *Client = NULL;
  1153. }
  1154. static void
  1155. Generate_MyToken( CLIENT *Client )
  1156. {
  1157. CLIENT *c;
  1158. int token;
  1159. c = My_Clients;
  1160. token = 2;
  1161. while( c )
  1162. {
  1163. if( c->mytoken == token )
  1164. {
  1165. /* The token is already in use */
  1166. token++;
  1167. c = My_Clients;
  1168. continue;
  1169. }
  1170. else c = (CLIENT *)c->next;
  1171. }
  1172. Client->mytoken = token;
  1173. LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
  1174. } /* Generate_MyToken */
  1175. static void
  1176. Adjust_Counters( CLIENT *Client )
  1177. {
  1178. long count;
  1179. assert( Client != NULL );
  1180. if( Client->type != CLIENT_USER ) return;
  1181. if( Client->conn_id != NONE )
  1182. {
  1183. /* Local connection */
  1184. count = Client_MyUserCount( );
  1185. if( count > My_Max_Users ) My_Max_Users = count;
  1186. }
  1187. count = Client_UserCount( );
  1188. if( count > Max_Users ) Max_Users = count;
  1189. } /* Adjust_Counters */
  1190. /**
  1191. * Register client in My_Whowas structure for further recall by WHOWAS.
  1192. * Note: Only clients that have been connected at least 30 seconds will be
  1193. * registered to prevent automated IRC bots to "destroy" a nice server
  1194. * history database.
  1195. */
  1196. GLOBAL void
  1197. Client_RegisterWhowas( CLIENT *Client )
  1198. {
  1199. int slot;
  1200. time_t now;
  1201. assert( Client != NULL );
  1202. /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
  1203. if (Conf_MorePrivacy)
  1204. return;
  1205. now = time(NULL);
  1206. /* Don't register clients that were connected less than 30 seconds. */
  1207. if( now - Client->starttime < 30 )
  1208. return;
  1209. slot = Last_Whowas + 1;
  1210. if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
  1211. #ifdef DEBUG
  1212. Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
  1213. #endif
  1214. My_Whowas[slot].time = now;
  1215. strlcpy( My_Whowas[slot].id, Client_ID( Client ),
  1216. sizeof( My_Whowas[slot].id ));
  1217. strlcpy( My_Whowas[slot].user, Client_User( Client ),
  1218. sizeof( My_Whowas[slot].user ));
  1219. strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
  1220. sizeof( My_Whowas[slot].host ));
  1221. strlcpy( My_Whowas[slot].info, Client_Info( Client ),
  1222. sizeof( My_Whowas[slot].info ));
  1223. strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
  1224. sizeof( My_Whowas[slot].server ));
  1225. Last_Whowas = slot;
  1226. } /* Client_RegisterWhowas */
  1227. GLOBAL const char *
  1228. Client_TypeText(CLIENT *Client)
  1229. {
  1230. assert(Client != NULL);
  1231. switch (Client_Type(Client)) {
  1232. case CLIENT_USER:
  1233. return "User";
  1234. break;
  1235. case CLIENT_SERVICE:
  1236. return "Service";
  1237. break;
  1238. case CLIENT_SERVER:
  1239. return "Server";
  1240. break;
  1241. default:
  1242. return "Client";
  1243. }
  1244. } /* Client_TypeText */
  1245. /**
  1246. * Destroy user or service client.
  1247. */
  1248. static void
  1249. Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
  1250. {
  1251. if(Client->conn_id != NONE) {
  1252. /* Local (directly connected) client */
  1253. Log(LOG_NOTICE,
  1254. "%s \"%s\" unregistered (connection %d): %s.",
  1255. Client_TypeText(Client), Client_Mask(Client),
  1256. Client->conn_id, Txt);
  1257. Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
  1258. Client_ID(Client), Client_User(Client),
  1259. Client_Hostname(Client), Txt);
  1260. if (SendQuit) {
  1261. /* Inforam all the other servers */
  1262. if (FwdMsg)
  1263. IRC_WriteStrServersPrefix(NULL,
  1264. Client, "QUIT :%s", FwdMsg );
  1265. else
  1266. IRC_WriteStrServersPrefix(NULL,
  1267. Client, "QUIT :");
  1268. }
  1269. } else {
  1270. /* Remote client */
  1271. LogDebug("%s \"%s\" unregistered: %s.",
  1272. Client_TypeText(Client), Client_Mask(Client), Txt);
  1273. if(SendQuit) {
  1274. /* Inform all the other servers, but the ones in the
  1275. * direction we got the QUIT from */
  1276. if(FwdMsg)
  1277. IRC_WriteStrServersPrefix(Client_NextHop(Client),
  1278. Client, "QUIT :%s", FwdMsg );
  1279. else
  1280. IRC_WriteStrServersPrefix(Client_NextHop(Client),
  1281. Client, "QUIT :" );
  1282. }
  1283. }
  1284. /* Unregister client from channels */
  1285. Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
  1286. /* Register client in My_Whowas structure */
  1287. Client_RegisterWhowas(Client);
  1288. } /* Destroy_UserOrService */
  1289. /**
  1290. * Introduce a new user or service client to a remote server.
  1291. *
  1292. * @param To The remote server to inform.
  1293. * @param Prefix Prefix for the generated commands.
  1294. * @param data CLIENT structure of the new client.
  1295. */
  1296. static void
  1297. cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
  1298. {
  1299. CLIENT *c = (CLIENT *)data;
  1300. (void)Client_Announce(To, Prefix, c);
  1301. } /* cb_introduceClient */
  1302. /**
  1303. * Announce an user or service to a server.
  1304. *
  1305. * This function differentiates between RFC1459 and RFC2813 server links and
  1306. * generates the appropriate commands to register the user or service.
  1307. *
  1308. * @param Client Server
  1309. * @param Prefix Prefix for the generated commands
  1310. * @param User User to announce
  1311. */
  1312. GLOBAL bool
  1313. Client_Announce(CLIENT * Client, CLIENT * Prefix, CLIENT * User)
  1314. {
  1315. CONN_ID conn;
  1316. char *modes, *user, *host;
  1317. modes = Client_Modes(User);
  1318. user = Client_User(User) ? Client_User(User) : "-";
  1319. host = Client_Hostname(User) ? Client_Hostname(User) : "-";
  1320. conn = Client_Conn(Client);
  1321. if (Conn_Options(conn) & CONN_RFC1459) {
  1322. /* RFC 1459 mode: separate NICK and USER commands */
  1323. if (! Conn_WriteStr(conn, "NICK %s :%d",
  1324. Client_ID(User), Client_Hops(User) + 1))
  1325. return DISCONNECTED;
  1326. if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
  1327. Client_ID(User), user, host,
  1328. Client_ID(Client_Introducer(User)),
  1329. Client_Info(User)))
  1330. return DISCONNECTED;
  1331. if (modes[0]) {
  1332. if (! Conn_WriteStr(conn, ":%s MODE %s +%s",
  1333. Client_ID(User), Client_ID(User),
  1334. modes))
  1335. return DISCONNECTED;
  1336. }
  1337. } else {
  1338. /* RFC 2813 mode: one combined NICK or SERVICE command */
  1339. if (Client_Type(User) == CLIENT_SERVICE
  1340. && Client_HasFlag(Client, 'S')) {
  1341. if (!IRC_WriteStrClientPrefix(Client, Prefix,
  1342. "SERVICE %s %d * +%s %d :%s",
  1343. Client_Mask(User),
  1344. Client_MyToken(Client_Introducer(User)),
  1345. modes, Client_Hops(User) + 1,
  1346. Client_Info(User)))
  1347. return DISCONNECTED;
  1348. } else {
  1349. if (!IRC_WriteStrClientPrefix(Client, Prefix,
  1350. "NICK %s %d %s %s %d +%s :%s",
  1351. Client_ID(User), Client_Hops(User) + 1,
  1352. user, host,
  1353. Client_MyToken(Client_Introducer(User)),
  1354. modes, Client_Info(User)))
  1355. return DISCONNECTED;
  1356. }
  1357. }
  1358. if (Client_HasFlag(Client, 'M')) {
  1359. /* Synchronize metadata */
  1360. if (Client_HostnameCloaked(User)) {
  1361. if (!IRC_WriteStrClientPrefix(Client, Prefix,
  1362. "METADATA %s cloakhost :%s",
  1363. Client_ID(User),
  1364. Client_HostnameCloaked(User)))
  1365. return DISCONNECTED;
  1366. }
  1367. if (Client_AccountName(User)) {
  1368. if (!IRC_WriteStrClientPrefix(Client, Prefix,
  1369. "METADATA %s accountname :%s",
  1370. Client_ID(User),
  1371. Client_AccountName(User)))
  1372. return DISCONNECTED;
  1373. }
  1374. if (Conn_GetCertFp(Client_Conn(User))) {
  1375. if (!IRC_WriteStrClientPrefix(Client, Prefix,
  1376. "METADATA %s certfp :%s",
  1377. Client_ID(User),
  1378. Conn_GetCertFp(Client_Conn(User))))
  1379. return DISCONNECTED;
  1380. }
  1381. }
  1382. return CONNECTED;
  1383. } /* Client_Announce */
  1384. #ifdef DEBUG
  1385. GLOBAL void
  1386. Client_DebugDump(void)
  1387. {
  1388. CLIENT *c;
  1389. Log(LOG_DEBUG, "Client status:");
  1390. c = My_Clients;
  1391. while (c) {
  1392. Log(LOG_DEBUG,
  1393. " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
  1394. Client_ID(c), Client_Type(c), Client_Hostname(c),
  1395. Client_User(c), Client_Conn(c), Client_StartTime(c),
  1396. Client_Flags(c));
  1397. c = (CLIENT *)c->next;
  1398. }
  1399. } /* Client_DumpClients */
  1400. #endif
  1401. /* -eof- */