4b3b27c130078fdd765c1809b48ef34e68da6db1
[metze/samba/wip.git] / source4 / lib / ldb / tools / ldbrename.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5    Copyright (C) Stefan Metzmacher  2004
6
7      ** NOTE! The following LGPL license applies to the ldb
8      ** library. This does NOT imply that all of Samba is released
9      ** under the LGPL
10    
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Lesser General Public
13    License as published by the Free Software Foundation; either
14    version 2 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Lesser General Public License for more details.
20
21    You should have received a copy of the GNU Lesser General Public
22    License along with this library; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 */
25
26 /*
27  *  Name: ldb
28  *
29  *  Component: ldbrename
30  *
31  *  Description: utility to rename records - modelled on ldapmodrdn
32  *
33  *  Author: Andrew Tridgell
34  *  Author: Stefan Metzmacher
35  */
36
37 #include "includes.h"
38 #include "ldb/include/ldb.h"
39 #include "ldb/include/ldb_private.h"
40 #include "ldb/tools/cmdline.h"
41
42 #ifdef _SAMBA_BUILD_
43 #include "system/filesys.h"
44 #endif
45
46 static void usage(void)
47 {
48         printf("Usage: ldbrename [<options>] <olddn> <newdn>\n");
49         printf("Options:\n");
50         printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
51         printf("  -o options       pass options like modules to activate\n");
52         printf("              e.g: -o modules:timestamps\n");
53         printf("\n");
54         printf("Renames records in a ldb\n\n");
55         exit(1);
56 }
57
58
59  int main(int argc, const char **argv)
60 {
61         struct ldb_context *ldb;
62         int ret;
63         struct ldb_cmdline *options;
64         const struct ldb_dn *dn1, *dn2;
65
66         ldb = ldb_init(NULL);
67
68         options = ldb_cmdline_process(ldb, argc, argv, usage);
69
70         if (options->argc < 2) {
71                 usage();
72         }
73
74         dn1 = ldb_dn_explode(ldb, options->argv[0]);
75         dn2 = ldb_dn_explode(ldb, options->argv[1]);
76
77         ret = ldb_rename(ldb, dn1, dn2);
78         if (ret == 0) {
79                 printf("Renamed 1 record\n");
80         } else  {
81                 printf("rename of '%s' to '%s' failed - %s\n", 
82                         options->argv[0], options->argv[1], ldb_errstring(ldb));
83         }
84
85         talloc_free(ldb);
86         
87         return ret;
88 }