Browse code

implement kakumei_session.c

Dario Rodriguez authored on 25/06/2014 20:18:39
Showing 6 changed files
... ...
@@ -1,5 +1,6 @@
1 1
 src/*.o
2 2
 src/kakumei
3
+src/kakumei-invite
3 4
 src/*~
4 5
 res/gen_res.c
5 6
 res/gen_res.h
... ...
@@ -2,7 +2,7 @@ WEBKERNEL=../../webkernel/src
2 2
 CC=gcc
3 3
 CFLAGS=-g -Wall -I$(WEBKERNEL) -I../res
4 4
 LDFLAGS=
5
-LDFLAGS_KAKUMEI=$(LDFLAGS) -lscrypt -lm
5
+LDFLAGS_KAKUMEI=$(LDFLAGS) -lscrypt -lm -lmhash
6 6
 
7 7
 all: kakumei kakumei-invite
8 8
 
... ...
@@ -89,8 +89,6 @@ kakumei_userexists(kakumei *ka, char *username)
89 89
         return(0);
90 90
 }
91 91
 
92
-
93
-
94 92
 wk_action
95 93
 callback_http(wk *web, int connid, wk_uri *uri, void *userptr)
96 94
 {
... ...
@@ -136,6 +134,8 @@ sigint(int signum)
136 134
         sigint_flag=1;
137 135
 }
138 136
 
137
+/* implement the "XmlHttpRequest"s (the reply is the new page to load) */
138
+
139 139
 wk_action
140 140
 http_login(wk *web, int connid, wk_uri *uri, void *userptr)
141 141
 {
... ...
@@ -21,6 +21,8 @@
21 21
 #define USERSDIR "data/users"
22 22
 #define POSTSDIR "data/posts"
23 23
 #define SESSIONSDIR "data/sessions"
24
+#define MAXUSERSIZE 32
25
+#define SESSIONSIZE 65
24 26
 
25 27
 typedef struct kakumei {
26 28
         sselect *ssel;
... ...
@@ -9,24 +9,128 @@
9 9
 
10 10
 #include <sys/stat.h>
11 11
 #include <sys/types.h>
12
+#include <sys/time.h>
13
+#include <fcntl.h>
14
+#include <time.h>
15
+#include <mhash.h>
12 16
 #include "kakumei.h"
13 17
 #include "kakumei_session.h"
14 18
 
15 19
 char *
16 20
 session_new(kakumei *ka, char *user, char *session, int sessionsize)
17 21
 {
22
+        static int init=0;
23
+        MHASH td;
24
+        struct timeval tv;
25
+        struct timezone tz;
26
+        int i;
27
+        long n;
28
+        char c;
29
+        char binhash[32];
30
+        char filename[1024];
31
+        int len;
32
+        int fd;
33
+        char oldsession[SESSIONSIZE];
34
+        if(ka==NULL || user==NULL || session==NULL || sessionsize<SESSIONSIZE || kakumei_uservalid(ka,user)!=0)
35
+                return(NULL);
36
+        if(init==0) {
37
+                gettimeofday(&tv,&tz);
38
+                srandom(tv.tv_sec+getpid()+tv.tv_usec);
39
+                init=1;
40
+        }
41
+        /* generate a not-entirely-trivial-to-guess hash */
42
+        if((td=mhash_init(MHASH_SHA256))==MHASH_FAILED)
43
+                return(NULL);
44
+        gettimeofday(&tv,&tz);
45
+        mhash(td,&tv,sizeof(tv));
46
+        mhash(td,user,strlen(user));
47
+        for(i=0;i<20;i++) {
48
+                n=random();
49
+                mhash(td,&n,sizeof(n));
50
+        }
51
+        mhash_deinit(td,&binhash);
52
+        for(i=0;i<sizeof(binhash);i++) {
53
+                c=(((unsigned char *)binhash)[i]>>4);
54
+                c=(c>=10)?(c-10+'a'):c;
55
+                session[i<<1]=c;
56
+                c=(((unsigned char *)binhash)[i]&0xf);
57
+                c=(c>=10)?(c-10+'a'):c;
58
+                session[(i<<1)+1]=c;
59
+        }
60
+        session[sizeof(binhash)]='\0';
61
+        /* save the hash */
18 62
         mkdir(DATADIR,0700);
19 63
         mkdir(SESSIONSDIR,0700);
20
-
64
+        snprintf(filename,sizeof(filename)-1,"%s/%s",SESSIONSDIR,session);
65
+        filename[sizeof(filename)-1]='\0';
66
+        if((fd=open(filename,O_WRONLY|O_TRUNC|O_CREAT,0600))==-1)
67
+                return(NULL);
68
+        len=strlen(user);
69
+        if(write(fd,user,len)!=len) {
70
+                close(fd),fd=-1;
71
+                return(NULL);
72
+        }
73
+        close(fd),fd=-1;
74
+        /* delete the previous session of the user */
75
+        snprintf(filename,sizeof(filename)-1,"%s/%s/session",USERSDIR,user);
76
+        filename[sizeof(filename)-1]='\0';
77
+        if((fd=open(filename,O_RDONLY))!=-1) {
78
+                memset(oldsession,0,sizeof(oldsession));
79
+                read(fd,oldsession,sizeof(oldsession)-1);
80
+                close(fd),fd=-1;
81
+                session_del(ka,oldsession);
82
+        }
83
+        /* write the current session */
84
+        if((fd=open(filename,O_WRONLY|O_TRUNC|O_CREAT,0600))!=-1) {
85
+                write(fd,session,strlen(session));
86
+                close(fd),fd=-1;
87
+        }
88
+        /* success */
89
+        return(session);
21 90
 }
22 91
 
23
-int
24
-session_check(kakumei *ka, char *session)
92
+char *
93
+session_check(kakumei *ka, char *session, char *user, int usersize)
25 94
 {
95
+        int i;
96
+        int fd;
97
+        char filename[1024];
98
+        if(ka==NULL || session==NULL || session[0]=='\0' || user==NULL || usersize<(MAXUSERSIZE+1))
99
+                return(NULL);
100
+        for(i=0;session[i]!='\0';i++) {
101
+                if(!(session[i]>='0' && session[i]<='0') &&
102
+                   !(session[i]>='a' && session[i]<='f')) {
103
+                        return(NULL);
104
+                }
105
+        }
106
+        snprintf(filename,sizeof(filename)-1,"%s/%s",SESSIONSDIR,session);
107
+        filename[sizeof(filename)-1]='\0';
108
+        if((fd=open(filename,O_RDONLY))==-1)
109
+                return(NULL);
110
+        memset(user,0,sizeof(usersize));
111
+        read(fd,user,usersize-1);
112
+        close(fd),fd=-1;
113
+        if(kakumei_uservalid(ka,user)!=0)
114
+                return(NULL);
115
+        return(user);
26 116
 }
27 117
 
28 118
 int
29 119
 session_del(kakumei *ka, char *session)
30 120
 {
121
+        int i;
122
+        char filename[1024];
123
+        if(ka==NULL || session==NULL || session[0]=='\0')
124
+                return(-1);
125
+        for(i=0;session[i]!='\0';i++) {
126
+                if(!(session[i]>='0' && session[i]<='0') &&
127
+                   !(session[i]>='a' && session[i]<='f')) {
128
+                        return(-1);
129
+                }
130
+        }
131
+        snprintf(filename,sizeof(filename)-1,"%s/%s",SESSIONSDIR,session);
132
+        filename[sizeof(filename)-1]='\0';
133
+        unlink(filename);
134
+        return(0);
31 135
 }
32 136
 
... ...
@@ -12,6 +12,6 @@
12 12
 #include "kakumei.h"
13 13
 
14 14
 char *session_new(kakumei *ka, char *user, char *session, int sessionsize);
15
-int session_check(kakumei *ka, char *session);
15
+char *session_check(kakumei *ka, char *session, char *user, int usersize);
16 16
 int session_del(kakumei *ka, char *session);
17 17