summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/back-ldif/config.c
blob: a62335a56f02d9f07560870d0b009988c5900229 (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
/** BEGIN COPYRIGHT BLOCK
 * Copyright 2001 Sun Microsystems, Inc.
 * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/
/* 
 *  File: config.c
 *
 *  Functions:
 * 
 *     ldif_back_config() - Reads in and stores ldif file for ldif backend
 *     ldif_read_one_record() - Reads in one record from ldif file as a string 
 *
 */

#include "back-ldif.h"
#define LDAPMOD_MAXLINE              4096
#define safe_realloc( ptr, size )	( ptr == NULL ? malloc( size ) : \
					 realloc( ptr, size ))
static char *ldif_read_one_record();


/*
 *  Function: ldif_back_config
 *  
 *  Returns: 0 if success, -1 if not
 *  
 *  Description: Reads the data in the ldif file specified in ldif.conf
 *               stores it in db
 */
int
ldif_back_config( Slapi_PBlock *pb )
{
  LDIF  *db;                  /*The ldif file will be read into this structure*/
  char  *fname;               /*Config file name*/
  int   lineno, argc;         /*Config file stuff*/
  char  **argv;               /*More config file stuff*/
  FILE  *fp;                  /*Pointer to ldif file*/
  char  *buf;                 /*Tmp storage for ldif entries*/
  int   first;                /*Boolean to determine if db is empty*/
  ldif_Entry *cur;            /*For db manipulation*/
  ldif_Entry *new;            /*For db manipulation*/
  Slapi_Entry *tmp;                 /*Used for initialization purposes*/


  LDAPDebug( LDAP_DEBUG_TRACE, "=> ldif_back_config\n", 0, 0, 0 );
  
  /* 
   * Get the private_info structure you created in ldif_back_init(). 
   * Also get the config file name, current line number, and arguments
   * from the current line, broken out into an argv.
   */
  if (slapi_pblock_get( pb, SLAPI_PLUGIN_PRIVATE, &db ) < 0 ||
      slapi_pblock_get( pb, SLAPI_CONFIG_FILENAME, &fname ) < 0 ||
      slapi_pblock_get( pb, SLAPI_CONFIG_LINENO, &lineno ) < 0 ||
      slapi_pblock_get( pb, SLAPI_CONFIG_ARGC, &argc ) < 0 ||
      slapi_pblock_get( pb, SLAPI_CONFIG_ARGV, &argv ) < 0 ){
    LDAPDebug(LDAP_DEBUG_ANY, "Ldif Backend: unable to get data from front end\n", 0, 0, 0);
    return(-1);
  }

  
  /*
   * Process the config info. For example, if the config file
   * contains a line like this:
   *
   * 	file	/path/to/ldif/file
   *
   * then argv[0] would be "file", and argv[1] would be the file
   * name. 
   */
  
  /*Check for the correct number of arguments*/
  if (argc != 2){
    LDAPDebug(LDAP_DEBUG_ANY, "Ldif Backend: Unable to configure; invalid ldif input file specification line format (file: %s, line: %d)\n", 
	    fname, lineno, 0); 
    return(-1);
  }
  
  /*Check for the right format*/
  if (strcmp(argv[0], "file") != 0){
    LDAPDebug(LDAP_DEBUG_ANY, "Ldif Backend: unable to configure; invalid parameter \"%s\" in config file (file: %s, line: %d)\n", 
	    argv[0], fname, lineno ); 
    return(-1);
  }
  
  /*Now we fopen the file and grab up the contents*/
  fp = fopen (argv[1], "r");
  if (fp == NULL){
    LDAPDebug(LDAP_DEBUG_ANY, "Ldif Backend: unable to read ldif file %s\n", argv[1], 0, 0);
    fp = fopen (argv[1], "w");
    if(fp == NULL){
      LDAPDebug(LDAP_DEBUG_ANY, "Ldif Backend: unable to create ldif file %s\n", argv[1], 0, 0);
      return -1;
    }
  }
    
  first = 1;

  /* Lock the database first, just to be safe*/
  PR_Lock( db->ldif_lock );

  /*Save the filename, for modifications to the file later*/
  if ((db->ldif_file = strdup(argv[1])) == NULL){
    LDAPDebug(LDAP_DEBUG_ANY, "Ldif Backend: out of memory\n", 0, 0, 0);
    PR_Unlock( db->ldif_lock );
    fclose(fp);
    return(-1);
  }
  
  /* 
   * Loop through the entries in the file, and add them to the end
   * of the linked list
   */
  while ((buf = ldif_read_one_record(fp)) != NULL){

    /*Create a new element for the linked list of entries*/
    tmp = (Slapi_Entry *) slapi_str2entry(buf,
				SLAPI_STR2ENTRY_NOT_WELL_FORMED_LDIF);
    new = (ldif_Entry *) ldifentry_init(tmp);
    if (new == NULL){
      LDAPDebug(LDAP_DEBUG_ANY, "Ldif Backend: unable to read in ldif file; out of memory\n",0 ,0 ,0 );
      PR_Unlock( db->ldif_lock );
      fclose(fp);
      return(-1);
    }
    
    /*
     * If this is the first entry we are adding,
     * we have to make it the first element in the list
     */
    if (first){
      db->ldif_entries = new;
      first = 0;
    } else{
      cur->next = new;
    }
    
    /*Reset the pointer to the last element in the list*/
    cur = new;

    /*Increment the number of entries*/
    db->ldif_n++;

    /*Free the buffer*/
    free ((void *) buf );

  }
    
  /*By now, the database should be read in*/
  PR_Unlock( db->ldif_lock );
  fclose(fp);

  LDAPDebug( LDAP_DEBUG_TRACE, "<= ldif_back_config\n", 0, 0, 0 );
  return( 0 );
}



/*
 *  Function: ldif_read_one_record
 *  
 *  Returns: a long string representing an ldif record
 *  
 *  Description: Returns a huge string comprised of 1 ldif record
 *               read from fp.
 *
 */
static char *
ldif_read_one_record( FILE *fp )
{
  int   len, gotnothing;
  char  *buff, line[ LDAPMOD_MAXLINE ];
  int	lcur, lmax;
  
  lcur = lmax = 0;
  buff = NULL;
  gotnothing = 1;
  
  while ( fgets( line, sizeof(line), fp ) != NULL ) {
    if ( (len = strlen( line )) < 2 ) {
      if ( gotnothing ) {
	continue;
      } else {
	break;
      }
    }
    gotnothing = 0;
    if ( lcur + len + 1 > lmax ) {
      lmax = LDAPMOD_MAXLINE
	* (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
      if (( buff = (char *)safe_realloc( buff, lmax )) == NULL ) {
	perror( "safe_realloc" );
	exit( LDAP_NO_MEMORY );
      }
    }
    strcpy( buff + lcur, line );
    lcur += len;
  }
  
  return( buff );
}