summaryrefslogtreecommitdiffstats
path: root/openbsc/src/libmsc/db.c
blob: 4ba12ca6968fad2257a262a8a0df4521474d1c73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
/* Simple HLR/VLR database backend using dbi */
/* (C) 2008 by Jan Luebbe <jluebbe@debian.org>
 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdint.h>
#include <inttypes.h>
#include <libgen.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dbi/dbi.h>

#include <openbsc/gsm_data.h>
#include <openbsc/gsm_subscriber.h>
#include <openbsc/gsm_04_11.h>
#include <openbsc/db.h>
#include <openbsc/debug.h>

#include <osmocom/gsm/protocol/gsm_23_003.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/statistics.h>
#include <osmocom/core/rate_ctr.h>
#include <osmocom/core/utils.h>

#include <openssl/rand.h>

/* Semi-Private-Interface (SPI) for the subscriber code */
void subscr_direct_free(struct gsm_subscriber *subscr);

static char *db_basename = NULL;
static char *db_dirname = NULL;
static dbi_conn conn;

#define SCHEMA_REVISION "5"

enum {
	SCHEMA_META,
	INSERT_META,
	SCHEMA_SUBSCRIBER,
	SCHEMA_AUTH,
	SCHEMA_EQUIPMENT,
	SCHEMA_EQUIPMENT_WATCH,
	SCHEMA_SMS,
	SCHEMA_VLR,
	SCHEMA_APDU,
	SCHEMA_COUNTERS,
	SCHEMA_RATE,
	SCHEMA_AUTHKEY,
	SCHEMA_AUTHLAST,
};

static const char *create_stmts[] = {
	[SCHEMA_META] = "CREATE TABLE IF NOT EXISTS Meta ("
		"id INTEGER PRIMARY KEY AUTOINCREMENT, "
		"key TEXT UNIQUE NOT NULL, "
		"value TEXT NOT NULL"
		")",
	[INSERT_META] = "INSERT OR IGNORE INTO Meta "
		"(key, value) "
		"VALUES "
		"('revision', " SCHEMA_REVISION ")",
	[SCHEMA_SUBSCRIBER] = "CREATE TABLE IF NOT EXISTS Subscriber ("
		"id INTEGER PRIMARY KEY AUTOINCREMENT, "
		"created TIMESTAMP NOT NULL, "
		"updated TIMESTAMP NOT NULL, "
		"imsi NUMERIC UNIQUE NOT NULL, "
		"name TEXT, "
		"extension TEXT UNIQUE, "
		"authorized INTEGER NOT NULL DEFAULT 0, "
		"tmsi TEXT UNIQUE, "
		"lac INTEGER NOT NULL DEFAULT 0, "
		"expire_lu TIMESTAMP DEFAULT NULL"
		")",
	[SCHEMA_AUTH] = "CREATE TABLE IF NOT EXISTS AuthToken ("
		"id INTEGER PRIMARY KEY AUTOINCREMENT, "
		"subscriber_id INTEGER UNIQUE NOT NULL, "
		"created TIMESTAMP NOT NULL, "
		"token TEXT UNIQUE NOT NULL"
		")",
	[SCHEMA_EQUIPMENT] = "CREATE TABLE IF NOT EXISTS Equipment ("
		"id INTEGER PRIMARY KEY AUTOINCREMENT, "
		"created TIMESTAMP NOT NULL, "
		"updated TIMESTAMP NOT NULL, "
		"name TEXT, "
		"classmark1 NUMERIC, "
		"classmark2 BLOB, "
		"classmark3 BLOB, "
		"imei NUMERIC UNIQUE NOT NULL"
		")",
	[SCHEMA_EQUIPMENT_WATCH] = "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
		"id INTEGER PRIMARY KEY AUTOINCREMENT, "
		"created TIMESTAMP NOT NULL, "
		"updated TIMESTAMP NOT NULL, "
		"subscriber_id NUMERIC NOT NULL, "
		"equipment_id NUMERIC NOT NULL, "
		"UNIQUE (subscriber_id, equipment_id) "
		")",
	[SCHEMA_SMS] = "CREATE TABLE IF NOT EXISTS SMS ("
		/* metadata, not part of sms */
		"id INTEGER PRIMARY KEY AUTOINCREMENT, "
		"created TIMESTAMP NOT NULL, "
		"sent TIMESTAMP, "
		"deliver_attempts INTEGER NOT NULL DEFAULT 0, "
		/* data directly copied/derived from SMS */
		"valid_until TIMESTAMP, "
		"reply_path_req INTEGER NOT NULL, "
		"status_rep_req INTEGER NOT NULL, "
		"is_report INTEGER NOT NULL, "
		"msg_ref INTEGER NOT NULL, "
		"protocol_id INTEGER NOT NULL, "
		"data_coding_scheme INTEGER NOT NULL, "
		"ud_hdr_ind INTEGER NOT NULL, "
		"src_addr TEXT NOT NULL, "
		"src_ton INTEGER NOT NULL, "
		"src_npi INTEGER NOT NULL, "
		"dest_addr TEXT NOT NULL, "
		"dest_ton INTEGER NOT NULL, "
		"dest_npi INTEGER NOT NULL, "
		"user_data BLOB, "	/* TP-UD */
		/* additional data, interpreted from SMS */
		"header BLOB, "		/* UD Header */
		"text TEXT "		/* decoded UD after UDH */
		")",
	[SCHEMA_VLR] = "CREATE TABLE IF NOT EXISTS VLR ("
		"id INTEGER PRIMARY KEY AUTOINCREMENT, "
		"created TIMESTAMP NOT NULL, "
		"updated TIMESTAMP NOT NULL, "
		"subscriber_id NUMERIC UNIQUE NOT NULL, "
		"last_bts NUMERIC NOT NULL "
		")",
	[SCHEMA_APDU] = "CREATE TABLE IF NOT EXISTS ApduBlobs ("
		"id INTEGER PRIMARY KEY AUTOINCREMENT, "
		"created TIMESTAMP NOT NULL, "
		"apdu_id_flags INTEGER NOT NULL, "
		"subscriber_id INTEGER NOT NULL, "
		"apdu BLOB "
		")",
	[SCHEMA_COUNTERS] = "CREATE TABLE IF NOT EXISTS Counters ("
		"id INTEGER PRIMARY KEY AUTOINCREMENT, "
		"timestamp TIMESTAMP NOT NULL, "
		"value INTEGER NOT NULL, "
		"name TEXT NOT NULL "
		")",
	[SCHEMA_RATE] = "CREATE TABLE IF NOT EXISTS RateCounters ("
		"id INTEGER PRIMARY KEY AUTOINCREMENT, "
		"timestamp TIMESTAMP NOT NULL, "
		"value INTEGER NOT NULL, "
		"name TEXT NOT NULL, "
		"idx INTEGER NOT NULL "
		")",
	[SCHEMA_AUTHKEY] = "CREATE TABLE IF NOT EXISTS AuthKeys ("
		"subscriber_id INTEGER PRIMARY KEY, "
		"algorithm_id INTEGER NOT NULL, "
		"a3a8_ki BLOB "
		")",
	[SCHEMA_AUTHLAST] = "CREATE TABLE IF NOT EXISTS AuthLastTuples ("
		"subscriber_id INTEGER PRIMARY KEY, "
		"issued TIMESTAMP NOT NULL, "
		"use_count INTEGER NOT NULL DEFAULT 0, "
		"key_seq INTEGER NOT NULL, "
		"rand BLOB NOT NULL, "
		"sres BLOB NOT NULL, "
		"kc BLOB NOT NULL "
		")",
};

void db_error_func(dbi_conn conn, void *data)
{
	const char *msg;
	dbi_conn_error(conn, &msg);
	LOGP(DDB, LOGL_ERROR, "DBI: %s\n", msg);
	osmo_log_backtrace(DDB, LOGL_ERROR);
}

static int update_db_revision_2(void)
{
	dbi_result result;

	result = dbi_conn_query(conn,
				"ALTER TABLE Subscriber "
				"ADD COLUMN expire_lu "
				"TIMESTAMP DEFAULT NULL");
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
		     "Failed to alter table Subscriber (upgrade from rev 2).\n");
		return -EINVAL;
	}
	dbi_result_free(result);

	result = dbi_conn_query(conn,
				"UPDATE Meta "
				"SET value = '3' "
				"WHERE key = 'revision'");
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
		     "Failed to update DB schema revision  (upgrade from rev 2).\n");
		return -EINVAL;
	}
	dbi_result_free(result);

	return 0;
}

/**
 * Copied from the normal sms_from_result_v3 to avoid having
 * to make sure that the real routine will remain backward
 * compatible.
 */
static struct gsm_sms *sms_from_result_v3(dbi_result result)
{
	struct gsm_sms *sms = sms_alloc();
	long long unsigned int sender_id;
	struct gsm_subscriber *sender;
	const char *text, *daddr;
	const unsigned char *user_data;
	char buf[32];

	if (!sms)
		return NULL;

	sms->id = dbi_result_get_ulonglong(result, "id");

	sender_id = dbi_result_get_ulonglong(result, "sender_id");
	snprintf(buf, sizeof(buf), "%llu", sender_id);
	sender = db_get_subscriber(GSM_SUBSCRIBER_ID, buf);
	OSMO_ASSERT(sender);
	osmo_strlcpy(sms->src.addr, sender->extension, sizeof(sms->src.addr));
	subscr_direct_free(sender);
	sender = NULL;

	sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
	sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
	sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
	sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
	sms->data_coding_scheme = dbi_result_get_ulonglong(result,
						  "data_coding_scheme");

	daddr = dbi_result_get_string(result, "dest_addr");
	if (daddr)
		osmo_strlcpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));

	sms->user_data_len = dbi_result_get_field_length(result, "user_data");
	user_data = dbi_result_get_binary(result, "user_data");
	if (sms->user_data_len > sizeof(sms->user_data))
		sms->user_data_len = (uint8_t) sizeof(sms->user_data);
	memcpy(sms->user_data, user_data, sms->user_data_len);

	text = dbi_result_get_string(result, "text");
	if (text)
		osmo_strlcpy(sms->text, text, sizeof(sms->text));
	return sms;
}

static int update_db_revision_3(void)
{
	dbi_result result;
	struct gsm_sms *sms;

	LOGP(DDB, LOGL_NOTICE, "Going to migrate from revision 3\n");

	result = dbi_conn_query(conn, "BEGIN EXCLUSIVE TRANSACTION");
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
			"Failed to begin transaction (upgrade from rev 3)\n");
		return -EINVAL;
	}
	dbi_result_free(result);

	/* Rename old SMS table to be able create a new one */
	result = dbi_conn_query(conn, "ALTER TABLE SMS RENAME TO SMS_3");
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
		     "Failed to rename the old SMS table (upgrade from rev 3).\n");
		goto rollback;
	}
	dbi_result_free(result);

	/* Create new SMS table with all the bells and whistles! */
	result = dbi_conn_query(conn, create_stmts[SCHEMA_SMS]);
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
		     "Failed to create a new SMS table (upgrade from rev 3).\n");
		goto rollback;
	}
	dbi_result_free(result);

	/* Cycle through old messages and convert them to the new format */
	result = dbi_conn_query(conn, "SELECT * FROM SMS_3");
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
		     "Failed fetch messages from the old SMS table (upgrade from rev 3).\n");
		goto rollback;
	}
	while (dbi_result_next_row(result)) {
		sms = sms_from_result_v3(result);
		if (db_sms_store(sms) != 0) {
			LOGP(DDB, LOGL_ERROR, "Failed to store message to the new SMS table(upgrade from rev 3).\n");
			sms_free(sms);
			dbi_result_free(result);
			goto rollback;
		}
		sms_free(sms);
	}
	dbi_result_free(result);

	/* Remove the temporary table */
	result = dbi_conn_query(conn, "DROP TABLE SMS_3");
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
		     "Failed to drop the old SMS table (upgrade from rev 3).\n");
		goto rollback;
	}
	dbi_result_free(result);

	/* We're done. Bump DB Meta revision to 4 */
	result = dbi_conn_query(conn,
				"UPDATE Meta "
				"SET value = '4' "
				"WHERE key = 'revision'");
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
		     "Failed to update DB schema revision (upgrade from rev 3).\n");
		goto rollback;
	}
	dbi_result_free(result);

	result = dbi_conn_query(conn, "COMMIT TRANSACTION");
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
			"Failed to commit the transaction (upgrade from rev 3)\n");
		return -EINVAL;
	} else {
		dbi_result_free(result);
	}

	/* Shrink DB file size by actually wiping out SMS_3 table data */
	result = dbi_conn_query(conn, "VACUUM");
	if (!result)
		LOGP(DDB, LOGL_ERROR,
			"VACUUM failed. Ignoring it (upgrade from rev 3).\n");
	else
		dbi_result_free(result);

	return 0;

rollback:
	result = dbi_conn_query(conn, "ROLLBACK TRANSACTION");
	if (!result)
		LOGP(DDB, LOGL_ERROR,
			"Rollback failed (upgrade from rev 3).\n");
	else
		dbi_result_free(result);
	return -EINVAL;
}

/* Just like v3, but there is a new message reference field for status reports,
 * that is set to zero for existing entries since there is no way we can infer
 * this.
 */
static struct gsm_sms *sms_from_result_v4(dbi_result result)
{
	struct gsm_sms *sms = sms_alloc();
	const unsigned char *user_data;
	const char *text, *addr;

	if (!sms)
		return NULL;

	sms->id = dbi_result_get_ulonglong(result, "id");

	sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
	sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
	sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
	sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
	sms->data_coding_scheme = dbi_result_get_ulonglong(result,
						  "data_coding_scheme");

	addr = dbi_result_get_string(result, "src_addr");
	osmo_strlcpy(sms->src.addr, addr, sizeof(sms->src.addr));
	sms->src.ton = dbi_result_get_ulonglong(result, "src_ton");
	sms->src.npi = dbi_result_get_ulonglong(result, "src_npi");

	addr = dbi_result_get_string(result, "dest_addr");
	osmo_strlcpy(sms->dst.addr, addr, sizeof(sms->dst.addr));
	sms->dst.ton = dbi_result_get_ulonglong(result, "dest_ton");
	sms->dst.npi = dbi_result_get_ulonglong(result, "dest_npi");

	sms->user_data_len = dbi_result_get_field_length(result, "user_data");
	user_data = dbi_result_get_binary(result, "user_data");
	if (sms->user_data_len > sizeof(sms->user_data))
		sms->user_data_len = (uint8_t) sizeof(sms->user_data);
	memcpy(sms->user_data, user_data, sms->user_data_len);

	text = dbi_result_get_string(result, "text");
	if (text)
		osmo_strlcpy(sms->text, text, sizeof(sms->text));
	return sms;
}

static int update_db_revision_4(void)
{
	dbi_result result;
	struct gsm_sms *sms;

	LOGP(DDB, LOGL_NOTICE, "Going to migrate from revision 4\n");

	result = dbi_conn_query(conn, "BEGIN EXCLUSIVE TRANSACTION");
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
			"Failed to begin transaction (upgrade from rev 4)\n");
		return -EINVAL;
	}
	dbi_result_free(result);

	/* Rename old SMS table to be able create a new one */
	result = dbi_conn_query(conn, "ALTER TABLE SMS RENAME TO SMS_4");
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
		     "Failed to rename the old SMS table (upgrade from rev 4).\n");
		goto rollback;
	}
	dbi_result_free(result);

	/* Create new SMS table with all the bells and whistles! */
	result = dbi_conn_query(conn, create_stmts[SCHEMA_SMS]);
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
		     "Failed to create a new SMS table (upgrade from rev 4).\n");
		goto rollback;
	}
	dbi_result_free(result);

	/* Cycle through old messages and convert them to the new format */
	result = dbi_conn_query(conn, "SELECT * FROM SMS_4");
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
		     "Failed fetch messages from the old SMS table (upgrade from rev 4).\n");
		goto rollback;
	}
	while (dbi_result_next_row(result)) {
		sms = sms_from_result_v4(result);
		if (db_sms_store(sms) != 0) {
			LOGP(DDB, LOGL_ERROR, "Failed to store message to the new SMS table(upgrade from rev 4).\n");
			sms_free(sms);
			dbi_result_free(result);
			goto rollback;
		}
		sms_free(sms);
	}
	dbi_result_free(result);

	/* Remove the temporary table */
	result = dbi_conn_query(conn, "DROP TABLE SMS_4");
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
		     "Failed to drop the old SMS table (upgrade from rev 4).\n");
		goto rollback;
	}
	dbi_result_free(result);

	/* We're done. Bump DB Meta revision to 4 */
	result = dbi_conn_query(conn,
				"UPDATE Meta "
				"SET value = '5' "
				"WHERE key = 'revision'");
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
		     "Failed to update DB schema revision (upgrade from rev 4).\n");
		goto rollback;
	}
	dbi_result_free(result);

	result = dbi_conn_query(conn, "COMMIT TRANSACTION");
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
			"Failed to commit the transaction (upgrade from rev 4)\n");
		return -EINVAL;
	} else {
		dbi_result_free(result);
	}

	/* Shrink DB file size by actually wiping out SMS_4 table data */
	result = dbi_conn_query(conn, "VACUUM");
	if (!result)
		LOGP(DDB, LOGL_ERROR,
			"VACUUM failed. Ignoring it (upgrade from rev 4).\n");
	else
		dbi_result_free(result);

	return 0;

rollback:
	result = dbi_conn_query(conn, "ROLLBACK TRANSACTION");
	if (!result)
		LOGP(DDB, LOGL_ERROR,
			"Rollback failed (upgrade from rev 4).\n");
	else
		dbi_result_free(result);
	return -EINVAL;
}

static int check_db_revision(void)
{
	dbi_result result;
	const char *rev_s;
	int db_rev = 0;

	/* Make a query */
	result = dbi_conn_query(conn,
		"SELECT value FROM Meta "
		"WHERE key = 'revision'");

	if (!result)
		return -EINVAL;

	if (!dbi_result_next_row(result)) {
		dbi_result_free(result);
		return -EINVAL;
	}

	/* Fetch the DB schema revision */
	rev_s = dbi_result_get_string(result, "value");
	if (!rev_s) {
		dbi_result_free(result);
		return -EINVAL;
	}

	if (!strcmp(rev_s, SCHEMA_REVISION)) {
		/* Everything is fine */
		dbi_result_free(result);
		return 0;
	}

	db_rev = atoi(rev_s);
	dbi_result_free(result);

	/* Incremental migration waterfall */
	switch (db_rev) {
	case 2:
		if (update_db_revision_2())
			goto error;
	case 3:
		if (update_db_revision_3())
			goto error;
	case 4:
		if (update_db_revision_4())
			goto error;

	/* The end of waterfall */
	break;
	default:
		LOGP(DDB, LOGL_FATAL,
			"Invalid database schema revision '%d'.\n", db_rev);
		return -EINVAL;
	}

	return 0;

error:
	LOGP(DDB, LOGL_FATAL, "Failed to update database "
		"from schema revision '%d'.\n", db_rev);
	return -EINVAL;
}

static int db_configure(void)
{
	dbi_result result;

	result = dbi_conn_query(conn,
				"PRAGMA synchronous = FULL");
	if (!result)
		return -EINVAL;

	dbi_result_free(result);
	return 0;
}

int db_init(const char *name)
{
	dbi_initialize(NULL);

	conn = dbi_conn_new("sqlite3");
	if (conn == NULL) {
		LOGP(DDB, LOGL_FATAL, "Failed to create connection.\n");
		return 1;
	}

	dbi_conn_error_handler( conn, db_error_func, NULL );

	/* MySQL
	dbi_conn_set_option(conn, "host", "localhost");
	dbi_conn_set_option(conn, "username", "your_name");
	dbi_conn_set_option(conn, "password", "your_password");
	dbi_conn_set_option(conn, "dbname", "your_dbname");
	dbi_conn_set_option(conn, "encoding", "UTF-8");
	*/

	/* SqLite 3 */
	db_basename = strdup(name);
	db_dirname = strdup(name);
	dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
	dbi_conn_set_option(conn, "dbname", basename(db_basename));

	if (dbi_conn_connect(conn) < 0)
		goto out_err;

	return 0;

out_err:
	free(db_dirname);
	free(db_basename);
	db_dirname = db_basename = NULL;
	return -1;
}


int db_prepare(void)
{
	dbi_result result;
	int i;

	for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
		result = dbi_conn_query(conn, create_stmts[i]);
		if (!result) {
			LOGP(DDB, LOGL_ERROR,
			     "Failed to create some table.\n");
			return 1;
		}
		dbi_result_free(result);
	}

	if (check_db_revision() < 0) {
		LOGP(DDB, LOGL_FATAL, "Database schema revision invalid, "
			"please update your database schema\n");
                return -1;
	}

	db_configure();

	return 0;
}

int db_fini(void)
{
	dbi_conn_close(conn);
	dbi_shutdown();

	free(db_dirname);
	free(db_basename);
	return 0;
}

struct gsm_subscriber *db_create_subscriber(const char *imsi, uint64_t smin,
					    uint64_t smax, bool alloc_exten)
{
	dbi_result result;
	struct gsm_subscriber *subscr;

	/* Is this subscriber known in the db? */
	subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
	if (subscr) {
		subscr_put(subscr);
		return NULL;
	}

	subscr = subscr_alloc();
	if (!subscr)
		return NULL;
	subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
	result = dbi_conn_queryf(conn,
		"INSERT INTO Subscriber "
		"(imsi, created, updated) "
		"VALUES "
		"(%s, datetime('now'), datetime('now')) ",
		imsi
	);
	if (!result) {
		LOGP(DDB, LOGL_ERROR, "Failed to create Subscriber by IMSI.\n");
		subscr_put(subscr);
		return NULL;
	}
	subscr->id = dbi_conn_sequence_last(conn, NULL);
	osmo_strlcpy(subscr->imsi, imsi, sizeof(subscr->imsi));
	dbi_result_free(result);
	LOGP(DDB, LOGL_INFO, "New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
	if (alloc_exten)
		db_subscriber_alloc_exten(subscr, smin, smax);
	return subscr;
}

osmo_static_assert(sizeof(unsigned char) == sizeof(struct gsm48_classmark1), classmark1_size);

static int get_equipment_by_subscr(struct gsm_subscriber *subscr)
{
	dbi_result result;
	const char *string;
	unsigned char cm1;
	const unsigned char *cm2, *cm3;
	struct gsm_equipment *equip = &subscr->equipment;

	result = dbi_conn_queryf(conn,
		"SELECT Equipment.* "
			"FROM Equipment JOIN EquipmentWatch ON "
				"EquipmentWatch.equipment_id=Equipment.id "
			"WHERE EquipmentWatch.subscriber_id = %llu "
			"ORDER BY EquipmentWatch.updated DESC", subscr->id);
	if (!result)
		return -EIO;

	if (!dbi_result_next_row(result)) {
		dbi_result_free(result);
		return -ENOENT;
	}

	equip->id = dbi_result_get_ulonglong(result, "id");

	string = dbi_result_get_string(result, "imei");
	if (string)
		osmo_strlcpy(equip->imei, string, sizeof(equip->imei));

	string = dbi_result_get_string(result, "classmark1");
	if (string) {
		cm1 = atoi(string) & 0xff;
		memcpy(&equip->classmark1, &cm1, sizeof(equip->classmark1));
	}

	equip->classmark2_len = dbi_result_get_field_length(result, "classmark2");
	cm2 = dbi_result_get_binary(result, "classmark2");
	if (equip->classmark2_len > sizeof(equip->classmark2))
		equip->classmark2_len = sizeof(equip->classmark2);
	if (cm2)
		memcpy(equip->classmark2, cm2, equip->classmark2_len);

	equip->classmark3_len = dbi_result_get_field_length(result, "classmark3");
	cm3 = dbi_result_get_binary(result, "classmark3");
	if (equip->classmark3_len > sizeof(equip->classmark3))
		equip->classmark3_len = sizeof(equip->classmark3);
	if (cm3)
		memcpy(equip->classmark3, cm3, equip->classmark3_len);

	dbi_result_free(result);

	return 0;
}

int db_get_authinfo_for_subscr(struct gsm_auth_info *ainfo,
                               struct gsm_subscriber *subscr)
{
	dbi_result result;
	const unsigned char *a3a8_ki;

	result = dbi_conn_queryf(conn,
			"SELECT * FROM AuthKeys WHERE subscriber_id=%llu",
			 subscr->id);
	if (!result)
		return -EIO;

	if (!dbi_result_next_row(result)) {
		dbi_result_free(result);
		return -ENOENT;
	}

	ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id");
	ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki");
	a3a8_ki = dbi_result_get_binary(result, "a3a8_ki");
	if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki))
		ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki);
	memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len);

	dbi_result_free(result);

	return 0;
}

int db_sync_authinfo_for_subscr(struct gsm_auth_info *ainfo,
                                struct gsm_subscriber *subscr)
{
	dbi_result result;
	struct gsm_auth_info ainfo_old;
	int rc, upd;
	unsigned char *ki_str;

	/* Deletion ? */
	if (ainfo == NULL) {
		result = dbi_conn_queryf(conn,
			"DELETE FROM AuthKeys WHERE subscriber_id=%llu",
			subscr->id);

		if (!result)
			return -EIO;

		dbi_result_free(result);

		return 0;
	}

	/* Check if already existing */
	rc = db_get_authinfo_for_subscr(&ainfo_old, subscr);
	if (rc && rc != -ENOENT)
		return rc;
	upd = rc ? 0 : 1;

	/* Update / Insert */
	dbi_conn_quote_binary_copy(conn,
		ainfo->a3a8_ki, ainfo->a3a8_ki_len, &ki_str);

	if (!upd) {
		result = dbi_conn_queryf(conn,
				"INSERT INTO AuthKeys "
				"(subscriber_id, algorithm_id, a3a8_ki) "
				"VALUES (%llu, %u, %s)",
				subscr->id, ainfo->auth_algo, ki_str);
	} else {
		result = dbi_conn_queryf(conn,
				"UPDATE AuthKeys "
				"SET algorithm_id=%u, a3a8_ki=%s "
				"WHERE subscriber_id=%llu",
				ainfo->auth_algo, ki_str, subscr->id);
	}

	free(ki_str);

	if (!result)
		return -EIO;

	dbi_result_free(result);

	return 0;
}

int db_get_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
                                    struct gsm_subscriber *subscr)
{
	dbi_result result;
	int len;
	const unsigned char *blob;

	result = dbi_conn_queryf(conn,
			"SELECT * FROM AuthLastTuples WHERE subscriber_id=%llu",
			subscr->id);
	if (!result)
		return -EIO;

	if (!dbi_result_next_row(result)) {
		dbi_result_free(result);
		return -ENOENT;
	}

	memset(atuple, 0, sizeof(*atuple));

	atuple->use_count = dbi_result_get_ulonglong(result, "use_count");
	atuple->key_seq = dbi_result_get_ulonglong(result, "key_seq");

	len = dbi_result_get_field_length(result, "rand");
	if (len != sizeof(atuple->vec.rand))
		goto err_size;

	blob = dbi_result_get_binary(result, "rand");
	memcpy(atuple->vec.rand, blob, len);

	len = dbi_result_get_field_length(result, "sres");
	if (len != sizeof(atuple->vec.sres))
		goto err_size;

	blob = dbi_result_get_binary(result, "sres");
	memcpy(atuple->vec.sres, blob, len);

	len = dbi_result_get_field_length(result, "kc");
	if (len != sizeof(atuple->vec.kc))
		goto err_size;

	blob = dbi_result_get_binary(result, "kc");
	memcpy(atuple->vec.kc, blob, len);

	dbi_result_free(result);

	return 0;

err_size:
	dbi_result_free(result);
	return -EIO;
}

int db_sync_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
                                     struct gsm_subscriber *subscr)
{
	dbi_result result;
	int rc, upd;
	struct gsm_auth_tuple atuple_old;
	unsigned char *rand_str, *sres_str, *kc_str;

	/* Deletion ? */
	if (atuple == NULL) {
		result = dbi_conn_queryf(conn,
			"DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
			subscr->id);

		if (!result)
			return -EIO;

		dbi_result_free(result);

		return 0;
	}

	/* Check if already existing */
	rc = db_get_lastauthtuple_for_subscr(&atuple_old, subscr);
	if (rc && rc != -ENOENT)
		return rc;
	upd = rc ? 0 : 1;

	/* Update / Insert */
	dbi_conn_quote_binary_copy(conn,
		atuple->vec.rand, sizeof(atuple->vec.rand), &rand_str);
	dbi_conn_quote_binary_copy(conn,
		atuple->vec.sres, sizeof(atuple->vec.sres), &sres_str);
	dbi_conn_quote_binary_copy(conn,
		atuple->vec.kc, sizeof(atuple->vec.kc), &kc_str);

	if (!upd) {
		result = dbi_conn_queryf(conn,
				"INSERT INTO AuthLastTuples "
				"(subscriber_id, issued, use_count, "
				 "key_seq, rand, sres, kc) "
				"VALUES (%llu, datetime('now'), %u, "
				 "%u, %s, %s, %s ) ",
				subscr->id, atuple->use_count, atuple->key_seq,
				rand_str, sres_str, kc_str);
	} else {
		char *issued = atuple->key_seq == atuple_old.key_seq ?
					"issued" : "datetime('now')";
		result = dbi_conn_queryf(conn,
				"UPDATE AuthLastTuples "
				"SET issued=%s, use_count=%u, "
				 "key_seq=%u, rand=%s, sres=%s, kc=%s "
				"WHERE subscriber_id = %llu",
				issued, atuple->use_count, atuple->key_seq,
				rand_str, sres_str, kc_str, subscr->id);
	}

	free(rand_str);
	free(sres_str);
	free(kc_str);

	if (!result)
		return -EIO;

	dbi_result_free(result);

	return 0;
}

static void db_set_from_query(struct gsm_subscriber *subscr, dbi_conn result)
{
	const char *string;
	string = dbi_result_get_string(result, "imsi");
	if (string)
		osmo_strlcpy(subscr->imsi, string, sizeof(subscr->imsi));

	string = dbi_result_get_string(result, "tmsi");
	if (string)
		subscr->tmsi = tmsi_from_string(string);

	string = dbi_result_get_string(result, "name");
	if (string)
		osmo_strlcpy(subscr->name, string, sizeof(subscr->name));

	string = dbi_result_get_string(result, "extension");
	if (string)
		osmo_strlcpy(subscr->extension, string, sizeof(subscr->extension));

	subscr->lac = dbi_result_get_ulonglong(result, "lac");

	if (!dbi_result_field_is_null(result, "expire_lu"))
		subscr->expire_lu = dbi_result_get_datetime(result, "expire_lu");
	else
		subscr->expire_lu = GSM_SUBSCRIBER_NO_EXPIRATION;

	subscr->authorized = dbi_result_get_ulonglong(result, "authorized");

}

#define BASE_QUERY "SELECT * FROM Subscriber "
struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field,
					 const char *id)
{
	dbi_result result;
	char *quoted;
	struct gsm_subscriber *subscr;

	switch (field) {
	case GSM_SUBSCRIBER_IMSI:
		dbi_conn_quote_string_copy(conn, id, &quoted);
		result = dbi_conn_queryf(conn,
			BASE_QUERY
			"WHERE imsi = %s ",
			quoted
		);
		free(quoted);
		break;
	case GSM_SUBSCRIBER_TMSI:
		dbi_conn_quote_string_copy(conn, id, &quoted);
		result = dbi_conn_queryf(conn,
			BASE_QUERY
			"WHERE tmsi = %s ",
			quoted
		);
		free(quoted);
		break;
	case GSM_SUBSCRIBER_EXTENSION:
		dbi_conn_quote_string_copy(conn, id, &quoted);
		result = dbi_conn_queryf(conn,
			BASE_QUERY
			"WHERE extension = %s ",
			quoted
		);
		free(quoted);
		break;
	case GSM_SUBSCRIBER_ID:
		dbi_conn_quote_string_copy(conn, id, &quoted);
		result = dbi_conn_queryf(conn,
			BASE_QUERY
			"WHERE id = %s ", quoted);
		free(quoted);
		break;
	default:
		LOGP(DDB, LOGL_NOTICE, "Unknown query selector for Subscriber.\n");
		return NULL;
	}
	if (!result) {
		LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber.\n");
		return NULL;
	}
	if (!dbi_result_next_row(result)) {
		DEBUGP(DDB, "Failed to find the Subscriber. '%u' '%s'\n",
			field, id);
		dbi_result_free(result);
		return NULL;
	}

	subscr = subscr_alloc();
	subscr->id = dbi_result_get_ulonglong(result, "id");

	db_set_from_query(subscr, result);
	DEBUGP(DDB, "Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %x, EXTEN '%s', LAC %hu, AUTH %u\n",
		subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
		subscr->lac, subscr->authorized);
	dbi_result_free(result);

	get_equipment_by_subscr(subscr);

	return subscr;
}

int db_subscriber_update(struct gsm_subscriber *subscr)
{
	char buf[32];
	dbi_result result;

	/* Copy the id to a string as queryf with %llu is failing */
	sprintf(buf, "%llu", subscr->id);
	result = dbi_conn_queryf(conn,
			BASE_QUERY
			"WHERE id = %s", buf);

	if (!result) {
		LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber: %llu\n", subscr->id);
		return -EIO;
	}
	if (!dbi_result_next_row(result)) {
		DEBUGP(DDB, "Failed to find the Subscriber. %llu\n",
			subscr->id);
		dbi_result_free(result);
		return -EIO;
	}

	db_set_from_query(subscr, result);
	dbi_result_free(result);
	get_equipment_by_subscr(subscr);

	return 0;
}

int db_sync_subscriber(struct gsm_subscriber *subscriber)
{
	dbi_result result;
	char tmsi[14];
	char *q_tmsi, *q_name, *q_extension;

	dbi_conn_quote_string_copy(conn, 
				   subscriber->name, &q_name);
	if (subscriber->extension[0] != '\0')
		dbi_conn_quote_string_copy(conn,
					   subscriber->extension, &q_extension);
	else
		q_extension = strdup("NULL");
	
	if (subscriber->tmsi != GSM_RESERVED_TMSI) {
		sprintf(tmsi, "%u", subscriber->tmsi);
		dbi_conn_quote_string_copy(conn,
				   tmsi,
				   &q_tmsi);
	} else
		q_tmsi = strdup("NULL");

	if (subscriber->expire_lu == GSM_SUBSCRIBER_NO_EXPIRATION) {
		result = dbi_conn_queryf(conn,
			"UPDATE Subscriber "
			"SET updated = datetime('now'), "
			"name = %s, "
			"extension = %s, "
			"authorized = %i, "
			"tmsi = %s, "
			"lac = %i, "
			"expire_lu = NULL "
			"WHERE imsi = %s ",
			q_name,
			q_extension,
			subscriber->authorized,
			q_tmsi,
			subscriber->lac,
			subscriber->imsi);
	} else {
		result = dbi_conn_queryf(conn,
			"UPDATE Subscriber "
			"SET updated = datetime('now'), "
			"name = %s, "
			"extension = %s, "
			"authorized = %i, "
			"tmsi = %s, "
			"lac = %i, "
			"expire_lu = datetime(%i, 'unixepoch') "
			"WHERE imsi = %s ",
			q_name,
			q_extension,
			subscriber->authorized,
			q_tmsi,
			subscriber->lac,
			(int) subscriber->expire_lu,
			subscriber->imsi);
	}

	free(q_tmsi);
	free(q_name);
	free(q_extension);

	if (!result) {
		LOGP(DDB, LOGL_ERROR, "Failed to update Subscriber (by IMSI).\n");
		return 1;
	}

	dbi_result_free(result);

	return 0;
}

int db_subscriber_delete(struct gsm_subscriber *subscr)
{
	dbi_result result;

	result = dbi_conn_queryf(conn,
			"DELETE FROM AuthKeys WHERE subscriber_id=%llu",
			subscr->id);
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
			"Failed to delete Authkeys for %llu\n", subscr->id);
		return -1;
	}
	dbi_result_free(result);

	result = dbi_conn_queryf(conn,
			"DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
			subscr->id);
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
			"Failed to delete AuthLastTuples for %llu\n", subscr->id);
		return -1;
	}
	dbi_result_free(result);

	result = dbi_conn_queryf(conn,
			"DELETE FROM AuthToken WHERE subscriber_id=%llu",
			subscr->id);
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
			"Failed to delete AuthToken for %llu\n", subscr->id);
		return -1;
	}
	dbi_result_free(result);

	result = dbi_conn_queryf(conn,
			"DELETE FROM EquipmentWatch WHERE subscriber_id=%llu",
			subscr->id);
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
			"Failed to delete EquipmentWatch for %llu\n", subscr->id);
		return -1;
	}
	dbi_result_free(result);

	if (subscr->extension[0] != '\0') {
		result = dbi_conn_queryf(conn,
			    "DELETE FROM SMS WHERE src_addr=%s OR dest_addr=%s",
					 subscr->extension, subscr->extension);
		if (!result) {
			LOGP(DDB, LOGL_ERROR,
			     "Failed to delete SMS for %llu\n", subscr->id);
			return -1;
		}
		dbi_result_free(result);
	}

	result = dbi_conn_queryf(conn,
			"DELETE FROM VLR WHERE subscriber_id=%llu",
			subscr->id);
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
			"Failed to delete VLR for %llu\n", subscr->id);
		return -1;
	}
	dbi_result_free(result);

	result = dbi_conn_queryf(conn,
			"DELETE FROM ApduBlobs WHERE subscriber_id=%llu",
			subscr->id);
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
			"Failed to delete ApduBlobs for %llu\n", subscr->id);
		return -1;
	}
	dbi_result_free(result);

	result = dbi_conn_queryf(conn,
			"DELETE FROM Subscriber WHERE id=%llu",
			subscr->id);
	if (!result) {
		LOGP(DDB, LOGL_ERROR,
			"Failed to delete Subscriber for %llu\n", subscr->id);
		return -1;
	}
	dbi_result_free(result);

	return 0;
}

/**
 * List all the authorized and non-expired subscribers. The callback will
 * be called one by one. The subscr argument is not fully initialize and
 * subscr_get/subscr_put must not be called. The passed in pointer will be
 * deleted after the callback by the database call.
 */
int db_subscriber_list_active(void (*cb)(struct gsm_subscriber*,void*), void *closure)
{
	dbi_result result;

	result = dbi_conn_query(conn,
		       "SELECT * from Subscriber WHERE LAC != 0 AND authorized = 1");
	if (!result) {
		LOGP(DDB, LOGL_ERROR, "Failed to list active subscribers\n");
		return -1;
	}

	while (dbi_result_next_row(result)) {
		struct gsm_subscriber *subscr;

		subscr = subscr_alloc();
		subscr->id = dbi_result_get_ulonglong(result, "id");
		db_set_from_query(subscr, result);
		cb(subscr, closure);
		OSMO_ASSERT(subscr->use_count == 1);
		llist_del(&subscr->entry);
		talloc_free(subscr);
	}

	dbi_result_free(result);
	return 0;
}

int db_sync_equipment(struct gsm_equipment *equip)
{
	dbi_result result;
	unsigned char *cm2, *cm3;
	char *q_imei;
	uint8_t classmark1;

	memcpy(&classmark1, &equip->classmark1, sizeof(classmark1));
	DEBUGP(DDB, "Sync Equipment IMEI=%s, classmark1=%02x",
		equip->imei, classmark1);
	if (equip->classmark2_len)
		DEBUGPC(DDB, ", classmark2=%s",
			osmo_hexdump(equip->classmark2, equip->classmark2_len));
	if (equip->classmark3_len)
		DEBUGPC(DDB, ", classmark3=%s",
			osmo_hexdump(equip->classmark3, equip->classmark3_len));
	DEBUGPC(DDB, "\n");

	dbi_conn_quote_binary_copy(conn, equip->classmark2,
				   equip->classmark2_len, &cm2);
	dbi_conn_quote_binary_copy(conn, equip->classmark3,
				   equip->classmark3_len, &cm3);
	dbi_conn_quote_string_copy(conn, equip->imei, &q_imei);

	result = dbi_conn_queryf(conn,
		"UPDATE Equipment SET "
			"updated = datetime('now'), "
			"classmark1 = %u, "
			"classmark2 = %s, "
			"classmark3 = %s "
		"WHERE imei = %s ",
		classmark1, cm2, cm3, q_imei);

	free(cm2);
	free(cm3);
	free(q_imei);

	if (!result) {
		LOGP(DDB, LOGL_ERROR, "Failed to update Equipment\n");
		return -EIO;
	}

	dbi_result_free(result);
	return 0;
}

int db_subscriber_expire(void *priv, void (*callback)(void *priv, long long unsigned int id))
{
	dbi_result result;

	result = dbi_conn_query(conn,
			"SELECT id "
			"FROM Subscriber "
			"WHERE lac != 0 AND "
				"( expire_lu is NOT NULL "
				"AND expire_lu < datetime('now') ) "
			"LIMIT 1");
	if (!result) {
		LOGP(DDB, LOGL_ERROR, "Failed to get expired subscribers\n");
		return -EIO;
	}

	while (dbi_result_next_row(result))
		callback(priv, dbi_result_get_ulonglong(result, "id"));

	dbi_result_free(result);
	return 0;
}

int db_subscriber_alloc_tmsi(struct gsm_subscriber *subscriber)
{
	dbi_result result = NULL;
	char tmsi[14];
	char *tmsi_quoted;

	for (;;) {
		if (RAND_bytes((uint8_t *) &subscriber->tmsi, sizeof(subscriber->tmsi)) != 1) {
			LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n");
			return 1;
		}
		if (subscriber->tmsi == GSM_RESERVED_TMSI)
			continue;

		sprintf(tmsi, "%u", subscriber->tmsi);
		dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted);
		result = dbi_conn_queryf(conn,
			"SELECT * FROM Subscriber "
			"WHERE tmsi = %s ",
			tmsi_quoted);

		free(tmsi_quoted);

		if (!result) {
			LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
				"while allocating new TMSI.\n");
			return 1;
		}
		if (dbi_result_get_numrows(result)) {
			dbi_result_free(result);
			continue;
		}
		if (!dbi_result_next_row(result)) {
			dbi_result_free(result);
			DEBUGP(DDB, "Allocated TMSI %u for IMSI %s.\n",
				subscriber->tmsi, subscriber->imsi);
			return db_sync_subscriber(subscriber);
		}
		dbi_result_free(result);
	}
	return 0;
}

int db_subscriber_alloc_exten(struct gsm_subscriber *subscriber, uint64_t smin,
			      uint64_t smax)
{
	dbi_result result = NULL;
	uint64_t try;

	for (;;) {
		try = (rand() % (smax - smin + 1) + smin);
		result = dbi_conn_queryf(conn,
			"SELECT * FROM Subscriber "
			"WHERE extension = %"PRIu64,
			try
		);
		if (!result) {
			LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
				"while allocating new extension.\n");
			return 1;
		}
		if (dbi_result_get_numrows(result)){
			dbi_result_free(result);
			continue;
		}
		if (!dbi_result_next_row(result)) {
			dbi_result_free(result);
			break;
		}
		dbi_result_free(result);
	}
	sprintf(subscriber->extension, "%"PRIu64, try);
	DEBUGP(DDB, "Allocated extension %"PRIu64 " for IMSI %s.\n", try, subscriber->imsi);
	return db_sync_subscriber(subscriber);
}
/*
 * try to allocate a new unique token for this subscriber and return it
 * via a parameter. if the subscriber already has a token, return
 * an error.
 */

int db_subscriber_alloc_token(struct gsm_subscriber *subscriber, uint32_t *token)
{
	dbi_result result;
	uint32_t try;

	for (;;) {
		if (RAND_bytes((uint8_t *) &try, sizeof(try)) != 1) {
			LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n");
			return 1;
		}
		if (!try) /* 0 is an invalid token */
			continue;
		result = dbi_conn_queryf(conn,
			"SELECT * FROM AuthToken "
			"WHERE subscriber_id = %llu OR token = \"%08X\" ",
			subscriber->id, try);
		if (!result) {
			LOGP(DDB, LOGL_ERROR, "Failed to query AuthToken "
				"while allocating new token.\n");
			return 1;
		}
		if (dbi_result_get_numrows(result)) {
			dbi_result_free(result);
			continue;
		}
		if (!dbi_result_next_row(result)) {
			dbi_result_free(result);
			break;
		}
		dbi_result_free(result);
	}
	result = dbi_conn_queryf(conn,
		"INSERT INTO AuthToken "
		"(subscriber_id, created, token) "
		"VALUES "
		"(%llu, datetime('now'), \"%08X\") ",
		subscriber->id, try);
	if (!result) {
		LOGP(DDB, LOGL_ERROR, "Failed to create token %08X for "
			"IMSI %s.\n", try, subscriber->imsi);
		return 1;
	}
	dbi_result_free(result);
	*token = try;
	DEBUGP(DDB, "Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);

	return 0;
}

int db_subscriber_assoc_imei(struct gsm_subscriber *subscriber, char imei[GSM23003_IMEISV_NUM_DIGITS])
{
	unsigned long long equipment_id, watch_id;
	dbi_result result;

	osmo_strlcpy(subscriber->equipment.imei, imei, sizeof(subscriber->equipment.imei));

	result = dbi_conn_queryf(conn,
		"INSERT OR IGNORE INTO Equipment "
		"(imei, created, updated) "
		"VALUES "
		"(%s, datetime('now'), datetime('now')) ",
		imei);
	if (!result) {
		LOGP(DDB, LOGL_ERROR, "Failed to create Equipment by IMEI.\n");
		return 1;
	}

	equipment_id = 0;
	if (dbi_result_get_numrows_affected(result)) {
		equipment_id = dbi_conn_sequence_last(conn, NULL);
	}
	dbi_result_free(result);

	if (equipment_id)
		DEBUGP(DDB, "New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
	else {
		result = dbi_conn_queryf(conn,
			"SELECT id FROM Equipment "
			"WHERE imei = %s ",
			imei
		);
		if (!result) {
			LOGP(DDB, LOGL_ERROR, "Failed to query Equipment by IMEI.\n");
			return 1;
		}
		if (!dbi_result_next_row(result)) {
			LOGP(DDB, LOGL_ERROR, "Failed to find the Equipment.\n");
			dbi_result_free(result);
			return 1;
		}
		equipment_id = dbi_result_get_ulonglong(result, "id");
		dbi_result_free(result);
	}

	result = dbi_conn_queryf(conn,
		"INSERT OR IGNORE INTO EquipmentWatch "
		"(subscriber_id, equipment_id, created, updated) "
		"VALUES "
		"(%llu, %llu, datetime('now'), datetime('now')) ",
		subscriber->id, equipment_id);
	if (!result) {
		LOGP(DDB, LOGL_ERROR, "Failed to create EquipmentWatch.\n");
		return 1;
	}

	watch_id = 0;
	if (dbi_result_get_numrows_affected(result))
		watch_id = dbi_conn_sequence_last(conn, NULL);

	dbi_result_free(result);
	if (watch_id)
		DEBUGP(DDB, "New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
			equipment_id, subscriber->imsi, imei);
	else {
		result = dbi_conn_queryf(conn,
			"UPDATE EquipmentWatch "
			"SET updated = datetime('now') "
			"WHERE subscriber_id = %llu AND equipment_id = %llu ",
			subscriber->id, equipment_id);
		if (!result) {
			LOGP(DDB, LOGL_ERROR, "Failed to update EquipmentWatch.\n");
			return 1;
		}
		dbi_result_free(result);
		DEBUGP(DDB, "Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
			equipment_id, subscriber->imsi, imei);
	}

	return 0;
}

/* store an [unsent] SMS to the database */
int db_sms_store(struct gsm_sms *sms)
{
	dbi_result result;
	char *q_text, *q_daddr, *q_saddr;
	unsigned char *q_udata;
	char *validity_timestamp = "2222-2-2";

	/* FIXME: generate validity timestamp based on validity_minutes */

	dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
	dbi_conn_quote_string_copy(conn, (char *)sms->dst.addr, &q_daddr);
	dbi_conn_quote_string_copy(conn, (char *)sms->src.addr, &q_saddr);
	dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
				   &q_udata);

	/* FIXME: correct validity period */
	result = dbi_conn_queryf(conn,
		"INSERT INTO SMS "
		"(created, valid_until, "
		 "reply_path_req, status_rep_req, is_report, "
		 "msg_ref, protocol_id, data_coding_scheme, "
		 "ud_hdr_ind, "
		 "user_data, text, "
		 "dest_addr, dest_ton, dest_npi, "
		 "src_addr, src_ton, src_npi) VALUES "
		"(datetime('now'), %u, "
		"%u, %u, %u, "
		"%u, %u, %u, "
		"%u, "
		"%s, %s, "
		"%s, %u, %u, "
		"%s, %u, %u)",
		validity_timestamp,
		sms->reply_path_req, sms->status_rep_req, sms->is_report,
		sms->msg_ref, sms->protocol_id, sms->data_coding_scheme,
		sms->ud_hdr_ind,
		q_udata, q_text,
		q_daddr, sms->dst.ton, sms->dst.npi,
		q_saddr, sms->src.ton, sms->src.npi);
	free(q_text);
	free(q_udata);
	free(q_daddr);
	free(q_saddr);

	if (!result)
		return -EIO;

	dbi_result_free(result);
	return 0;
}

static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
{
	struct gsm_sms *sms = sms_alloc();
	const char *text, *daddr, *saddr;
	const unsigned char *user_data;

	if (!sms)
		return NULL;

	sms->id = dbi_result_get_ulonglong(result, "id");

	/* FIXME: validity */
	/* FIXME: those should all be get_uchar, but sqlite3 is braindead */
	sms->created = dbi_result_get_datetime(result, "created");
	sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
	sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
	sms->is_report = dbi_result_get_ulonglong(result, "is_report");
	sms->msg_ref = dbi_result_get_ulonglong(result, "msg_ref");
	sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
	sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
	sms->data_coding_scheme = dbi_result_get_ulonglong(result,
						  "data_coding_scheme");

	sms->dst.npi = dbi_result_get_ulonglong(result, "dest_npi");
	sms->dst.ton = dbi_result_get_ulonglong(result, "dest_ton");
	daddr = dbi_result_get_string(result, "dest_addr");
	if (daddr)
		osmo_strlcpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
	sms->receiver = subscr_get_by_extension(net->subscr_group, sms->dst.addr);

	sms->src.npi = dbi_result_get_ulonglong(result, "src_npi");
	sms->src.ton = dbi_result_get_ulonglong(result, "src_ton");
	saddr = dbi_result_get_string(result, "src_addr");
	if (saddr)
		osmo_strlcpy(sms->src.addr, saddr, sizeof(sms->src.addr));

	sms->user_data_len = dbi_result_get_field_length(result, "user_data");
	user_data = dbi_result_get_binary(result, "user_data");
	if (sms->user_data_len > sizeof(sms->user_data))
		sms->user_data_len = (uint8_t) sizeof(sms->user_data);
	memcpy(sms->user_data, user_data, sms->user_data_len);

	text = dbi_result_get_string(result, "text");
	if (text)
		osmo_strlcpy(sms->text, text, sizeof(sms->text));
	return sms;
}

struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id)
{
	dbi_result result;
	struct gsm_sms *sms;

	result = dbi_conn_queryf(conn,
		"SELECT * FROM SMS WHERE SMS.id = %llu", id);
	if (!result)
		return NULL;

	if (!dbi_result_next_row(result)) {
		dbi_result_free(result);
		return NULL;
	}

	sms = sms_from_result(net, result);

	dbi_result_free(result);

	return sms;
}

/* retrieve the next unsent SMS with ID >= min_id */
struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, unsigned long long min_id)
{
	dbi_result result;
	struct gsm_sms *sms;

	result = dbi_conn_queryf(conn,
		"SELECT SMS.* "
			"FROM SMS JOIN Subscriber ON "
				"SMS.dest_addr = Subscriber.extension "
			"WHERE SMS.id >= %llu AND SMS.sent IS NULL "
				"AND Subscriber.lac > 0 "
			"ORDER BY SMS.id LIMIT 1",
		min_id);
	if (!result)
		return NULL;

	if (!dbi_result_next_row(result)) {
		dbi_result_free(result);
		return NULL;
	}

	sms = sms_from_result(net, result);

	dbi_result_free(result);

	return sms;
}

struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net,
					    unsigned long long min_subscr_id,
					    unsigned int failed)
{
	dbi_result result;
	struct gsm_sms *sms;

	result = dbi_conn_queryf(conn,
		"SELECT SMS.* "
			"FROM SMS JOIN Subscriber ON "
				"SMS.dest_addr = Subscriber.extension "
			"WHERE Subscriber.id >= %llu AND SMS.sent IS NULL "
				"AND Subscriber.lac > 0 AND SMS.deliver_attempts < %u "
			"ORDER BY Subscriber.id, SMS.id LIMIT 1",
		min_subscr_id, failed);
	if (!result)
		return NULL;

	if (!dbi_result_next_row(result)) {
		dbi_result_free(result);
		return NULL;
	}

	sms = sms_from_result(net, result);

	dbi_result_free(result);

	return sms;
}

/* retrieve the next unsent SMS for a given subscriber */
struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
{
	dbi_result result;
	struct gsm_sms *sms;

	result = dbi_conn_queryf(conn,
		"SELECT SMS.* "
			"FROM SMS JOIN Subscriber ON "
				"SMS.dest_addr = Subscriber.extension "
			"WHERE Subscriber.id = %llu AND SMS.sent IS NULL "
				"AND Subscriber.lac > 0 "
			"ORDER BY SMS.id LIMIT 1",
		subscr->id);
	if (!result)
		return NULL;

	if (!dbi_result_next_row(result)) {
		dbi_result_free(result);
		return NULL;
	}

	sms = sms_from_result(subscr->group->net, result);

	dbi_result_free(result);

	return sms;
}

/* mark a given SMS as delivered */
int db_sms_mark_delivered(struct gsm_sms *sms)
{
	dbi_result result;

	result = dbi_conn_queryf(conn,
		"UPDATE SMS "
		"SET sent = datetime('now') "
		"WHERE id = %llu", sms->id);
	if (!result) {
		LOGP(DDB, LOGL_ERROR, "Failed to mark SMS %llu as sent.\n", sms->id);
		return 1;
	}

	dbi_result_free(result);
	return 0;
}

/* increase the number of attempted deliveries */
int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
{
	dbi_result result;

	result = dbi_conn_queryf(conn,
		"UPDATE SMS "
		"SET deliver_attempts = deliver_attempts + 1 "
		"WHERE id = %llu", sms->id);
	if (!result) {
		LOGP(DDB, LOGL_ERROR, "Failed to inc deliver attempts for "
			"SMS %llu.\n", sms->id);
		return 1;
	}

	dbi_result_free(result);
	return 0;
}

int db_apdu_blob_store(struct gsm_subscriber *subscr,
			uint8_t apdu_id_flags, uint8_t len,
			uint8_t *apdu)
{
	dbi_result result;
	unsigned char *q_apdu;

	dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);

	result = dbi_conn_queryf(conn,
		"INSERT INTO ApduBlobs "
		"(created,subscriber_id,apdu_id_flags,apdu) VALUES "
		"(datetime('now'),%llu,%u,%s)",
		subscr->id, apdu_id_flags, q_apdu);

	free(q_apdu);

	if (!result)
		return -EIO;

	dbi_result_free(result);
	return 0;
}

int db_store_counter(struct osmo_counter *ctr)
{
	dbi_result result;
	char *q_name;

	dbi_conn_quote_string_copy(conn, ctr->name, &q_name);

	result = dbi_conn_queryf(conn,
		"INSERT INTO Counters "
		"(timestamp,name,value) VALUES "
		"(datetime('now'),%s,%lu)", q_name, ctr->value);

	free(q_name);

	if (!result)
		return -EIO;

	dbi_result_free(result);
	return 0;
}

static int db_store_rate_ctr(struct rate_ctr_group *ctrg, unsigned int num,
			     char *q_prefix)
{
	dbi_result result;
	char *q_name;

	dbi_conn_quote_string_copy(conn, ctrg->desc->ctr_desc[num].name,
				   &q_name);

	result = dbi_conn_queryf(conn,
		"Insert INTO RateCounters "
		"(timestamp,name,idx,value) VALUES "
		"(datetime('now'),%s.%s,%u,%"PRIu64")",
		q_prefix, q_name, ctrg->idx, ctrg->ctr[num].current);

	free(q_name);

	if (!result)
		return -EIO;

	dbi_result_free(result);
	return 0;
}

int db_store_rate_ctr_group(struct rate_ctr_group *ctrg)
{
	unsigned int i;
	char *q_prefix;

	dbi_conn_quote_string_copy(conn, ctrg->desc->group_name_prefix, &q_prefix);

	for (i = 0; i < ctrg->desc->num_ctr; i++)
		db_store_rate_ctr(ctrg, i, q_prefix);

	free(q_prefix);

	return 0;
}