Browse code

implement sending notification e-mails (with the corresponding config file support)

Dario Rodriguez authored on 24/07/2014 11:03:47
Showing 1 changed files
... ...
@@ -19,6 +19,13 @@
19 19
 #include "kakumei_config.h"
20 20
 
21 21
 #define MAXLINESIZE 2048
22
+#define LINESBLOCK 32
23
+
24
+typedef enum esection {
25
+        section_none=0,
26
+        section_general,
27
+        section_mail
28
+} esection;
22 29
 
23 30
 kaconfig *
24 31
 kaconfig_init(char *configfile)
... ...
@@ -28,7 +35,7 @@ kaconfig_init(char *configfile)
28 35
         char line[MAXLINESIZE];
29 36
         int len;
30 37
         char *ptr,*sep,*value;
31
-        int generalsection;
38
+        esection section;
32 39
         int lineno;
33 40
         char **configvalue;
34 41
         if((f=fopen(configfile,"r"))==NULL)
... ...
@@ -38,15 +45,22 @@ kaconfig_init(char *configfile)
38 45
                 return(NULL);
39 46
         }
40 47
         memset(config,0,sizeof(kaconfig));
41
-        generalsection=0;
48
+        section=section_none;
42 49
         lineno=0;
43 50
         while(fgets(line,sizeof(line),f)!=NULL) {
44 51
                 lineno++;
45 52
                 line[sizeof(line)-1]='\0';
46 53
                 /* trim line */
54
+                for(ptr=line;*ptr!='\0' && strchr("\t ",*ptr)!=NULL;ptr++)
55
+                        ;
47 56
                 len=strlen(line);
48
-                while(len>1 && strchr("\n\t ",line[len-1])!=NULL)
49
-                        line[len-1]='\0',len--;
57
+                if(!(section==section_mail && memcmp(ptr,"line.",5)==0)) {
58
+                        while(len>0 && strchr("\n\t ",line[len-1])!=NULL)
59
+                                line[len-1]='\0',len--;
60
+                } else {
61
+                        while(len>0 && line[len-1]=='\n')
62
+                                line[len-1]='\0',len--;
63
+                }
50 64
                 for(ptr=line;*ptr!='\0' && strchr("\t ",*ptr)!=NULL;ptr++)
51 65
                         ;
52 66
                 len=strlen(ptr);
... ...
@@ -56,11 +70,13 @@ kaconfig_init(char *configfile)
56 70
                 /* parse line */
57 71
                 if(*ptr=='[' && ptr[len-1]==']') {
58 72
                         /* section */
59
-                        if(strcmp(ptr,"[general]")==0)
60
-                                generalsection=1;
61
-                        else {
73
+                        if(strcmp(ptr,"[general]")==0) {
74
+                                section=section_general;
75
+                        } else if(strcmp(ptr,"[mail]")==0) {
76
+                                section=section_mail;
77
+                        } else {
62 78
                                 log_write("CONF","%s:%i: unknown section name \"%s\"\n",configfile,lineno,ptr);
63
-                                generalsection=0;
79
+                                section=section_none;
64 80
                         }
65 81
                         continue;
66 82
                 } else if((sep=strchr(ptr,'='))!=NULL) {
... ...
@@ -71,38 +87,77 @@ kaconfig_init(char *configfile)
71 87
                                 sep--;
72 88
                                 *sep='\0';
73 89
                         }
74
-                        /* trim value */
75
-                        while(*value!='\0' && strchr(" \t",*value)!=NULL)
76
-                                value++;
90
+                        if(!(section==section_mail && memcmp(ptr,"line.",5)==0)) {
91
+                                /* trim value */
92
+                                while(*value!='\0' && strchr(" \t",*value)!=NULL)
93
+                                        value++;
94
+                        }
77 95
                         /* sanity check */
78 96
                         if(*value=='\0') {
79
-                                log_write("CONF","%s:%i: ignoring key without value; key:\"%s\"\n",configfile,lineno,ptr);
97
+                                if(!(section==section_mail && memcmp(ptr,"line.",5)==0))
98
+                                        log_write("CONF","%s:%i: ignoring key without value; key:\"%s\"\n",configfile,lineno,ptr);
80 99
                                 continue;
81 100
                         }
82 101
                         /* assign value */
83
-                        if(generalsection!=1) {
102
+                        if(section==section_none) {
84 103
                                 log_write("CONF","%s:%i: ignoring key-value pair in unknown section; key:\"%s\"\n",configfile,lineno,ptr);
85 104
                                 continue;
86 105
                         }
87 106
                         /* identify key-value pair */
88
-                        if(strcmp(ptr,"logfile")==0) {
89
-                                configvalue=&(config->logfile);
90
-                        } else if(strcmp(ptr,"cookiename")==0) {
91
-                                configvalue=&(config->cookiename);
92
-                        } else if(strcmp(ptr,"cookiedomain")==0) {
93
-                                configvalue=&(config->cookiedomain);
94
-                        } else if(strcmp(ptr,"bannerpath")==0) {
95
-                                configvalue=&(config->bannerpath);
96
-                        } else if(strcmp(ptr,"sslproxy")==0) {
97
-				if(strcmp(value,"true")==0 || strcmp(value,"1")==0 || strcmp(value,"yes")==0)
98
-					config->sslproxy=1;
99
-				else
100
-					config->sslproxy=0;
101
-				continue;
102
-                        } else {
103
-                                log_write("CONF","%s:%i: unknown key, ignoring key-value pair; key:\"%s\"\n",configfile,lineno,ptr);
107
+                        if(section==section_general) {
108
+                                if(strcmp(ptr,"logfile")==0) {
109
+                                        configvalue=&(config->logfile);
110
+                                } else if(strcmp(ptr,"cookiename")==0) {
111
+                                        configvalue=&(config->cookiename);
112
+                                } else if(strcmp(ptr,"cookiedomain")==0) {
113
+                                        configvalue=&(config->cookiedomain);
114
+                                } else if(strcmp(ptr,"bannerpath")==0) {
115
+                                        configvalue=&(config->bannerpath);
116
+                                } else if(strcmp(ptr,"sslproxy")==0) {
117
+                                        if(strcmp(value,"true")==0 || strcmp(value,"1")==0 || strcmp(value,"yes")==0)
118
+                                                config->sslproxy=1;
119
+                                        else
120
+                                                config->sslproxy=0;
121
+                                        continue;
122
+                                } else {
123
+                                        log_write("CONF","%s:%i: unknown key, ignoring key-value pair; key:\"%s\"\n",configfile,lineno,ptr);
124
+                                        continue;
125
+                                }
126
+                        } else if(section==section_mail) {
127
+                                if(strcmp(ptr,"from")==0) {
128
+                                        configvalue=&(config->from);
129
+                                } else if(strcmp(ptr,"subjpost")==0) {
130
+                                        configvalue=&(config->subjpost);
131
+                                } else if(strcmp(ptr,"subjcomment")==0) {
132
+                                        configvalue=&(config->subjcomment);
133
+                                } else if(memcmp(ptr,"line.",5)==0) {
134
+                                        int lineno=atoi(ptr+5);
135
+                                        if(lineno<0)
136
+                                                lineno=0;
137
+                                        if(lineno>=config->sizelines) {
138
+                                                char **lines;
139
+                                                int minsize;
140
+                                                minsize=(lineno+LINESBLOCK)/LINESBLOCK;
141
+                                                minsize*=LINESBLOCK;
142
+                                                if((lines=realloc(config->lines,minsize*sizeof(char *)))==NULL) {
143
+                                                        fclose(f),f=NULL;
144
+                                                        kaconfig_free(config),config=NULL;
145
+                                                        return(NULL); /* insufficient memory */
146
+                                                }
147
+                                                config->lines=lines;
148
+                                                memset(lines+config->sizelines,0,sizeof(char *)*(minsize-config->sizelines));
149
+                                                config->sizelines=minsize;
150
+                                        }
151
+                                        configvalue=config->lines+lineno;
152
+                                        if(config->usedlines<=lineno)
153
+                                                config->usedlines=lineno+1;
154
+                                } else {
155
+                                        log_write("CONF","%s:%i: unknown key, ignoring key-value pair; key:\"%s\"\n",configfile,lineno,ptr);
156
+                                        continue;
157
+                                }
158
+
159
+                        } else
104 160
                                 continue;
105
-                        }
106 161
                         /* store value */
107 162
                         if(*configvalue!=NULL) {
108 163
                                 log_write("CONF","%s:%i: duplicate key, ignoring key-value pair; key:\"%s\"\n",configfile,lineno,ptr);
... ...
@@ -137,6 +192,22 @@ kaconfig_free(kaconfig *config)
137 192
                 free(config->cookiedomain),config->cookiedomain=NULL;
138 193
         if(config->bannerpath!=NULL)
139 194
                 free(config->bannerpath),config->bannerpath=NULL;
195
+        if(config->from!=NULL)
196
+                free(config->from),config->from=NULL;
197
+        if(config->subjpost!=NULL)
198
+                free(config->subjpost),config->subjpost=NULL;
199
+        if(config->subjcomment!=NULL)
200
+                free(config->subjcomment),config->subjcomment=NULL;
201
+        if(config->lines!=NULL) {
202
+                int i;
203
+                for(i=0;i<config->usedlines;i++) {
204
+                        if(config->lines[i]!=NULL)
205
+                                free(config->lines[i]),config->lines[i]=NULL;
206
+                }
207
+                config->sizelines=0;
208
+                config->usedlines=0;
209
+                free(config->lines),config->lines=NULL;
210
+        }
140 211
         free(config),config=NULL;
141 212
         return;
142 213
 }
... ...
@@ -163,6 +234,7 @@ kaconfig_write(char *configfile,char *logfile, char *cookiename,char *cookiedoma
163 234
         fprintf(f,"cookiedomain=%s\n",(cookiedomain!=NULL)?cookiedomain:"localhost");
164 235
         fprintf(f,"bannerpath=%s\n",(bannerpath!=NULL)?bannerpath:"default.png");
165 236
         fprintf(f,"sslproxy=%s\n",(sslproxy==0)?"no":"yes");
237
+        fprintf(f,"\n[mail]\nfrom=\nsubjpost=New post\nsubjcomment=New comment in post\nline.0=There is a new post or comment\nline.1=Best regards, kakumei web\n");
166 238
         return(0);
167 239
 }
168 240
 
Browse code

delete unused variable

Dario Rodriguez authored on 15/07/2014 11:18:51
Showing 1 changed files
... ...
@@ -31,7 +31,6 @@ kaconfig_init(char *configfile)
31 31
         int generalsection;
32 32
         int lineno;
33 33
         char **configvalue;
34
-	char *sslproxydummy;
35 34
         if((f=fopen(configfile,"r"))==NULL)
36 35
                 return(NULL);
37 36
         if((config=malloc(sizeof(kaconfig)))==NULL) {
Browse code

add logfile configuration option

Dario Rodriguez authored on 14/07/2014 12:29:25
Showing 1 changed files
... ...
@@ -86,7 +86,9 @@ kaconfig_init(char *configfile)
86 86
                                 continue;
87 87
                         }
88 88
                         /* identify key-value pair */
89
-                        if(strcmp(ptr,"cookiename")==0) {
89
+                        if(strcmp(ptr,"logfile")==0) {
90
+                                configvalue=&(config->logfile);
91
+                        } else if(strcmp(ptr,"cookiename")==0) {
90 92
                                 configvalue=&(config->cookiename);
91 93
                         } else if(strcmp(ptr,"cookiedomain")==0) {
92 94
                                 configvalue=&(config->cookiedomain);
... ...
@@ -128,6 +130,8 @@ kaconfig_free(kaconfig *config)
128 130
 {
129 131
         if(config==NULL)
130 132
                 return;
133
+        if(config->logfile!=NULL)
134
+                free(config->logfile),config->logfile=NULL;
131 135
         if(config->cookiename!=NULL)
132 136
                 free(config->cookiename),config->cookiename=NULL;
133 137
         if(config->cookiedomain!=NULL)
... ...
@@ -148,13 +152,14 @@ kaconfig_exists(char *configfile)
148 152
 }
149 153
 
150 154
 int
151
-kaconfig_write(char *configfile,char *cookiename,char *cookiedomain, char *bannerpath, int sslproxy)
155
+kaconfig_write(char *configfile,char *logfile, char *cookiename,char *cookiedomain, char *bannerpath, int sslproxy)
152 156
 {
153 157
         FILE *f;
154 158
         if((f=fopen(configfile,"w"))==NULL)
155 159
                 return(-1);
156 160
         fprintf(f,"; kakumei config file\n");
157 161
         fprintf(f,"[general]\n");
162
+        fprintf(f,"logfile=%s\n",(logfile!=NULL)?logfile:"kakumei.log");
158 163
         fprintf(f,"cookiename=%s\n",(cookiename!=NULL)?cookiename:"kakumeiauthid");
159 164
         fprintf(f,"cookiedomain=%s\n",(cookiedomain!=NULL)?cookiedomain:"localhost");
160 165
         fprintf(f,"bannerpath=%s\n",(bannerpath!=NULL)?bannerpath:"default.png");
Browse code

make the secure attribute in the cookie optional (only works if using a ssl proxy, as in stunnel)

Dario Rodriguez authored on 14/07/2014 12:10:33
Showing 1 changed files
... ...
@@ -31,6 +31,7 @@ kaconfig_init(char *configfile)
31 31
         int generalsection;
32 32
         int lineno;
33 33
         char **configvalue;
34
+	char *sslproxydummy;
34 35
         if((f=fopen(configfile,"r"))==NULL)
35 36
                 return(NULL);
36 37
         if((config=malloc(sizeof(kaconfig)))==NULL) {
... ...
@@ -91,6 +92,12 @@ kaconfig_init(char *configfile)
91 92
                                 configvalue=&(config->cookiedomain);
92 93
                         } else if(strcmp(ptr,"bannerpath")==0) {
93 94
                                 configvalue=&(config->bannerpath);
95
+                        } else if(strcmp(ptr,"sslproxy")==0) {
96
+				if(strcmp(value,"true")==0 || strcmp(value,"1")==0 || strcmp(value,"yes")==0)
97
+					config->sslproxy=1;
98
+				else
99
+					config->sslproxy=0;
100
+				continue;
94 101
                         } else {
95 102
                                 log_write("CONF","%s:%i: unknown key, ignoring key-value pair; key:\"%s\"\n",configfile,lineno,ptr);
96 103
                                 continue;
... ...
@@ -141,7 +148,7 @@ kaconfig_exists(char *configfile)
141 148
 }
142 149
 
143 150
 int
144
-kaconfig_write(char *configfile,char *cookiename,char *cookiedomain, char *bannerpath)
151
+kaconfig_write(char *configfile,char *cookiename,char *cookiedomain, char *bannerpath, int sslproxy)
145 152
 {
146 153
         FILE *f;
147 154
         if((f=fopen(configfile,"w"))==NULL)
... ...
@@ -151,6 +158,7 @@ kaconfig_write(char *configfile,char *cookiename,char *cookiedomain, char *banne
151 158
         fprintf(f,"cookiename=%s\n",(cookiename!=NULL)?cookiename:"kakumeiauthid");
152 159
         fprintf(f,"cookiedomain=%s\n",(cookiedomain!=NULL)?cookiedomain:"localhost");
153 160
         fprintf(f,"bannerpath=%s\n",(bannerpath!=NULL)?bannerpath:"default.png");
161
+        fprintf(f,"sslproxy=%s\n",(sslproxy==0)?"no":"yes");
154 162
         return(0);
155 163
 }
156 164
 
Browse code

add authid cookie to complement the session in QUERY_STRING

Dario Rodriguez authored on 13/07/2014 21:41:07
Showing 1 changed files
... ...
@@ -85,8 +85,8 @@ kaconfig_init(char *configfile)
85 85
                                 continue;
86 86
                         }
87 87
                         /* identify key-value pair */
88
-                        if(strcmp(ptr,"cookieprefix")==0) {
89
-                                configvalue=&(config->cookieprefix);
88
+                        if(strcmp(ptr,"cookiename")==0) {
89
+                                configvalue=&(config->cookiename);
90 90
                         } else if(strcmp(ptr,"cookiedomain")==0) {
91 91
                                 configvalue=&(config->cookiedomain);
92 92
                         } else if(strcmp(ptr,"bannerpath")==0) {
... ...
@@ -121,8 +121,8 @@ kaconfig_free(kaconfig *config)
121 121
 {
122 122
         if(config==NULL)
123 123
                 return;
124
-        if(config->cookieprefix!=NULL)
125
-                free(config->cookieprefix),config->cookieprefix=NULL;
124
+        if(config->cookiename!=NULL)
125
+                free(config->cookiename),config->cookiename=NULL;
126 126
         if(config->cookiedomain!=NULL)
127 127
                 free(config->cookiedomain),config->cookiedomain=NULL;
128 128
         if(config->bannerpath!=NULL)
... ...
@@ -141,14 +141,14 @@ kaconfig_exists(char *configfile)
141 141
 }
142 142
 
143 143
 int
144
-kaconfig_write(char *configfile,char *cookieprefix,char *cookiedomain, char *bannerpath)
144
+kaconfig_write(char *configfile,char *cookiename,char *cookiedomain, char *bannerpath)
145 145
 {
146 146
         FILE *f;
147 147
         if((f=fopen(configfile,"w"))==NULL)
148 148
                 return(-1);
149 149
         fprintf(f,"; kakumei config file\n");
150 150
         fprintf(f,"[general]\n");
151
-        fprintf(f,"cookieprefix=%s\n",(cookieprefix!=NULL)?cookieprefix:"kakumei_");
151
+        fprintf(f,"cookiename=%s\n",(cookiename!=NULL)?cookiename:"kakumeiauthid");
152 152
         fprintf(f,"cookiedomain=%s\n",(cookiedomain!=NULL)?cookiedomain:"localhost");
153 153
         fprintf(f,"bannerpath=%s\n",(bannerpath!=NULL)?bannerpath:"default.png");
154 154
         return(0);
Browse code

add config file

Dario Rodriguez authored on 11/07/2014 10:53:59
Showing 1 changed files
1 1
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
+