Browse code

implement kakumei_pass, add ÃÃlibscrypt to Makefile

Dario Rodriguez authored on 25/06/2014 19:22:09
Showing 5 changed files
... ...
@@ -2,6 +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 6
 
6 7
 all: kakumei kakumei-invite
7 8
 
... ...
@@ -11,7 +12,7 @@ clean:
11 12
 kakumei: kakumei.o loglib.o parselib.o sbuf.o \
12 13
 		socklib.o webkernel.o gen_res.o \
13 14
 		kakumei_session.o kakumei_pass.o
14
-	$(CC) $(LDFLAGS) kakumei.o loglib.o parselib.o sbuf.o \
15
+	$(CC) $(LDFLAGS_KAKUMEI) kakumei.o loglib.o parselib.o sbuf.o \
15 16
 		socklib.o webkernel.o gen_res.o \
16 17
 		kakumei_session.o kakumei_pass.o \
17 18
 		-o kakumei
... ...
@@ -63,6 +63,34 @@ main(int argc, char *argv[])
63 63
         return(0);
64 64
 }
65 65
 
66
+int
67
+kakumei_uservalid(kakumei *ka, char *username)
68
+{
69
+        if(username==NULL)
70
+                return(-1);
71
+        if(strchr(username,'/')!=NULL)
72
+                return(-1);
73
+        if(strcmp(username,".")==0 || strcmp(username,"..")==0)
74
+                return(-1);
75
+        return(0);
76
+}
77
+
78
+int
79
+kakumei_userexists(kakumei *ka, char *username)
80
+{
81
+        char filename[1024];
82
+        struct stat st;
83
+        if(kakumei_uservalid(ka,username)!=0)
84
+                return(-1);
85
+        snprintf(filename,sizeof(filename),"%s/%s/passwd",USERSDIR,username);
86
+        filename[sizeof(filename)-1]='\0';
87
+        if(stat(filename,&st)!=0 || !S_ISREG(st.st_mode))
88
+                return(-1);
89
+        return(0);
90
+}
91
+
92
+
93
+
66 94
 wk_action
67 95
 callback_http(wk *web, int connid, wk_uri *uri, void *userptr)
68 96
 {
... ...
@@ -27,4 +27,7 @@ typedef struct kakumei {
27 27
         wk *web;
28 28
 } kakumei;
29 29
 
30
+int kakumei_uservalid(kakumei *ka, char *username); /* no unallowed characters */
31
+int kakumei_userexists(kakumei *ka, char *username); /* it has a directory with a passwd file */
32
+
30 33
 #endif
... ...
@@ -7,19 +7,57 @@
7 7
  * This progran is licensed under the terms of the Affero GPL v1+
8 8
  */
9 9
 
10
+#include <stdio.h>
11
+#include <stdlib.h>
12
+#include <unistd.h>
13
+#include <string.h>
14
+#include <sys/types.h>
15
+#include <sys/stat.h>
16
+#include <fcntl.h>
10 17
 #include "kakumei.h"
18
+#include "libscrypt.h"
11 19
 
12 20
 int
13 21
 pass_new(kakumei *ka, char *user, char *passwd)
14 22
 {
15
-#warning TODO
16
-        return(-1);
23
+        int fd;
24
+        char filename[1024];
25
+        char mcf[SCRYPT_MCF_LEN+1];
26
+        int len;
27
+        if(kakumei_uservalid(ka,user)!=0)
28
+                return(-1);
29
+        snprintf(filename,sizeof(filename)-1,"%s/%s/passwd",USERSDIR,user);
30
+        filename[sizeof(filename)-1]='\0';
31
+        memset(mcf,0,sizeof(mcf));
32
+        libscrypt_hash(mcf,passwd,SCRYPT_N,SCRYPT_r,SCRYPT_p);
33
+        if((fd=open(filename,O_WRONLY|O_TRUNC|O_CREAT,0600))==-1)
34
+                return(-1);
35
+        len=strlen(mcf);
36
+        if(write(fd,mcf,len)!=len) {
37
+                close(fd),fd=-1;
38
+                return(-1);
39
+        }
40
+        close(fd),fd=-1;
41
+        return(0);
17 42
 }
18 43
 
19 44
 int
20 45
 pass_check(kakumei *ka, char *user, char *passwd)
21 46
 {
22
-#warning TODO
23
-        return(-1);
47
+        int fd;
48
+        char filename[1024];
49
+        char mcf[SCRYPT_MCF_LEN+1];
50
+        if(kakumei_userexists(ka,user)!=0)
51
+                return(-1);
52
+        snprintf(filename,sizeof(filename)-1,"%s/%s/passwd",USERSDIR,user);
53
+        filename[sizeof(filename)-1]='\0';
54
+        if((fd=open(filename,O_RDONLY))==-1)
55
+                return(-1);
56
+        memset(mcf,0,sizeof(mcf));
57
+        read(fd,mcf,sizeof(mcf)-1);
58
+        close(fd),fd=-1;
59
+        if(libscrypt_check(mcf,passwd)<=0)
60
+                return(-1);
61
+        return(0);
24 62
 }
25 63
 
... ...
@@ -7,12 +7,16 @@
7 7
  * This progran is licensed under the terms of the Affero GPL v1+
8 8
  */
9 9
 
10
+#include <sys/stat.h>
11
+#include <sys/types.h>
10 12
 #include "kakumei.h"
11 13
 #include "kakumei_session.h"
12 14
 
13 15
 char *
14 16
 session_new(kakumei *ka, char *user, char *session, int sessionsize)
15 17
 {
18
+        mkdir(DATADIR,0700);
19
+        mkdir(SESSIONSDIR,0700);
16 20
 
17 21
 }
18 22