auditlog.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include "slap.h"
  42. /*
  43. * JCM - The audit log might be better implemented as a post-op plugin.
  44. */
  45. #define ATTR_CHANGETYPE "changetype"
  46. #define ATTR_NEWRDN "newrdn"
  47. #define ATTR_DELETEOLDRDN "deleteoldrdn"
  48. #define ATTR_MODIFIERSNAME "modifiersname"
  49. char *attr_changetype = ATTR_CHANGETYPE;
  50. char *attr_newrdn = ATTR_NEWRDN;
  51. char *attr_deleteoldrdn = ATTR_DELETEOLDRDN;
  52. char *attr_modifiersname = ATTR_MODIFIERSNAME;
  53. /* Forward Declarations */
  54. static void write_audit_file( int optype, const char *dn, void *change, int flag, time_t curtime );
  55. void
  56. write_audit_log_entry( Slapi_PBlock *pb )
  57. {
  58. time_t curtime;
  59. Slapi_DN *sdn;
  60. const char *dn;
  61. void *change;
  62. int flag = 0;
  63. Operation *op;
  64. /* if the audit log is not enabled, just skip all of
  65. this stuff */
  66. if (!config_get_auditlog_logging_enabled()) {
  67. return;
  68. }
  69. slapi_pblock_get( pb, SLAPI_OPERATION, &op );
  70. slapi_pblock_get( pb, SLAPI_TARGET_SDN, &sdn );
  71. switch ( operation_get_type(op) )
  72. {
  73. case SLAPI_OPERATION_MODIFY:
  74. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &change );
  75. break;
  76. case SLAPI_OPERATION_ADD:
  77. slapi_pblock_get( pb, SLAPI_ADD_ENTRY, &change );
  78. break;
  79. case SLAPI_OPERATION_DELETE:
  80. {
  81. char * deleterDN = NULL;
  82. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &deleterDN);
  83. change = deleterDN;
  84. }
  85. break;
  86. case SLAPI_OPERATION_MODDN:
  87. /* newrdn: change is just for logging -- case does not matter. */
  88. slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &change );
  89. slapi_pblock_get( pb, SLAPI_MODRDN_DELOLDRDN, &flag );
  90. break;
  91. default:
  92. return; /* Unsupported operation type. */
  93. }
  94. curtime = current_time();
  95. /* log the raw, unnormalized DN */
  96. dn = slapi_sdn_get_udn(sdn);
  97. write_audit_file( operation_get_type(op), dn, change, flag, curtime );
  98. }
  99. /*
  100. * Function: write_audit_file
  101. * Arguments:
  102. * optype - type of LDAP operation being logged
  103. * dn - distinguished name of entry being changed
  104. * change - pointer to the actual change operation
  105. * For a delete operation, may contain the modifier's DN.
  106. * flag - only used by modrdn operations - value of deleteoldrdn flag
  107. * curtime - the current time
  108. * Returns: nothing
  109. */
  110. static void
  111. write_audit_file(
  112. int optype,
  113. const char *dn,
  114. void *change,
  115. int flag,
  116. time_t curtime
  117. )
  118. {
  119. LDAPMod **mods;
  120. Slapi_Entry *e;
  121. char *newrdn, *tmp, *tmpsave;
  122. int len, i, j;
  123. char *timestr;
  124. lenstr *l;
  125. l = lenstr_new();
  126. addlenstr( l, "time: " );
  127. timestr = format_localTime( curtime );
  128. addlenstr( l, timestr );
  129. slapi_ch_free((void **) &timestr );
  130. addlenstr( l, "\n" );
  131. addlenstr( l, "dn: " );
  132. addlenstr( l, dn );
  133. addlenstr( l, "\n" );
  134. switch ( optype )
  135. {
  136. case SLAPI_OPERATION_MODIFY:
  137. addlenstr( l, attr_changetype );
  138. addlenstr( l, ": modify\n" );
  139. mods = change;
  140. for ( j = 0; mods[j] != NULL; j++ )
  141. {
  142. int operationtype= mods[j]->mod_op & ~LDAP_MOD_BVALUES;
  143. switch ( operationtype )
  144. {
  145. case LDAP_MOD_ADD:
  146. addlenstr( l, "add: " );
  147. addlenstr( l, mods[j]->mod_type );
  148. addlenstr( l, "\n" );
  149. break;
  150. case LDAP_MOD_DELETE:
  151. addlenstr( l, "delete: " );
  152. addlenstr( l, mods[j]->mod_type );
  153. addlenstr( l, "\n" );
  154. break;
  155. case LDAP_MOD_REPLACE:
  156. addlenstr( l, "replace: " );
  157. addlenstr( l, mods[j]->mod_type );
  158. addlenstr( l, "\n" );
  159. break;
  160. default:
  161. operationtype= LDAP_MOD_IGNORE;
  162. break;
  163. }
  164. if(operationtype!=LDAP_MOD_IGNORE)
  165. {
  166. for ( i = 0; mods[j]->mod_bvalues != NULL && mods[j]->mod_bvalues[i] != NULL; i++ )
  167. {
  168. char *buf, *bufp;
  169. len = strlen( mods[j]->mod_type );
  170. len = LDIF_SIZE_NEEDED( len, mods[j]->mod_bvalues[i]->bv_len ) + 1;
  171. buf = slapi_ch_malloc( len );
  172. bufp = buf;
  173. slapi_ldif_put_type_and_value_with_options( &bufp, mods[j]->mod_type,
  174. mods[j]->mod_bvalues[i]->bv_val,
  175. mods[j]->mod_bvalues[i]->bv_len, 0 );
  176. *bufp = '\0';
  177. addlenstr( l, buf );
  178. slapi_ch_free( (void**)&buf );
  179. }
  180. }
  181. addlenstr( l, "-\n" );
  182. }
  183. break;
  184. case SLAPI_OPERATION_ADD:
  185. e = change;
  186. addlenstr( l, attr_changetype );
  187. addlenstr( l, ": add\n" );
  188. tmp = slapi_entry2str( e, &len );
  189. tmpsave = tmp;
  190. while (( tmp = strchr( tmp, '\n' )) != NULL )
  191. {
  192. tmp++;
  193. if ( !ldap_utf8isspace( tmp ))
  194. {
  195. break;
  196. }
  197. }
  198. addlenstr( l, tmp );
  199. slapi_ch_free((void**)&tmpsave );
  200. break;
  201. case SLAPI_OPERATION_DELETE:
  202. tmp = change;
  203. addlenstr( l, attr_changetype );
  204. addlenstr( l, ": delete\n" );
  205. if (tmp && tmp[0]) {
  206. addlenstr( l, attr_modifiersname );
  207. addlenstr( l, ": ");
  208. addlenstr( l, tmp);
  209. addlenstr( l, "\n");
  210. }
  211. break;
  212. case SLAPI_OPERATION_MODDN:
  213. newrdn = change;
  214. addlenstr( l, attr_changetype );
  215. addlenstr( l, ": modrdn\n" );
  216. addlenstr( l, attr_newrdn );
  217. addlenstr( l, ": " );
  218. addlenstr( l, newrdn );
  219. addlenstr( l, "\n" );
  220. addlenstr( l, attr_deleteoldrdn );
  221. addlenstr( l, ": " );
  222. addlenstr( l, flag ? "1" : "0" );
  223. addlenstr( l, "\n" );
  224. }
  225. addlenstr( l, "\n" );
  226. slapd_log_audit_proc (l->ls_buf, l->ls_len);
  227. lenstr_free( &l );
  228. }