LCOV - code coverage report
Current view: top level - hostapd - hlr_auc_gw.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1426431149 Lines: 373 588 63.4 %
Date: 2015-03-15 Functions: 18 23 78.3 %

          Line data    Source code
       1             : /*
       2             :  * HLR/AuC testing gateway for hostapd EAP-SIM/AKA database/authenticator
       3             :  * Copyright (c) 2005-2007, 2012-2013, Jouni Malinen <j@w1.fi>
       4             :  *
       5             :  * This software may be distributed under the terms of the BSD license.
       6             :  * See README for more details.
       7             :  *
       8             :  * This is an example implementation of the EAP-SIM/AKA database/authentication
       9             :  * gateway interface to HLR/AuC. It is expected to be replaced with an
      10             :  * implementation of SS7 gateway to GSM/UMTS authentication center (HLR/AuC) or
      11             :  * a local implementation of SIM triplet and AKA authentication data generator.
      12             :  *
      13             :  * hostapd will send SIM/AKA authentication queries over a UNIX domain socket
      14             :  * to and external program, e.g., this hlr_auc_gw. This interface uses simple
      15             :  * text-based format:
      16             :  *
      17             :  * EAP-SIM / GSM triplet query/response:
      18             :  * SIM-REQ-AUTH <IMSI> <max_chal>
      19             :  * SIM-RESP-AUTH <IMSI> Kc1:SRES1:RAND1 Kc2:SRES2:RAND2 [Kc3:SRES3:RAND3]
      20             :  * SIM-RESP-AUTH <IMSI> FAILURE
      21             :  * GSM-AUTH-REQ <IMSI> RAND1:RAND2[:RAND3]
      22             :  * GSM-AUTH-RESP <IMSI> Kc1:SRES1:Kc2:SRES2[:Kc3:SRES3]
      23             :  * GSM-AUTH-RESP <IMSI> FAILURE
      24             :  *
      25             :  * EAP-AKA / UMTS query/response:
      26             :  * AKA-REQ-AUTH <IMSI>
      27             :  * AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES>
      28             :  * AKA-RESP-AUTH <IMSI> FAILURE
      29             :  *
      30             :  * EAP-AKA / UMTS AUTS (re-synchronization):
      31             :  * AKA-AUTS <IMSI> <AUTS> <RAND>
      32             :  *
      33             :  * IMSI and max_chal are sent as an ASCII string,
      34             :  * Kc/SRES/RAND/AUTN/IK/CK/RES/AUTS as hex strings.
      35             :  *
      36             :  * An example implementation here reads GSM authentication triplets from a
      37             :  * text file in IMSI:Kc:SRES:RAND format, IMSI in ASCII, other fields as hex
      38             :  * strings. This is used to simulate an HLR/AuC. As such, it is not very useful
      39             :  * for real life authentication, but it is useful both as an example
      40             :  * implementation and for EAP-SIM/AKA/AKA' testing.
      41             :  *
      42             :  * For a stronger example design, Milenage and GSM-Milenage algorithms can be
      43             :  * used to dynamically generate authenticatipn information for EAP-AKA/AKA' and
      44             :  * EAP-SIM, respectively, if Ki is known.
      45             :  *
      46             :  * SQN generation follows the not time-based Profile 2 described in
      47             :  * 3GPP TS 33.102 Annex C.3.2. The length of IND is 5 bits by default, but this
      48             :  * can be changed with a command line options if needed.
      49             :  */
      50             : 
      51             : #include "includes.h"
      52             : #include <sys/un.h>
      53             : #ifdef CONFIG_SQLITE
      54             : #include <sqlite3.h>
      55             : #endif /* CONFIG_SQLITE */
      56             : 
      57             : #include "common.h"
      58             : #include "crypto/milenage.h"
      59             : #include "crypto/random.h"
      60             : 
      61             : static const char *default_socket_path = "/tmp/hlr_auc_gw.sock";
      62             : static const char *socket_path;
      63             : static int serv_sock = -1;
      64             : static char *milenage_file = NULL;
      65             : static int update_milenage = 0;
      66             : static int sqn_changes = 0;
      67             : static int ind_len = 5;
      68             : static int stdout_debug = 1;
      69             : 
      70             : /* GSM triplets */
      71             : struct gsm_triplet {
      72             :         struct gsm_triplet *next;
      73             :         char imsi[20];
      74             :         u8 kc[8];
      75             :         u8 sres[4];
      76             :         u8 _rand[16];
      77             : };
      78             : 
      79             : static struct gsm_triplet *gsm_db = NULL, *gsm_db_pos = NULL;
      80             : 
      81             : /* OPc and AMF parameters for Milenage (Example algorithms for AKA). */
      82             : struct milenage_parameters {
      83             :         struct milenage_parameters *next;
      84             :         char imsi[20];
      85             :         u8 ki[16];
      86             :         u8 opc[16];
      87             :         u8 amf[2];
      88             :         u8 sqn[6];
      89             :         int set;
      90             : };
      91             : 
      92             : static struct milenage_parameters *milenage_db = NULL;
      93             : 
      94             : #define EAP_SIM_MAX_CHAL 3
      95             : 
      96             : #define EAP_AKA_RAND_LEN 16
      97             : #define EAP_AKA_AUTN_LEN 16
      98             : #define EAP_AKA_AUTS_LEN 14
      99             : #define EAP_AKA_RES_MAX_LEN 16
     100             : #define EAP_AKA_IK_LEN 16
     101             : #define EAP_AKA_CK_LEN 16
     102             : 
     103             : 
     104             : #ifdef CONFIG_SQLITE
     105             : 
     106             : static sqlite3 *sqlite_db = NULL;
     107             : static struct milenage_parameters db_tmp_milenage;
     108             : 
     109             : 
     110           0 : static int db_table_exists(sqlite3 *db, const char *name)
     111             : {
     112             :         char cmd[128];
     113           0 :         os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
     114           0 :         return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
     115             : }
     116             : 
     117             : 
     118           0 : static int db_table_create_milenage(sqlite3 *db)
     119             : {
     120           0 :         char *err = NULL;
     121           0 :         const char *sql =
     122             :                 "CREATE TABLE milenage("
     123             :                 "  imsi INTEGER PRIMARY KEY NOT NULL,"
     124             :                 "  ki CHAR(32) NOT NULL,"
     125             :                 "  opc CHAR(32) NOT NULL,"
     126             :                 "  amf CHAR(4) NOT NULL,"
     127             :                 "  sqn CHAR(12) NOT NULL"
     128             :                 ");";
     129             : 
     130           0 :         printf("Adding database table for milenage information\n");
     131           0 :         if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
     132           0 :                 printf("SQLite error: %s\n", err);
     133           0 :                 sqlite3_free(err);
     134           0 :                 return -1;
     135             :         }
     136             : 
     137           0 :         return 0;
     138             : }
     139             : 
     140             : 
     141           0 : static sqlite3 * db_open(const char *db_file)
     142             : {
     143             :         sqlite3 *db;
     144             : 
     145           0 :         if (sqlite3_open(db_file, &db)) {
     146           0 :                 printf("Failed to open database %s: %s\n",
     147             :                        db_file, sqlite3_errmsg(db));
     148           0 :                 sqlite3_close(db);
     149           0 :                 return NULL;
     150             :         }
     151             : 
     152           0 :         if (!db_table_exists(db, "milenage") &&
     153           0 :             db_table_create_milenage(db) < 0) {
     154           0 :                 sqlite3_close(db);
     155           0 :                 return NULL;
     156             :         }
     157             : 
     158           0 :         return db;
     159             : }
     160             : 
     161             : 
     162           0 : static int get_milenage_cb(void *ctx, int argc, char *argv[], char *col[])
     163             : {
     164           0 :         struct milenage_parameters *m = ctx;
     165             :         int i;
     166             : 
     167           0 :         m->set = 1;
     168             : 
     169           0 :         for (i = 0; i < argc; i++) {
     170           0 :                 if (os_strcmp(col[i], "ki") == 0 && argv[i] &&
     171           0 :                     hexstr2bin(argv[i], m->ki, sizeof(m->ki))) {
     172           0 :                         printf("Invalid ki value in database\n");
     173           0 :                         return -1;
     174             :                 }
     175             : 
     176           0 :                 if (os_strcmp(col[i], "opc") == 0 && argv[i] &&
     177           0 :                     hexstr2bin(argv[i], m->opc, sizeof(m->opc))) {
     178           0 :                         printf("Invalid opcvalue in database\n");
     179           0 :                         return -1;
     180             :                 }
     181             : 
     182           0 :                 if (os_strcmp(col[i], "amf") == 0 && argv[i] &&
     183           0 :                     hexstr2bin(argv[i], m->amf, sizeof(m->amf))) {
     184           0 :                         printf("Invalid amf value in database\n");
     185           0 :                         return -1;
     186             :                 }
     187             : 
     188           0 :                 if (os_strcmp(col[i], "sqn") == 0 && argv[i] &&
     189           0 :                     hexstr2bin(argv[i], m->sqn, sizeof(m->sqn))) {
     190           0 :                         printf("Invalid sqn value in database\n");
     191           0 :                         return -1;
     192             :                 }
     193             :         }
     194             : 
     195           0 :         return 0;
     196             : }
     197             : 
     198             : 
     199           2 : static struct milenage_parameters * db_get_milenage(const char *imsi_txt)
     200             : {
     201             :         char cmd[128];
     202             :         unsigned long long imsi;
     203             : 
     204           2 :         os_memset(&db_tmp_milenage, 0, sizeof(db_tmp_milenage));
     205           2 :         imsi = atoll(imsi_txt);
     206           2 :         os_snprintf(db_tmp_milenage.imsi, sizeof(db_tmp_milenage.imsi),
     207             :                     "%llu", imsi);
     208           2 :         os_snprintf(cmd, sizeof(cmd),
     209             :                     "SELECT ki,opc,amf,sqn FROM milenage WHERE imsi=%llu;",
     210             :                     imsi);
     211           2 :         if (sqlite3_exec(sqlite_db, cmd, get_milenage_cb, &db_tmp_milenage,
     212             :                          NULL) != SQLITE_OK)
     213           2 :                 return NULL;
     214             : 
     215           0 :         if (!db_tmp_milenage.set)
     216           0 :                 return NULL;
     217           0 :         return &db_tmp_milenage;
     218             : }
     219             : 
     220             : 
     221          52 : static int db_update_milenage_sqn(struct milenage_parameters *m)
     222             : {
     223             :         char cmd[128], val[13], *pos;
     224             : 
     225          52 :         if (sqlite_db == NULL)
     226          52 :                 return 0;
     227             : 
     228           0 :         pos = val;
     229           0 :         pos += wpa_snprintf_hex(pos, sizeof(val), m->sqn, 6);
     230           0 :         *pos = '\0';
     231           0 :         os_snprintf(cmd, sizeof(cmd),
     232             :                     "UPDATE milenage SET sqn='%s' WHERE imsi=%s;",
     233           0 :                     val, m->imsi);
     234           0 :         if (sqlite3_exec(sqlite_db, cmd, NULL, NULL, NULL) != SQLITE_OK) {
     235           0 :                 printf("Failed to update SQN in database for IMSI %s\n",
     236           0 :                        m->imsi);
     237           0 :                 return -1;
     238             :         }
     239           0 :         return 0;
     240             : }
     241             : 
     242             : #endif /* CONFIG_SQLITE */
     243             : 
     244             : 
     245           5 : static int open_socket(const char *path)
     246             : {
     247             :         struct sockaddr_un addr;
     248             :         int s;
     249             : 
     250           5 :         s = socket(PF_UNIX, SOCK_DGRAM, 0);
     251           5 :         if (s < 0) {
     252           0 :                 perror("socket(PF_UNIX)");
     253           0 :                 return -1;
     254             :         }
     255             : 
     256           5 :         memset(&addr, 0, sizeof(addr));
     257           5 :         addr.sun_family = AF_UNIX;
     258           5 :         os_strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
     259           5 :         if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
     260           0 :                 perror("hlr-auc-gw: bind(PF_UNIX)");
     261           0 :                 close(s);
     262           0 :                 return -1;
     263             :         }
     264             : 
     265           5 :         return s;
     266             : }
     267             : 
     268             : 
     269           5 : static int read_gsm_triplets(const char *fname)
     270             : {
     271             :         FILE *f;
     272             :         char buf[200], *pos, *pos2;
     273           5 :         struct gsm_triplet *g = NULL;
     274           5 :         int line, ret = 0;
     275             : 
     276           5 :         if (fname == NULL)
     277           0 :                 return -1;
     278             : 
     279           5 :         f = fopen(fname, "r");
     280           5 :         if (f == NULL) {
     281           0 :                 printf("Could not open GSM tripler data file '%s'\n", fname);
     282           0 :                 return -1;
     283             :         }
     284             : 
     285           5 :         line = 0;
     286          95 :         while (fgets(buf, sizeof(buf), f)) {
     287          85 :                 line++;
     288             : 
     289             :                 /* Parse IMSI:Kc:SRES:RAND */
     290          85 :                 buf[sizeof(buf) - 1] = '\0';
     291          85 :                 if (buf[0] == '#')
     292          45 :                         continue;
     293          40 :                 pos = buf;
     294        2300 :                 while (*pos != '\0' && *pos != '\n')
     295        2220 :                         pos++;
     296          40 :                 if (*pos == '\n')
     297          40 :                         *pos = '\0';
     298          40 :                 pos = buf;
     299          40 :                 if (*pos == '\0')
     300          10 :                         continue;
     301             : 
     302          30 :                 g = os_zalloc(sizeof(*g));
     303          30 :                 if (g == NULL) {
     304           0 :                         ret = -1;
     305           0 :                         break;
     306             :                 }
     307             : 
     308             :                 /* IMSI */
     309          30 :                 pos2 = strchr(pos, ':');
     310          30 :                 if (pos2 == NULL) {
     311           0 :                         printf("%s:%d - Invalid IMSI (%s)\n",
     312             :                                fname, line, pos);
     313           0 :                         ret = -1;
     314           0 :                         break;
     315             :                 }
     316          30 :                 *pos2 = '\0';
     317          30 :                 if (strlen(pos) >= sizeof(g->imsi)) {
     318           0 :                         printf("%s:%d - Too long IMSI (%s)\n",
     319             :                                fname, line, pos);
     320           0 :                         ret = -1;
     321           0 :                         break;
     322             :                 }
     323          30 :                 os_strlcpy(g->imsi, pos, sizeof(g->imsi));
     324          30 :                 pos = pos2 + 1;
     325             : 
     326             :                 /* Kc */
     327          30 :                 pos2 = strchr(pos, ':');
     328          30 :                 if (pos2 == NULL) {
     329           0 :                         printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
     330           0 :                         ret = -1;
     331           0 :                         break;
     332             :                 }
     333          30 :                 *pos2 = '\0';
     334          30 :                 if (strlen(pos) != 16 || hexstr2bin(pos, g->kc, 8)) {
     335           0 :                         printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
     336           0 :                         ret = -1;
     337           0 :                         break;
     338             :                 }
     339          30 :                 pos = pos2 + 1;
     340             : 
     341             :                 /* SRES */
     342          30 :                 pos2 = strchr(pos, ':');
     343          30 :                 if (pos2 == NULL) {
     344           0 :                         printf("%s:%d - Invalid SRES (%s)\n", fname, line,
     345             :                                pos);
     346           0 :                         ret = -1;
     347           0 :                         break;
     348             :                 }
     349          30 :                 *pos2 = '\0';
     350          30 :                 if (strlen(pos) != 8 || hexstr2bin(pos, g->sres, 4)) {
     351           0 :                         printf("%s:%d - Invalid SRES (%s)\n", fname, line,
     352             :                                pos);
     353           0 :                         ret = -1;
     354           0 :                         break;
     355             :                 }
     356          30 :                 pos = pos2 + 1;
     357             : 
     358             :                 /* RAND */
     359          30 :                 pos2 = strchr(pos, ':');
     360          30 :                 if (pos2)
     361           0 :                         *pos2 = '\0';
     362          30 :                 if (strlen(pos) != 32 || hexstr2bin(pos, g->_rand, 16)) {
     363           0 :                         printf("%s:%d - Invalid RAND (%s)\n", fname, line,
     364             :                                pos);
     365           0 :                         ret = -1;
     366           0 :                         break;
     367             :                 }
     368          30 :                 pos = pos2 + 1;
     369             : 
     370          30 :                 g->next = gsm_db;
     371          30 :                 gsm_db = g;
     372          30 :                 g = NULL;
     373             :         }
     374           5 :         os_free(g);
     375             : 
     376           5 :         fclose(f);
     377             : 
     378           5 :         return ret;
     379             : }
     380             : 
     381             : 
     382           4 : static struct gsm_triplet * get_gsm_triplet(const char *imsi)
     383             : {
     384           4 :         struct gsm_triplet *g = gsm_db_pos;
     385             : 
     386          11 :         while (g) {
     387           5 :                 if (strcmp(g->imsi, imsi) == 0) {
     388           2 :                         gsm_db_pos = g->next;
     389           2 :                         return g;
     390             :                 }
     391           3 :                 g = g->next;
     392             :         }
     393             : 
     394           2 :         g = gsm_db;
     395           7 :         while (g && g != gsm_db_pos) {
     396           4 :                 if (strcmp(g->imsi, imsi) == 0) {
     397           1 :                         gsm_db_pos = g->next;
     398           1 :                         return g;
     399             :                 }
     400           3 :                 g = g->next;
     401             :         }
     402             : 
     403           1 :         return NULL;
     404             : }
     405             : 
     406             : 
     407           5 : static int read_milenage(const char *fname)
     408             : {
     409             :         FILE *f;
     410             :         char buf[200], *pos, *pos2;
     411           5 :         struct milenage_parameters *m = NULL;
     412           5 :         int line, ret = 0;
     413             : 
     414           5 :         if (fname == NULL)
     415           0 :                 return -1;
     416             : 
     417           5 :         f = fopen(fname, "r");
     418           5 :         if (f == NULL) {
     419           0 :                 printf("Could not open Milenage data file '%s'\n", fname);
     420           0 :                 return -1;
     421             :         }
     422             : 
     423           5 :         line = 0;
     424          75 :         while (fgets(buf, sizeof(buf), f)) {
     425          65 :                 line++;
     426             : 
     427             :                 /* Parse IMSI Ki OPc AMF SQN */
     428          65 :                 buf[sizeof(buf) - 1] = '\0';
     429          65 :                 if (buf[0] == '#')
     430          45 :                         continue;
     431          20 :                 pos = buf;
     432        1030 :                 while (*pos != '\0' && *pos != '\n')
     433         990 :                         pos++;
     434          20 :                 if (*pos == '\n')
     435          20 :                         *pos = '\0';
     436          20 :                 pos = buf;
     437          20 :                 if (*pos == '\0')
     438          10 :                         continue;
     439             : 
     440          10 :                 m = os_zalloc(sizeof(*m));
     441          10 :                 if (m == NULL) {
     442           0 :                         ret = -1;
     443           0 :                         break;
     444             :                 }
     445             : 
     446             :                 /* IMSI */
     447          10 :                 pos2 = strchr(pos, ' ');
     448          10 :                 if (pos2 == NULL) {
     449           0 :                         printf("%s:%d - Invalid IMSI (%s)\n",
     450             :                                fname, line, pos);
     451           0 :                         ret = -1;
     452           0 :                         break;
     453             :                 }
     454          10 :                 *pos2 = '\0';
     455          10 :                 if (strlen(pos) >= sizeof(m->imsi)) {
     456           0 :                         printf("%s:%d - Too long IMSI (%s)\n",
     457             :                                fname, line, pos);
     458           0 :                         ret = -1;
     459           0 :                         break;
     460             :                 }
     461          10 :                 os_strlcpy(m->imsi, pos, sizeof(m->imsi));
     462          10 :                 pos = pos2 + 1;
     463             : 
     464             :                 /* Ki */
     465          10 :                 pos2 = strchr(pos, ' ');
     466          10 :                 if (pos2 == NULL) {
     467           0 :                         printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
     468           0 :                         ret = -1;
     469           0 :                         break;
     470             :                 }
     471          10 :                 *pos2 = '\0';
     472          10 :                 if (strlen(pos) != 32 || hexstr2bin(pos, m->ki, 16)) {
     473           0 :                         printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
     474           0 :                         ret = -1;
     475           0 :                         break;
     476             :                 }
     477          10 :                 pos = pos2 + 1;
     478             : 
     479             :                 /* OPc */
     480          10 :                 pos2 = strchr(pos, ' ');
     481          10 :                 if (pos2 == NULL) {
     482           0 :                         printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
     483           0 :                         ret = -1;
     484           0 :                         break;
     485             :                 }
     486          10 :                 *pos2 = '\0';
     487          10 :                 if (strlen(pos) != 32 || hexstr2bin(pos, m->opc, 16)) {
     488           0 :                         printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
     489           0 :                         ret = -1;
     490           0 :                         break;
     491             :                 }
     492          10 :                 pos = pos2 + 1;
     493             : 
     494             :                 /* AMF */
     495          10 :                 pos2 = strchr(pos, ' ');
     496          10 :                 if (pos2 == NULL) {
     497           0 :                         printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
     498           0 :                         ret = -1;
     499           0 :                         break;
     500             :                 }
     501          10 :                 *pos2 = '\0';
     502          10 :                 if (strlen(pos) != 4 || hexstr2bin(pos, m->amf, 2)) {
     503           0 :                         printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
     504           0 :                         ret = -1;
     505           0 :                         break;
     506             :                 }
     507          10 :                 pos = pos2 + 1;
     508             : 
     509             :                 /* SQN */
     510          10 :                 pos2 = strchr(pos, ' ');
     511          10 :                 if (pos2)
     512           0 :                         *pos2 = '\0';
     513          10 :                 if (strlen(pos) != 12 || hexstr2bin(pos, m->sqn, 6)) {
     514           0 :                         printf("%s:%d - Invalid SEQ (%s)\n", fname, line, pos);
     515           0 :                         ret = -1;
     516           0 :                         break;
     517             :                 }
     518          10 :                 pos = pos2 + 1;
     519             : 
     520          10 :                 m->next = milenage_db;
     521          10 :                 milenage_db = m;
     522          10 :                 m = NULL;
     523             :         }
     524           5 :         os_free(m);
     525             : 
     526           5 :         fclose(f);
     527             : 
     528           5 :         return ret;
     529             : }
     530             : 
     531             : 
     532           5 : static void update_milenage_file(const char *fname)
     533             : {
     534             :         FILE *f, *f2;
     535             :         char buf[500], *pos;
     536           5 :         char *end = buf + sizeof(buf);
     537             :         struct milenage_parameters *m;
     538             :         size_t imsi_len;
     539             : 
     540           5 :         f = fopen(fname, "r");
     541           5 :         if (f == NULL) {
     542           0 :                 printf("Could not open Milenage data file '%s'\n", fname);
     543           0 :                 return;
     544             :         }
     545             : 
     546           5 :         snprintf(buf, sizeof(buf), "%s.new", fname);
     547           5 :         f2 = fopen(buf, "w");
     548           5 :         if (f2 == NULL) {
     549           0 :                 printf("Could not write Milenage data file '%s'\n", buf);
     550           0 :                 fclose(f);
     551           0 :                 return;
     552             :         }
     553             : 
     554          75 :         while (fgets(buf, sizeof(buf), f)) {
     555             :                 /* IMSI Ki OPc AMF SQN */
     556          65 :                 buf[sizeof(buf) - 1] = '\0';
     557             : 
     558          65 :                 pos = strchr(buf, ' ');
     559          65 :                 if (buf[0] == '#' || pos == NULL || pos - buf >= 20)
     560             :                         goto no_update;
     561             : 
     562          10 :                 imsi_len = pos - buf;
     563             : 
     564          15 :                 for (m = milenage_db; m; m = m->next) {
     565          25 :                         if (strncmp(buf, m->imsi, imsi_len) == 0 &&
     566          10 :                             m->imsi[imsi_len] == '\0')
     567          10 :                                 break;
     568             :                 }
     569             : 
     570          10 :                 if (!m)
     571           0 :                         goto no_update;
     572             : 
     573          10 :                 pos = buf;
     574          10 :                 pos += snprintf(pos, end - pos, "%s ", m->imsi);
     575          10 :                 pos += wpa_snprintf_hex(pos, end - pos, m->ki, 16);
     576          10 :                 *pos++ = ' ';
     577          10 :                 pos += wpa_snprintf_hex(pos, end - pos, m->opc, 16);
     578          10 :                 *pos++ = ' ';
     579          10 :                 pos += wpa_snprintf_hex(pos, end - pos, m->amf, 2);
     580          10 :                 *pos++ = ' ';
     581          10 :                 pos += wpa_snprintf_hex(pos, end - pos, m->sqn, 6);
     582          10 :                 *pos++ = '\n';
     583             : 
     584             :         no_update:
     585          65 :                 fprintf(f2, "%s", buf);
     586             :         }
     587             : 
     588           5 :         fclose(f2);
     589           5 :         fclose(f);
     590             : 
     591           5 :         snprintf(buf, sizeof(buf), "%s.bak", fname);
     592           5 :         if (rename(fname, buf) < 0) {
     593           0 :                 perror("rename");
     594           0 :                 return;
     595             :         }
     596             : 
     597           5 :         snprintf(buf, sizeof(buf), "%s.new", fname);
     598           5 :         if (rename(buf, fname) < 0) {
     599           0 :                 perror("rename");
     600           0 :                 return;
     601             :         }
     602             : 
     603             : }
     604             : 
     605             : 
     606          87 : static struct milenage_parameters * get_milenage(const char *imsi)
     607             : {
     608          87 :         struct milenage_parameters *m = milenage_db;
     609             : 
     610         246 :         while (m) {
     611         157 :                 if (strcmp(m->imsi, imsi) == 0)
     612          85 :                         break;
     613          72 :                 m = m->next;
     614             :         }
     615             : 
     616             : #ifdef CONFIG_SQLITE
     617          87 :         if (!m)
     618           2 :                 m = db_get_milenage(imsi);
     619             : #endif /* CONFIG_SQLITE */
     620             : 
     621          87 :         return m;
     622             : }
     623             : 
     624             : 
     625          34 : static int sim_req_auth(char *imsi, char *resp, size_t resp_len)
     626             : {
     627             :         int count, max_chal, ret;
     628             :         char *pos;
     629             :         char *rpos, *rend;
     630             :         struct milenage_parameters *m;
     631             :         struct gsm_triplet *g;
     632             : 
     633          34 :         resp[0] = '\0';
     634             : 
     635          34 :         pos = strchr(imsi, ' ');
     636          34 :         if (pos) {
     637          34 :                 *pos++ = '\0';
     638          34 :                 max_chal = atoi(pos);
     639          34 :                 if (max_chal < 1 || max_chal > EAP_SIM_MAX_CHAL)
     640           0 :                         max_chal = EAP_SIM_MAX_CHAL;
     641             :         } else
     642           0 :                 max_chal = EAP_SIM_MAX_CHAL;
     643             : 
     644          34 :         rend = resp + resp_len;
     645          34 :         rpos = resp;
     646          34 :         ret = snprintf(rpos, rend - rpos, "SIM-RESP-AUTH %s", imsi);
     647          34 :         if (ret < 0 || ret >= rend - rpos)
     648           0 :                 return -1;
     649          34 :         rpos += ret;
     650             : 
     651          34 :         m = get_milenage(imsi);
     652          34 :         if (m) {
     653             :                 u8 _rand[16], sres[4], kc[8];
     654         128 :                 for (count = 0; count < max_chal; count++) {
     655          96 :                         if (random_get_bytes(_rand, 16) < 0)
     656           0 :                                 return -1;
     657          96 :                         gsm_milenage(m->opc, m->ki, _rand, sres, kc);
     658          96 :                         *rpos++ = ' ';
     659          96 :                         rpos += wpa_snprintf_hex(rpos, rend - rpos, kc, 8);
     660          96 :                         *rpos++ = ':';
     661          96 :                         rpos += wpa_snprintf_hex(rpos, rend - rpos, sres, 4);
     662          96 :                         *rpos++ = ':';
     663          96 :                         rpos += wpa_snprintf_hex(rpos, rend - rpos, _rand, 16);
     664             :                 }
     665          32 :                 *rpos = '\0';
     666          32 :                 return 0;
     667             :         }
     668             : 
     669           2 :         count = 0;
     670           7 :         while (count < max_chal && (g = get_gsm_triplet(imsi))) {
     671           3 :                 if (strcmp(g->imsi, imsi) != 0)
     672           0 :                         continue;
     673             : 
     674           3 :                 if (rpos < rend)
     675           3 :                         *rpos++ = ' ';
     676           3 :                 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->kc, 8);
     677           3 :                 if (rpos < rend)
     678           3 :                         *rpos++ = ':';
     679           3 :                 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->sres, 4);
     680           3 :                 if (rpos < rend)
     681           3 :                         *rpos++ = ':';
     682           3 :                 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->_rand, 16);
     683           3 :                 count++;
     684             :         }
     685             : 
     686           2 :         if (count == 0) {
     687           1 :                 printf("No GSM triplets found for %s\n", imsi);
     688           1 :                 ret = snprintf(rpos, rend - rpos, " FAILURE");
     689           1 :                 if (ret < 0 || ret >= rend - rpos)
     690           0 :                         return -1;
     691           1 :                 rpos += ret;
     692             :         }
     693             : 
     694           2 :         return 0;
     695             : }
     696             : 
     697             : 
     698           0 : static int gsm_auth_req(char *imsi, char *resp, size_t resp_len)
     699             : {
     700             :         int count, ret;
     701             :         char *pos, *rpos, *rend;
     702             :         struct milenage_parameters *m;
     703             : 
     704           0 :         resp[0] = '\0';
     705             : 
     706           0 :         pos = os_strchr(imsi, ' ');
     707           0 :         if (!pos)
     708           0 :                 return -1;
     709           0 :         *pos++ = '\0';
     710             : 
     711           0 :         rend = resp + resp_len;
     712           0 :         rpos = resp;
     713           0 :         ret = os_snprintf(rpos, rend - rpos, "GSM-AUTH-RESP %s", imsi);
     714           0 :         if (os_snprintf_error(rend - rpos, ret))
     715           0 :                 return -1;
     716           0 :         rpos += ret;
     717             : 
     718           0 :         m = get_milenage(imsi);
     719           0 :         if (m) {
     720             :                 u8 _rand[16], sres[4], kc[8];
     721           0 :                 for (count = 0; count < EAP_SIM_MAX_CHAL; count++) {
     722           0 :                         if (hexstr2bin(pos, _rand, 16) != 0)
     723           0 :                                 return -1;
     724           0 :                         gsm_milenage(m->opc, m->ki, _rand, sres, kc);
     725           0 :                         *rpos++ = count == 0 ? ' ' : ':';
     726           0 :                         rpos += wpa_snprintf_hex(rpos, rend - rpos, kc, 8);
     727           0 :                         *rpos++ = ':';
     728           0 :                         rpos += wpa_snprintf_hex(rpos, rend - rpos, sres, 4);
     729           0 :                         pos += 16 * 2;
     730           0 :                         if (*pos != ':')
     731           0 :                                 break;
     732           0 :                         pos++;
     733             :                 }
     734           0 :                 *rpos = '\0';
     735           0 :                 return 0;
     736             :         }
     737             : 
     738           0 :         printf("No GSM triplets found for %s\n", imsi);
     739           0 :         ret = os_snprintf(rpos, rend - rpos, " FAILURE");
     740           0 :         if (os_snprintf_error(rend - rpos, ret))
     741           0 :                 return -1;
     742           0 :         rpos += ret;
     743             : 
     744           0 :         return 0;
     745             : }
     746             : 
     747             : 
     748          49 : static void inc_sqn(u8 *sqn)
     749             : {
     750             :         u64 val, seq, ind;
     751             : 
     752             :         /*
     753             :          * SQN = SEQ | IND = SEQ1 | SEQ2 | IND
     754             :          *
     755             :          * The mechanism used here is not time-based, so SEQ2 is void and
     756             :          * SQN = SEQ1 | IND. The length of IND is ind_len bits and the length
     757             :          * of SEQ1 is 48 - ind_len bits.
     758             :          */
     759             : 
     760             :         /* Increment both SEQ and IND by one */
     761          49 :         val = ((u64) WPA_GET_BE32(sqn) << 16) | ((u64) WPA_GET_BE16(sqn + 4));
     762          49 :         seq = (val >> ind_len) + 1;
     763          49 :         ind = (val + 1) & ((1 << ind_len) - 1);
     764          49 :         val = (seq << ind_len) | ind;
     765          49 :         WPA_PUT_BE32(sqn, val >> 16);
     766          49 :         WPA_PUT_BE16(sqn + 4, val & 0xffff);
     767          49 : }
     768             : 
     769             : 
     770          49 : static int aka_req_auth(char *imsi, char *resp, size_t resp_len)
     771             : {
     772             :         /* AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES> */
     773             :         char *pos, *end;
     774             :         u8 _rand[EAP_AKA_RAND_LEN];
     775             :         u8 autn[EAP_AKA_AUTN_LEN];
     776             :         u8 ik[EAP_AKA_IK_LEN];
     777             :         u8 ck[EAP_AKA_CK_LEN];
     778             :         u8 res[EAP_AKA_RES_MAX_LEN];
     779             :         size_t res_len;
     780             :         int ret;
     781             :         struct milenage_parameters *m;
     782          49 :         int failed = 0;
     783             : 
     784          49 :         m = get_milenage(imsi);
     785          49 :         if (m) {
     786          49 :                 if (random_get_bytes(_rand, EAP_AKA_RAND_LEN) < 0)
     787           0 :                         return -1;
     788          49 :                 res_len = EAP_AKA_RES_MAX_LEN;
     789          49 :                 inc_sqn(m->sqn);
     790             : #ifdef CONFIG_SQLITE
     791          49 :                 db_update_milenage_sqn(m);
     792             : #endif /* CONFIG_SQLITE */
     793          49 :                 sqn_changes = 1;
     794          49 :                 if (stdout_debug) {
     795         294 :                         printf("AKA: Milenage with SQN=%02x%02x%02x%02x%02x%02x\n",
     796         147 :                                m->sqn[0], m->sqn[1], m->sqn[2],
     797         147 :                                m->sqn[3], m->sqn[4], m->sqn[5]);
     798             :                 }
     799          49 :                 milenage_generate(m->opc, m->amf, m->ki, m->sqn, _rand,
     800             :                                   autn, ik, ck, res, &res_len);
     801             :         } else {
     802           0 :                 printf("Unknown IMSI: %s\n", imsi);
     803             : #ifdef AKA_USE_FIXED_TEST_VALUES
     804             :                 printf("Using fixed test values for AKA\n");
     805             :                 memset(_rand, '0', EAP_AKA_RAND_LEN);
     806             :                 memset(autn, '1', EAP_AKA_AUTN_LEN);
     807             :                 memset(ik, '3', EAP_AKA_IK_LEN);
     808             :                 memset(ck, '4', EAP_AKA_CK_LEN);
     809             :                 memset(res, '2', EAP_AKA_RES_MAX_LEN);
     810             :                 res_len = EAP_AKA_RES_MAX_LEN;
     811             : #else /* AKA_USE_FIXED_TEST_VALUES */
     812           0 :                 failed = 1;
     813             : #endif /* AKA_USE_FIXED_TEST_VALUES */
     814             :         }
     815             : 
     816          49 :         pos = resp;
     817          49 :         end = resp + resp_len;
     818          49 :         ret = snprintf(pos, end - pos, "AKA-RESP-AUTH %s ", imsi);
     819          49 :         if (ret < 0 || ret >= end - pos)
     820           0 :                 return -1;
     821          49 :         pos += ret;
     822          49 :         if (failed) {
     823           0 :                 ret = snprintf(pos, end - pos, "FAILURE");
     824           0 :                 if (ret < 0 || ret >= end - pos)
     825           0 :                         return -1;
     826           0 :                 pos += ret;
     827           0 :                 return 0;
     828             :         }
     829          49 :         pos += wpa_snprintf_hex(pos, end - pos, _rand, EAP_AKA_RAND_LEN);
     830          49 :         *pos++ = ' ';
     831          49 :         pos += wpa_snprintf_hex(pos, end - pos, autn, EAP_AKA_AUTN_LEN);
     832          49 :         *pos++ = ' ';
     833          49 :         pos += wpa_snprintf_hex(pos, end - pos, ik, EAP_AKA_IK_LEN);
     834          49 :         *pos++ = ' ';
     835          49 :         pos += wpa_snprintf_hex(pos, end - pos, ck, EAP_AKA_CK_LEN);
     836          49 :         *pos++ = ' ';
     837          49 :         pos += wpa_snprintf_hex(pos, end - pos, res, res_len);
     838             : 
     839          49 :         return 0;
     840             : }
     841             : 
     842             : 
     843           4 : static int aka_auts(char *imsi, char *resp, size_t resp_len)
     844             : {
     845             :         char *auts, *__rand;
     846             :         u8 _auts[EAP_AKA_AUTS_LEN], _rand[EAP_AKA_RAND_LEN], sqn[6];
     847             :         struct milenage_parameters *m;
     848             : 
     849           4 :         resp[0] = '\0';
     850             : 
     851             :         /* AKA-AUTS <IMSI> <AUTS> <RAND> */
     852             : 
     853           4 :         auts = strchr(imsi, ' ');
     854           4 :         if (auts == NULL)
     855           0 :                 return -1;
     856           4 :         *auts++ = '\0';
     857             : 
     858           4 :         __rand = strchr(auts, ' ');
     859           4 :         if (__rand == NULL)
     860           0 :                 return -1;
     861           4 :         *__rand++ = '\0';
     862             : 
     863           4 :         if (stdout_debug) {
     864           4 :                 printf("AKA-AUTS: IMSI=%s AUTS=%s RAND=%s\n",
     865             :                        imsi, auts, __rand);
     866             :         }
     867           8 :         if (hexstr2bin(auts, _auts, EAP_AKA_AUTS_LEN) ||
     868           4 :             hexstr2bin(__rand, _rand, EAP_AKA_RAND_LEN)) {
     869           0 :                 printf("Could not parse AUTS/RAND\n");
     870           0 :                 return -1;
     871             :         }
     872             : 
     873           4 :         m = get_milenage(imsi);
     874           4 :         if (m == NULL) {
     875           0 :                 printf("Unknown IMSI: %s\n", imsi);
     876           0 :                 return -1;
     877             :         }
     878             : 
     879           4 :         if (milenage_auts(m->opc, m->ki, _rand, _auts, sqn)) {
     880           1 :                 printf("AKA-AUTS: Incorrect MAC-S\n");
     881             :         } else {
     882           3 :                 memcpy(m->sqn, sqn, 6);
     883           3 :                 if (stdout_debug) {
     884          18 :                         printf("AKA-AUTS: Re-synchronized: "
     885             :                                "SQN=%02x%02x%02x%02x%02x%02x\n",
     886          18 :                                sqn[0], sqn[1], sqn[2], sqn[3], sqn[4], sqn[5]);
     887             :                 }
     888             : #ifdef CONFIG_SQLITE
     889           3 :                 db_update_milenage_sqn(m);
     890             : #endif /* CONFIG_SQLITE */
     891           3 :                 sqn_changes = 1;
     892             :         }
     893             : 
     894           4 :         return 0;
     895             : }
     896             : 
     897             : 
     898          87 : static int process_cmd(char *cmd, char *resp, size_t resp_len)
     899             : {
     900          87 :         if (os_strncmp(cmd, "SIM-REQ-AUTH ", 13) == 0)
     901          34 :                 return sim_req_auth(cmd + 13, resp, resp_len);
     902             : 
     903          53 :         if (os_strncmp(cmd, "GSM-AUTH-REQ ", 13) == 0)
     904           0 :                 return gsm_auth_req(cmd + 13, resp, resp_len);
     905             : 
     906          53 :         if (os_strncmp(cmd, "AKA-REQ-AUTH ", 13) == 0)
     907          49 :                 return aka_req_auth(cmd + 13, resp, resp_len);
     908             : 
     909           4 :         if (os_strncmp(cmd, "AKA-AUTS ", 9) == 0)
     910           4 :                 return aka_auts(cmd + 9, resp, resp_len);
     911             : 
     912           0 :         printf("Unknown request: %s\n", cmd);
     913           0 :         return -1;
     914             : }
     915             : 
     916             : 
     917          92 : static int process(int s)
     918             : {
     919             :         char buf[1000], resp[1000];
     920             :         struct sockaddr_un from;
     921             :         socklen_t fromlen;
     922             :         ssize_t res;
     923             : 
     924          92 :         fromlen = sizeof(from);
     925          92 :         res = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &from,
     926             :                        &fromlen);
     927          87 :         if (res < 0) {
     928           0 :                 perror("recvfrom");
     929           0 :                 return -1;
     930             :         }
     931             : 
     932          87 :         if (res == 0)
     933           0 :                 return 0;
     934             : 
     935          87 :         if ((size_t) res >= sizeof(buf))
     936           0 :                 res = sizeof(buf) - 1;
     937          87 :         buf[res] = '\0';
     938             : 
     939          87 :         printf("Received: %s\n", buf);
     940             : 
     941          87 :         if (process_cmd(buf, resp, sizeof(resp)) < 0) {
     942           0 :                 printf("Failed to process request\n");
     943           0 :                 return -1;
     944             :         }
     945             : 
     946          87 :         if (resp[0] == '\0') {
     947           4 :                 printf("No response\n");
     948           4 :                 return 0;
     949             :         }
     950             : 
     951          83 :         printf("Send: %s\n", resp);
     952             : 
     953          83 :         if (sendto(s, resp, os_strlen(resp), 0, (struct sockaddr *) &from,
     954             :                    fromlen) < 0)
     955           0 :                 perror("send");
     956             : 
     957          83 :         return 0;
     958             : }
     959             : 
     960             : 
     961           5 : static void cleanup(void)
     962             : {
     963             :         struct gsm_triplet *g, *gprev;
     964             :         struct milenage_parameters *m, *prev;
     965             : 
     966           5 :         if (update_milenage && milenage_file && sqn_changes)
     967           5 :                 update_milenage_file(milenage_file);
     968             : 
     969           5 :         g = gsm_db;
     970          40 :         while (g) {
     971          30 :                 gprev = g;
     972          30 :                 g = g->next;
     973          30 :                 os_free(gprev);
     974             :         }
     975             : 
     976           5 :         m = milenage_db;
     977          20 :         while (m) {
     978          10 :                 prev = m;
     979          10 :                 m = m->next;
     980          10 :                 os_free(prev);
     981             :         }
     982             : 
     983           5 :         if (serv_sock >= 0)
     984           5 :                 close(serv_sock);
     985           5 :         if (socket_path)
     986           5 :                 unlink(socket_path);
     987             : 
     988             : #ifdef CONFIG_SQLITE
     989           5 :         if (sqlite_db) {
     990           0 :                 sqlite3_close(sqlite_db);
     991           0 :                 sqlite_db = NULL;
     992             :         }
     993             : #endif /* CONFIG_SQLITE */
     994           5 : }
     995             : 
     996             : 
     997           5 : static void handle_term(int sig)
     998             : {
     999           5 :         printf("Signal %d - terminate\n", sig);
    1000           5 :         exit(0);
    1001             : }
    1002             : 
    1003             : 
    1004          15 : static void usage(void)
    1005             : {
    1006          15 :         printf("HLR/AuC testing gateway for hostapd EAP-SIM/AKA "
    1007             :                "database/authenticator\n"
    1008             :                "Copyright (c) 2005-2007, 2012-2013, Jouni Malinen <j@w1.fi>\n"
    1009             :                "\n"
    1010             :                "usage:\n"
    1011             :                "hlr_auc_gw [-hu] [-s<socket path>] [-g<triplet file>] "
    1012             :                "[-m<milenage file>] \\\n"
    1013             :                "        [-D<DB file>] [-i<IND len in bits>] [command]\n"
    1014             :                "\n"
    1015             :                "options:\n"
    1016             :                "  -h = show this usage help\n"
    1017             :                "  -u = update SQN in Milenage file on exit\n"
    1018             :                "  -s<socket path> = path for UNIX domain socket\n"
    1019             :                "                    (default: %s)\n"
    1020             :                "  -g<triplet file> = path for GSM authentication triplets\n"
    1021             :                "  -m<milenage file> = path for Milenage keys\n"
    1022             :                "  -D<DB file> = path to SQLite database\n"
    1023             :                "  -i<IND len in bits> = IND length for SQN (default: 5)\n"
    1024             :                "\n"
    1025             :                "If the optional command argument, like "
    1026             :                "\"AKA-REQ-AUTH <IMSI>\" is used, a single\n"
    1027             :                "command is processed with response sent to stdout. Otherwise, "
    1028             :                "hlr_auc_gw opens\n"
    1029             :                "a control interface and processes commands sent through it "
    1030             :                "(e.g., by EAP server\n"
    1031             :                "in hostapd).\n",
    1032             :                default_socket_path);
    1033          15 : }
    1034             : 
    1035             : 
    1036          25 : int main(int argc, char *argv[])
    1037             : {
    1038             :         int c;
    1039          25 :         char *gsm_triplet_file = NULL;
    1040          25 :         char *sqlite_db_file = NULL;
    1041          25 :         int ret = 0;
    1042             : 
    1043          25 :         if (os_program_init())
    1044           0 :                 return -1;
    1045             : 
    1046          25 :         socket_path = default_socket_path;
    1047             : 
    1048             :         for (;;) {
    1049          55 :                 c = getopt(argc, argv, "D:g:hi:m:s:u");
    1050          55 :                 if (c < 0)
    1051          10 :                         break;
    1052          45 :                 switch (c) {
    1053             :                 case 'D':
    1054             : #ifdef CONFIG_SQLITE
    1055           5 :                         sqlite_db_file = optarg;
    1056           5 :                         break;
    1057             : #else /* CONFIG_SQLITE */
    1058             :                         printf("No SQLite support included in the build\n");
    1059             :                         return -1;
    1060             : #endif /* CONFIG_SQLITE */
    1061             :                 case 'g':
    1062           5 :                         gsm_triplet_file = optarg;
    1063           5 :                         break;
    1064             :                 case 'h':
    1065           5 :                         usage();
    1066           5 :                         return 0;
    1067             :                 case 'i':
    1068          10 :                         ind_len = atoi(optarg);
    1069          10 :                         if (ind_len < 0 || ind_len > 32) {
    1070           5 :                                 printf("Invalid IND length\n");
    1071           5 :                                 return -1;
    1072             :                         }
    1073           5 :                         break;
    1074             :                 case 'm':
    1075           5 :                         milenage_file = optarg;
    1076           5 :                         break;
    1077             :                 case 's':
    1078           5 :                         socket_path = optarg;
    1079           5 :                         break;
    1080             :                 case 'u':
    1081           5 :                         update_milenage = 1;
    1082           5 :                         break;
    1083             :                 default:
    1084           5 :                         usage();
    1085           5 :                         return -1;
    1086             :                 }
    1087          30 :         }
    1088             : 
    1089          10 :         if (!gsm_triplet_file && !milenage_file && !sqlite_db_file) {
    1090           5 :                 usage();
    1091           5 :                 return -1;
    1092             :         }
    1093             : 
    1094             : #ifdef CONFIG_SQLITE
    1095           5 :         if (sqlite_db_file && (sqlite_db = db_open(sqlite_db_file)) == NULL)
    1096           0 :                 return -1;
    1097             : #endif /* CONFIG_SQLITE */
    1098             : 
    1099           5 :         if (gsm_triplet_file && read_gsm_triplets(gsm_triplet_file) < 0)
    1100           0 :                 return -1;
    1101             : 
    1102           5 :         if (milenage_file && read_milenage(milenage_file) < 0)
    1103           0 :                 return -1;
    1104             : 
    1105           5 :         if (optind == argc) {
    1106           5 :                 serv_sock = open_socket(socket_path);
    1107           5 :                 if (serv_sock < 0)
    1108           0 :                         return -1;
    1109             : 
    1110           5 :                 printf("Listening for requests on %s\n", socket_path);
    1111             : 
    1112           5 :                 atexit(cleanup);
    1113           5 :                 signal(SIGTERM, handle_term);
    1114           5 :                 signal(SIGINT, handle_term);
    1115             : 
    1116             :                 for (;;)
    1117          92 :                         process(serv_sock);
    1118             :         } else {
    1119             :                 char buf[1000];
    1120           0 :                 socket_path = NULL;
    1121           0 :                 stdout_debug = 0;
    1122           0 :                 if (process_cmd(argv[optind], buf, sizeof(buf)) < 0) {
    1123           0 :                         printf("FAIL\n");
    1124           0 :                         ret = -1;
    1125             :                 } else {
    1126           0 :                         printf("%s\n", buf);
    1127             :                 }
    1128           0 :                 cleanup();
    1129             :         }
    1130             : 
    1131             : #ifdef CONFIG_SQLITE
    1132           0 :         if (sqlite_db) {
    1133           0 :                 sqlite3_close(sqlite_db);
    1134           0 :                 sqlite_db = NULL;
    1135             :         }
    1136             : #endif /* CONFIG_SQLITE */
    1137             : 
    1138           0 :         os_program_deinit();
    1139             : 
    1140           0 :         return ret;
    1141             : }

Generated by: LCOV version 1.10