Browse code

add config file

Dario Rodriguez authored on 11/07/2014 10:53:59
Showing 6 changed files
... ...
@@ -8,4 +8,4 @@ res/*.workingnote
8 8
 src/data
9 9
 src/*.txt
10 10
 src/*.workingnote
11
-
11
+src/kakumei.conf
... ...
@@ -12,11 +12,11 @@ clean:
12 12
 kakumei: kakumei.o loglib.o parselib.o sbuf.o \
13 13
 		socklib.o webkernel.o gen_res.o \
14 14
 		kakumei_session.o kakumei_pass.o \
15
-		kakumei_posts.o
15
+		kakumei_posts.o kakumei_config.o
16 16
 	$(CC) $(LDFLAGS_KAKUMEI) kakumei.o loglib.o parselib.o sbuf.o \
17 17
 		socklib.o webkernel.o gen_res.o \
18 18
 		kakumei_session.o kakumei_pass.o \
19
-		kakumei_posts.o \
19
+		kakumei_posts.o kakumei_config.o \
20 20
 		-o kakumei
21 21
 
22 22
 kakumei.o: kakumei.c ../src/gen_res.c
... ...
@@ -23,6 +23,12 @@
23 23
 #include "kakumei_session.h"
24 24
 #include "kakumei_pass.h"
25 25
 #include "kakumei_posts.h"
26
+#include "kakumei_config.h"
27
+
28
+#define CONFIGFILE "kakumei.conf"
29
+#define CFCOOKIEPREFIX "kakumei_"
30
+#define CFCOOKIEDOMAIN "localhost"
31
+#define CFBANNERPATH "default.png"
26 32
 
27 33
 static int signal_init(int signum, void (*fn)(int));
28 34
 static void sigint(int signum);
... ...
@@ -52,6 +58,14 @@ main(int argc, char *argv[])
52 58
                 return(1);
53 59
         }
54 60
         port=atoi(argv[1]);
61
+        if(kaconfig_exists(CONFIGFILE)!=0) {
62
+                log_write("INIT","Config file not found, writing default file %s",CONFIGFILE);
63
+                kaconfig_write(CONFIGFILE,CFCOOKIEPREFIX,CFCOOKIEDOMAIN,CFBANNERPATH);
64
+        }
65
+        if((ka->config=kaconfig_init(CONFIGFILE))==NULL) {
66
+                log_write("INIT","ERROR: insufficient memory or config file error");
67
+                return(1);
68
+        }
55 69
         if((ka->ssel=sselect_init())==NULL) {
56 70
                 log_write("INIT","ERROR: insufficient memory");
57 71
                 return(1);
... ...
@@ -70,6 +84,7 @@ main(int argc, char *argv[])
70 84
         }
71 85
         wk_free(ka->web),ka->web=NULL;
72 86
         sselect_free(ka->ssel),ka->ssel=NULL;
87
+        kaconfig_free(ka->config),ka->config=NULL;
73 88
         log_write("FINI","SIGINT detected, exiting...");
74 89
         return(0);
75 90
 }
... ...
@@ -166,6 +181,8 @@ callback_http(wk *web, int connid, wk_uri *uri, void *userptr)
166 181
         partialpath[sizeof(partialpath)-1]='\0';
167 182
         if(strcmp(uri->path,"/")==0)
168 183
                 strcpy(partialpath,"/index.html");
184
+        if(strcmp(uri->path,"/banner.png")==0)
185
+                strcpy(partialpath,(ka->config->bannerpath!=NULL)?ka->config->bannerpath:"/" CFBANNERPATH);
169 186
         if((ptr=strchr(partialpath,'?'))!=NULL)
170 187
                 *ptr='\0';
171 188
         /* check whitelist */
... ...
@@ -199,6 +216,14 @@ callback_http(wk *web, int connid, wk_uri *uri, void *userptr)
199 216
                         wk_serve_error(web,connid,wkerr_internal);
200 217
                         return(wkact_finished); /* internal error */
201 218
                 }
219
+        } else if(strcmp(uri->path,"/banner.png")==0) {
220
+                log_write("HTTP","Serving banner from disk, file %s",partialpath);
221
+                if(wk_serve_file(web,connid,partialpath,mime_getdefault(partialpath,"image/png"))!=0) {
222
+                        log_write("HTTP","File not found, file %s",partialpath);
223
+                        wk_serve_error(web,connid,wkerr_notfound);
224
+                        return(wkact_finished); /* internal error */
225
+                }
226
+                return(wkact_finished);
202 227
         }
203 228
         /* check for actions */
204 229
         if(memcmp(uri->path,"/login?",7)==0) {
... ...
@@ -14,6 +14,7 @@
14 14
 
15 15
 #include "socklib.h"
16 16
 #include "webkernel.h"
17
+#include "kakumei_config.h"
17 18
 
18 19
 #define INVITATIONUSER "invitation"
19 20
 #define DATADIR "data"
... ...
@@ -28,6 +29,7 @@
28 29
 typedef struct kakumei {
29 30
         sselect *ssel;
30 31
         wk *web;
32
+        kaconfig *config;
31 33
 } kakumei;
32 34
 
33 35
 int kakumei_uservalid(kakumei *ka, char *username); /* no unallowed characters */
34 36
new file mode 100644
... ...
@@ -0,0 +1,156 @@
1
+/*
2
+ * kakumei_config.c
3
+ *
4
+ * Config management for the kakumei server.
5
+ *
6
+ * Author: Dario Rodriguez dario@softhome.net
7
+ * This program is licensed under the terms of the Affero GPL v1.0+ license.
8
+ */
9
+
10
+
11
+#include <stdio.h>
12
+#include <stdlib.h>
13
+#include <unistd.h>
14
+#include <string.h>
15
+#include <sys/types.h>
16
+#include <sys/stat.h>
17
+
18
+#include "loglib.h"
19
+#include "kakumei_config.h"
20
+
21
+#define MAXLINESIZE 2048
22
+
23
+kaconfig *
24
+kaconfig_init(char *configfile)
25
+{
26
+        kaconfig *config;
27
+        FILE *f;
28
+        char line[MAXLINESIZE];
29
+        int len;
30
+        char *ptr,*sep,*value;
31
+        int generalsection;
32
+        int lineno;
33
+        char **configvalue;
34
+        if((f=fopen(configfile,"r"))==NULL)
35
+                return(NULL);
36
+        if((config=malloc(sizeof(kaconfig)))==NULL) {
37
+                fclose(f),f=NULL;
38
+                return(NULL);
39
+        }
40
+        memset(config,0,sizeof(kaconfig));
41
+        generalsection=0;
42
+        lineno=0;
43
+        while(fgets(line,sizeof(line),f)!=NULL) {
44
+                lineno++;
45
+                line[sizeof(line)-1]='\0';
46
+                /* trim line */
47
+                len=strlen(line);
48
+                while(len>1 && strchr("\n\t ",line[len-1])!=NULL)
49
+                        line[len-1]='\0',len--;
50
+                for(ptr=line;*ptr!='\0' && strchr("\t ",*ptr)!=NULL;ptr++)
51
+                        ;
52
+                len=strlen(ptr);
53
+                /* ignore empty lines and comments */
54
+                if(*ptr=='\0' || strchr("#;",*ptr)!=NULL)
55
+                        continue;
56
+                /* parse line */
57
+                if(*ptr=='[' && ptr[len-1]==']') {
58
+                        /* section */
59
+                        if(strcmp(ptr,"[general]")==0)
60
+                                generalsection=1;
61
+                        else {
62
+                                log_write("CONF","%s:%i: unknown section name \"%s\"\n",configfile,lineno,ptr);
63
+                                generalsection=0;
64
+                        }
65
+                        continue;
66
+                } else if((sep=strchr(ptr,'='))!=NULL) {
67
+                        *sep='\0';
68
+                        value=sep+1;
69
+                        /* trim key */
70
+                        while(sep>ptr && strchr(" \t",sep[-1])!=NULL) {
71
+                                sep--;
72
+                                *sep='\0';
73
+                        }
74
+                        /* trim value */
75
+                        while(*value!='\0' && strchr(" \t",*value)!=NULL)
76
+                                value++;
77
+                        /* sanity check */
78
+                        if(*value=='\0') {
79
+                                log_write("CONF","%s:%i: ignoring key without value; key:\"%s\"\n",configfile,lineno,ptr);
80
+                                continue;
81
+                        }
82
+                        /* assign value */
83
+                        if(generalsection!=1) {
84
+                                log_write("CONF","%s:%i: ignoring key-value pair in unknown section; key:\"%s\"\n",configfile,lineno,ptr);
85
+                                continue;
86
+                        }
87
+                        /* identify key-value pair */
88
+                        if(strcmp(ptr,"cookieprefix")==0) {
89
+                                configvalue=&(config->cookieprefix);
90
+                        } else if(strcmp(ptr,"cookiedomain")==0) {
91
+                                configvalue=&(config->cookiedomain);
92
+                        } else if(strcmp(ptr,"bannerpath")==0) {
93
+                                configvalue=&(config->bannerpath);
94
+                        } else {
95
+                                log_write("CONF","%s:%i: unknown key, ignoring key-value pair; key:\"%s\"\n",configfile,lineno,ptr);
96
+                                continue;
97
+                        }
98
+                        /* store value */
99
+                        if(*configvalue!=NULL) {
100
+                                log_write("CONF","%s:%i: duplicate key, ignoring key-value pair; key:\"%s\"\n",configfile,lineno,ptr);
101
+                                continue;
102
+                        }
103
+                        if((*configvalue=strdup(value))==NULL) {
104
+                                fclose(f),f=NULL;
105
+                                kaconfig_free(config),config=NULL;
106
+                                return(NULL); /* insufficient memory */
107
+                        }
108
+                        continue;
109
+                } else {
110
+                        log_write("CONF","%s:%i: malformed line, ignoring; contents:\"%s\"\n",configfile,lineno,ptr);
111
+                        continue;
112
+                }
113
+        }
114
+        fclose(f),f=NULL;
115
+        return(config);
116
+
117
+}
118
+
119
+void
120
+kaconfig_free(kaconfig *config)
121
+{
122
+        if(config==NULL)
123
+                return;
124
+        if(config->cookieprefix!=NULL)
125
+                free(config->cookieprefix),config->cookieprefix=NULL;
126
+        if(config->cookiedomain!=NULL)
127
+                free(config->cookiedomain),config->cookiedomain=NULL;
128
+        if(config->bannerpath!=NULL)
129
+                free(config->bannerpath),config->bannerpath=NULL;
130
+        free(config),config=NULL;
131
+        return;
132
+}
133
+
134
+int
135
+kaconfig_exists(char *configfile)
136
+{
137
+        struct stat st;
138
+        if(stat(configfile,&st)!=0)
139
+                return(-1);
140
+        return(0);
141
+}
142
+
143
+int
144
+kaconfig_write(char *configfile,char *cookieprefix,char *cookiedomain, char *bannerpath)
145
+{
146
+        FILE *f;
147
+        if((f=fopen(configfile,"w"))==NULL)
148
+                return(-1);
149
+        fprintf(f,"; kakumei config file\n");
150
+        fprintf(f,"[general]\n");
151
+        fprintf(f,"cookieprefix=%s\n",(cookieprefix!=NULL)?cookieprefix:"kakumei_");
152
+        fprintf(f,"cookiedomain=%s\n",(cookiedomain!=NULL)?cookiedomain:"localhost");
153
+        fprintf(f,"bannerpath=%s\n",(bannerpath!=NULL)?bannerpath:"default.png");
154
+        return(0);
155
+}
156
+
0 157
new file mode 100644
... ...
@@ -0,0 +1,25 @@
1
+/*
2
+ * kakumei_config.h
3
+ *
4
+ * Config management for the kakumei server.
5
+ *
6
+ * Header file.
7
+ *
8
+ * Author: Dario Rodriguez dario@softhome.net
9
+ * This program is licensed under the terms of the Affero GPL v1.0+ license.
10
+ */
11
+
12
+#ifndef KAKUMEI_CONFIG_H
13
+#define KAKUMEI_CONFIG_H
14
+typedef struct kaconfig {
15
+        char *cookieprefix;
16
+        char *cookiedomain;
17
+        char *bannerpath;
18
+} kaconfig;
19
+
20
+kaconfig *kaconfig_init(char *configfile);
21
+void kaconfig_free(kaconfig *config);
22
+
23
+int kaconfig_exists(char *configfile);
24
+int kaconfig_write(char *configfile,char *cookieprefix,char *cookiedomain, char *bannerpath);
25
+#endif