auditlog.c 7.6 KB

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