summaryrefslogtreecommitdiffstats
path: root/ldap/admin/lib/dsalib_db.c
blob: 02d9498fc43cfb81c66d6e369f1837024308164f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/** BEGIN COPYRIGHT BLOCK
 * Copyright 2001 Sun Microsystems, Inc.
 * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/
#if defined( XP_WIN32 )
#include <windows.h>
#include <process.h>
#include <io.h>
#endif
#include "dsalib.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#if !defined( XP_WIN32 )
#include <dirent.h>
#include <unistd.h>
#else
#define popen _popen
#define pclose _pclose
#endif
#include "portable.h"

/*
 * Get a listing of backup directories
 * Return NULL for errors  and a NULL list for an empty list.
 */
 
DS_EXPORT_SYMBOL char **
ds_get_bak_dirs()
{
    char	format_str[PATH_MAX];
    char    *root;
    int		i = 0;
    char	**bak_dirs = NULL;

    if ( (root = ds_get_install_root()) == NULL ) 
	{
        ds_send_error("Cannot find server root directory.", 0);
        return(bak_dirs);
    }

    sprintf( format_str, "%s%cbak", root, FILE_SEP );
	bak_dirs = ds_get_file_list( format_str );
	if( bak_dirs )
	{
		while( bak_dirs[i] != NULL )
		{
			/* Prepend the filename with the install root */
			char filename[PATH_MAX];
			sprintf( filename, "%s%cbak%c%s", root, FILE_SEP,
					 FILE_SEP, bak_dirs[i] );
			free( bak_dirs[i] );
			bak_dirs[i] = strdup( filename );
#if defined( XP_WIN32 )
			ds_dostounixpath( bak_dirs[i] );
#endif
			i++;
		}
	}

    return(bak_dirs);
}

/*
 * Restore a database based on a backup directory name.
 * 0:             success
 * anything else: failure
 */
DS_EXPORT_SYMBOL int
ds_bak2db(char *file)
{
    char        startup_line[BIG_LINE];
    char        statfile[PATH_MAX];
    char        *tmp_dir;
    char        *root;
    int         haderror = 0;
    int         error = -1;
    int         status;
    FILE        *sf = NULL;
	struct stat	fstats;

    if ( file == NULL ) {
        return DS_NULL_PARAMETER;
    }
    status = ds_get_updown_status();
    if ( status == DS_SERVER_UP ) {
        return DS_SERVER_MUST_BE_DOWN;
    }
    if ( (root = ds_get_install_root()) == NULL ) {
        return DS_NO_SERVER_ROOT;
    }

    if ( file[strlen(file) - 1] == '\n' )	/* strip out returns */
		file[strlen(file) - 1] = '\0';

    if( stat( file, &fstats ) == -1 && errno == ENOENT ) {
        return DS_CANNOT_OPEN_BACKUP_FILE;
    } else if( !(fstats.st_mode & S_IFDIR) ) {
        return DS_NOT_A_DIRECTORY;
    }

    tmp_dir = ds_get_tmp_dir();
    sprintf(statfile, "%s%cbak2db.%d", tmp_dir, FILE_SEP, (int)getpid());
    sprintf(startup_line,
			"%s%cbak2db "
			"%s%s%s > "
			"%s%s%s 2>&1",
			root, FILE_SEP, 
			ENQUOTE, file, ENQUOTE, 
			ENQUOTE, statfile, ENQUOTE );
    alter_startup_line(startup_line);
    fflush(0);
    error = system(startup_line);
    fflush(0);
    if ( error == -1 ) {
        return DS_CANNOT_EXEC;
    }
	fflush(0);
    if( !(sf = fopen(statfile, "r")) )  {
        return DS_CANNOT_OPEN_STAT_FILE;
    }

    while ( fgets(startup_line, BIG_LINE, sf) ) {
		if ((strstr(startup_line, "- Restoring file")) || 
			(strstr(startup_line, "- Checkpointing"))) {
			ds_show_message(startup_line);
		} else {
			haderror = 1;
			ds_send_error(startup_line, 0);
		}
    }

    fclose(sf);
    unlink(statfile);

    if ( haderror )
        return DS_UNKNOWN_ERROR;
    return 0;
}

/*
 * Create a backup based on a file name.
 * 0:             success
 * anything else: failure
 */
DS_EXPORT_SYMBOL int
ds_db2bak(char *file)
{
    char        startup_line[BIG_LINE];
    char        statfile[PATH_MAX];
    char        *tmp_dir;
    char        *root;
    int         haderror = 0;
    int         error = -1;
    FILE        *sf = NULL;
    int		lite = 0;
#ifdef XP_WIN32
    time_t	ltime;
#endif

    if ( (root = ds_get_install_root()) == NULL ) {
        return DS_NO_SERVER_ROOT;
    }

    if ( (file == NULL) || (strlen(file) == 0) )
        file = NULL;

	tmp_dir = ds_get_tmp_dir();
    sprintf(statfile, "%s%cdb2bak.%d", tmp_dir, FILE_SEP, (int)getpid());
	
					
#if defined( XP_WIN32 )
	if( file == NULL )
	{
		file = malloc( BIG_LINE );

		time( &ltime );
		sprintf( file, "%s", ctime( &ltime ) );
		ds_timetofname( file );
	}

	/* Check if the directory exists or can be created */
	if ( !ds_file_exists( file ) ) {
		char *errmsg = ds_mkdir_p( file, NEWDIR_MODE );
		if( errmsg != NULL ) {
/*			ds_send_error(errmsg, 10);
 */
			return DS_CANNOT_CREATE_DIRECTORY;
		}
	}
#endif

/* DBDB: note on the following line. 
 * Originally this had quotes round the directory name.
 * I found that this made the script not work becuase
 * a path of the form "foo"/bar/"baz" was passed to slapd.
 * the c runtime didn't like this. Perhaps there's a simple
 * solution, but for now I've modified this line here to 
 * not quote the directory name. This means that backup
 * directories can't have spaces in them.
 */


    sprintf(startup_line,
			"%s%cdb2bak "
			"%s%s%s > "
			"%s%s%s 2>&1",
			root, FILE_SEP,
			ENQUOTE,
			(file == NULL) ? "" : file,
			ENQUOTE,
			ENQUOTE, statfile, ENQUOTE);

	PATH_FOR_PLATFORM( startup_line );
    alter_startup_line(startup_line);
    fflush(0);
    error = system(startup_line);
    if ( error == -1 ) {
        return DS_CANNOT_EXEC;
    }
    if( !(sf = fopen(statfile, "r")) )  {
        return DS_CANNOT_OPEN_STAT_FILE;
    }

    while ( fgets(startup_line, BIG_LINE, sf) ) {
        if (strstr(startup_line, " - Backing up file") ||
            strstr(startup_line, " - Checkpointing database")) {
            ds_show_message(startup_line);
        } else {
            haderror = 1;
            if (strstr ( startup_line, "restricted mode")) {
                lite = 1;
            }
            ds_send_error(startup_line, 0);
        }
    }
    fclose(sf);
    unlink(statfile);

    if ( lite && haderror )
	return DS_HAS_TOBE_READONLY_MODE;

    if ( haderror )
        return DS_UNKNOWN_ERROR;
    return 0;
}

static void
process_and_report( char *line, int line_size, FILE *cmd )
{
	while(fgets(line, line_size, cmd))  {
		/* Strip off line feeds */
		int ind = strlen( line ) - 1;
		while ( (ind >= 0) &&
				((line[ind] == '\n') ||
				 (line[ind] == '\r')) ) {
			line[ind] = 0;
			ind--;
		}
		if ( ind < 1 ) {
			continue;
		}
		ds_send_status(line);
	}
}

static int exec_and_report( char *startup_line )
{
	FILE        *cmd = NULL;
    char        line[BIG_LINE];
    int         haderror = 0;

	PATH_FOR_PLATFORM( startup_line );
    alter_startup_line(startup_line);

	/*
	  fprintf( stdout, "Launching <%s>\n", startup_line );
	*/

    fflush(0);
	cmd = popen(startup_line, "r");
	if(!cmd) {
        return DS_CANNOT_EXEC;
    }
	process_and_report( line, sizeof(line), cmd );
	pclose(cmd);

    /*
    ** The VLV indexing code prints OK,
    ** if the index was successfully created.
    */
	if (strcmp(line,"OK")==0) {
		haderror = 0;
	} else {
		haderror = DS_UNKNOWN_ERROR;
	}

    return haderror;
}

/*
 * Create a vlv index
 * 0:             success
 * anything else: failure
 */
DS_EXPORT_SYMBOL int
ds_vlvindex(char **backendList, char **vlvList)
{
    char        startup_line[BIG_LINE];
    char        *root;
    char        *instroot;
	char		**vlvc = NULL;
			

    root = ds_get_server_root();
    instroot = ds_get_install_root();
    if ( (root == NULL) || (instroot == NULL) ) {
        return DS_NO_SERVER_ROOT;
    }

    sprintf(startup_line, "%s/bin/slapd/server/%s db2index "
			"-D %s%s/%s "
			"-n %s ",
			root, SLAPD_NAME,			
			ENQUOTE, instroot, ENQUOTE,
			backendList[0]);


	/* Create vlv TAG */
	vlvc=vlvList;
	while( *vlvc != NULL ) {
		sprintf( startup_line, "%s -T %s%s%s", startup_line,"\"",*vlvc,"\"" );
		vlvc++;
	}	
   
	return exec_and_report( startup_line );
}

/*
 * Create one or more indexes
 * 0:             success
 * anything else: failure
 */
DS_EXPORT_SYMBOL int
ds_addindex(char **attrList, char *backendName)
{
    char        startup_line[BIG_LINE];
    char        *root;
    char        *instroot;
 
    root = ds_get_server_root();
    instroot = ds_get_install_root();

    if ( (root == NULL) || (instroot == NULL) ) {
        return DS_NO_SERVER_ROOT;
    }

	sprintf(startup_line, "%s/bin/slapd/server/%s db2index "
			"-D %s%s%s "
			"-n %s",
			root, SLAPD_NAME,			
			ENQUOTE, instroot, ENQUOTE,
			backendName);

	while( *attrList != NULL ) {
		sprintf( startup_line, "%s -t %s", startup_line, *attrList );
		attrList++;
	}

	return exec_and_report( startup_line );
}