hss_config.c 7.08 KB
Newer Older
winckel's avatar
 
winckel committed
1
/*******************************************************************************
gauthier's avatar
GPLv3    
gauthier committed
2
3
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom
winckel's avatar
 
winckel committed
4

gauthier's avatar
GPLv3    
gauthier committed
5
6
7
8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
winckel's avatar
 
winckel committed
9
10


gauthier's avatar
GPLv3    
gauthier committed
11
12
13
14
    OpenAirInterface 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 General Public License for more details.
winckel's avatar
 
winckel committed
15

gauthier's avatar
GPLv3    
gauthier committed
16
17
18
19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
   see <http://www.gnu.org/licenses/>.
winckel's avatar
 
winckel committed
20
21

  Contact Information
gauthier's avatar
GPLv3    
gauthier committed
22
23
24
25
26
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr

  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.
winckel's avatar
 
winckel committed
27

gauthier's avatar
GPLv3    
gauthier committed
28
 *******************************************************************************/
winckel's avatar
 
winckel committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

/*! \file hss_config.c
 * \brief Base configuration for the HSS. Parse command line and configuration file
 * \author Sebastien ROUX <sebastien.roux@eurecom.fr>
 * \date 2013
 * \version 0.1
 */

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
45
#include <stdint.h>
winckel's avatar
 
winckel committed
46
47
48
49
50
51
#include <errno.h>
#include <getopt.h>

#include "hss_config.h"
#include "hss_parser.h"

52
53
54
55
56
57
58
59
60
61
62
63
64
65

#ifndef PACKAGE_NAME
#define PACKAGE_NAME "OPENAIR-HSS"
#endif
#ifndef PACKAGE_VERSION
#define PACKAGE_VERSION "UNKNOWN-EXPERIMENTAL"
#endif
#ifndef PACKAGE_BUGREPORT
#define PACKAGE_BUGREPORT "openair4G-devel@eurecom.fr"
#endif

// LG TODO fd_g_debug_lvl
int fd_g_debug_lvl = 1;

winckel's avatar
 
winckel committed
66
67
/* YACC forward declarations */
extern int  yyparse (struct hss_config_s *hss_config_p);
68
extern uint8_t opc[16];
winckel's avatar
 
winckel committed
69
70
71
72
73
74
75
static int config_parse_command_line(int argc, char *argv[],
                                     hss_config_t *hss_config_p);
static int config_parse_file(hss_config_t *hss_config_p);
static void display_banner(void);
static void config_display(hss_config_t *hss_config_p);

static struct option long_options[] = {
76
77
78
79
  { "config",  1, 0, 'c' },
  { "help",    0, 0, 'h' },
  { "version", 0, 0, 'v' },
  { 0, 0, 0, 0 },
winckel's avatar
 
winckel committed
80
81
82
83
84
85
};

static const char option_string[] = "c:vh";

int config_init(int argc, char *argv[], hss_config_t *hss_config_p)
{
86
  int ret = 0;
winckel's avatar
 
winckel committed
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  if (hss_config_p == NULL) {
    return EINVAL;
  }

  if ((ret = config_parse_command_line(argc, argv, hss_config_p)) != 0) {
    return ret;
  }

  display_banner();

  if ((ret = config_parse_file(hss_config_p)) != 0) {
    /* Parsing of the file failed. -> abort */
    abort();
  }

  config_display(hss_config_p);

  // post processing for op key
  if (hss_config_p->operator_key) {
    if (strlen(hss_config_p->operator_key) == 32) {
      ret = sscanf(hss_config_p->operator_key,
                   "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
110
111
112
113
114
115
116
117
                   (unsigned int*)&opc[0],(unsigned int*)&opc[1],
                   (unsigned int*)&opc[2],(unsigned int*)&opc[3],
                   (unsigned int*)&opc[4],(unsigned int*)&opc[5],
                   (unsigned int*)&opc[6],(unsigned int*)&opc[7],
                   (unsigned int*)&opc[8],(unsigned int*)&opc[9],
                   (unsigned int*)&opc[10],(unsigned int*)&opc[11],
                   (unsigned int*)&opc[12],(unsigned int*)&opc[13],
                   (unsigned int*)&opc[14],(unsigned int*)&opc[15]);
118
119
120
121
122

      if (ret != 16) {
        fprintf(stderr,
                "Error in configuration file: operator key: %s\n",
                hss_config_p->operator_key);
winckel's avatar
 
winckel committed
123
        abort();
124
      }
125
    } else {
126
127
128
129
      fprintf(stderr,
              "Error in configuration file: operator key length != 32 (16 hex bytes): %s\n",
              hss_config_p->operator_key);
      abort();
130
    }
131
132
133
134
135
136
  } else {
    fprintf(stderr, "Error in configuration file: operator key is null\n");
    abort();
  }

  return 0;
winckel's avatar
 
winckel committed
137
138
139
140
}

static void display_banner(void)
{
141
142
  fprintf(stdout, "==== EURECOM %s v%s ====\n", PACKAGE_NAME, PACKAGE_VERSION);
  fprintf(stdout, "Please report any bug to: %s\n\n", PACKAGE_BUGREPORT);
winckel's avatar
 
winckel committed
143
144
145
146
}

static void usage(void)
{
147
148
149
150
151
152
153
154
155
156
  display_banner();
  fprintf(stdout, "Usage: openair_hss [options]\n\n");
  fprintf(stdout, "Available options:\n");
  fprintf(stdout, "\t--help\n\t-h\n");
  fprintf(stdout, "\t\tPrint this help and return\n\n");
  fprintf(stdout, "\t--config=<path>\n\t-c<path>\n");
  fprintf(stdout, "\t\tSet the configuration file for hss\n");
  fprintf(stdout, "\t\tSee template in conf dir\n\n");
  fprintf(stdout, "\t--version\n\t-v\n");
  fprintf(stdout, "\t\tPrint %s version and return\n", PACKAGE_NAME);
winckel's avatar
 
winckel committed
157
158
159
160
}

static void config_display(hss_config_t *hss_config_p)
{
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  fprintf(stdout, "Configuration\n");
  fprintf(stdout, "* Global:\n");
  fprintf(stdout, "\t- File .............: %s\n", hss_config_p->config);
  fprintf(stdout, "* MYSQL:\n");
  fprintf(stdout, "\t- Server ...........: %s\n", hss_config_p->mysql_server);
  fprintf(stdout, "\t- Database .........: %s\n", hss_config_p->mysql_database);
  fprintf(stdout, "\t- User .............: %s\n", hss_config_p->mysql_user);
  fprintf(stdout, "\t- Password .........: %s\n",
          (hss_config_p->mysql_password == NULL) ? "None" : "*****");
  fprintf(stdout, "* FreeDiameter:\n");
  fprintf(stdout, "\t- Conf file ........: %s\n",
          hss_config_p->freediameter_config);
  fprintf(stdout, "* Security:\n");
  fprintf(stdout, "\t- Operator key......: %s\n",
          hss_config_p->operator_key);
winckel's avatar
 
winckel committed
176
177
178
179
180
}

static int config_parse_command_line(int argc, char *argv[],
                                     hss_config_t *hss_config_p)
{
181
182
183
184
185
186
187
188
  int c;
  int option_index = 0;

  while ((c = getopt_long (argc, argv, option_string, long_options,
                           &option_index)) != -1) {
    switch(c) {
    case 'c': {
      hss_config_p->config = strdup(optarg);
winckel's avatar
 
winckel committed
189
    }
190
    break;
winckel's avatar
 
winckel committed
191

192
193
194
195
196
197
    case 'v': {
      /* We display version and return immediately */
      display_banner();
      exit(0);
    }
    break;
winckel's avatar
 
winckel committed
198

199
200
201
202
    default:
    case 'h': {
      usage();
      exit(0);
winckel's avatar
 
winckel committed
203
    }
204
    break;
winckel's avatar
 
winckel committed
205
    }
206
  }
winckel's avatar
 
winckel committed
207

208
209
  return 0;
}
winckel's avatar
 
winckel committed
210

211
212
213
214
static int config_parse_file(hss_config_t *hss_config_p)
{
  extern FILE *yyin;
  int ret = -1;
winckel's avatar
 
winckel committed
215

216
217
218
  if (hss_config_p == NULL) {
    return ret;
  }
winckel's avatar
 
winckel committed
219

220
  if (hss_config_p->config == NULL) {
winckel's avatar
 
winckel committed
221
    return ret;
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  }

  printf("Parsing configuration file: %s\n", hss_config_p->config);

  yyin = fopen(hss_config_p->config, "r");

  if (!yyin) {
    /* We failed to open the file */
    fprintf(stderr, "Unable to open the configuration file: %s (%d:%s)\n",
            hss_config_p->config, errno, strerror(errno));
    return errno;
  }

  /* Call the yacc parser */
  ret = yyparse(hss_config_p);

  /* Close the file descriptor */
  if (fclose(yyin) != 0) {
    fprintf(stderr, "Unable to close the configuration file: %s (%d:%s)\n",
            hss_config_p->config, errno, strerror(errno));
    return errno;
  }

  return ret;
winckel's avatar
 
winckel committed
246
}