client.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  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. #define __client_c__
  12. #include "portab.h"
  13. /**
  14. * @file
  15. * Client management.
  16. */
  17. #include "imp.h"
  18. #include <assert.h>
  19. #include <unistd.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <strings.h>
  24. #include <netdb.h>
  25. #include "defines.h"
  26. #include "conn.h"
  27. #include "exp.h"
  28. #include "client.h"
  29. #include <imp.h>
  30. #include "ngircd.h"
  31. #include "channel.h"
  32. #include "conf.h"
  33. #include "hash.h"
  34. #include "irc-write.h"
  35. #include "log.h"
  36. #include "messages.h"
  37. #include <exp.h>
  38. #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
  39. static CLIENT *This_Server, *My_Clients;
  40. static WHOWAS My_Whowas[MAX_WHOWAS];
  41. static int Last_Whowas = -1;
  42. static long Max_Users, My_Max_Users;
  43. static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
  44. static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
  45. static CLIENT *New_Client_Struct PARAMS(( void ));
  46. static void Generate_MyToken PARAMS(( CLIENT *Client ));
  47. static void Adjust_Counters PARAMS(( CLIENT *Client ));
  48. static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
  49. CLIENT *TopServer, int Type, const char *ID,
  50. const char *User, const char *Hostname, const char *Info,
  51. int Hops, int Token, const char *Modes,
  52. bool Idented));
  53. static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
  54. bool SendQuit));
  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-Struktur dieses Servers */
  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. {
  94. cnt++;
  95. next = (CLIENT *)c->next;
  96. free( c );
  97. c = next;
  98. }
  99. if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
  100. } /* Client_Exit */
  101. GLOBAL CLIENT *
  102. Client_ThisServer( void )
  103. {
  104. return This_Server;
  105. } /* Client_ThisServer */
  106. /**
  107. * Initialize new local client; wrapper function for Init_New_Client().
  108. * @return New CLIENT structure.
  109. */
  110. GLOBAL CLIENT *
  111. Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
  112. {
  113. return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
  114. Hostname, NULL, 0, 0, NULL, Idented);
  115. } /* Client_NewLocal */
  116. /**
  117. * Initialize new remote server; wrapper function for Init_New_Client().
  118. * @return New CLIENT structure.
  119. */
  120. GLOBAL CLIENT *
  121. Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
  122. int Hops, int Token, const char *Info, bool Idented)
  123. {
  124. return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
  125. Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
  126. } /* Client_NewRemoteServer */
  127. /**
  128. * Initialize new remote client; wrapper function for Init_New_Client().
  129. * @return New CLIENT structure.
  130. */
  131. GLOBAL CLIENT *
  132. Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
  133. const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
  134. {
  135. return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
  136. User, Hostname, Info, Hops, Token, Modes, Idented);
  137. } /* Client_NewRemoteUser */
  138. /**
  139. * Initialize new client and set up the given parameters like client type,
  140. * user name, host name, introducing server etc. ...
  141. * @return New CLIENT structure.
  142. */
  143. static CLIENT *
  144. Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
  145. int Type, const char *ID, const char *User, const char *Hostname,
  146. const char *Info, int Hops, int Token, const char *Modes, bool Idented)
  147. {
  148. CLIENT *client;
  149. assert(Idx >= NONE);
  150. assert(Introducer != NULL);
  151. assert(Hostname != 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 (strchr(client->modes, 'a'))
  177. strlcpy(client->away, DEFAULT_AWAY_MSG, sizeof(client->away));
  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[LINE_LEN];
  189. const char *txt;
  190. assert( Client != NULL );
  191. if( LogMsg ) txt = LogMsg;
  192. else txt = FwdMsg;
  193. if( ! txt ) 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. {
  232. if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
  233. else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
  234. }
  235. /* inform other servers */
  236. if( ! NGIRCd_SignalQuit )
  237. {
  238. if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
  239. else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
  240. }
  241. }
  242. else
  243. {
  244. if( c->conn_id != NONE )
  245. {
  246. if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
  247. else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
  248. } else {
  249. Log(LOG_WARNING, "Unregistered unknown client \"%s\": %s",
  250. c->id[0] ? c->id : "(No Nick)", txt );
  251. }
  252. }
  253. free( c );
  254. break;
  255. }
  256. last = c;
  257. c = (CLIENT *)c->next;
  258. }
  259. } /* Client_Destroy */
  260. GLOBAL void
  261. Client_SetHostname( CLIENT *Client, const char *Hostname )
  262. {
  263. assert( Client != NULL );
  264. assert( Hostname != NULL );
  265. if (strlen(Conf_CloakHost)) {
  266. strlcpy( Client->host, Conf_CloakHost, sizeof( Client->host ));
  267. } else {
  268. strlcpy( Client->host, Hostname, sizeof( Client->host ));
  269. }
  270. } /* Client_SetHostname */
  271. GLOBAL void
  272. Client_SetID( CLIENT *Client, const char *ID )
  273. {
  274. assert( Client != NULL );
  275. assert( ID != NULL );
  276. strlcpy( Client->id, ID, sizeof( Client->id ));
  277. if (Conf_CloakUserToNick) {
  278. strlcpy( Client->user, ID, sizeof( Client->user ));
  279. strlcpy( Client->info, ID, sizeof( Client->info ));
  280. }
  281. /* Hash */
  282. Client->hash = Hash( Client->id );
  283. } /* Client_SetID */
  284. GLOBAL void
  285. Client_SetUser( CLIENT *Client, const char *User, bool Idented )
  286. {
  287. /* set clients username */
  288. assert( Client != NULL );
  289. assert( User != NULL );
  290. if (Conf_CloakUserToNick) {
  291. strlcpy(Client->user, Client->id, sizeof(Client->user));
  292. } else if (Idented) {
  293. strlcpy(Client->user, User, sizeof(Client->user));
  294. } else {
  295. Client->user[0] = '~';
  296. strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
  297. }
  298. } /* Client_SetUser */
  299. /**
  300. * Set "original" user name of a client.
  301. * This function saves the "original" user name, the user name specified by
  302. * the peer using the USER command, into the CLIENT structure. This user
  303. * name may be used for authentication, for example.
  304. * @param Client The client.
  305. * @param User User name to set.
  306. */
  307. GLOBAL void
  308. Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
  309. {
  310. assert(Client != NULL);
  311. assert(User != NULL);
  312. #if defined(PAM) && defined(IDENTAUTH)
  313. strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
  314. #endif
  315. } /* Client_SetOrigUser */
  316. GLOBAL void
  317. Client_SetInfo( CLIENT *Client, const char *Info )
  318. {
  319. /* set client hostname */
  320. assert( Client != NULL );
  321. assert( Info != NULL );
  322. if (Conf_CloakUserToNick)
  323. strlcpy(Client->info, Client->id, sizeof(Client->info));
  324. else
  325. strlcpy(Client->info, Info, sizeof(Client->info));
  326. } /* Client_SetInfo */
  327. GLOBAL void
  328. Client_SetModes( CLIENT *Client, const char *Modes )
  329. {
  330. assert( Client != NULL );
  331. assert( Modes != NULL );
  332. strlcpy(Client->modes, Modes, sizeof( Client->modes ));
  333. } /* Client_SetModes */
  334. GLOBAL void
  335. Client_SetFlags( CLIENT *Client, const char *Flags )
  336. {
  337. assert( Client != NULL );
  338. assert( Flags != NULL );
  339. strlcpy(Client->flags, Flags, sizeof(Client->flags));
  340. } /* Client_SetFlags */
  341. GLOBAL void
  342. Client_SetPassword( CLIENT *Client, const char *Pwd )
  343. {
  344. /* set password sent by client */
  345. assert( Client != NULL );
  346. assert( Pwd != NULL );
  347. strlcpy(Client->pwd, Pwd, sizeof(Client->pwd));
  348. } /* Client_SetPassword */
  349. GLOBAL void
  350. Client_SetAway( CLIENT *Client, const char *Txt )
  351. {
  352. /* Set AWAY reason of client */
  353. assert( Client != NULL );
  354. assert( Txt != NULL );
  355. strlcpy( Client->away, Txt, sizeof( Client->away ));
  356. LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
  357. Client_Mask(Client), Txt);
  358. } /* Client_SetAway */
  359. GLOBAL void
  360. Client_SetType( CLIENT *Client, int Type )
  361. {
  362. assert( Client != NULL );
  363. Client->type = Type;
  364. if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
  365. Adjust_Counters( Client );
  366. } /* Client_SetType */
  367. GLOBAL void
  368. Client_SetHops( CLIENT *Client, int Hops )
  369. {
  370. assert( Client != NULL );
  371. Client->hops = Hops;
  372. } /* Client_SetHops */
  373. GLOBAL void
  374. Client_SetToken( CLIENT *Client, int Token )
  375. {
  376. assert( Client != NULL );
  377. Client->token = Token;
  378. } /* Client_SetToken */
  379. GLOBAL void
  380. Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
  381. {
  382. assert( Client != NULL );
  383. assert( Introducer != NULL );
  384. Client->introducer = Introducer;
  385. } /* Client_SetIntroducer */
  386. GLOBAL void
  387. Client_SetOperByMe( CLIENT *Client, bool OperByMe )
  388. {
  389. assert( Client != NULL );
  390. Client->oper_by_me = OperByMe;
  391. } /* Client_SetOperByMe */
  392. GLOBAL bool
  393. Client_ModeAdd( CLIENT *Client, char Mode )
  394. {
  395. /* Set Mode.
  396. * If Client already alread had Mode, return false.
  397. * If the Mode was newly set, return true.
  398. */
  399. char x[2];
  400. assert( Client != NULL );
  401. x[0] = Mode; x[1] = '\0';
  402. if (!strchr( Client->modes, x[0])) {
  403. strlcat( Client->modes, x, sizeof( Client->modes ));
  404. return true;
  405. }
  406. else return false;
  407. } /* Client_ModeAdd */
  408. GLOBAL bool
  409. Client_ModeDel( CLIENT *Client, char Mode )
  410. {
  411. /* Delete Mode.
  412. * If Mode was removed, return true.
  413. * If Client did not have Mode, return false.
  414. */
  415. char x[2], *p;
  416. assert( Client != NULL );
  417. x[0] = Mode; x[1] = '\0';
  418. p = strchr( Client->modes, x[0] );
  419. if( ! p ) return false;
  420. /* Client has Mode -> delete */
  421. while( *p )
  422. {
  423. *p = *(p + 1);
  424. p++;
  425. }
  426. return true;
  427. } /* Client_ModeDel */
  428. GLOBAL CLIENT *
  429. Client_Search( const char *Nick )
  430. {
  431. /* return Client-Structure that has the corresponding Nick.
  432. * If none is found, return NULL.
  433. */
  434. char search_id[CLIENT_ID_LEN], *ptr;
  435. CLIENT *c = NULL;
  436. UINT32 search_hash;
  437. assert( Nick != NULL );
  438. /* copy Nick and truncate hostmask if necessary */
  439. strlcpy( search_id, Nick, sizeof( search_id ));
  440. ptr = strchr( search_id, '!' );
  441. if( ptr ) *ptr = '\0';
  442. search_hash = Hash(search_id);
  443. c = My_Clients;
  444. while (c) {
  445. if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
  446. return c;
  447. c = (CLIENT *)c->next;
  448. }
  449. return NULL;
  450. } /* Client_Search */
  451. /**
  452. * Get client structure ("introducer") identfied by a server token.
  453. * @return CLIENT structure or NULL if none could be found.
  454. */
  455. GLOBAL CLIENT *
  456. Client_GetFromToken( CLIENT *Client, int Token )
  457. {
  458. CLIENT *c;
  459. assert( Client != NULL );
  460. if (!Token)
  461. return NULL;
  462. c = My_Clients;
  463. while (c) {
  464. if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
  465. (c->token == Token))
  466. return c;
  467. c = (CLIENT *)c->next;
  468. }
  469. return NULL;
  470. } /* Client_GetFromToken */
  471. GLOBAL int
  472. Client_Type( CLIENT *Client )
  473. {
  474. assert( Client != NULL );
  475. return Client->type;
  476. } /* Client_Type */
  477. GLOBAL CONN_ID
  478. Client_Conn( CLIENT *Client )
  479. {
  480. assert( Client != NULL );
  481. return Client->conn_id;
  482. } /* Client_Conn */
  483. GLOBAL char *
  484. Client_ID( CLIENT *Client )
  485. {
  486. assert( Client != NULL );
  487. #ifdef DEBUG
  488. if(Client->type == CLIENT_USER)
  489. assert(strlen(Client->id) < Conf_MaxNickLength);
  490. #endif
  491. if( Client->id[0] ) return Client->id;
  492. else return "*";
  493. } /* Client_ID */
  494. GLOBAL char *
  495. Client_Info( CLIENT *Client )
  496. {
  497. assert( Client != NULL );
  498. return Client->info;
  499. } /* Client_Info */
  500. GLOBAL char *
  501. Client_User( CLIENT *Client )
  502. {
  503. assert( Client != NULL );
  504. return Client->user[0] ? Client->user : "~";
  505. } /* Client_User */
  506. #ifdef PAM
  507. /**
  508. * Get the "original" user name as supplied by the USER command.
  509. * The user name as given by the client is used for authentication instead
  510. * of the one detected using IDENT requests.
  511. * @param Client The client.
  512. * @return Original user name.
  513. */
  514. GLOBAL char *
  515. Client_OrigUser(CLIENT *Client) {
  516. #ifndef IDENTAUTH
  517. char *user = Client->user;
  518. if (user[0] == '~')
  519. user++;
  520. return user;
  521. #else
  522. return Client->orig_user;
  523. #endif
  524. } /* Client_OrigUser */
  525. #endif
  526. /**
  527. * Return the hostname of a client.
  528. * @param Client Pointer to client structure
  529. * @return Pointer to client hostname
  530. */
  531. GLOBAL char *
  532. Client_Hostname(CLIENT *Client)
  533. {
  534. assert (Client != NULL);
  535. return Client->host;
  536. } /* Client_Hostname */
  537. /**
  538. * Get potentially cloaked hostname of a client.
  539. * If the client has not enabled cloaking, the real hostname is used.
  540. * @param Client Pointer to client structure
  541. * @return Pointer to client hostname
  542. */
  543. GLOBAL char *
  544. Client_HostnameCloaked(CLIENT *Client)
  545. {
  546. assert(Client != NULL);
  547. if (Client_HasMode(Client, 'x'))
  548. return Client_ID(Client->introducer);
  549. else
  550. return Client_Hostname(Client);
  551. } /* Client_HostnameCloaked */
  552. GLOBAL char *
  553. Client_Password( CLIENT *Client )
  554. {
  555. assert( Client != NULL );
  556. return Client->pwd;
  557. } /* Client_Password */
  558. GLOBAL char *
  559. Client_Modes( CLIENT *Client )
  560. {
  561. assert( Client != NULL );
  562. return Client->modes;
  563. } /* Client_Modes */
  564. GLOBAL char *
  565. Client_Flags( CLIENT *Client )
  566. {
  567. assert( Client != NULL );
  568. return Client->flags;
  569. } /* Client_Flags */
  570. GLOBAL bool
  571. Client_OperByMe( CLIENT *Client )
  572. {
  573. assert( Client != NULL );
  574. return Client->oper_by_me;
  575. } /* Client_OperByMe */
  576. GLOBAL int
  577. Client_Hops( CLIENT *Client )
  578. {
  579. assert( Client != NULL );
  580. return Client->hops;
  581. } /* Client_Hops */
  582. GLOBAL int
  583. Client_Token( CLIENT *Client )
  584. {
  585. assert( Client != NULL );
  586. return Client->token;
  587. } /* Client_Token */
  588. GLOBAL int
  589. Client_MyToken( CLIENT *Client )
  590. {
  591. assert( Client != NULL );
  592. return Client->mytoken;
  593. } /* Client_MyToken */
  594. GLOBAL CLIENT *
  595. Client_NextHop( CLIENT *Client )
  596. {
  597. CLIENT *c;
  598. assert( Client != NULL );
  599. c = Client;
  600. while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
  601. c = c->introducer;
  602. return c;
  603. } /* Client_NextHop */
  604. /**
  605. * Return ID of a client: "client!user@host"
  606. * This client ID is used for IRC prefixes, for example.
  607. * Please note that this function uses a global static buffer, so you can't
  608. * nest invocations without overwriting erlier results!
  609. * @param Client Pointer to client structure
  610. * @return Pointer to global buffer containing the client ID
  611. */
  612. GLOBAL char *
  613. Client_Mask( CLIENT *Client )
  614. {
  615. static char Mask_Buffer[GETID_LEN];
  616. assert (Client != NULL);
  617. /* Servers: return name only, there is no "mask" */
  618. if (Client->type == CLIENT_SERVER)
  619. return Client->id;
  620. snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
  621. Client->id, Client->user, Client->host);
  622. return Mask_Buffer;
  623. } /* Client_Mask */
  624. /**
  625. * Return ID of a client with cloaked hostname: "client!user@server-name"
  626. * This client ID is used for IRC prefixes, for example.
  627. * Please note that this function uses a global static buffer, so you can't
  628. * nest invocations without overwriting erlier results!
  629. * If the client has not enabled cloaking, the real hostname is used.
  630. * @param Client Pointer to client structure
  631. * @return Pointer to global buffer containing the client ID
  632. */
  633. GLOBAL char *
  634. Client_MaskCloaked(CLIENT *Client)
  635. {
  636. static char Mask_Buffer[GETID_LEN];
  637. assert (Client != NULL);
  638. /* Is the client using cloaking at all? */
  639. if (!Client_HasMode(Client, 'x'))
  640. return Client_Mask(Client);
  641. snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
  642. Client->id, Client->user, Client_ID(Client->introducer));
  643. return Mask_Buffer;
  644. } /* Client_MaskCloaked */
  645. GLOBAL CLIENT *
  646. Client_Introducer( CLIENT *Client )
  647. {
  648. assert( Client != NULL );
  649. return Client->introducer;
  650. } /* Client_Introducer */
  651. GLOBAL CLIENT *
  652. Client_TopServer( CLIENT *Client )
  653. {
  654. assert( Client != NULL );
  655. return Client->topserver;
  656. } /* Client_TopServer */
  657. GLOBAL bool
  658. Client_HasMode( CLIENT *Client, char Mode )
  659. {
  660. assert( Client != NULL );
  661. return strchr( Client->modes, Mode ) != NULL;
  662. } /* Client_HasMode */
  663. GLOBAL char *
  664. Client_Away( CLIENT *Client )
  665. {
  666. assert( Client != NULL );
  667. return Client->away;
  668. } /* Client_Away */
  669. GLOBAL bool
  670. Client_CheckNick( CLIENT *Client, char *Nick )
  671. {
  672. assert( Client != NULL );
  673. assert( Nick != NULL );
  674. if (! Client_IsValidNick( Nick ))
  675. {
  676. IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
  677. return false;
  678. }
  679. /* Nick bereits vergeben? */
  680. if( Client_Search( Nick ))
  681. {
  682. /* den Nick gibt es bereits */
  683. IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
  684. return false;
  685. }
  686. return true;
  687. } /* Client_CheckNick */
  688. GLOBAL bool
  689. Client_CheckID( CLIENT *Client, char *ID )
  690. {
  691. char str[COMMAND_LEN];
  692. CLIENT *c;
  693. assert( Client != NULL );
  694. assert( Client->conn_id > NONE );
  695. assert( ID != NULL );
  696. /* ID too long? */
  697. if (strlen(ID) > CLIENT_ID_LEN) {
  698. IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID(Client), ID);
  699. return false;
  700. }
  701. /* ID already in use? */
  702. c = My_Clients;
  703. while (c) {
  704. if (strcasecmp(c->id, ID) == 0) {
  705. snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
  706. if (c->conn_id != NONE)
  707. Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
  708. else
  709. Log(LOG_ERR, "%s (via network)!", str);
  710. Conn_Close(Client->conn_id, str, str, true);
  711. return false;
  712. }
  713. c = (CLIENT *)c->next;
  714. }
  715. return true;
  716. } /* Client_CheckID */
  717. GLOBAL CLIENT *
  718. Client_First( void )
  719. {
  720. return My_Clients;
  721. } /* Client_First */
  722. GLOBAL CLIENT *
  723. Client_Next( CLIENT *c )
  724. {
  725. assert( c != NULL );
  726. return (CLIENT *)c->next;
  727. } /* Client_Next */
  728. GLOBAL long
  729. Client_UserCount( void )
  730. {
  731. return Count( CLIENT_USER );
  732. } /* Client_UserCount */
  733. GLOBAL long
  734. Client_ServiceCount( void )
  735. {
  736. return Count( CLIENT_SERVICE );;
  737. } /* Client_ServiceCount */
  738. GLOBAL long
  739. Client_ServerCount( void )
  740. {
  741. return Count( CLIENT_SERVER );
  742. } /* Client_ServerCount */
  743. GLOBAL long
  744. Client_MyUserCount( void )
  745. {
  746. return MyCount( CLIENT_USER );
  747. } /* Client_MyUserCount */
  748. GLOBAL long
  749. Client_MyServiceCount( void )
  750. {
  751. return MyCount( CLIENT_SERVICE );
  752. } /* Client_MyServiceCount */
  753. GLOBAL unsigned long
  754. Client_MyServerCount( void )
  755. {
  756. CLIENT *c;
  757. unsigned long cnt = 0;
  758. c = My_Clients;
  759. while( c )
  760. {
  761. if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
  762. c = (CLIENT *)c->next;
  763. }
  764. return cnt;
  765. } /* Client_MyServerCount */
  766. GLOBAL unsigned long
  767. Client_OperCount( void )
  768. {
  769. CLIENT *c;
  770. unsigned long cnt = 0;
  771. c = My_Clients;
  772. while( c )
  773. {
  774. if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
  775. c = (CLIENT *)c->next;
  776. }
  777. return cnt;
  778. } /* Client_OperCount */
  779. GLOBAL unsigned long
  780. Client_UnknownCount( void )
  781. {
  782. CLIENT *c;
  783. unsigned long cnt = 0;
  784. c = My_Clients;
  785. while( c )
  786. {
  787. if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
  788. c = (CLIENT *)c->next;
  789. }
  790. return cnt;
  791. } /* Client_UnknownCount */
  792. GLOBAL long
  793. Client_MaxUserCount( void )
  794. {
  795. return Max_Users;
  796. } /* Client_MaxUserCount */
  797. GLOBAL long
  798. Client_MyMaxUserCount( void )
  799. {
  800. return My_Max_Users;
  801. } /* Client_MyMaxUserCount */
  802. GLOBAL bool
  803. Client_IsValidNick( const char *Nick )
  804. {
  805. const char *ptr;
  806. static const char goodchars[] = ";0123456789-";
  807. assert( Nick != NULL );
  808. if( Nick[0] == '#' ) return false;
  809. if( strchr( goodchars, Nick[0] )) return false;
  810. if( strlen( Nick ) >= Conf_MaxNickLength) return false;
  811. ptr = Nick;
  812. while( *ptr )
  813. {
  814. if (( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return false;
  815. if ( *ptr > '}' ) return false;
  816. ptr++;
  817. }
  818. return true;
  819. } /* Client_IsValidNick */
  820. /**
  821. * Return pointer to "My_Whowas" structure.
  822. */
  823. GLOBAL WHOWAS *
  824. Client_GetWhowas( void )
  825. {
  826. return My_Whowas;
  827. } /* Client_GetWhowas */
  828. /**
  829. * Return the index of the last used WHOWAS entry.
  830. */
  831. GLOBAL int
  832. Client_GetLastWhowasIndex( void )
  833. {
  834. return Last_Whowas;
  835. } /* Client_GetLastWhowasIndex */
  836. /**
  837. * Get the start time of this client.
  838. * The result is the start time in seconds since 1970-01-01, as reported
  839. * by the C function time(NULL).
  840. */
  841. GLOBAL time_t
  842. Client_StartTime(CLIENT *Client)
  843. {
  844. assert( Client != NULL );
  845. return Client->starttime;
  846. } /* Client_Uptime */
  847. static unsigned long
  848. Count( CLIENT_TYPE Type )
  849. {
  850. CLIENT *c;
  851. unsigned long cnt = 0;
  852. c = My_Clients;
  853. while( c )
  854. {
  855. if( c->type == Type ) cnt++;
  856. c = (CLIENT *)c->next;
  857. }
  858. return cnt;
  859. } /* Count */
  860. static unsigned long
  861. MyCount( CLIENT_TYPE Type )
  862. {
  863. CLIENT *c;
  864. unsigned long cnt = 0;
  865. c = My_Clients;
  866. while( c )
  867. {
  868. if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
  869. c = (CLIENT *)c->next;
  870. }
  871. return cnt;
  872. } /* MyCount */
  873. static CLIENT *
  874. New_Client_Struct( void )
  875. {
  876. /* Neue CLIENT-Struktur pre-initialisieren */
  877. CLIENT *c;
  878. c = (CLIENT *)malloc( sizeof( CLIENT ));
  879. if( ! c )
  880. {
  881. Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
  882. return NULL;
  883. }
  884. memset( c, 0, sizeof ( CLIENT ));
  885. c->type = CLIENT_UNKNOWN;
  886. c->conn_id = NONE;
  887. c->oper_by_me = false;
  888. c->hops = -1;
  889. c->token = -1;
  890. c->mytoken = -1;
  891. return c;
  892. } /* New_Client */
  893. static void
  894. Generate_MyToken( CLIENT *Client )
  895. {
  896. CLIENT *c;
  897. int token;
  898. c = My_Clients;
  899. token = 2;
  900. while( c )
  901. {
  902. if( c->mytoken == token )
  903. {
  904. /* Das Token wurde bereits vergeben */
  905. token++;
  906. c = My_Clients;
  907. continue;
  908. }
  909. else c = (CLIENT *)c->next;
  910. }
  911. Client->mytoken = token;
  912. LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
  913. } /* Generate_MyToken */
  914. static void
  915. Adjust_Counters( CLIENT *Client )
  916. {
  917. long count;
  918. assert( Client != NULL );
  919. if( Client->type != CLIENT_USER ) return;
  920. if( Client->conn_id != NONE )
  921. {
  922. /* Local connection */
  923. count = Client_MyUserCount( );
  924. if( count > My_Max_Users ) My_Max_Users = count;
  925. }
  926. count = Client_UserCount( );
  927. if( count > Max_Users ) Max_Users = count;
  928. } /* Adjust_Counters */
  929. /**
  930. * Register client in My_Whowas structure for further recall by WHOWAS.
  931. * Note: Only clients that have been connected at least 30 seconds will be
  932. * registered to prevent automated IRC bots to "destroy" a nice server
  933. * history database.
  934. */
  935. GLOBAL void
  936. Client_RegisterWhowas( CLIENT *Client )
  937. {
  938. int slot;
  939. time_t now;
  940. assert( Client != NULL );
  941. /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
  942. if (Conf_MorePrivacy)
  943. return;
  944. now = time(NULL);
  945. /* Don't register clients that were connected less than 30 seconds. */
  946. if( now - Client->starttime < 30 )
  947. return;
  948. slot = Last_Whowas + 1;
  949. if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
  950. #ifdef DEBUG
  951. Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
  952. #endif
  953. My_Whowas[slot].time = now;
  954. strlcpy( My_Whowas[slot].id, Client_ID( Client ),
  955. sizeof( My_Whowas[slot].id ));
  956. strlcpy( My_Whowas[slot].user, Client_User( Client ),
  957. sizeof( My_Whowas[slot].user ));
  958. strlcpy( My_Whowas[slot].host, Client_HostnameCloaked( Client ),
  959. sizeof( My_Whowas[slot].host ));
  960. strlcpy( My_Whowas[slot].info, Client_Info( Client ),
  961. sizeof( My_Whowas[slot].info ));
  962. strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
  963. sizeof( My_Whowas[slot].server ));
  964. Last_Whowas = slot;
  965. } /* Client_RegisterWhowas */
  966. GLOBAL const char *
  967. Client_TypeText(CLIENT *Client)
  968. {
  969. assert(Client != NULL);
  970. switch (Client_Type(Client)) {
  971. case CLIENT_USER:
  972. return "User";
  973. break;
  974. case CLIENT_SERVICE:
  975. return "Service";
  976. break;
  977. case CLIENT_SERVER:
  978. return "Server";
  979. break;
  980. default:
  981. return "Client";
  982. }
  983. } /* Client_TypeText */
  984. /**
  985. * Destroy user or service client.
  986. */
  987. static void
  988. Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
  989. {
  990. if(Client->conn_id != NONE) {
  991. /* Local (directly connected) client */
  992. Log(LOG_NOTICE,
  993. "%s \"%s\" unregistered (connection %d): %s",
  994. Client_TypeText(Client), Client_Mask(Client),
  995. Client->conn_id, Txt);
  996. Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
  997. Client_ID(Client), Client_User(Client),
  998. Client_Hostname(Client), Txt);
  999. if (SendQuit) {
  1000. /* Inforam all the other servers */
  1001. if (FwdMsg)
  1002. IRC_WriteStrServersPrefix(NULL,
  1003. Client, "QUIT :%s", FwdMsg );
  1004. else
  1005. IRC_WriteStrServersPrefix(NULL,
  1006. Client, "QUIT :");
  1007. }
  1008. } else {
  1009. /* Remote client */
  1010. LogDebug("%s \"%s\" unregistered: %s",
  1011. Client_TypeText(Client), Client_Mask(Client), Txt);
  1012. if(SendQuit) {
  1013. /* Inform all the other servers, but the ones in the
  1014. * direction we got the QUIT from */
  1015. if(FwdMsg)
  1016. IRC_WriteStrServersPrefix(Client_NextHop(Client),
  1017. Client, "QUIT :%s", FwdMsg );
  1018. else
  1019. IRC_WriteStrServersPrefix(Client_NextHop(Client),
  1020. Client, "QUIT :" );
  1021. }
  1022. }
  1023. /* Unregister client from channels */
  1024. Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
  1025. /* Register client in My_Whowas structure */
  1026. Client_RegisterWhowas(Client);
  1027. } /* Destroy_UserOrService */
  1028. #ifdef DEBUG
  1029. GLOBAL void
  1030. Client_DebugDump(void)
  1031. {
  1032. CLIENT *c;
  1033. Log(LOG_DEBUG, "Client status:");
  1034. c = My_Clients;
  1035. while (c) {
  1036. Log(LOG_DEBUG,
  1037. " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
  1038. Client_ID(c), Client_Type(c), Client_Hostname(c),
  1039. Client_User(c), Client_Conn(c), Client_StartTime(c),
  1040. Client_Flags(c));
  1041. c = (CLIENT *)c->next;
  1042. }
  1043. } /* Client_DumpClients */
  1044. #endif
  1045. /* -eof- */