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