auditlog.c 8.0 KB

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