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<time.h>
32 #include<ctype.h>
33 #include <syslog.h>
34 #include <stdarg.h>
35 
36 
37 FILE *flog;
38 extern char logfile[256];
39 
40 #include "dhisd.h"
41 #include "misc.h"
42 #include "ddb.h"
43 
44 
strtolower(char * s)45 void strtolower(char *s) {
46 
47 	while(*s!='\0') { *s = tolower(*s); s++; }
48 }
49 
50 
off_nl(char * s)51 void off_nl(char *s) {
52 
53 	while(*s!='\0' && *s!='\n' && *s!='\r') s++;
54 	*s='\0';
55 	return;
56 }
57 
line_ptr(int idx,char * buff)58 char *line_ptr(int idx,char *buff) {
59 
60 	int i;
61 	static char m[1];
62 
63 
64 	idx--;
65 	m[0]='\0';
66 
67 	while((*buff==' ' || *buff=='\t') && *buff!='\0' && *buff!='\n')
68 		buff++;
69 	if(*buff=='\0' || *buff=='\n') return(m);
70 
71 	for(i=0;i<idx;i++) {
72 		while(*buff!=' ' && *buff!='\t' && *buff!='\0' &&
73 			*buff!='\n') buff++;
74 		if(*buff=='\0' || *buff=='\n') return(m);
75 		while((*buff==' ' || *buff=='\t') && *buff!='\0' && *buff!='\n')
76 			buff++;
77 		if(*buff=='\0' || *buff=='\n') return(m);
78 	}
79 	return(buff);
80 }
81 
line_entry(int idx,char * buff)82 char *line_entry(int idx,char *buff) {
83 
84 	static char b2[1024],*pb2;
85 	int i;
86 
87 	idx--;
88 	b2[0]='\0';
89 	pb2=b2;
90 
91 	while((*buff==' ' || *buff=='\t') && *buff!='\0' && *buff!='\n')
92 		buff++;
93 	if(*buff=='\0' || *buff=='\n') return(b2);
94 
95 	for(i=0;i<idx;i++) {
96 		while(*buff!=' ' && *buff!='\t' && *buff!='\0' &&
97 			*buff!='\n') buff++;
98 		if(*buff=='\0' || *buff=='\n') return(b2);
99 		while((*buff==' ' || *buff=='\t') && *buff!='\0' && *buff!='\n')
100 			buff++;
101 		if(*buff=='\0' || *buff=='\n') return(b2);
102 	}
103 	while(*buff!=' ' && *buff!='\t' && *buff!='\0' && *buff!='\n') {
104 		*pb2 = *buff;
105 		buff++;
106 		pb2++;
107 	}
108 	*pb2 = '\0';
109 	return(b2);
110 }
111 
msg_log(const char * msg)112 int msg_log(const char *msg) {
113 
114 	time_t	tt;
115 	char buff[256];
116 
117 	DSYSLOG(1,(LOG_DEBUG,"msg_log(): Logging %s\n",msg));
118 
119 	flog=fopen(logfile,"a");
120 	if(flog==NULL) return(0);
121 	tt=time(NULL);
122 	strcpy(buff,ctime(&tt));
123 	off_nl(buff);
124 	strcat(buff," : ");
125 	strcat(buff,msg);
126 
127 #ifdef	WITH_MYSQL
128 	sql_log(buff);
129 #endif
130 
131 	strcat(buff,"\n");
132 	fputs(buff,flog);
133 	fclose(flog);
134 	return(1);
135 }
136 
137 /* R3 hashing function */
r3_pass_encrypt(unsigned char * pass,int n)138 int r3_pass_encrypt(unsigned char *pass,int n) {
139 
140 	int res=0;
141 
142 	while(*pass!='\0')
143 		res += (*pass++ * (n%256));
144 	res += n+ 8 + (n%100);
145 	return(res);
146 }
147 
148