r3093: - implment ldb_rename() and ldbrename
[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
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("\n");
45         printf("Renames records in a ldb\n\n");
46         exit(1);
47 }
48
49
50  int main(int argc, char * const argv[])
51 {
52         struct ldb_context *ldb;
53         const char *ldb_url;
54         int opt, ret;
55
56         ldb_url = getenv("LDB_URL");
57
58         while ((opt = getopt(argc, argv, "hH:")) != EOF) {
59                 switch (opt) {
60                 case 'H':
61                         ldb_url = optarg;
62                         break;
63
64                 case 'h':
65                 default:
66                         usage();
67                         break;
68                 }
69         }
70
71         if (!ldb_url) {
72                 fprintf(stderr, "You must specify a ldb URL\n\n");
73                 usage();
74         }
75
76         argc -= optind;
77         argv += optind;
78
79         ldb = ldb_connect(ldb_url, 0, NULL);
80
81         if (!ldb) {
82                 perror("ldb_connect");
83                 exit(1);
84         }
85
86         ldb_set_debug_stderr(ldb);
87
88         if (argc < 2) {
89                 usage();
90         }
91
92         ret = ldb_rename(ldb, argv[0], argv[1]);
93         if (ret == 0) {
94                 printf("Renamed 1 record\n");
95         } else  {
96                 printf("rename of '%s' to '%s' failed - %s\n", 
97                         argv[0], argv[1], ldb_errstring(ldb));
98         }
99
100         ldb_close(ldb);
101         
102         return ret;
103 }