File: db.c

package info (click to toggle)
dhis-server 5.3-2.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 428 kB
  • ctags: 568
  • sloc: ansic: 1,935; makefile: 73; sh: 67
file content (285 lines) | stat: -rw-r--r-- 6,047 bytes parent folder | download | duplicates (3)
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
/*-
 * Copyright (c) 1998-2005 Joao Cabral
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      DHIS(c)  Dynamic Host Information System Release 5
 */

#include "dhisd.h"
#include "db.h"
#include "misc.h"

db_t *dbase=NULL;

char dbase_file[256];

int db_add_service(char *service,db_service_t **sbp) {

	struct service_s *sp;
	db_service_t *bp;
		
	sp=service_get_by_name(service);
	if(sp==NULL) return(1);
	
	bp=(*sbp);
	if(bp==NULL) {
		(*sbp)=(db_service_t *)malloc(sizeof(db_service_t));
		if(*sbp == NULL) return(1);
		bp=(*sbp);
	} else {
		while(bp->next!=NULL) bp=bp->next;
		bp->next=(db_service_t *)malloc(sizeof(db_service_t));
		if(bp->next==NULL) return(1);
		bp=bp->next;
	}
	bp->next=NULL;
	bp->sp=sp;
	return(0);
}
	

int db_reload(void) {

	FILE *fp;
	char str[1024];
	db_t *rec;
	char keyn[1024];

	if(dbase!=NULL) db_free();

	DSYSLOG(1,(LOG_DEBUG,"db_reload(): Loading DHIS database %s\n",dbase_file));

	fp=fopen(dbase_file,"r");
	if(fp==NULL) return(0);
	while(fgets(str,1024,fp)!=NULL) {
	 off_nl(str);
         if(strchr(line_entry(1,str),';')==NULL)
	 if(!strcmp(line_entry(2,str),"{")) {
		char cmd[64];
		char *cp;
		rec=(db_t *)malloc(sizeof(db_t));
		if(rec==NULL) { fclose(fp); return(0); }
		rec->id=atoi(line_entry(1,str));
		rec->ltod=0;
		rec->oncmd[0]='\0';
		rec->offcmd[0]='\0';
		rec->service=NULL;
		rec->xstage=0;
		rec->refresh=NEXT_CHECK;
		rec->atype=0;
		keyn[0]='\0';
		do {
		if(fgets(str,1024,fp)==NULL) 
		{
		fclose(fp);
		if(rec->atype==AQRC) mpz_clear(rec->authn);
		free(rec);
		return(0);
		}
		off_nl(str);

		strcpy(cmd,line_entry(1,str));
		strtolower(cmd);
		if(!strcmp(cmd,"hostpass")) {
			strcpy(rec->pass,line_entry(2,str));
			rec->atype=APASS;

                        if((cp=strchr(rec->pass,';'))!=NULL)
                                *cp='\0';

		}

		if(!strcmp(cmd,"oncmd")) {
			strcpy(rec->oncmd,line_ptr(2,str));
                        if((cp=strchr(rec->oncmd,';'))!=NULL)
                                *cp='\0';
		}
		if(!strcmp(cmd,"offcmd")) {
			strcpy(rec->offcmd,line_ptr(2,str));
                        if((cp=strchr(rec->offcmd,';'))!=NULL)
                                *cp='\0';
		}
		if(!strcmp(cmd,"service")) {
			if(db_add_service(line_entry(2,str),&(rec->service))) {
			syslog(LOG_ERR,"Failed to load service %s for %d",
				line_entry(2,str),rec->id);

			DSYSLOG(1,(LOG_DEBUG,"db_reload(): Failed to load service %s for %d\n",
				   line_entry(2,str),rec->id));

			}
		}

		if(!strcmp(cmd,"authn")) {
			strcat(keyn,line_entry(2,str));
                        if((cp=strchr(keyn,';'))!=NULL)
                                *cp='\0';
			rec->atype=AQRC;
		}
		
		} while(strcmp(line_entry(1,str),"}"));
		
		if(!rec->atype) free(rec); else
		if(rec->atype==AQRC && keyn[0]=='\0') free(rec); else {
			if(rec->atype==AQRC) {
			mpz_init(rec->authn);
			mpz_set_str(rec->authn,keyn,10);
			}
			if(rec->service==NULL) 
				db_add_service("default",&(rec->service));
			db_add(rec);
		}

		DSYSLOG(1,(LOG_DEBUG,"db_reload(): Read record %d\n",rec->id));

	 } /* End if */
	} /* End while */
	fclose(fp);

	DSYSLOG(1,(LOG_DEBUG,"db_reload(): Database loaded\n"));

	return(1);
}

int db_free() {

	db_t *p1,*p2;

	if(dbase==NULL) return(1);
	p1=dbase;
	while(p1!=NULL) {
		db_service_t *dbp;
		
		while(p1->service!=NULL) {
		dbp=p1->service->next;
		free(p1->service);
		p1->service=dbp;
		}
		p2=p1;
		p1=p1->next;
		if(p2->atype==AQRC) mpz_clear(p2->authn);
		if(p2->atype==AQRC && p2->xstage==1) mpz_clear(p2->x); 
		free(p2);
	}
	dbase=NULL;
	return(1);
}


char *db_password(int id) {

	db_t *p;
	static char pass[MAX_PASS];

	p=dbase;
	while(p!=NULL) {
		if(p->id==id) {
			strcpy(pass,p->pass);
			return(pass);
		}
		p=p->next;
	}
	return(NULL);
}

int db_add(db_t *rec) {

	db_t *p;
	p=dbase;
	if(dbase==NULL) {
	dbase=rec;
	dbase->next=NULL;
	return(1);
	}

	while(p->next!=NULL) p=p->next;
	p->next=rec;
	p->next->next=NULL;
	return(1);
}


int db_get_ltod(int id) {

	db_t *p;

	p=dbase;
	while(p!=NULL) {
		if(id==p->id) 
			return(p->ltod);
		p=p->next;
	}
	return(0);
}

int db_set_ltod(int id,int ltod) {

	db_t *p;

	p=dbase;
	while(p!=NULL) {
		if(p->id==id) {
			p->ltod=ltod;

			DSYSLOG(1,(LOG_DEBUG,"db_set_ltod(): Setting LTOD to %d on %d\n",
				   p->ltod,p->id));

			return(1);
		}
		p=p->next;
	}
	return(0);
}

db_service_t *db_get_service_by_id(int id) {

	db_t *dp;
	dp=dbase;
	while(dp!=NULL) {
	if(dp->id==id) return(dp->service);
	dp=dp->next;
	}
	return(NULL);
}

char *db_get_oncmd(int id) {
	db_t *dp;
	dp=dbase;
	while(dp!=NULL) {
	if(dp->id==id) return(dp->oncmd);
	dp=dp->next;
	}
	return(NULL);
}

char *db_get_offcmd(int id) {
	db_t *dp;
	dp=dbase;
	while(dp!=NULL) {
	if(dp->id==id) return(dp->offcmd);
	dp=dp->next;
	}
	return(NULL);
}