irc-oper.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.22 2005/06/12 18:02:09 fw 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.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. LOCAL 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. 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. assert( Client != NULL );
  77. assert( Req != NULL );
  78. /* Not a local IRC operator? */
  79. if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
  80. /* Bad number of parameters? */
  81. if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  82. Log( LOG_NOTICE|LOG_snotice, "Got DIE command from \"%s\" ...", Client_Mask( Client ));
  83. NGIRCd_SignalQuit = true;
  84. return CONNECTED;
  85. } /* IRC_DIE */
  86. GLOBAL bool
  87. IRC_REHASH( CLIENT *Client, REQUEST *Req )
  88. {
  89. /* Reload configuration file */
  90. assert( Client != NULL );
  91. assert( Req != NULL );
  92. /* Not a local IRC operator? */
  93. if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
  94. /* Bad number of parameters? */
  95. if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  96. Log( LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...", Client_Mask( Client ));
  97. NGIRCd_SignalRehash = true;
  98. return CONNECTED;
  99. } /* IRC_REHASH */
  100. GLOBAL bool
  101. IRC_RESTART( CLIENT *Client, REQUEST *Req )
  102. {
  103. /* Restart IRC server (fork a new process) */
  104. assert( Client != NULL );
  105. assert( Req != NULL );
  106. /* Not a local IRC operator? */
  107. if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
  108. /* Bad number of parameters? */
  109. if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  110. Log( LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...", Client_Mask( Client ));
  111. NGIRCd_SignalRestart = true;
  112. return CONNECTED;
  113. } /* IRC_RESTART */
  114. GLOBAL bool
  115. IRC_CONNECT(CLIENT *Client, REQUEST *Req )
  116. {
  117. /* Connect configured or new server */
  118. assert( Client != NULL );
  119. assert( Req != NULL );
  120. /* Not a local IRC operator? */
  121. if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
  122. /* Bad number of parameters? */
  123. if(( Req->argc != 2 ) && ( Req->argc != 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  124. /* Invalid port number? */
  125. if( atoi( Req->argv[1] ) < 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  126. Log( LOG_NOTICE|LOG_snotice, "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask( Client ), Req->argv[0]);
  127. if( Req->argc == 2 )
  128. {
  129. /* Connect configured server */
  130. if( ! Conf_EnableServer( Req->argv[0], atoi( Req->argv[1] ))) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
  131. }
  132. else
  133. {
  134. /* Add server */
  135. if( ! Conf_AddServer( Req->argv[0], atoi( Req->argv[1] ), Req->argv[2], Req->argv[3], Req->argv[4] )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
  136. }
  137. return CONNECTED;
  138. } /* IRC_CONNECT */
  139. GLOBAL bool
  140. IRC_DISCONNECT(CLIENT *Client, REQUEST *Req )
  141. {
  142. /* Disconnect and disable configured server */
  143. CONN_ID my_conn;
  144. assert( Client != NULL );
  145. assert( Req != NULL );
  146. /* Not a local IRC operator? */
  147. if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
  148. /* Bad number of parameters? */
  149. if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  150. Log( LOG_NOTICE|LOG_snotice, "Got DISCONNECT command from \"%s\" for0 \"%s\".", Client_Mask( Client ), Req->argv[0]);
  151. /* Save ID of this connection */
  152. my_conn = Client_Conn( Client );
  153. /* Connect configured server */
  154. if( ! Conf_DisableServer( Req->argv[0] )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
  155. /* Are we still connected or were we killed, too? */
  156. if( Client_GetFromConn( my_conn )) return CONNECTED;
  157. else return DISCONNECTED;
  158. } /* IRC_CONNECT */
  159. /* -eof- */