summaryrefslogtreecommitdiff
path: root/cleopatre/application/libspid/src/config_backup_eoc.c
blob: 6b5ef0e240c2a4bb428451b7982e7251c6f887fc (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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/* SPC300 bundle {{{
 *
 * Copyright (C) 2012 MStar
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    application/libspid/src/config_backup_eoc.c
 * \brief   Functions specific to EoC config backup
 * \ingroup libspid
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <arpa/inet.h>
#include <errno.h>
#include <ctype.h>

#include "libspid.h"

/**
 * Returns the config_backup entry for a config_backup table with the
 * given index.<BR>
 * \param index of the config_backup server index
 * \param eoc_config_backup_entry config_backup List entry
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_NOT_FOUND: entry not found
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno for details
 */
extern libspid_error_t
libspid_eoc_config_backup_get (char *index,
                               libspid_eoc_config_backup_entry_t * entry)
{
    const char delimiters[2] = LIBSPID_CONFIG_BACKUP_DELIMITER "\0";
    char key[LIBSPID_CONFIG_KEY_MAX_LEN];
    unsigned int elt_number = LIBSPID_CONFIG_ELT_MAX_NB;
    char *elt[LIBSPID_CONFIG_ELT_MAX_NB];
    char buffer[LIBSPID_CONFIG_LINE_MAX_LEN];
    libspid_error_t error;

    /* check input parameters */
    if ((index == NULL) || (entry == NULL))
    {
        return LIBSPID_ERROR_PARAM;
    }

    strcpy (key, index);
    strcpy (entry->index, index);
    error = libspid_config_read_line (LIBSPID_CONFIG_BACKUP_PATH,
                                      delimiters, key, &elt_number,
                                      elt, buffer,
                                      LIBSPID_CONFIG_LINE_MAX_LEN);

    if (LIBSPID_SUCCESS != error)
    {
        return error;
    }

    if (elt_number < LIBSPID_CONFIG_BACKUP_PARAM_NUM)
    {
        strcpy (entry->protocol, "");
        strcpy (entry->ip_address, "");
        strcpy (entry->server_port, "");
        strcpy (entry->login, "");
        strcpy (entry->password, "");
        strcpy (entry->type, "");
        strcpy (entry->fileName, "");
        strcpy (entry->result, "");
    }
    else
    {
        strcpy (entry->protocol, elt[0]);
        strcpy (entry->ip_address, elt[1]);
        strcpy (entry->server_port, elt[2]);
        strcpy (entry->login, elt[3]);
        strcpy (entry->password, elt[4]);
        strcpy (entry->type, elt[5]);
        strcpy (entry->fileName, elt[6]);
        strcpy (entry->result, elt[7]);
    }

    return LIBSPID_SUCCESS;
}


/**
 * Returns the whole Config backup Table from configuration file in a pointer
 * to an array of Config backup Table entries.<BR>
 * \param entry pointer to an array of config_backup entries
 * \param count pointer to an integer for count of entries,this
 *        param is in/out,you must give the eoc_config_backup_entry
 *        max num as in,and will be returnd the num found in the config file
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_NOT_FOUND: entry not found
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno for details
 */
extern libspid_error_t
libspid_eoc_config_backup_get_list (libspid_eoc_config_backup_entry_t * entry,
                                    unsigned int *count)
{
    const char delimiters[2] = LIBSPID_CONFIG_BACKUP_DELIMITER "\0";
    char key[LIBSPID_CONFIG_KEY_MAX_LEN];
    unsigned int elt_number = LIBSPID_CONFIG_ELT_MAX_NB;
    char *elt[LIBSPID_CONFIG_ELT_MAX_NB];
    char buffer[LIBSPID_CONFIG_LINE_MAX_LEN];
    libspid_error_t error = LIBSPID_SUCCESS;
    libspid_eoc_config_backup_entry_t *entry_ptr;
    void *user_data = NULL;

    /* check input parameters */
    if ((entry == NULL) || (count == NULL))
        return LIBSPID_ERROR_PARAM;

    memset (key, 0x0, sizeof (key));
    memset (entry, 0x0, sizeof (entry));
    *count = 0;

    entry_ptr = entry;

    /* get the first key */
    error = libspid_config_read_line_repetitive (LIBSPID_CONFIG_BACKUP_PATH,
                                                 delimiters, key, &elt_number,
                                                 elt, buffer,
                                                 LIBSPID_CONFIG_LINE_MAX_LEN,
                                                 &user_data);
    if (LIBSPID_SUCCESS != error)
        return error;

    for (; *count < LIBSPID_CONFIG_BACKUP_ITEM_MAX && strcmp (key, "");
         (*count)++)
    {
        /* Get a line from the conf file */
        elt_number = LIBSPID_CONFIG_ELT_MAX_NB;
        error =
            libspid_config_read_line_repetitive (LIBSPID_CONFIG_BACKUP_PATH,
                                                 delimiters, key, &elt_number,
                                                 elt, buffer,
                                                 LIBSPID_CONFIG_LINE_MAX_LEN,
                                                 &user_data);
        if (LIBSPID_SUCCESS != error)
            return error;

        strcpy (entry_ptr->index, buffer);
        if (elt_number < LIBSPID_CONFIG_BACKUP_PARAM_NUM)
        {
            strcpy (entry_ptr->protocol, "");
            strcpy (entry_ptr->ip_address, "");
            strcpy (entry_ptr->server_port, "");
            strcpy (entry_ptr->login, "");
            strcpy (entry_ptr->password, "");
            strcpy (entry_ptr->type, "");
            strcpy (entry_ptr->fileName, "");
            strcpy (entry_ptr->result, "");
        }
        else
        {
            strcpy (entry_ptr->protocol, elt[0]);
            strcpy (entry_ptr->ip_address, elt[1]);
            strcpy (entry_ptr->server_port, elt[2]);
            strcpy (entry_ptr->login, elt[3]);
            strcpy (entry_ptr->password, elt[4]);
            strcpy (entry_ptr->type, elt[5]);
            strcpy (entry_ptr->fileName, elt[6]);
            strcpy (entry_ptr->result, elt[7]);
        }

        entry_ptr++;
    }
    libspid_config_read_line_repetitive (0, 0, 0, 0, 0, 0, 0, &user_data);
    return LIBSPID_SUCCESS;
}

/**
 * Set a config backup with the given index.<BR>
 * \param index  config backup index
 * \param entry  config backup definition
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_NOT_FOUND: entry not found
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno for details
 */
extern libspid_error_t
libspid_eoc_config_backup_set (char *index,
                               libspid_eoc_config_backup_entry_t * entry)
{
    const char delimiter = LIBSPID_CONFIG_BACKUP_DELIMITER[0];
    char *elt[LIBSPID_CONFIG_BACKUP_PARAM_NUM];
    libspid_error_t ret;
    int i;

    /* check input parameters */
    if ((index == NULL) || (entry == NULL))
        return LIBSPID_ERROR_PARAM;

    /* allocate buffers */
    for (i = 0; i < LIBSPID_CONFIG_BACKUP_PARAM_NUM; i++)
    {
        elt[i] = (char *) malloc (256 * sizeof (char));
        if (NULL != elt[i])
            memset (elt[i], 0, 256);
        else
        {
            LIBSPID_EOC_LOG_2 ("%s: error allocating element buffers"
                               " (errno=%d)", __FUNCTION__, errno);
            return LIBSPID_ERROR_SYSTEM;
        }
    }

    strcpy (elt[0], entry->protocol);
    strcpy (elt[1], entry->ip_address);
    strcpy (elt[2], entry->server_port);
    strcpy (elt[3], entry->login);
    strcpy (elt[4], entry->password);
    strcpy (elt[5], entry->type);
    strcpy (elt[6], entry->fileName);
    strcpy (elt[7], entry->result);

    ret = libspid_config_write_line (LIBSPID_CONFIG_BACKUP_PATH,
                                     delimiter, index,
                                     LIBSPID_CONFIG_BACKUP_PARAM_NUM, elt);

    /* free element buffers */
    for (i = 0; i < LIBSPID_CONFIG_BACKUP_PARAM_NUM; i++)
    {
        if (elt[i])
        {
            free (elt[i]);
            elt[i] = NULL;
        }
    }

    if (ret != LIBSPID_SUCCESS)
    {
        LIBSPID_EOC_LOG_2 ("%s: cannot write config backup entry (errno=%d)",
                           __FUNCTION__, errno);
        return ret;
    }

    return LIBSPID_SUCCESS;
}

/**
 * Remove the config backup entry with the given index.<BR>
 * \param index  config backup index
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_NOT_FOUND: entry not found
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno for details
 */
extern libspid_error_t
libspid_eoc_config_backup_remove (char *index)
{
    libspid_error_t ret;

    /* check input parameters */
    if (index == NULL)
        return LIBSPID_ERROR_PARAM;

    ret = libspid_config_remove_line (LIBSPID_CONFIG_BACKUP_PATH,
                                      LIBSPID_CONFIG_BACKUP_DELIMITER, index);

    if (ret != LIBSPID_SUCCESS)
    {
        LIBSPID_EOC_LOG_2 ("%s: cannot remove config backup entry (errno=%d)",
                           __FUNCTION__, errno);
        return ret;
    }

    return LIBSPID_SUCCESS;
}

/**
 * Remove all config_backup table list.<BR>
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno for details
 */
extern libspid_error_t
libspid_eoc_config_backup_flush (void)
{
    int ret;
    const char delimiters[2] = LIBSPID_CONFIG_BACKUP_DELIMITER "\0";
    char key[LIBSPID_CONFIG_KEY_MAX_LEN];
    unsigned int elt_number = LIBSPID_CONFIG_ELT_MAX_NB;
    char *elt[LIBSPID_CONFIG_ELT_MAX_NB];
    char buffer[LIBSPID_CONFIG_LINE_MAX_LEN];
    libspid_error_t error = LIBSPID_SUCCESS;

    while (1)
    {
        /* Get the key of the current line */
        memset (key, 0x0, sizeof (key));
        ret = libspid_config_read_line (LIBSPID_CONFIG_BACKUP_PATH,
                                        delimiters,
                                        key, &elt_number, elt, buffer,
                                        LIBSPID_CONFIG_LINE_MAX_LEN);

        if (LIBSPID_SUCCESS != ret)
            return ret;

        if (strcmp (key, "") == 0)
            break;

        error = libspid_eoc_config_backup_remove (key);
        if (LIBSPID_SUCCESS != error)
            return error;
    }

    return LIBSPID_SUCCESS;
}

/** internal helper function for check parameters of confbak.conf */
int
config_backup_check_input_parameters (libspid_eoc_config_backup_entry_t *
                                      entry,
                                      libspid_config_backup_check_error_t *
                                      error_types)
{
    int error_number = 0;
    int ret = 0;
    char *pnumber;
    int value = 0;

    struct in_addr ipaddr;

    if (entry == NULL || error_types == NULL)
    {
        return -1;
    }

    /** check ip format */
    if (0 == inet_aton ((char *) entry->ip_address, &ipaddr))
    {
        ret = -1;
    }

    if (ret != 0)
    {
        error_types[error_number] = LIBSPID_ERROR_CONFIG_BACKUP_IP_FORMAT;
        error_number++;
        ret = 0;
    }

    /** check backup index field value */
    value = strtol (entry->index, &pnumber, 10);
    if (*pnumber != '\0' || strcmp (entry->index, "") == 0)
        ret = -1;
    else
        ret = (value < 0 || value >= LIBSPID_CONFIG_BACKUP_ITEM_MAX) ? -1 : 0;

    if (ret != 0)
    {
        error_types[error_number] = LIBSPID_ERROR_CONFIG_BACKUP_INDEX_FORMAT;
        error_number++;
        ret = 0;
    }

    /** check backup protocol field value */
    value = strtol (entry->protocol, &pnumber, 10);
    if (*pnumber != '\0' || strcmp (entry->protocol, "") == 0)
        ret = -1;
    else
        ret = (value < 0 || value > 4) ? -1 : 0;

    if (ret != 0)
    {
        error_types[error_number] =
            LIBSPID_ERROR_CONFIG_BACKUP_PROTOCOL_FORMAT;
        error_number++;
        ret = 0;
    }

    /** check backup port field value */
    int i;

    if (strlen (entry->server_port) >= 6)
        ret = -1;
    else
    {
        for (i = 0; i < (int)strlen (entry->server_port); i++)
        {
            entry->server_port[i] = tolower (entry->server_port[i]);
            if (!
                (entry->server_port[i] >= '0'
                 && entry->server_port[i] <= '9'))
            {
                ret = -1;
                break;
            }
        }
    }
    if (ret != 0)
    {
        error_types[error_number] =
            LIBSPID_ERROR_CONFIG_BACKUP_SERVER_PORT_FORMAT;
        error_number++;
        ret = 0;
    }

    /** check backup login field value */
    ret = (strlen (entry->login) >= LIBSPID_CONFIG_BACKUP_STRING_LIMIT) ?
           -1 : 0;
    if (ret != 0)
    {
        error_types[error_number] = LIBSPID_ERROR_CONFIG_BACKUP_LOGIN_FORMAT;
        error_number++;
        ret = 0;
    }

    /** check backup password field value */
    ret = (strlen (entry->password) >= LIBSPID_CONFIG_BACKUP_STRING_LIMIT) ?
           -1 : 0;
    if (ret != 0)
    {
        error_types[error_number] =
            LIBSPID_ERROR_CONFIG_BACKUP_PASSWORD_FORMAT;
        error_number++;
        ret = 0;
    }

    /** check backup fileName field value */
    ret = (strlen (entry->fileName) >= LIBSPID_CONFIG_BACKUP_STRING_LIMIT) ?
           -1 : 0;
    if (ret != 0)
    {
        error_types[error_number] =
            LIBSPID_ERROR_CONFIG_BACKUP_FILENAME_FORMAT;
        error_number++;
        ret = 0;
    }

    /** check backup type field value */
    value = strtol (entry->type, &pnumber, 10);
    if (*pnumber != '\0' || strcmp (entry->type, "") == 0)
        ret = -1;
    else
        ret = (value != 1) ? -1 : 0;
    if (ret != 0)
    {
        error_types[error_number] = LIBSPID_ERROR_CONFIG_BACKUP_TYPE_FORMAT;
        error_number++;
        ret = 0;
    }
    /** check backup type field value */
    if (0 != strcmp (entry->result, LIBSPID_CONFIG_BACKUP_RESULT_FAIL) &&
        0 != strcmp (entry->result, LIBSPID_CONFIG_BACKUP_RESULT_SUC) &&
        0 != strcmp (entry->result, LIBSPID_CONFIG_BACKUP_RESULT_NONE))

    {
        ret = -1;
    }
    else
    {
        ret = 0;
    }

    if (ret != 0)
    {
        error_types[error_number] = LIBSPID_ERROR_CONFIG_BACKUP_RESULT_FORMAT;
        error_number++;
        ret = 0;
    }
    return error_number;
}

/** internal function for compare theip*/
static int
cmp_string_index (const void *p1, const void *p2)
{
    return strcmp ((char *) ((libspid_eoc_config_backup_entry_t *) p1)->index,
                   (char *) ((libspid_eoc_config_backup_entry_t *) p2)->
                   index);
}


/**
 * Check confbak.conf file
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_CHECK: check utility failed
 */
extern libspid_error_t
libspid_eoc_config_backup_check (void)
{
    int count_cnf = LIBSPID_CONFIG_BACKUP_ITEM_MAX;
    int ret = -1;
    int i = 0;
    libspid_eoc_config_backup_entry_t
        entries_cnf[LIBSPID_CONFIG_BACKUP_ITEM_MAX];
    libspid_eoc_config_backup_entry_t
        entries_cnf_sort[LIBSPID_CONFIG_BACKUP_ITEM_MAX];
    libspid_eoc_config_backup_entry_t entry;
    unsigned char index_not_unique[4];

    LIBSPID_EOC_LOG_1 ("%s: Check confbak.conf...", __FUNCTION__);
    /** obtain the maclimitation list from configuration file */
    ret = libspid_eoc_config_backup_get_list (entries_cnf,
                                              (unsigned int *) &count_cnf);

    if (ret != LIBSPID_SUCCESS)
    {
        return ret;
    }

    memcpy (entries_cnf_sort, entries_cnf,
            count_cnf * sizeof (libspid_eoc_config_backup_entry_t));
    /* sort confbak.conf */
    qsort (entries_cnf_sort, count_cnf,
           sizeof (libspid_eoc_config_backup_entry_t), cmp_string_index);

    /* check if Index is unique key */
    for (i = 0; i < count_cnf - 1; i++)
    {
        if (strcmp ((char *) entries_cnf_sort[i].index,
                    (char *) entries_cnf_sort[i + 1].index) == 0)
        {
            LIBSPID_EOC_LOG_0 ("index_not_unique have.");
            strcpy ((char *) index_not_unique,
                    (char *) entries_cnf_sort[i].index);
            break;
        }
    }

    for (i = 0; i < count_cnf; i++)
    {
        int number_of_error = 0;
        libspid_config_backup_check_error_t
            error_types[LIBSPID_CONFIG_BACKUP_PARAM_NUM];
        entry = entries_cnf[i];

        /** check format of the input parameters */
        number_of_error = config_backup_check_input_parameters (&entry,
                                                                error_types);

        /* if Index is not unique key */
        if (strcmp ((char *) index_not_unique, (char *) entry.index) == 0)
        {
            number_of_error += 1;
            error_types[number_of_error - 1] =
                LIBSPID_ERROR_CONFIG_BACKUP_INDEX_DUPLICATE;
        }

        if (number_of_error > 0)
        {
            int j = 0;
            ret = LIBSPID_ERROR_CHECK;
            libspid_boolean_t unique_index = LIBSPID_TRUE;
            LIBSPID_EOC_LOG_1 ("Error in line:%d\n", i + 2);

            for (j = 0; j < number_of_error; j++)
            {
                switch (error_types[j])
                {
                case LIBSPID_ERROR_CONFIG_BACKUP_IP_FORMAT:
                    LIBSPID_EOC_LOG_0 ("Malformed Ip address.");
                    break;
                case LIBSPID_ERROR_CONFIG_BACKUP_INDEX_FORMAT:
                    LIBSPID_EOC_LOG_1
                        ("Wrong index value specified (expected:" " 0..%d)",
                         LIBSPID_CONFIG_BACKUP_ITEM_MAX - 1);
                    break;
                case LIBSPID_ERROR_CONFIG_BACKUP_INDEX_DUPLICATE:
                    unique_index = LIBSPID_FALSE;
                    LIBSPID_EOC_LOG_1 ("index %s is not unique key.",
                                       index_not_unique);
                    break;
                case LIBSPID_ERROR_CONFIG_BACKUP_PROTOCOL_FORMAT:
                    LIBSPID_EOC_LOG_0 ("Wrong protocol value specified"
                                       "(expected: 1..4)");
                    break;
                case LIBSPID_ERROR_CONFIG_BACKUP_SERVER_PORT_FORMAT:
                    LIBSPID_EOC_LOG_0 ("Wrong server port val");
                    break;
                case LIBSPID_ERROR_CONFIG_BACKUP_LOGIN_FORMAT:
                    LIBSPID_EOC_LOG_0 ("Wrong login length longer");
                    break;
                case LIBSPID_ERROR_CONFIG_BACKUP_PASSWORD_FORMAT:
                    LIBSPID_EOC_LOG_0 ("Wrong password length longer");
                    break;
                case LIBSPID_ERROR_CONFIG_BACKUP_TYPE_FORMAT:
                    LIBSPID_EOC_LOG_0 ("Wrong type value specified "
                                       "(expected: 1)");
                    break;
                case LIBSPID_ERROR_CONFIG_BACKUP_FILENAME_FORMAT:
                    LIBSPID_EOC_LOG_0 ("Wrong filename length longer");
                    break;
                case LIBSPID_ERROR_CONFIG_BACKUP_RESULT_FORMAT:
                    LIBSPID_EOC_LOG_0 ("Wrong result value specified"
                                       " (expected: 0..2)");
                    break;
                default:
                    break;
                }
            }
            if (unique_index == LIBSPID_FALSE)
                break;
        }
        else if (number_of_error == -1)
            return LIBSPID_ERROR_PARAM;
    }
    return ret;
}