1 /*-
2  * Copyright (c) 1998-2008 DHIS, Dynamic Host Information System
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include<stdio.h>
29 #include<stdlib.h>
30 #include<string.h>
31 #include<ctype.h>
32 #include<time.h>
33 #include<sys/time.h>
34 #include<sys/types.h>
35 #include<netdb.h>
36 #include<sys/stat.h>
37 #include<fcntl.h>
38 #include<netinet/in.h>
39 #include<sys/socket.h>
40 #include<arpa/inet.h>
41 #include<arpa/nameser.h>
42 #include<resolv.h>
43 #include<sys/utsname.h>
44 #include<unistd.h>
45 #include<signal.h>
46 #include<sys/signal.h>
47 #include<sys/wait.h>
48 #include<syslog.h>
49 // #include<varargs.h>
50 #include<gmp.h>
51 
52 #define	DHISD_CONF_FILE		"/usr/local/etc/dhisd.conf"
53 #define	DHISD_DB_FILE		"/usr/local/etc/dhis.db"
54 
55 #define	DHISD_PID		"/var/run/dhis/dhisd.pid"
56 #define	DHISD_LOG		"/var/log/dhis/dhisd.log"
57 
58 #define	DHISD_PORT	58800
59 
60 #define	DHIS_VERSION		55	/* Current version */
61 #define	DHIS_RELEASE		55	/* Current release */
62 #define	DHIS_R4			40	/* R4 starting version */
63 #define	DHIS_MIN_VERSION	30	/* Minimum required version to run */
64 
65 #define	KA_OFFLINE		200	/* timeout for R3 clients */
66 #define	PARSE_TIMEOUT		10	/* frequency of parse  in sec */
67 #define	MIN_NEXT_CHECK		60	/* mim allowed refresh */
68 #define	MAX_NEXT_CHECK		3600 	/* max allowed refresh secs */
69 #define	CHECK_FAILS		3	/* maximum check fails */
70 #define NEXT_CHECK      	60      /* default refresh */
71 
72 #define	MAX_HOSTNAME	64
73 #define	MAX_PASS	16
74 
75 /* R3 messages */
76 #define	R3_ONLINE_REQ	0x00000311
77 #define	R3_OFFLINE_REQ	0x00000312
78 
79 
80 /* R4 messages */
81 #define	R4_ECHO_REQ	0x00000411
82 #define	R4_ECHO_ACK	0x00000412
83 #define	R4_AUTH_REQ	0x00000421
84 #define	R4_AUTH_DENY	0x00000422
85 #define	R4_AUTH_ACK	0x00000423
86 #define	R4_AUTH_SX	0x00000424
87 #define	R4_AUTH_SY	0x00000425
88 #define	R4_CHECK_REQ	0x00000441
89 #define	R4_CHECK_ACK	0x00000442
90 #define	R4_OFFLINE_REQ	0x00000451
91 
92 /* R5 messages */
93 #define	ECHO_REQ	0x00000511
94 #define	ECHO_ACK	0x00000512
95 #define	AUTH_REQ	0x00000521
96 #define	AUTH_DENY	0x00000522
97 #define	AUTH_ACK	0x00000523
98 #define	AUTH_SX		0x00000524
99 #define	AUTH_SY		0x00000525
100 #define	R51_AUTH_ACK	0x00000526
101 #define	CHECK_REQ	0x00000541
102 #define	CHECK_ACK	0x00000542
103 #define	OFFLINE_REQ	0x00000551
104 
105 extern int debug_level;
106 
107 #define DSYSLOG(d,m)	\
108   do {			\
109     if (debug_level>=d)	\
110       syslog m;		\
111   } while(0)
112 
113 
114