3ee001cf23f02bc0e6de1bebc9919a18a2f020a5
[mat/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 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
41 static void usage(void)
42 {
43         printf("Usage: ldbrename [<options>] <olddn> <newdn>\n");
44         printf("Options:\n");
45         printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
46         printf("  -o options       pass options like modules to activate\n");
47         printf("              e.g: -o modules:timestamps\n");
48         printf("\n");
49         printf("Renames records in a ldb\n\n");
50         exit(1);
51 }
52
53
54  int main(int argc, char * const argv[])
55 {
56         struct ldb_context *ldb;
57         const char *ldb_url;
58         const char **options = NULL;
59         int ldbopts;
60         int opt, ret;
61
62         ldb_url = getenv("LDB_URL");
63
64         ldbopts = 0;
65         while ((opt = getopt(argc, argv, "hH:o:")) != EOF) {
66                 switch (opt) {
67                 case 'H':
68                         ldb_url = optarg;
69                         break;
70
71                 case 'o':
72                         options = ldb_options_parse(options, &ldbopts, optarg);
73                         break;
74
75                 case 'h':
76                 default:
77                         usage();
78                         break;
79                 }
80         }
81
82         if (!ldb_url) {
83                 fprintf(stderr, "You must specify a ldb URL\n\n");
84                 usage();
85         }
86
87         argc -= optind;
88         argv += optind;
89
90         ldb = ldb_connect(ldb_url, 0, options);
91
92         if (!ldb) {
93                 perror("ldb_connect");
94                 exit(1);
95         }
96
97         ldb_set_debug_stderr(ldb);
98
99         if (argc < 2) {
100                 usage();
101         }
102
103         ret = ldb_rename(ldb, argv[0], argv[1]);
104         if (ret == 0) {
105                 printf("Renamed 1 record\n");
106         } else  {
107                 printf("rename of '%s' to '%s' failed - %s\n", 
108                         argv[0], argv[1], ldb_errstring(ldb));
109         }
110
111         ldb_close(ldb);
112         
113         return ret;
114 }