testgetip.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. /************************************************************
  39. testgetip.c
  40. This source file provides an example of a pre-operation plug-in
  41. function that gets the IP address of the client and the IP
  42. address of the server.
  43. testgetip logs this information to the server error log.
  44. To test this plug-in function, stop the server, edit the dse.ldif file
  45. (in the <server_root>/slapd-<server_id>/config directory)
  46. and add the following lines before restarting the server :
  47. dn: cn=Test GetIP,cn=plugins,cn=config
  48. objectClass: top
  49. objectClass: nsSlapdPlugin
  50. objectClass: extensibleObject
  51. cn: Test GetIP
  52. nsslapd-pluginPath: <server_root>/plugins/slapd/slapi/examples/libtest-plugin.so
  53. nsslapd-pluginInitfunc: testgetip_init
  54. nsslapd-pluginType: preoperation
  55. nsslapd-pluginEnabled: on
  56. nsslapd-plugin-depends-on-type: database
  57. nsslapd-pluginId: test-getip
  58. ************************************************************/
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include <sys/types.h>
  62. #ifndef _WIN32
  63. #include <sys/socket.h>
  64. #include <netinet/in.h>
  65. #include <arpa/inet.h>
  66. #endif
  67. #include "slapi-plugin.h"
  68. #include "nspr.h"
  69. Slapi_PluginDesc getippdesc = { "test-getip", "Fedora Project", "1.0.2",
  70. "sample pre-operation plugin" };
  71. static char *netaddr2str( PRNetAddr *addrp, char *buf, size_t buflen );
  72. int
  73. testgetip( Slapi_PBlock *pb )
  74. {
  75. void *conn;
  76. PRNetAddr client_addr, server_addr;
  77. char addrbuf[ 512 ], *addrstr;
  78. /*
  79. * Don't do anything for internal operations (NULL connection).
  80. */
  81. if ( slapi_pblock_get( pb, SLAPI_CONNECTION, &conn ) != 0 ||
  82. ( conn == NULL )) {
  83. return( 0 );
  84. }
  85. /*
  86. * Get the client's IP address and log it
  87. */
  88. if ( slapi_pblock_get( pb, SLAPI_CONN_CLIENTNETADDR, &client_addr )
  89. != 0 || ( client_addr.raw.family == 0 )) {
  90. slapi_log_error( SLAPI_LOG_PLUGIN, "testgetip",
  91. "Could not get client IP.\n" );
  92. } else if (( addrstr = netaddr2str( &client_addr, addrbuf,
  93. sizeof(addrbuf))) != NULL ) {
  94. slapi_log_error( SLAPI_LOG_PLUGIN, "testgetip",
  95. "Client's IP address is %s\n", addrstr );
  96. }
  97. /*
  98. * Get the destination (server) IP address and log it
  99. */
  100. if ( slapi_pblock_get( pb, SLAPI_CONN_SERVERNETADDR, &server_addr )
  101. != 0 || ( server_addr.raw.family == 0 )) {
  102. slapi_log_error( SLAPI_LOG_PLUGIN, "testgetip",
  103. "Could not get server IP.\n" );
  104. } else if (( addrstr = netaddr2str( &server_addr, addrbuf,
  105. sizeof(addrbuf))) != NULL ) {
  106. slapi_log_error( SLAPI_LOG_PLUGIN, "testgetip",
  107. "Client sent request to server IP %s\n", addrstr );
  108. }
  109. return( 0 );
  110. }
  111. #ifdef _WIN32
  112. __declspec(dllexport)
  113. #endif
  114. int
  115. testgetip_init( Slapi_PBlock *pb )
  116. {
  117. /* Register the pre-operation plug-in function. */
  118. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  119. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  120. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  121. (void *)&getippdesc ) != 0 ||
  122. slapi_pblock_set( pb, SLAPI_PLUGIN_PRE_SEARCH_FN,
  123. (void *) testgetip ) != 0 ) {
  124. slapi_log_error( SLAPI_LOG_FATAL, "testgetip_init",
  125. "Failed to set version and functions.\n" );
  126. return( -1 );
  127. }
  128. slapi_log_error( SLAPI_LOG_PLUGIN, "testgetip_init",
  129. "Registered preop plugins.\n" );
  130. return( 0 );
  131. }
  132. /*
  133. * Utility function to convert a PRNetAddr to a human readable string.
  134. */
  135. static char *
  136. netaddr2str( PRNetAddr *addrp, char *buf, size_t buflen )
  137. {
  138. char *addrstr;
  139. *buf = '\0';
  140. if ( PR_NetAddrToString( addrp, buf, buflen ) != PR_SUCCESS ) {
  141. slapi_log_error( SLAPI_LOG_PLUGIN, "testgetip",
  142. "PR_NetAddrToString failed.\n" );
  143. return( NULL );
  144. }
  145. /* skip past leading ::ffff: if IPv4 address */
  146. if ( strlen( buf ) > 7 && strncmp( buf, "::ffff:", 7 ) == 0 ) {
  147. addrstr = buf + 7;
  148. } else {
  149. addrstr = buf;
  150. }
  151. return( addrstr );
  152. }