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