irc-oper.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2008 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. #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 "irc-write.h"
  27. #include "log.h"
  28. #include "match.h"
  29. #include "messages.h"
  30. #include "parse.h"
  31. #include "op.h"
  32. #include <exp.h>
  33. #include "irc-oper.h"
  34. /**
  35. * Handle invalid received OPER command.
  36. * Log OPER attempt and send error message to client.
  37. */
  38. static bool
  39. Bad_OperPass(CLIENT *Client, char *errtoken, char *errmsg)
  40. {
  41. Log(LOG_WARNING, "Got invalid OPER from \"%s\": \"%s\" -- %s",
  42. Client_Mask(Client), errtoken, errmsg);
  43. IRC_SetPenalty(Client, 3);
  44. return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
  45. Client_ID(Client));
  46. } /* Bad_OperPass */
  47. GLOBAL bool
  48. IRC_OPER( CLIENT *Client, REQUEST *Req )
  49. {
  50. struct Conf_Oper *op;
  51. size_t len, i;
  52. assert( Client != NULL );
  53. assert( Req != NULL );
  54. if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  55. len = array_length(&Conf_Opers, sizeof(*op));
  56. op = array_start(&Conf_Opers);
  57. for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
  58. ;
  59. if (i >= len)
  60. return Bad_OperPass(Client, Req->argv[0], "not configured");
  61. if (strcmp(op[i].pwd, Req->argv[1]) != 0)
  62. return Bad_OperPass(Client, op[i].name, "bad password");
  63. if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
  64. return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
  65. if( ! Client_HasMode( Client, 'o' ))
  66. {
  67. Client_ModeAdd( Client, 'o' );
  68. if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
  69. IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
  70. }
  71. if( ! Client_OperByMe( Client )) Log( LOG_NOTICE|LOG_snotice, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
  72. Client_SetOperByMe( Client, true);
  73. return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
  74. } /* IRC_OPER */
  75. GLOBAL bool
  76. IRC_DIE(CLIENT * Client, REQUEST * Req)
  77. {
  78. /* Shut down server */
  79. CONN_ID c;
  80. CLIENT *cl;
  81. assert(Client != NULL);
  82. assert(Req != NULL);
  83. if (!Op_Check(Client, Req))
  84. return Op_NoPrivileges(Client, Req);
  85. /* Bad number of parameters? */
  86. #ifdef STRICT_RFC
  87. if (Req->argc != 0)
  88. #else
  89. if (Req->argc > 1)
  90. #endif
  91. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  92. Client_ID(Client), Req->command);
  93. /* Is a message given? */
  94. if (Req->argc > 0) {
  95. c = Conn_First();
  96. while (c != NONE) {
  97. cl = Conn_GetClient(c);
  98. if (Client_Type(cl) == CLIENT_USER)
  99. IRC_WriteStrClient(cl, "NOTICE %s :%s",
  100. Client_ID(cl), Req->argv[0]);
  101. c = Conn_Next(c);
  102. }
  103. }
  104. Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
  105. Client_Mask(Client));
  106. NGIRCd_SignalQuit = true;
  107. return CONNECTED;
  108. } /* IRC_DIE */
  109. GLOBAL bool
  110. IRC_REHASH( CLIENT *Client, REQUEST *Req )
  111. {
  112. /* Reload configuration file */
  113. assert( Client != NULL );
  114. assert( Req != NULL );
  115. if (!Op_Check(Client, Req))
  116. return Op_NoPrivileges(Client, Req);
  117. /* Bad number of parameters? */
  118. if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  119. Log( LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...", Client_Mask( Client ));
  120. raise(SIGHUP);
  121. return CONNECTED;
  122. } /* IRC_REHASH */
  123. GLOBAL bool
  124. IRC_RESTART( CLIENT *Client, REQUEST *Req )
  125. {
  126. /* Restart IRC server (fork a new process) */
  127. assert( Client != NULL );
  128. assert( Req != NULL );
  129. if (!Op_Check(Client, Req))
  130. return Op_NoPrivileges(Client, Req);
  131. /* Bad number of parameters? */
  132. if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  133. Log( LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...", Client_Mask( Client ));
  134. NGIRCd_SignalRestart = true;
  135. return CONNECTED;
  136. } /* IRC_RESTART */
  137. /**
  138. * Connect configured or new server.
  139. */
  140. GLOBAL bool
  141. IRC_CONNECT(CLIENT * Client, REQUEST * Req)
  142. {
  143. CLIENT *from, *target;
  144. assert(Client != NULL);
  145. assert(Req != NULL);
  146. if (Client_Type(Client) != CLIENT_SERVER
  147. && !Client_HasMode(Client, 'o'))
  148. return Op_NoPrivileges(Client, Req);
  149. /* Bad number of parameters? */
  150. if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
  151. Req->argc != 5 && Req->argc != 6)
  152. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  153. Client_ID(Client), Req->command);
  154. /* Invalid port number? */
  155. if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
  156. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  157. Client_ID(Client), Req->command);
  158. from = Client;
  159. target = Client_ThisServer();
  160. if (Req->argc == 3 || Req->argc == 6) {
  161. /* This CONNECT has a target parameter */
  162. if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
  163. from = Client_Search(Req->prefix);
  164. if (! from)
  165. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
  166. Client_ID(Client), Req->prefix);
  167. target = (Req->argc == 3) ? Client_Search(Req->argv[2])
  168. : Client_Search(Req->argv[5]);
  169. if (! target || Client_Type(target) != CLIENT_SERVER)
  170. return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
  171. Client_ID(from), Req->argv[0]);
  172. }
  173. if (target != Client_ThisServer()) {
  174. /* Forward CONNECT command ... */
  175. if (Req->argc == 3)
  176. IRC_WriteStrClientPrefix(target, from,
  177. "CONNECT %s %s :%s", Req->argv[0],
  178. Req->argv[1], Req->argv[2]);
  179. else
  180. IRC_WriteStrClientPrefix(target, from,
  181. "CONNECT %s %s %s %s %s :%s", Req->argv[0],
  182. Req->argv[1], Req->argv[2], Req->argv[3],
  183. Req->argv[4], Req->argv[5]);
  184. return CONNECTED;
  185. }
  186. if (!Op_Check(from, Req))
  187. return Op_NoPrivileges(Client, Req);
  188. switch (Req->argc) {
  189. case 1:
  190. if (!Conf_EnablePassiveServer(Req->argv[0]))
  191. return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
  192. Client_ID(from),
  193. Req->argv[0]);
  194. break;
  195. case 2:
  196. case 3:
  197. /* Connect configured server */
  198. if (!Conf_EnableServer
  199. (Req->argv[0], (UINT16) atoi(Req->argv[1])))
  200. return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
  201. Client_ID(from),
  202. Req->argv[0]);
  203. break;
  204. default:
  205. /* Add server */
  206. if (!Conf_AddServer
  207. (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
  208. Req->argv[3], Req->argv[4]))
  209. return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
  210. Client_ID(from),
  211. Req->argv[0]);
  212. }
  213. Log(LOG_NOTICE | LOG_snotice,
  214. "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
  215. Req->argv[0]);
  216. IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
  217. "Received CONNECT %s from %s",
  218. Req->argv[0], Client_ID(from));
  219. return CONNECTED;
  220. } /* IRC_CONNECT */
  221. /**
  222. * Disconnect (and disable) configured server.
  223. */
  224. GLOBAL bool
  225. IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
  226. {
  227. CONN_ID my_conn;
  228. assert(Client != NULL);
  229. assert(Req != NULL);
  230. if (!Op_Check(Client, Req))
  231. return Op_NoPrivileges(Client, Req);
  232. /* Bad number of parameters? */
  233. if (Req->argc != 1)
  234. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  235. Client_ID(Client), Req->command);
  236. IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
  237. "Received DISCONNECT %s from %s",
  238. Req->argv[0], Client_ID(Client));
  239. Log(LOG_NOTICE | LOG_snotice,
  240. "Got DISCONNECT command from \"%s\" for \"%s\".",
  241. Client_Mask(Client), Req->argv[0]);
  242. /* Save ID of this connection */
  243. my_conn = Client_Conn(Client);
  244. /* Disconnect configured server */
  245. if (!Conf_DisableServer(Req->argv[0]))
  246. return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
  247. Client_ID(Client), Req->argv[0]);
  248. /* Are we still connected or were we killed, too? */
  249. if (Conn_GetClient(my_conn))
  250. return CONNECTED;
  251. else
  252. return DISCONNECTED;
  253. } /* IRC_DISCONNECT */
  254. GLOBAL bool
  255. IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
  256. {
  257. CLIENT *from;
  258. assert( Client != NULL );
  259. assert( Req != NULL );
  260. if (Req->argc != 1)
  261. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
  262. switch (Client_Type(Client)) {
  263. case CLIENT_USER:
  264. if (!Client_OperByMe(Client))
  265. return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG, Client_ID(Client));
  266. from = Client;
  267. break;
  268. case CLIENT_SERVER:
  269. from = Client_Search(Req->prefix);
  270. break;
  271. default:
  272. return CONNECTED;
  273. }
  274. if (!from)
  275. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
  276. IRC_SendWallops(Client, from, "%s", Req->argv[0]);
  277. return CONNECTED;
  278. } /* IRC_WALLOPS */
  279. /* -eof- */