OpenDNSSEC-enforcer  1.3.15
database_init_rundown.c
Go to the documentation of this file.
1 /*
2  * $Id: database_init_rundown.c 2120 2009-10-07 08:40:35Z sion $
3  *
4  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 /*+
30  * database_init_rundown.c - Database Access Initialization
31  *
32  * Description:
33  * Contains the functions needed to initialize and run down the
34  * database access module.
35 -*/
36 
37 #include "ksm/database.h"
38 #include "ksm/dbsdef.h"
39 #include "ksm/dbsmsg.h"
40 #include "ksm/kmedef.h"
41 #include "ksm/message.h"
42 
43 /* Flag as to whether the database modules have been initialized */
44 
45 static int m_initialized = 0; /* Default is not */
46 
47 
48 
49 /*+
50  * DbInit - Initialize Database Access
51  *
52  * Description:
53  * Initializes the Database Modules if not already initialized.
54  *
55  * Arguments:
56  * None.
57 -*/
58 
59 void DbInit(void)
60 {
61  if (! m_initialized) {
62  MsgRegister(DBS_MIN_VALUE, DBS_MAX_VALUE, d_messages, NULL);
63  m_initialized = 1;
64  }
65 
66  return;
67 }
68 
69 
70 
71 /*+
72  * DbRundown - Rundown Database Access
73  *
74  * Description:
75  * Performs any rundown needed of the database module.
76  *
77  * Arguments:
78  * None.
79 -*/
80 
81 void DbRundown(void)
82 {
83  return;
84 }
85 
86 int DbFlavour(void)
87 {
88 #ifdef USE_MYSQL
89  return MYSQL_DB;
90 #else
91  return SQLITE_DB;
92 #endif
93 }
94 
95 /*+
96  * db_version_check
97  *
98  * Description:
99  * Check the version of the database against the version in database.h
100  *
101  * Arguments:
102  * None
103 -*/
104 
106 {
107  char* sql = "select version from dbadmin"; /* SQL query */
108  int status = 0; /* Status return */
109  DB_RESULT result; /* Result of the query */
110  DB_ROW row = NULL; /* Row data */
111  int version = 0; /* Version returned */
112 
113  /* Select rows */
114  status = DbExecuteSql(DbHandle(), sql, &result);
115  if (status == 0) {
116  status = DbFetchRow(result, &row);
117  while (status == 0) {
118  /* Got a row, print it */
119  DbInt(row, 0, &version);
120 
121  /* Check it */
122  if (version != KSM_DB_VERSION) {
123  DbFreeRow(row);
124  DbFreeResult(result);
125  return MsgLog(KME_WRONG_DB_VER, KSM_DB_VERSION, version);
126  }
127 
128  status = DbFetchRow(result, &row);
129  /* should only have one row */
130  if (status == 0) {
131  DbFreeRow(row);
132  DbFreeResult(result);
133  return MsgLog(KME_DB_ADMIN);
134  }
135  }
136 
137  /* Convert EOF status to success */
138  if (status == -1) {
139  status = 0;
140  }
141 
142  DbFreeResult(result);
143  }
144 
145  DbFreeRow(row);
146  return status;
147 }