auditlog.c 7.6 KB

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