2004-04-03 16:29:21 +04:00
/*
ldb database library
Copyright ( C ) Andrew Tridgell 2004
* * NOTE ! The following LGPL license applies to the ldb
* * library . This does NOT imply that all of Samba is released
* * under the LGPL
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation ; either
2007-07-10 06:46:15 +04:00
version 3 of the License , or ( at your option ) any later version .
2004-04-03 16:29:21 +04:00
This library is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public
License along with this library ; if not , write to the Free Software
Foundation , Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
*/
/*
* Name : ldb
*
* Component : ldbmodify
*
* Description : utility to modify records - modelled on ldapmodify
*
* Author : Andrew Tridgell
*/
2007-05-05 22:50:56 +04:00
# include "ldb_includes.h"
# include "tools/cmdline.h"
2004-04-03 16:29:21 +04:00
2004-04-11 00:18:22 +04:00
static int failures ;
2004-04-03 16:29:21 +04:00
2004-04-11 00:18:22 +04:00
static void usage ( void )
{
printf ( " Usage: ldbmodify <options> <ldif...> \n " ) ;
printf ( " Options: \n " ) ;
printf ( " -H ldb_url choose the database (or $LDB_URL) \n " ) ;
2004-11-15 17:16:10 +03:00
printf ( " -o options pass options like modules to activate \n " ) ;
printf ( " e.g: -o modules:timestamps \n " ) ;
2004-04-11 00:18:22 +04:00
printf ( " \n " ) ;
printf ( " Modifies a ldb based upon ldif change records \n \n " ) ;
exit ( 1 ) ;
}
2004-04-03 16:29:21 +04:00
2004-04-11 00:18:22 +04:00
/*
process modifies for one file
*/
2006-10-14 12:26:10 +04:00
static int process_file ( struct ldb_context * ldb , FILE * f , int * count )
2004-04-11 00:18:22 +04:00
{
struct ldb_ldif * ldif ;
2006-10-14 12:26:10 +04:00
int ret = LDB_SUCCESS ;
2004-04-11 00:18:22 +04:00
2004-05-20 17:25:06 +04:00
while ( ( ldif = ldb_ldif_read_file ( ldb , f ) ) ) {
2004-04-03 16:29:21 +04:00
switch ( ldif - > changetype ) {
case LDB_CHANGETYPE_NONE :
case LDB_CHANGETYPE_ADD :
2005-01-02 10:49:29 +03:00
ret = ldb_add ( ldb , ldif - > msg ) ;
2004-04-03 16:29:21 +04:00
break ;
case LDB_CHANGETYPE_DELETE :
2005-01-02 10:49:29 +03:00
ret = ldb_delete ( ldb , ldif - > msg - > dn ) ;
2004-04-03 16:29:21 +04:00
break ;
case LDB_CHANGETYPE_MODIFY :
2005-01-02 10:49:29 +03:00
ret = ldb_modify ( ldb , ldif - > msg ) ;
2004-04-03 16:29:21 +04:00
break ;
}
2006-10-14 12:26:10 +04:00
if ( ret ! = LDB_SUCCESS ) {
2004-04-03 16:29:21 +04:00
fprintf ( stderr , " ERR: \" %s \" on DN %s \n " ,
2006-11-22 05:05:19 +03:00
ldb_errstring ( ldb ) , ldb_dn_get_linearized ( ldif - > msg - > dn ) ) ;
2004-04-03 16:29:21 +04:00
failures + + ;
} else {
2006-10-14 12:26:10 +04:00
( * count ) + + ;
2004-04-03 16:29:21 +04:00
}
2004-05-20 17:25:06 +04:00
ldb_ldif_read_free ( ldb , ldif ) ;
2004-04-03 16:29:21 +04:00
}
2006-10-14 12:26:10 +04:00
return ret ;
2004-04-11 00:18:22 +04:00
}
2006-05-01 05:34:04 +04:00
int main ( int argc , const char * * argv )
2004-04-11 00:18:22 +04:00
{
struct ldb_context * ldb ;
int count = 0 ;
2006-10-14 12:26:10 +04:00
int i , ret = LDB_SUCCESS ;
2005-06-18 11:42:21 +04:00
struct ldb_cmdline * options ;
2004-04-11 00:18:22 +04:00
2006-03-02 19:32:53 +03:00
ldb_global_init ( ) ;
2005-06-18 11:42:21 +04:00
ldb = ldb_init ( NULL ) ;
2004-11-15 14:40:27 +03:00
2005-06-18 11:42:21 +04:00
options = ldb_cmdline_process ( ldb , argc , argv , usage ) ;
2004-04-11 00:18:22 +04:00
2005-06-18 11:42:21 +04:00
if ( options - > argc = = 0 ) {
2006-10-14 12:26:10 +04:00
ret = process_file ( ldb , stdin , & count ) ;
2005-06-19 08:20:54 +04:00
} else {
for ( i = 0 ; i < options - > argc ; i + + ) {
const char * fname = options - > argv [ i ] ;
FILE * f ;
2005-06-18 11:42:21 +04:00
f = fopen ( fname , " r " ) ;
2005-06-19 08:20:54 +04:00
if ( ! f ) {
perror ( fname ) ;
exit ( 1 ) ;
}
2006-10-14 12:26:10 +04:00
ret = process_file ( ldb , f , & count ) ;
2004-04-11 00:18:22 +04:00
}
}
2005-02-27 14:35:47 +03:00
talloc_free ( ldb ) ;
2004-04-03 16:29:21 +04:00
printf ( " Modified %d records with %d failures \n " , count , failures ) ;
2004-05-08 03:54:41 +04:00
2006-10-14 12:26:10 +04:00
return ret ;
2004-04-03 16:29:21 +04:00
}