irc-oper.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001,2002 by 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. * IRC operator commands
  12. */
  13. #include "portab.h"
  14. static char UNUSED id[] = "$Id: irc-oper.c,v 1.27 2006/07/23 15:43:18 alex Exp $";
  15. #include "imp.h"
  16. #include <assert.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "ngircd.h"
  20. #include "resolve.h"
  21. #include "conn-func.h"
  22. #include "conf.h"
  23. #include "client.h"
  24. #include "channel.h"
  25. #include "irc-write.h"
  26. #include "log.h"
  27. #include "match.h"
  28. #include "messages.h"
  29. #include "parse.h"
  30. #include <exp.h>
  31. #include "irc-oper.h"
  32. static bool
  33. Bad_OperPass(CLIENT *Client, char *errtoken, char *errmsg)
  34. {
  35. Log( LOG_WARNING, "Got invalid OPER from \"%s\": \"%s\" -- %s", Client_Mask( Client ),
  36. errtoken, errmsg);
  37. IRC_SetPenalty(Client, 3);
  38. return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
  39. }
  40. GLOBAL bool
  41. IRC_OPER( CLIENT *Client, REQUEST *Req )
  42. {
  43. unsigned int i;
  44. assert( Client != NULL );
  45. assert( Req != NULL );
  46. /* Falsche Anzahl Parameter? */
  47. if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  48. /* Operator suchen */
  49. for( i = 0; i < Conf_Oper_Count; i++)
  50. {
  51. if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
  52. }
  53. if( i >= Conf_Oper_Count )
  54. return Bad_OperPass(Client, Req->argv[0], "not configured");
  55. /* Stimmt das Passwort? */
  56. if( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )
  57. return Bad_OperPass(Client, Conf_Oper[i].name, "Bad password");
  58. /* Authorized Mask? */
  59. if( Conf_Oper[i].mask && (! Match( Conf_Oper[i].mask, Client_Mask( Client ) )))
  60. return Bad_OperPass(Client, Conf_Oper[i].mask, "hostmask check failed" );
  61. if( ! Client_HasMode( Client, 'o' ))
  62. {
  63. /* noch kein o-Mode gesetzt */
  64. Client_ModeAdd( Client, 'o' );
  65. if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
  66. IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
  67. }
  68. if( ! Client_OperByMe( Client )) Log( LOG_NOTICE|LOG_snotice, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
  69. Client_SetOperByMe( Client, true);
  70. return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
  71. } /* IRC_OPER */
  72. GLOBAL bool
  73. IRC_DIE(CLIENT * Client, REQUEST * Req)
  74. {
  75. /* Shut down server */
  76. CONN_ID c;
  77. CLIENT *cl;
  78. assert(Client != NULL);
  79. assert(Req != NULL);
  80. /* Not a local IRC operator? */
  81. if ((!Client_HasMode(Client, 'o')) || (!Client_OperByMe(Client)))
  82. return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
  83. Client_ID(Client));
  84. /* Bad number of parameters? */
  85. #ifdef STRICT_RFC
  86. if (Req->argc != 0)
  87. #else
  88. if (Req->argc > 1)
  89. #endif
  90. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  91. Client_ID(Client), Req->command);
  92. /* Is a message given? */
  93. if (Req->argc > 0) {
  94. c = Conn_First();
  95. while (c != NONE) {
  96. cl = Conn_GetClient(c);
  97. if (Client_Type(cl) == CLIENT_USER)
  98. IRC_WriteStrClient(cl, "NOTICE %s :%s",
  99. Client_ID(cl), Req->argv[0]);
  100. c = Conn_Next(c);
  101. }
  102. }
  103. Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
  104. Client_Mask(Client));
  105. NGIRCd_SignalQuit = true;
  106. return CONNECTED;
  107. } /* IRC_DIE */
  108. GLOBAL bool
  109. IRC_REHASH( CLIENT *Client, REQUEST *Req )
  110. {
  111. /* Reload configuration file */
  112. assert( Client != NULL );
  113. assert( Req != NULL );
  114. /* Not a local IRC operator? */
  115. if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
  116. /* Bad number of parameters? */
  117. if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  118. Log( LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...", Client_Mask( Client ));
  119. NGIRCd_SignalRehash = true;
  120. return CONNECTED;
  121. } /* IRC_REHASH */
  122. GLOBAL bool
  123. IRC_RESTART( CLIENT *Client, REQUEST *Req )
  124. {
  125. /* Restart IRC server (fork a new process) */
  126. assert( Client != NULL );
  127. assert( Req != NULL );
  128. /* Not a local IRC operator? */
  129. if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
  130. /* Bad number of parameters? */
  131. if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  132. Log( LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...", Client_Mask( Client ));
  133. NGIRCd_SignalRestart = true;
  134. return CONNECTED;
  135. } /* IRC_RESTART */
  136. /**
  137. * Connect configured or new server.
  138. */
  139. GLOBAL bool
  140. IRC_CONNECT(CLIENT * Client, REQUEST * Req)
  141. {
  142. assert(Client != NULL);
  143. assert(Req != NULL);
  144. /* Not a local IRC operator? */
  145. if ((!Client_HasMode(Client, 'o')) || (!Client_OperByMe(Client)))
  146. return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
  147. Client_ID(Client));
  148. /* Bad number of parameters? */
  149. if ((Req->argc != 2) && (Req->argc != 5))
  150. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  151. Client_ID(Client), Req->command);
  152. /* Invalid port number? */
  153. if (atoi(Req->argv[1]) < 1)
  154. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  155. Client_ID(Client), Req->command);
  156. Log(LOG_NOTICE | LOG_snotice,
  157. "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(Client),
  158. Req->argv[0]);
  159. if (Req->argc == 2) {
  160. /* Connect configured server */
  161. if (!Conf_EnableServer
  162. (Req->argv[0], (UINT16) atoi(Req->argv[1])))
  163. return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
  164. Client_ID(Client),
  165. Req->argv[0]);
  166. } else {
  167. /* Add server */
  168. if (!Conf_AddServer
  169. (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
  170. Req->argv[3], Req->argv[4]))
  171. return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
  172. Client_ID(Client),
  173. Req->argv[0]);
  174. }
  175. return CONNECTED;
  176. } /* IRC_CONNECT */
  177. GLOBAL bool
  178. IRC_DISCONNECT(CLIENT *Client, REQUEST *Req )
  179. {
  180. /* Disconnect and disable configured server */
  181. CONN_ID my_conn;
  182. assert( Client != NULL );
  183. assert( Req != NULL );
  184. /* Not a local IRC operator? */
  185. if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
  186. /* Bad number of parameters? */
  187. if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  188. Log( LOG_NOTICE|LOG_snotice, "Got DISCONNECT command from \"%s\" for0 \"%s\".", Client_Mask( Client ), Req->argv[0]);
  189. /* Save ID of this connection */
  190. my_conn = Client_Conn( Client );
  191. /* Connect configured server */
  192. if( ! Conf_DisableServer( Req->argv[0] )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
  193. /* Are we still connected or were we killed, too? */
  194. if( Conn_GetClient( my_conn )) return CONNECTED;
  195. else return DISCONNECTED;
  196. } /* IRC_CONNECT */
  197. /* -eof- */