irc-oper.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. #include "portab.h"
  12. /**
  13. * @file
  14. * IRC operator commands
  15. */
  16. #include <assert.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <signal.h>
  21. #include <time.h>
  22. #include "ngircd.h"
  23. #include "conn-func.h"
  24. #include "conf.h"
  25. #include "channel.h"
  26. #include "class.h"
  27. #include "parse.h"
  28. #include "irc.h"
  29. #include "irc-write.h"
  30. #include "lists.h"
  31. #include "log.h"
  32. #include "match.h"
  33. #include "messages.h"
  34. #include "op.h"
  35. #include "irc-oper.h"
  36. /**
  37. * Handle invalid received OPER command.
  38. * Log OPER attempt and send error message to client.
  39. */
  40. static bool
  41. Bad_OperPass(CLIENT *Client, char *errtoken, char *errmsg)
  42. {
  43. Log(LOG_WARNING, "Got invalid OPER from \"%s\": \"%s\" -- %s!",
  44. Client_Mask(Client), errtoken, errmsg);
  45. /* Increase penalty to slow down possible brute force attacks */
  46. IRC_SetPenalty(Client, 10);
  47. return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
  48. Client_ID(Client));
  49. } /* Bad_OperPass */
  50. /**
  51. * Handler for the IRC "OPER" command.
  52. *
  53. * @param Client The client from which this command has been received.
  54. * @param Req Request structure with prefix and all parameters.
  55. * @return CONNECTED or DISCONNECTED.
  56. */
  57. GLOBAL bool
  58. IRC_OPER( CLIENT *Client, REQUEST *Req )
  59. {
  60. struct Conf_Oper *op;
  61. size_t len, i;
  62. assert( Client != NULL );
  63. assert( Req != NULL );
  64. len = array_length(&Conf_Opers, sizeof(*op));
  65. op = array_start(&Conf_Opers);
  66. for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
  67. ;
  68. if (i >= len)
  69. return Bad_OperPass(Client, Req->argv[0], "not configured");
  70. if (strcmp(op[i].pwd, Req->argv[1]) != 0)
  71. return Bad_OperPass(Client, op[i].name, "bad password");
  72. if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
  73. return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
  74. if (!Client_HasMode(Client, 'o')) {
  75. Client_ModeAdd(Client, 'o');
  76. if (!IRC_WriteStrClient(Client, "MODE %s :+o",
  77. Client_ID(Client)))
  78. return DISCONNECTED;
  79. IRC_WriteStrServersPrefix(NULL, Client, "MODE %s :+o",
  80. Client_ID(Client));
  81. }
  82. Log(LOG_NOTICE|LOG_snotice,
  83. "Got valid OPER for \"%s\" from \"%s\", user is an IRC operator now.",
  84. Req->argv[0], Client_Mask(Client));
  85. return IRC_WriteStrClient(Client, RPL_YOUREOPER_MSG, Client_ID(Client));
  86. } /* IRC_OPER */
  87. /**
  88. * Handler for the IRC "DIE" command.
  89. *
  90. * @param Client The client from which this command has been received.
  91. * @param Req Request structure with prefix and all parameters.
  92. * @return CONNECTED or DISCONNECTED.
  93. */
  94. GLOBAL bool
  95. IRC_DIE(CLIENT * Client, REQUEST * Req)
  96. {
  97. /* Shut down server */
  98. CONN_ID c;
  99. CLIENT *cl;
  100. assert(Client != NULL);
  101. assert(Req != NULL);
  102. if (!Op_Check(Client, Req))
  103. return Op_NoPrivileges(Client, Req);
  104. /* Is a message given? */
  105. if (Req->argc > 0) {
  106. c = Conn_First();
  107. while (c != NONE) {
  108. cl = Conn_GetClient(c);
  109. if (Client_Type(cl) == CLIENT_USER)
  110. IRC_WriteStrClient(cl, "NOTICE %s :%s",
  111. Client_ID(cl), Req->argv[0]);
  112. c = Conn_Next(c);
  113. }
  114. }
  115. Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
  116. Client_Mask(Client));
  117. NGIRCd_SignalQuit = true;
  118. return CONNECTED;
  119. } /* IRC_DIE */
  120. /**
  121. * Handler for the IRC "REHASH" command.
  122. *
  123. * @param Client The client from which this command has been received.
  124. * @param Req Request structure with prefix and all parameters.
  125. * @return CONNECTED or DISCONNECTED.
  126. */
  127. GLOBAL bool
  128. IRC_REHASH( CLIENT *Client, REQUEST *Req )
  129. {
  130. /* Reload configuration file */
  131. assert( Client != NULL );
  132. assert( Req != NULL );
  133. if (!Op_Check(Client, Req))
  134. return Op_NoPrivileges(Client, Req);
  135. Log(LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...",
  136. Client_Mask(Client));
  137. IRC_WriteStrClient(Client, RPL_REHASHING_MSG, Client_ID(Client));
  138. raise(SIGHUP);
  139. return CONNECTED;
  140. } /* IRC_REHASH */
  141. /**
  142. * Handler for the IRC "RESTART" command.
  143. *
  144. * @param Client The client from which this command has been received.
  145. * @param Req Request structure with prefix and all parameters.
  146. * @return CONNECTED or DISCONNECTED.
  147. */
  148. GLOBAL bool
  149. IRC_RESTART( CLIENT *Client, REQUEST *Req )
  150. {
  151. /* Restart IRC server (fork a new process) */
  152. assert( Client != NULL );
  153. assert( Req != NULL );
  154. if (!Op_Check(Client, Req))
  155. return Op_NoPrivileges(Client, Req);
  156. Log(LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...",
  157. Client_Mask(Client));
  158. NGIRCd_SignalRestart = true;
  159. return CONNECTED;
  160. } /* IRC_RESTART */
  161. /**
  162. * Handler for the IRC "CONNECT" command.
  163. *
  164. * @param Client The client from which this command has been received.
  165. * @param Req Request structure with prefix and all parameters.
  166. * @return CONNECTED or DISCONNECTED.
  167. */
  168. GLOBAL bool
  169. IRC_CONNECT(CLIENT * Client, REQUEST * Req)
  170. {
  171. CLIENT *from, *target;
  172. assert(Client != NULL);
  173. assert(Req != NULL);
  174. /* Bad number of parameters? */
  175. if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
  176. Req->argc != 5 && Req->argc != 6)
  177. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  178. Client_ID(Client), Req->command);
  179. /* Invalid port number? */
  180. if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
  181. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  182. Client_ID(Client), Req->command);
  183. if (Client_Type(Client) != CLIENT_SERVER
  184. && !Client_HasMode(Client, 'o'))
  185. return Op_NoPrivileges(Client, Req);
  186. from = Client;
  187. target = Client_ThisServer();
  188. if (Req->argc == 3 || Req->argc == 6) {
  189. /* This CONNECT has a target parameter */
  190. if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
  191. from = Client_Search(Req->prefix);
  192. if (! from)
  193. return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
  194. Client_ID(Client), Req->prefix);
  195. target = (Req->argc == 3) ? Client_Search(Req->argv[2])
  196. : Client_Search(Req->argv[5]);
  197. if (! target || Client_Type(target) != CLIENT_SERVER)
  198. return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
  199. Client_ID(from), Req->argv[0]);
  200. }
  201. if (target != Client_ThisServer()) {
  202. /* Forward CONNECT command ... */
  203. if (Req->argc == 3)
  204. IRC_WriteStrClientPrefix(target, from,
  205. "CONNECT %s %s :%s", Req->argv[0],
  206. Req->argv[1], Req->argv[2]);
  207. else
  208. IRC_WriteStrClientPrefix(target, from,
  209. "CONNECT %s %s %s %s %s :%s", Req->argv[0],
  210. Req->argv[1], Req->argv[2], Req->argv[3],
  211. Req->argv[4], Req->argv[5]);
  212. return CONNECTED;
  213. }
  214. if (!Op_Check(from, Req))
  215. return Op_NoPrivileges(Client, Req);
  216. switch (Req->argc) {
  217. case 1:
  218. if (!Conf_EnablePassiveServer(Req->argv[0]))
  219. return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
  220. Client_ID(from),
  221. Req->argv[0]);
  222. break;
  223. case 2:
  224. case 3:
  225. /* Connect configured server */
  226. if (!Conf_EnableServer
  227. (Req->argv[0], (UINT16) atoi(Req->argv[1])))
  228. return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
  229. Client_ID(from),
  230. Req->argv[0]);
  231. break;
  232. default:
  233. /* Add server */
  234. if (!Conf_AddServer
  235. (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
  236. Req->argv[3], Req->argv[4]))
  237. return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
  238. Client_ID(from),
  239. Req->argv[0]);
  240. }
  241. Log(LOG_NOTICE | LOG_snotice,
  242. "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
  243. Req->argv[0]);
  244. IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
  245. "Received CONNECT %s from %s",
  246. Req->argv[0], Client_ID(from));
  247. return CONNECTED;
  248. } /* IRC_CONNECT */
  249. /**
  250. * Handler for the IRC "DISCONNECT" command.
  251. *
  252. * This command is not specified in the IRC RFCs, it is an extension
  253. * of ngIRCd: it shuts down and disables a configured server connection.
  254. *
  255. * @param Client The client from which this command has been received.
  256. * @param Req Request structure with prefix and all parameters.
  257. * @return CONNECTED or DISCONNECTED.
  258. */
  259. GLOBAL bool
  260. IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
  261. {
  262. CONN_ID my_conn;
  263. assert(Client != NULL);
  264. assert(Req != NULL);
  265. if (!Op_Check(Client, Req))
  266. return Op_NoPrivileges(Client, Req);
  267. IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
  268. "Received DISCONNECT %s from %s",
  269. Req->argv[0], Client_ID(Client));
  270. Log(LOG_NOTICE | LOG_snotice,
  271. "Got DISCONNECT command from \"%s\" for \"%s\".",
  272. Client_Mask(Client), Req->argv[0]);
  273. /* Save ID of this connection */
  274. my_conn = Client_Conn(Client);
  275. /* Disconnect configured server */
  276. if (!Conf_DisableServer(Req->argv[0]))
  277. return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
  278. Client_ID(Client), Req->argv[0]);
  279. /* Are we still connected or were we killed, too? */
  280. if (Conn_GetClient(my_conn))
  281. return CONNECTED;
  282. else
  283. return DISCONNECTED;
  284. } /* IRC_DISCONNECT */
  285. /**
  286. * Handler for the IRC "WALLOPS" command.
  287. *
  288. * @param Client The client from which this command has been received.
  289. * @param Req Request structure with prefix and all parameters.
  290. * @return CONNECTED or DISCONNECTED.
  291. */
  292. GLOBAL bool
  293. IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
  294. {
  295. CLIENT *from;
  296. assert( Client != NULL );
  297. assert( Req != NULL );
  298. switch (Client_Type(Client)) {
  299. case CLIENT_USER:
  300. if (!Op_Check(Client, Req))
  301. return Op_NoPrivileges(Client, Req);
  302. from = Client;
  303. break;
  304. case CLIENT_SERVER:
  305. from = Client_Search(Req->prefix);
  306. break;
  307. default:
  308. return CONNECTED;
  309. }
  310. if (!from)
  311. return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
  312. Client_ID(Client), Req->prefix);
  313. IRC_SendWallops(Client, from, "%s", Req->argv[0]);
  314. return CONNECTED;
  315. } /* IRC_WALLOPS */
  316. /**
  317. * Handle <?>LINE commands (GLINE, KLINE).
  318. *
  319. * @param Client The client from which this command has been received.
  320. * @param Req Request structure with prefix and all parameters.
  321. * @return CONNECTED or DISCONNECTED.
  322. */
  323. GLOBAL bool
  324. IRC_xLINE(CLIENT *Client, REQUEST *Req)
  325. {
  326. CLIENT *from, *c, *c_next;
  327. char reason[COMMAND_LEN], class_c;
  328. struct list_head *list;
  329. time_t timeout;
  330. int class;
  331. assert(Client != NULL);
  332. assert(Req != NULL);
  333. /* Bad number of parameters? */
  334. if (Req->argc != 1 && Req->argc != 3)
  335. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  336. Client_ID(Client), Req->command);
  337. from = Op_Check(Client, Req);
  338. if (!from)
  339. return Op_NoPrivileges(Client, Req);
  340. switch(Req->command[0]) {
  341. case 'g':
  342. case 'G':
  343. class = CLASS_GLINE; class_c = 'G';
  344. break;
  345. case 'k':
  346. case 'K':
  347. class = CLASS_KLINE; class_c = 'K';
  348. break;
  349. default:
  350. Log(LOG_CRIT,
  351. "IRC_xLINE() called for unknown line: %c!? Ignored.",
  352. Req->command[0]);
  353. return CONNECTED;
  354. }
  355. if (Req->argc == 1) {
  356. /* Delete mask from list */
  357. Class_DeleteMask(class, Req->argv[0]);
  358. Log(LOG_NOTICE|LOG_snotice,
  359. "\"%s\" deleted \"%s\" from %c-Line list.",
  360. Client_Mask(from), Req->argv[0], class_c);
  361. if (class == CLASS_GLINE) {
  362. /* Inform other servers */
  363. IRC_WriteStrServersPrefix(Client, from, "%s %s",
  364. Req->command, Req->argv[0]);
  365. }
  366. } else {
  367. /* Add new mask to list */
  368. timeout = atol(Req->argv[1]);
  369. if (timeout > 0)
  370. timeout += time(NULL);
  371. if (Class_AddMask(class, Req->argv[0],
  372. timeout,
  373. Req->argv[2])) {
  374. Log(LOG_NOTICE|LOG_snotice,
  375. "\"%s\" added \"%s\" to %c-Line list: \"%s\" (%ld seconds).",
  376. Client_Mask(from), Req->argv[0], class_c,
  377. Req->argv[2], atol(Req->argv[1]));
  378. if (class == CLASS_GLINE) {
  379. /* Inform other servers */
  380. IRC_WriteStrServersPrefix(Client, from,
  381. "%s %s %s :%s", Req->command,
  382. Req->argv[0], Req->argv[1],
  383. Req->argv[2]);
  384. }
  385. /* Check currently connected clients */
  386. snprintf(reason, sizeof(reason), "%c-Line by \"%s\": \"%s\"",
  387. class_c, Client_ID(from), Req->argv[2]);
  388. list = Class_GetList(class);
  389. c = Client_First();
  390. while (c) {
  391. c_next = Client_Next(c);
  392. if ((class == CLASS_GLINE || Client_Conn(c) > NONE)
  393. && Lists_Check(list, c))
  394. IRC_KillClient(Client, NULL,
  395. Client_ID(c), reason);
  396. c = c_next;
  397. }
  398. }
  399. }
  400. return CONNECTED;
  401. }
  402. /* -eof- */