r23798: updated old Temple Place FSF addresses to new URL
[samba.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_includes.h"
37 #include "tools/cmdline.h"
38
39 static void usage(void)
40 {
41         printf("Usage: ldbrename [<options>] <olddn> <newdn>\n");
42         printf("Options:\n");
43         printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
44         printf("  -o options       pass options like modules to activate\n");
45         printf("              e.g: -o modules:timestamps\n");
46         printf("\n");
47         printf("Renames records in a ldb\n\n");
48         exit(1);
49 }
50
51
52 int main(int argc, const char **argv)
53 {
54         struct ldb_context *ldb;
55         int ret;
56         struct ldb_cmdline *options;
57         struct ldb_dn *dn1, *dn2;
58
59         ldb_global_init();
60
61         ldb = ldb_init(NULL);
62
63         options = ldb_cmdline_process(ldb, argc, argv, usage);
64
65         if (options->argc < 2) {
66                 usage();
67         }
68
69         dn1 = ldb_dn_new(ldb, ldb, options->argv[0]);
70         dn2 = ldb_dn_new(ldb, ldb, options->argv[1]);
71
72         if ( ! ldb_dn_validate(dn1)) {
73                 printf("Invalid DN1: %s\n", options->argv[0]);
74                 return -1;
75         }
76         if ( ! ldb_dn_validate(dn2)) {
77                 printf("Invalid DN2: %s\n", options->argv[1]);
78                 return -1;
79         }
80
81         ret = ldb_rename(ldb, dn1, dn2);
82         if (ret == 0) {
83                 printf("Renamed 1 record\n");
84         } else  {
85                 printf("rename of '%s' to '%s' failed - %s\n", 
86                         options->argv[0], options->argv[1], ldb_errstring(ldb));
87         }
88
89         talloc_free(ldb);
90         
91         return ret;
92 }