e4087f42b8a790f5a93bf4ab45d73e09af88c9ea
[ira/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 3 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, see <http://www.gnu.org/licenses/>.
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldbrename
29  *
30  *  Description: utility to rename records - modelled on ldapmodrdn
31  *
32  *  Author: Andrew Tridgell
33  *  Author: Stefan Metzmacher
34  */
35
36 #include "ldb.h"
37 #include "tools/cmdline.h"
38
39 static void usage(struct ldb_context *ldb)
40 {
41         printf("Usage: ldbrename [<options>] <olddn> <newdn>\n");
42         printf("Renames records in a ldb\n\n");
43         ldb_cmdline_help(ldb, "ldbmodify", stdout);
44         exit(1);
45 }
46
47
48 int main(int argc, const char **argv)
49 {
50         struct ldb_context *ldb;
51         int ret;
52         struct ldb_cmdline *options;
53         struct ldb_dn *dn1, *dn2;
54         TALLOC_CTX *mem_ctx = talloc_new(NULL);
55
56         ldb = ldb_init(mem_ctx, NULL);
57
58         options = ldb_cmdline_process(ldb, argc, argv, usage);
59
60         if (options->argc < 2) {
61                 usage(ldb);
62         }
63
64         dn1 = ldb_dn_new(ldb, ldb, options->argv[0]);
65         dn2 = ldb_dn_new(ldb, ldb, options->argv[1]);
66
67         ret = ldb_rename(ldb, dn1, dn2);
68         if (ret == LDB_SUCCESS) {
69                 printf("Renamed 1 record\n");
70         } else  {
71                 printf("rename of '%s' to '%s' failed - %s\n", 
72                         options->argv[0], options->argv[1], ldb_errstring(ldb));
73         }
74
75         talloc_free(mem_ctx);
76         
77         return ret;
78 }