Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 1 | /* (C) 2015 by Harald Welte <laforge@gnumonks.org> |
| 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Affero General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU Affero General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Affero General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include <osmocom/core/utils.h> |
| 23 | #include <osmocom/crypt/auth.h> |
| 24 | |
| 25 | #include <sqlite3.h> |
| 26 | |
| 27 | #include "logging.h" |
| 28 | #include "db.h" |
| 29 | |
| 30 | #define LOGHLR(imsi, level, fmt, args ...) LOGP(DAUC, level, "%s: " fmt, imsi, ## args) |
| 31 | |
| 32 | #define SL3_TXT(x, stmt, idx) do { \ |
| 33 | const char *_txt = (const char *) sqlite3_column_text(stmt, idx); \ |
| 34 | if (_txt) \ |
| 35 | strncpy(x, _txt, sizeof(x)); \ |
| 36 | x[sizeof(x)-1] = '\0'; \ |
| 37 | } while (0) |
| 38 | |
| 39 | int db_subscr_get(struct db_context *dbc, const char *imsi, |
| 40 | struct hlr_subscriber *subscr) |
| 41 | { |
| 42 | sqlite3_stmt *stmt = dbc->stmt[SEL_BY_IMSI]; |
| 43 | int rc, ret = 0; |
| 44 | |
| 45 | rc = sqlite3_bind_text(stmt, 1, imsi, -1, SQLITE_STATIC); |
| 46 | if (rc != SQLITE_OK) { |
| 47 | LOGHLR(imsi, LOGL_ERROR, "Error binding IMSI: %d\n", rc); |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | /* execute the statement */ |
| 52 | rc = sqlite3_step(stmt); |
| 53 | if (rc != SQLITE_ROW) { |
| 54 | LOGHLR(imsi, LOGL_ERROR, "Error executing SQL: %d\n", rc); |
| 55 | ret = -2; |
| 56 | goto out; |
| 57 | } |
| 58 | |
| 59 | /* obtain the various columns */ |
| 60 | subscr->id = sqlite3_column_int64(stmt, 0); |
| 61 | SL3_TXT(subscr->imsi, stmt, 1); |
| 62 | SL3_TXT(subscr->msisdn, stmt, 2); |
| 63 | SL3_TXT(subscr->vlr_number, stmt, 3); |
| 64 | SL3_TXT(subscr->sgsn_number, stmt, 4); |
| 65 | SL3_TXT(subscr->sgsn_address, stmt, 5); |
| 66 | subscr->periodic_lu_timer = sqlite3_column_int(stmt, 6); |
| 67 | subscr->periodic_rau_tau_timer = sqlite3_column_int(stmt, 7); |
| 68 | subscr->nam_cs = sqlite3_column_int(stmt, 8); |
| 69 | subscr->nam_ps = sqlite3_column_int(stmt, 9); |
| 70 | subscr->lmsi = sqlite3_column_int(stmt, 10); |
| 71 | subscr->ms_purged_cs = sqlite3_column_int(stmt, 11); |
| 72 | subscr->ms_purged_ps = sqlite3_column_int(stmt, 12); |
| 73 | |
| 74 | out: |
| 75 | /* remove bindings and reset statement to be re-executed */ |
| 76 | rc = sqlite3_clear_bindings(stmt); |
| 77 | if (rc != SQLITE_OK) { |
| 78 | LOGP(DAUC, LOGL_ERROR, "Error clerearing bindings: %d\n", rc); |
| 79 | } |
| 80 | rc = sqlite3_reset(stmt); |
| 81 | if (rc != SQLITE_OK) { |
| 82 | LOGP(DAUC, LOGL_ERROR, "Error in sqlite3_reset: %d\n", rc); |
| 83 | } |
| 84 | |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | int db_subscr_lu(struct db_context *dbc, |
| 89 | const struct hlr_subscriber *subscr, |
| 90 | const char *vlr_or_sgsn_number, bool lu_is_ps) |
| 91 | { |
| 92 | sqlite3_stmt *stmt = dbc->stmt[UPD_VLR_BY_ID]; |
| 93 | const char *txt; |
| 94 | int rc, ret = 0; |
| 95 | |
| 96 | if (lu_is_ps) { |
| 97 | stmt = dbc->stmt[UPD_SGSN_BY_ID]; |
| 98 | txt = subscr->sgsn_number; |
| 99 | } else { |
| 100 | stmt = dbc->stmt[UPD_VLR_BY_ID]; |
| 101 | txt = subscr->vlr_number; |
| 102 | } |
| 103 | |
| 104 | rc = sqlite3_bind_int64(stmt, 1, subscr->id); |
| 105 | if (rc != SQLITE_OK) { |
| 106 | LOGP(DAUC, LOGL_ERROR, "Error binding ID: %d\n", rc); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | rc = sqlite3_bind_text(stmt, 2, txt, -1, SQLITE_STATIC); |
| 111 | if (rc != SQLITE_OK) { |
| 112 | LOGP(DAUC, LOGL_ERROR, "Error binding VLR/SGSN Number: %d\n", rc); |
| 113 | ret = -2; |
| 114 | goto out; |
| 115 | } |
| 116 | |
| 117 | /* execute the statement */ |
| 118 | rc = sqlite3_step(stmt); |
| 119 | if (rc != SQLITE_DONE) { |
| 120 | LOGP(DAUC, LOGL_ERROR, "Error updating SQN: %d\n", rc); |
| 121 | ret = -3; |
| 122 | goto out; |
| 123 | } |
| 124 | out: |
| 125 | /* remove bindings and reset statement to be re-executed */ |
| 126 | rc = sqlite3_clear_bindings(stmt); |
| 127 | if (rc != SQLITE_OK) { |
| 128 | LOGP(DAUC, LOGL_ERROR, "Error clerearing bindings: %d\n", rc); |
| 129 | } |
| 130 | rc = sqlite3_reset(stmt); |
| 131 | if (rc != SQLITE_OK) { |
| 132 | LOGP(DAUC, LOGL_ERROR, "Error in sqlite3_reset: %d\n", rc); |
| 133 | } |
| 134 | |
| 135 | return ret; |
| 136 | } |