summaryrefslogtreecommitdiffstats
path: root/pki/base/migrate/TpsTo80/migrateTPSData.c
blob: a4cf340ab1438df8ee76ca1acc418f924065f6ac (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// --- BEGIN COPYRIGHT BLOCK ---
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation;
// version 2.1 of the License.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA  02110-1301  USA 
// 
// Copyright (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef SOLARIS
#include <mozldap6/ldif.h>
#else
#include <mozldap/ldif.h>
#endif
#include <ctype.h>
#include <nspr4/nspr.h>
#include <nspr4/plstr.h>
#include <nspr4/plhash.h>
#include <nspr4/prmem.h>
#include <nspr4/prprf.h>
#include <nspr4/prsystem.h>
#include <errno.h>
#include <string.h>

#define SHORT_LEN 512
#define NO_TOKEN_TYPE "no_token_type"

static PLHashTable *token_set;
static FILE *infile;
static FILE *outfile;

/* hash functions */
static PR_CALLBACK void*
_AllocTable(void* pool, PRSize size)
{
    return PR_MALLOC(size);
}

static PR_CALLBACK void
_FreeTable(void* pool, void* item)
{
    PR_DELETE(item);
}

static PR_CALLBACK PLHashEntry*
_AllocEntry(void* pool, const void* key)
{
    return PR_NEW(PLHashEntry);
}

static PR_CALLBACK void
_FreeEntry(void* pool, PLHashEntry* he, PRUintn flag)
{
    if( he == NULL ) {
        return;
    }

    if (flag == HT_FREE_VALUE) {
        if( he->value != NULL ) {
            PL_strfree( ( char* ) he->value );
            he->value = NULL;
        }
    } else if (flag == HT_FREE_ENTRY) {
        if( he->key != NULL ) {
            PL_strfree( ( char* ) he->key );
            he->key = NULL;
        }
        if( he->value != NULL ) {
            PL_strfree( ( char* ) he->value );
            he->value = NULL;
        }
        PR_DELETE(he);
    }
}

static PLHashAllocOps _AllocOps = {
    _AllocTable,
    _FreeTable,
    _AllocEntry,
    _FreeEntry
};

/* utility functions */
#ifdef SOLARIS
void do_free(char * buf)
{
    if (buf != NULL) {
        PR_Free(buf);
        buf = NULL;
    }
}
#else
inline void do_free(char * buf)
{
    if (buf != NULL) {
        PR_Free(buf);
        buf = NULL;
    }
}
#endif



char *get_field( char *s, char* fname, int len)
{
    char *end = NULL;
    int  n;

    if( ( s = PL_strstr( s, fname ) ) == NULL ) {
        return NULL;
    }

    s += strlen(fname);
    end = PL_strchr( s, ' ' );

    if( end != NULL ) {
        n = end - s;
    } else {
        n = PL_strlen( s );
    }

    if (n == 0) {
        return NULL;
    } else if (n > len) {
        /* string too long */
        return NULL;
    } else {
        return PL_strndup( s, n );
    }
}

/*
 * Read the ldif, munge the entry and write to output.
 */
int read_and_modify_ldif() {
    // user changes
    static char agent_entry[] = "dn: cn=TUS Agents,ou=Groups";
    static int agent_ent_len = sizeof(agent_entry)-1;
    static char admin_entry[] = "dn: cn=TUS Adminstrators,ou=Groups";
    static int admin_ent_len = sizeof(admin_entry)-1;
    static char operator_entry[] = "dn: cn=TUS Officers,ou=Groups";
    static int operator_ent_len = sizeof(operator_entry)-1;
    static char user_entry[] = "ou=People";

    // token changes
    static char token_entry[] = "ou=Tokens";

    // activity changes
    static char activity_entry[] = "ou=Activities";

    char *entry = 0;
    int lineno = 0;

    while ((entry = ldif_get_entry(infile, &lineno))) {
        char *begin = entry;

        if (!PL_strncasecmp(entry, agent_entry, agent_ent_len)) {
            process_agent_entry(entry);
        } else if (!PL_strncasecmp(entry, admin_entry, admin_ent_len)) {
            process_admin_entry(entry);
        } else if (!PL_strncasecmp(entry, operator_entry, operator_ent_len)) {
            process_operator_entry(entry);
        } else if (PL_strstr(entry, token_entry) != NULL) {
            process_token_entry(entry);
        } else if (PL_strstr(entry, activity_entry) != NULL) {
            process_activity_entry(entry);
        } else if ((PL_strstr(entry, user_entry) != NULL) && 
                   (PL_strstr(entry, "objectClass: organizationalunit") == NULL))  {
            process_user_entry(entry);
        } else {
            process_unchanged_entry(entry);
            fprintf(outfile, "\n");
        }
        free(begin);
    }
    return 0;
}

/**
 * read the file, parse the activity records
 * record the tokenTypes found for later use
 */
int parse_ldif_activities() {
    // activity changes
    static char activity_entry[] = "ou=Activities";

    char *entry = 0;
    int lineno = 0;

    while ((entry = ldif_get_entry(infile, &lineno))) {
        char *begin = entry;
        if (PL_strstr(entry, activity_entry) != NULL) {
            parse_activity_entry(entry);
        }
        free(begin);
    }
    return 0;
}

int parse_activity_entry(char* entry) {
    static char tokenMsg_attr[] = "tokenMsg";
    static char tokenid_attr[] = "tokenID";
    char *line = entry;
    char *tokenType = NULL;
    char *cuid = NULL;
    while ((line = ldif_getline(&entry))) {
        char *type, *value;
        int vlen = 0;
        int rc;

        if ( *line == '\n' || *line == '\0' ) {
            break;
        }

        /* this call modifies line */
        rc = ldif_parse_line(line, &type, &value, &vlen);
        if (rc != 0) {
            printf("Unknown error processing ldif entry: %s\n", entry);
        } else {
            if (!PL_strncasecmp(type, tokenMsg_attr, SHORT_LEN)) {
                tokenType = get_field(value, "tokenType=",SHORT_LEN);
            } else if (!PL_strncasecmp(type, tokenid_attr, SHORT_LEN)) {
                cuid = PL_strdup(value);
            }
        }
    }

    if ((tokenType != NULL) && (cuid != NULL)) {
        if ((char *) PL_HashTableLookupConst(token_set, cuid) == NULL) {
            PL_HashTableAdd(token_set, PL_strdup(cuid), PL_strdup(tokenType));
            //printf("Adding entry: %s %s to hash\n", cuid, tokenType); 
        }
    }
    do_free(cuid);
    do_free(tokenType);
    return 0;
}



/* change uniqueMember -> member */
int process_agent_entry(char* entry) {
    static char member_attr[] = "uniqueMember";

    char *line = entry;
    while ((line = ldif_getline(&entry))) {
        char *type, *value;
        int vlen = 0;
        int rc;

        if ( *line == '\n' || *line == '\0' ) {
            break;
        }

        /* this call modifies line */
        rc = ldif_parse_line(line, &type, &value, &vlen);
        if (rc != 0) {
            printf("Unknown error processing ldif entry: %s\n", entry);
        } else {
            if (!PL_strncasecmp(type, member_attr, SHORT_LEN)) {
		fprintf(outfile, "member: %s\n", value);
            } else if ((!PL_strncasecmp(type, "objectClass", SHORT_LEN)) && (!PL_strncasecmp(value, "groupOfUniqueNames", SHORT_LEN))) {
                fprintf(outfile, "objectClass: groupOfNames\n");
            } else {
                fprintf(outfile, "%s", ldif_type_and_value(type, value, vlen));
            }
        }
    } 
    fprintf(outfile, "\n");
    return 0;
}

/* same as agent */
int process_operator_entry(char* entry) {
    return process_agent_entry(entry);
}

/* change uniqueMember -> member 
 * change typo in dn
 */
int process_admin_entry(char* entry) {
    static char member_attr[] = "uniqueMember";
    static char dn_attr[] = "dn";

    char *line = entry;
    while ((line = ldif_getline(&entry))) {
        char *type, *value;
        int vlen = 0;
        int rc;

        if ( *line == '\n' || *line == '\0' ) {
            break;
        }

        /* this call modifies line */
        rc = ldif_parse_line(line, &type, &value, &vlen);
        if (rc != 0) {
            printf("Unknown error processing ldif entry: %s", entry);
        } else {
            if (!PL_strncasecmp(type, member_attr, SHORT_LEN)) {
                fprintf(outfile, "member: %s\n", value);
            } else if (!PL_strncasecmp(type, dn_attr, SHORT_LEN)) {
                int rep_size = PL_strlen("cn=TUS Adminstrators,ou=Groups,");
                fprintf(outfile, "dn: cn=TUS Administrators,ou=Groups,%s\n", value + rep_size);
            } else if ((!PL_strncasecmp(type, "objectClass", SHORT_LEN)) && (!PL_strncasecmp(value, "groupOfUniqueNames", SHORT_LEN))) {
                fprintf(outfile, "objectClass: groupOfNames\n");
            } else {
                fprintf(outfile, "%s", ldif_type_and_value(type, value, vlen));
            }
        }
    }
    fprintf(outfile, "\n");
    return 0;
}

int process_user_entry(char *entry) {
    process_unchanged_entry(entry);
    fprintf(outfile, "objectClass: tpsProfileId\n");
    fprintf(outfile, "profileID: All Profiles\n");
    fprintf(outfile, "\n");
    return 0;
}
    
int process_unchanged_entry(char *entry) {
    char *line = entry;
    while ((line = ldif_getline(&entry))) {
        char *type, *value;
        int vlen = 0;
        int rc;

        if ( *line == '\n' || *line == '\0' ) {
            break;
        }

        /* this call modifies line */
        rc = ldif_parse_line(line, &type, &value, &vlen);
        if (rc != 0) {
            printf("Unknown error processing ldif entry: %s\n", entry);
        } else {
            fprintf(outfile, "%s", ldif_type_and_value(type, value, vlen));
        }
    }
    return 0;
}

int process_activity_entry(char *entry) {
    static char tokenmsg_attr[] = "tokenMsg";
    static char tokenid_attr[] = "tokenID";
    char *line = entry;
    char *tokenType = NULL;
    char *cuid = NULL;
    char *dn = NULL;
    while ((line = ldif_getline(&entry))) {
        char *type, *value;
        int vlen = 0;
        int rc;

        if ( *line == '\n' || *line == '\0' ) {
            break;
        }

        /* this call modifies line */
        rc = ldif_parse_line(line, &type, &value, &vlen);
        if (rc != 0) {
            printf("Unknown error processing ldif entry: %s\n", entry);
        } else {
            fprintf(outfile, "%s", ldif_type_and_value(type, value, vlen));
            
            if (!PL_strncasecmp(type, tokenmsg_attr, SHORT_LEN)) {
                tokenType = get_field(value, "tokenType=",SHORT_LEN);
                if (tokenType != NULL) {
                    fprintf(outfile, "tokenType: %s\n", tokenType);
                }
            } else if (!PL_strncasecmp(type, tokenid_attr, SHORT_LEN)) {
                cuid = PL_strdup(value);
            } else if (!PL_strncasecmp(type, tokenid_attr, SHORT_LEN)) {
                dn = PL_strdup(value);
            }
        }
    }

    if ((tokenType == NULL) && (cuid!= NULL)) {
        // check hash for a value
        if (PL_HashTableLookupConst(token_set, cuid) != NULL) {
            fprintf(outfile, "tokenType: %s\n", (char *) PL_HashTableLookupConst(token_set, cuid));
        } else {
            fprintf(outfile, "tokenType: %s\n", NO_TOKEN_TYPE);
            // log error here - unable to set token type using dn
        }
    }
    fprintf(outfile, "\n");
    do_free(cuid);
    do_free(dn);
    do_free(tokenType);
    
    return 0;
}

int process_token_entry(char* entry) {
    static char cn_attr[] = "cn";
    static char dn_attr[] = "dn";
    char *line = entry;
    char *tokenType = NULL;
    char *dn = NULL;
    while ((line = ldif_getline(&entry))) {
        char *type, *value;
        int vlen = 0;
        int rc;

        if ( *line == '\n' || *line == '\0' ) {
            break;
        }

        /* this call modifies line */
        rc = ldif_parse_line(line, &type, &value, &vlen);
        if (rc != 0) {
            printf("Unknown error processing ldif entry: %s\n", entry);
        } else {
            fprintf(outfile, "%s", ldif_type_and_value(type, value, vlen));

            if (!PL_strncasecmp(type, cn_attr, SHORT_LEN)) {
                if (value != NULL) {
                    tokenType = (char *) PL_HashTableLookupConst(token_set, value);
                }
                if (tokenType != NULL) {
                    fprintf(outfile, "tokenType: %s\n", tokenType);
                } else {
                    fprintf(outfile, "tokenType: %s\n", NO_TOKEN_TYPE);
                }
            } else if (!PL_strncasecmp(type, dn_attr, SHORT_LEN)) {
                dn = PL_strdup(value);
            }
        }
    } 
    if ((tokenType == NULL) && (dn != NULL)) {
        //log the error
    }
    fprintf(outfile, "\n");
    do_free(dn);
    return 0;
}


int main (int argc, char *argv[]) {
   char *in_fname = NULL;
   char *out_fname = NULL;

    if (argc < 3) {
        printf ("Usage:\n  %s infile outfile\n", argv[0]);
        return 1;
    }

    in_fname = argv[1];
    infile = fopen(in_fname, "r");
    if (infile == NULL) {
        perror("Error opening input file");
        return 1;
    }
    
    out_fname = argv[2];
    outfile = fopen(out_fname, "w");
    if (outfile == NULL) {
        perror("Error opening output file");
        return 1;
    }

    //declare hash
    token_set = PL_NewHashTable(3, PL_HashString,
        PL_CompareStrings, PL_CompareValues,
        &_AllocOps, NULL);

   printf("Parsing LDIF file for Token Activities\n");
   parse_ldif_activities();
   rewind(infile);

   printf("Parsing old LDIF file, and creating new LDIF file\n\n");
   read_and_modify_ldif();

   printf("Operation is complete.\nA new LDIF file has been written at %s, \n", out_fname);
   printf("to be imported into the database of your new TPS. \nPlease attend to any errors reported.\n\n");

   fclose(infile);
   fclose(outfile);
   return 0;
}