Check os_snprintf() result more consistently - automatic 1
[mech_eap.git] / src / eap_server / eap_sim_db.c
index e41c99f..3b689ed 100644 (file)
@@ -38,7 +38,6 @@ struct eap_sim_db_pending {
        char imsi[20];
        enum { PENDING, SUCCESS, FAILURE } state;
        void *cb_session_ctx;
-       struct os_time timestamp;
        int aka;
        union {
                struct {
@@ -574,16 +573,14 @@ static void eap_sim_db_receive(int sock, void *eloop_ctx, void *sock_ctx)
        char buf[1000], *pos, *cmd, *imsi;
        int res;
 
-       res = recv(sock, buf, sizeof(buf), 0);
+       res = recv(sock, buf, sizeof(buf) - 1, 0);
        if (res < 0)
                return;
+       buf[res] = '\0';
        wpa_hexdump_ascii_key(MSG_MSGDUMP, "EAP-SIM DB: Received from an "
                              "external source", (u8 *) buf, res);
        if (res == 0)
                return;
-       if (res >= (int) sizeof(buf))
-               res = sizeof(buf) - 1;
-       buf[res] = '\0';
 
        if (data->get_complete_cb == NULL) {
                wpa_printf(MSG_DEBUG, "EAP-SIM DB: No get_complete_cb "
@@ -630,7 +627,7 @@ static int eap_sim_db_open_socket(struct eap_sim_db_data *data)
 
        data->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
        if (data->sock < 0) {
-               perror("socket(eap_sim_db)");
+               wpa_printf(MSG_INFO, "socket(eap_sim_db): %s", strerror(errno));
                return -1;
        }
 
@@ -640,8 +637,13 @@ static int eap_sim_db_open_socket(struct eap_sim_db_data *data)
                    "/tmp/eap_sim_db_%d-%d", getpid(), counter++);
        os_free(data->local_sock);
        data->local_sock = os_strdup(addr.sun_path);
+       if (data->local_sock == NULL) {
+               close(data->sock);
+               data->sock = -1;
+               return -1;
+       }
        if (bind(data->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-               perror("bind(eap_sim_db)");
+               wpa_printf(MSG_INFO, "bind(eap_sim_db): %s", strerror(errno));
                close(data->sock);
                data->sock = -1;
                return -1;
@@ -651,12 +653,16 @@ static int eap_sim_db_open_socket(struct eap_sim_db_data *data)
        addr.sun_family = AF_UNIX;
        os_strlcpy(addr.sun_path, data->fname + 5, sizeof(addr.sun_path));
        if (connect(data->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-               perror("connect(eap_sim_db)");
+               wpa_printf(MSG_INFO, "connect(eap_sim_db): %s",
+                          strerror(errno));
                wpa_hexdump_ascii(MSG_INFO, "HLR/AuC GW socket",
                                  (u8 *) addr.sun_path,
                                  os_strlen(addr.sun_path));
                close(data->sock);
                data->sock = -1;
+               unlink(data->local_sock);
+               os_free(data->local_sock);
+               data->local_sock = NULL;
                return -1;
        }
 
@@ -804,7 +810,8 @@ static int eap_sim_db_send(struct eap_sim_db_data *data, const char *msg,
 
        if (send(data->sock, msg, len, 0) < 0) {
                _errno = errno;
-               perror("send[EAP-SIM DB UNIX]");
+               wpa_printf(MSG_INFO, "send[EAP-SIM DB UNIX]: %s",
+                          strerror(errno));
        }
 
        if (_errno == ENOTCONN || _errno == EDESTADDRREQ || _errno == EINVAL ||
@@ -816,7 +823,8 @@ static int eap_sim_db_send(struct eap_sim_db_data *data, const char *msg,
                wpa_printf(MSG_DEBUG, "EAP-SIM DB: Reconnected to the "
                           "external server");
                if (send(data->sock, msg, len, 0) < 0) {
-                       perror("send[EAP-SIM DB UNIX]");
+                       wpa_printf(MSG_INFO, "send[EAP-SIM DB UNIX]: %s",
+                                  strerror(errno));
                        return -1;
                }
        }
@@ -919,7 +927,7 @@ int eap_sim_db_get_gsm_triplets(struct eap_sim_db_data *data,
        os_memcpy(msg + len, imsi, imsi_len);
        len += imsi_len;
        ret = os_snprintf(msg + len, sizeof(msg) - len, " %d", max_chal);
-       if (ret < 0 || (size_t) ret >= sizeof(msg) - len)
+       if (os_snprintf_error(sizeof(msg) - len, ret))
                return EAP_SIM_DB_FAILURE;
        len += ret;
 
@@ -932,7 +940,6 @@ int eap_sim_db_get_gsm_triplets(struct eap_sim_db_data *data,
        if (entry == NULL)
                return EAP_SIM_DB_FAILURE;
 
-       os_get_time(&entry->timestamp);
        os_strlcpy(entry->imsi, imsi, sizeof(entry->imsi));
        entry->cb_session_ctx = cb_session_ctx;
        entry->state = PENDING;
@@ -957,7 +964,7 @@ static char * eap_sim_db_get_next(struct eap_sim_db_data *data, char prefix)
        pos = id;
        end = id + sizeof(buf) * 2 + 2;
        *pos++ = prefix;
-       pos += wpa_snprintf_hex(pos, end - pos, buf, sizeof(buf));
+       wpa_snprintf_hex(pos, end - pos, buf, sizeof(buf));
        
        return id;
 }
@@ -1223,11 +1230,6 @@ eap_sim_db_get_permanent(struct eap_sim_db_data *data, const char *pseudonym)
 {
        struct eap_sim_pseudonym *p;
 
-       if (pseudonym[0] != EAP_SIM_PSEUDONYM_PREFIX &&
-           pseudonym[0] != EAP_AKA_PSEUDONYM_PREFIX &&
-           pseudonym[0] != EAP_AKA_PRIME_PSEUDONYM_PREFIX)
-               return NULL;
-
 #ifdef CONFIG_SQLITE
        if (data->sqlite_db)
                return db_get_pseudonym(data, pseudonym);
@@ -1256,11 +1258,6 @@ eap_sim_db_get_reauth_entry(struct eap_sim_db_data *data,
 {
        struct eap_sim_reauth *r;
 
-       if (reauth_id[0] != EAP_SIM_REAUTH_ID_PREFIX &&
-           reauth_id[0] != EAP_AKA_REAUTH_ID_PREFIX &&
-           reauth_id[0] != EAP_AKA_PRIME_REAUTH_ID_PREFIX)
-               return NULL;
-
 #ifdef CONFIG_SQLITE
        if (data->sqlite_db)
                return db_get_reauth(data, reauth_id);
@@ -1402,7 +1399,6 @@ int eap_sim_db_get_aka_auth(struct eap_sim_db_data *data, const char *username,
        if (entry == NULL)
                return EAP_SIM_DB_FAILURE;
 
-       os_get_time(&entry->timestamp);
        entry->aka = 1;
        os_strlcpy(entry->imsi, imsi, sizeof(entry->imsi));
        entry->cb_session_ctx = cb_session_ctx;
@@ -1459,13 +1455,13 @@ int eap_sim_db_resynchronize(struct eap_sim_db_data *data,
                len += imsi_len;
 
                ret = os_snprintf(msg + len, sizeof(msg) - len, " ");
-               if (ret < 0 || (size_t) ret >= sizeof(msg) - len)
+               if (os_snprintf_error(sizeof(msg) - len, ret))
                        return -1;
                len += ret;
                len += wpa_snprintf_hex(msg + len, sizeof(msg) - len,
                                        auts, EAP_AKA_AUTS_LEN);
                ret = os_snprintf(msg + len, sizeof(msg) - len, " ");
-               if (ret < 0 || (size_t) ret >= sizeof(msg) - len)
+               if (os_snprintf_error(sizeof(msg) - len, ret))
                        return -1;
                len += ret;
                len += wpa_snprintf_hex(msg + len, sizeof(msg) - len,
@@ -1490,7 +1486,6 @@ int eap_sim_db_resynchronize(struct eap_sim_db_data *data,
  */
 char * sim_get_username(const u8 *identity, size_t identity_len)
 {
-       char *username;
        size_t pos;
 
        if (identity == NULL)
@@ -1501,11 +1496,5 @@ char * sim_get_username(const u8 *identity, size_t identity_len)
                        break;
        }
 
-       username = os_malloc(pos + 1);
-       if (username == NULL)
-               return NULL;
-       os_memcpy(username, identity, pos);
-       username[pos] = '\0';
-
-       return username;
+       return dup_binstr(identity, pos);
 }