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> |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 21 | #include <errno.h> |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 22 | |
| 23 | #include <osmocom/core/utils.h> |
| 24 | #include <osmocom/crypt/auth.h> |
| 25 | |
| 26 | #include <sqlite3.h> |
| 27 | |
| 28 | #include "logging.h" |
| 29 | #include "db.h" |
| 30 | |
| 31 | #define LOGHLR(imsi, level, fmt, args ...) LOGP(DAUC, level, "%s: " fmt, imsi, ## args) |
| 32 | |
| 33 | #define SL3_TXT(x, stmt, idx) do { \ |
| 34 | const char *_txt = (const char *) sqlite3_column_text(stmt, idx); \ |
| 35 | if (_txt) \ |
| 36 | strncpy(x, _txt, sizeof(x)); \ |
| 37 | x[sizeof(x)-1] = '\0'; \ |
| 38 | } while (0) |
| 39 | |
Neels Hofmeyr | 518335e | 2017-10-06 03:20:14 +0200 | [diff] [blame] | 40 | int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi, |
| 41 | struct hlr_subscriber *subscr) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 42 | { |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 43 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMSI]; |
Max | adc6648 | 2017-02-20 11:23:20 +0100 | [diff] [blame] | 44 | int rc; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 45 | |
Neels Hofmeyr | f314459 | 2017-10-06 03:40:52 +0200 | [diff] [blame] | 46 | if (!db_bind_text(stmt, NULL, imsi)) |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 47 | return -EINVAL; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 48 | |
| 49 | /* execute the statement */ |
| 50 | rc = sqlite3_step(stmt); |
| 51 | if (rc != SQLITE_ROW) { |
| 52 | LOGHLR(imsi, LOGL_ERROR, "Error executing SQL: %d\n", rc); |
Max | adc6648 | 2017-02-20 11:23:20 +0100 | [diff] [blame] | 53 | db_remove_reset(stmt); |
| 54 | return -ENOEXEC; |
| 55 | } |
| 56 | |
| 57 | if (!subscr) { |
| 58 | db_remove_reset(stmt); |
| 59 | return 0; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* obtain the various columns */ |
| 63 | subscr->id = sqlite3_column_int64(stmt, 0); |
| 64 | SL3_TXT(subscr->imsi, stmt, 1); |
| 65 | SL3_TXT(subscr->msisdn, stmt, 2); |
Harald Welte | 9990927 | 2016-05-05 18:24:15 +0200 | [diff] [blame] | 66 | /* FIXME: These should all be BLOBs as they might contain NUL */ |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 67 | SL3_TXT(subscr->vlr_number, stmt, 3); |
| 68 | SL3_TXT(subscr->sgsn_number, stmt, 4); |
| 69 | SL3_TXT(subscr->sgsn_address, stmt, 5); |
| 70 | subscr->periodic_lu_timer = sqlite3_column_int(stmt, 6); |
| 71 | subscr->periodic_rau_tau_timer = sqlite3_column_int(stmt, 7); |
| 72 | subscr->nam_cs = sqlite3_column_int(stmt, 8); |
| 73 | subscr->nam_ps = sqlite3_column_int(stmt, 9); |
| 74 | subscr->lmsi = sqlite3_column_int(stmt, 10); |
| 75 | subscr->ms_purged_cs = sqlite3_column_int(stmt, 11); |
| 76 | subscr->ms_purged_ps = sqlite3_column_int(stmt, 12); |
| 77 | |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 78 | db_remove_reset(stmt); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 79 | |
Max | adc6648 | 2017-02-20 11:23:20 +0100 | [diff] [blame] | 80 | return 0; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 83 | int db_subscr_ps(struct db_context *dbc, const char *imsi, bool enable) |
| 84 | { |
| 85 | sqlite3_stmt *stmt = |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 86 | dbc->stmt[enable ? DB_STMT_SET_NAM_PS_BY_IMSI : DB_STMT_UNSET_NAM_PS_BY_IMSI]; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 87 | int rc; |
| 88 | |
Neels Hofmeyr | f314459 | 2017-10-06 03:40:52 +0200 | [diff] [blame] | 89 | if (!db_bind_text(stmt, NULL, imsi)) |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 90 | return -EINVAL; |
| 91 | |
| 92 | rc = sqlite3_step(stmt); /* execute the statement */ |
| 93 | if (rc != SQLITE_DONE) { |
| 94 | LOGHLR(imsi, LOGL_ERROR, "Error executing SQL: %d\n", rc); |
Neels Hofmeyr | 743cf42 | 2017-03-16 20:38:44 +0100 | [diff] [blame] | 95 | db_remove_reset(stmt); |
| 96 | return -ENOEXEC; |
Max | 3ce3686 | 2017-02-20 11:18:04 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | rc = sqlite3_changes(dbc->db); /* verify execution result */ |
| 100 | if (rc != 1) { |
| 101 | LOGHLR(imsi, LOGL_ERROR, "SQL modified %d rows (expected 1)\n", |
| 102 | rc); |
| 103 | rc = -EINVAL; |
| 104 | } |
| 105 | |
| 106 | db_remove_reset(stmt); |
| 107 | return rc; |
| 108 | } |
| 109 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 110 | int db_subscr_lu(struct db_context *dbc, |
| 111 | const struct hlr_subscriber *subscr, |
| 112 | const char *vlr_or_sgsn_number, bool lu_is_ps) |
| 113 | { |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 114 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_UPD_VLR_BY_ID]; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 115 | const char *txt; |
| 116 | int rc, ret = 0; |
| 117 | |
| 118 | if (lu_is_ps) { |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 119 | stmt = dbc->stmt[DB_STMT_UPD_SGSN_BY_ID]; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 120 | txt = subscr->sgsn_number; |
| 121 | } else { |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 122 | stmt = dbc->stmt[DB_STMT_UPD_VLR_BY_ID]; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 123 | txt = subscr->vlr_number; |
| 124 | } |
| 125 | |
| 126 | rc = sqlite3_bind_int64(stmt, 1, subscr->id); |
| 127 | if (rc != SQLITE_OK) { |
| 128 | LOGP(DAUC, LOGL_ERROR, "Error binding ID: %d\n", rc); |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 129 | return -EINVAL; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | rc = sqlite3_bind_text(stmt, 2, txt, -1, SQLITE_STATIC); |
| 133 | if (rc != SQLITE_OK) { |
| 134 | LOGP(DAUC, LOGL_ERROR, "Error binding VLR/SGSN Number: %d\n", rc); |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 135 | ret = -EBADMSG; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 136 | goto out; |
| 137 | } |
| 138 | |
| 139 | /* execute the statement */ |
| 140 | rc = sqlite3_step(stmt); |
| 141 | if (rc != SQLITE_DONE) { |
| 142 | LOGP(DAUC, LOGL_ERROR, "Error updating SQN: %d\n", rc); |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 143 | ret = -ENOEXEC; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 144 | } |
| 145 | out: |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 146 | db_remove_reset(stmt); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 147 | |
| 148 | return ret; |
| 149 | } |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 150 | |
| 151 | int db_subscr_purge(struct db_context *dbc, const char *imsi, bool is_ps) |
| 152 | { |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 153 | sqlite3_stmt *stmt = dbc->stmt[DB_STMT_UPD_VLR_BY_ID]; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 154 | int rc, ret = 1; |
| 155 | |
| 156 | if (is_ps) |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 157 | stmt = dbc->stmt[DB_STMT_UPD_PURGE_PS_BY_IMSI]; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 158 | else |
Neels Hofmeyr | 4bde949 | 2017-10-06 03:09:34 +0200 | [diff] [blame] | 159 | stmt = dbc->stmt[DB_STMT_UPD_PURGE_CS_BY_IMSI]; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 160 | |
Neels Hofmeyr | f314459 | 2017-10-06 03:40:52 +0200 | [diff] [blame] | 161 | if (!db_bind_text(stmt, NULL, imsi)) |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 162 | return -EINVAL; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 163 | |
| 164 | /* execute the statement */ |
| 165 | rc = sqlite3_step(stmt); |
| 166 | if (rc != SQLITE_DONE) { |
| 167 | LOGP(DAUC, LOGL_ERROR, "Error setting Purged: %d\n", rc); |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 168 | ret = -ENOEXEC; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 169 | } |
| 170 | /* FIXME: return 0 in case IMSI not known */ |
Max | 00b3715 | 2017-02-20 11:09:27 +0100 | [diff] [blame] | 171 | |
| 172 | db_remove_reset(stmt); |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 173 | |
| 174 | return ret; |
| 175 | } |