irc-oper.c 13 KB

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