Browse code

Initial configuration management for sshchatd

Dario Rodriguez authored on 17/12/2015 21:38:20
Showing 6 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,2 @@
1
+src/.sshchat
2
+src/users
... ...
@@ -1 +1 @@
1
-myid
1
+testuser1
... ...
@@ -2,11 +2,14 @@ CC=gcc
2 2
 CFLAGS=-g -Wall -I../../webkernel/src
3 3
 LDFLAGS=
4 4
 
5
-all: sshchat
5
+all: sshchat sshchatd
6 6
 
7 7
 sshchat: sshchat.o sbuf.o socklib.o
8 8
 	$(CC) sshchat.o sbuf.o socklib.o -o sshchat
9 9
 
10
+sshchatd: sshchatd.o sshchatd_config.o
11
+	$(CC) sshchatd.o sshchatd_config.o socklib.o -o sshchatd
12
+
10 13
 sbuf.o: ../../webkernel/src/sbuf.c
11 14
 	$(CC) $(CFLAGS) $(LDFLAGS) -c ../../webkernel/src/sbuf.c -o sbuf.o
12 15
 
... ...
@@ -14,4 +17,4 @@ socklib.o: ../../webkernel/src/socklib.c
14 17
 	$(CC) $(CFLAGS) $(LDFLAGS) -c ../../webkernel/src/socklib.c -o socklib.o
15 18
 
16 19
 clean:
17
-	rm -f sshchat *.o
20
+	rm -f sshchat sshchatd *.o
... ...
@@ -7,4 +7,34 @@
7 7
  * This program is licensed under the terms of the GPL v2.1+
8 8
  */
9 9
 
10
+#include <stdlib.h>
11
+#include <stdio.h>
12
+#include <unistd.h>
13
+#include <string.h>
10 14
 
15
+#include "socklib.h"
16
+#include "sbuf.h"
17
+
18
+#include "sshchat.h"
19
+#include "sshchatd_config.h"
20
+
21
+int
22
+main(int argc, char *argv[])
23
+{
24
+	cconfig *conf;
25
+	if((conf=cconfig_init())==NULL) {
26
+		fprintf(stderr,"ERROR: Couldn't init user data. Maybe there are no users in users/ ?\n");
27
+		return(1);
28
+	}
29
+#warning TODO
30
+	fprintf(stderr,"%i users loaded.\n",conf->numusers);
31
+	{
32
+		int i;
33
+		for(i=0;i<conf->numusers;i++) {
34
+			fprintf(stderr," [%i]: \"%s\"\n",i,conf->users[i]);
35
+		}
36
+	}
37
+#warning TODO
38
+	cconfig_free(conf),conf=NULL;
39
+	return(0);
40
+}
11 41
new file mode 100644
... ...
@@ -0,0 +1,83 @@
1
+/*
2
+ * sshchatd_config.c
3
+ *
4
+ * load/save the config information
5
+ *
6
+ * Author: Dario Rodriguez dario@softhome.net
7
+ * This program is licensed under the terms of the GNU GPL v2.1+
8
+ */
9
+
10
+#include <stdio.h>
11
+#include <stdlib.h>
12
+#include <unistd.h>
13
+#include <string.h>
14
+#include <sys/stat.h>
15
+#include <sys/types.h>
16
+#ifndef _BSD_SOURCE
17
+#define _BSD_SOURCE
18
+#endif
19
+#include <dirent.h>
20
+
21
+#include "sshchatd_config.h"
22
+
23
+cconfig *
24
+cconfig_init(void)
25
+{
26
+	cconfig *conf;
27
+	DIR *dir;
28
+	struct dirent *e;
29
+	int pass,count,total;
30
+	if((conf=malloc(sizeof(cconfig)))==NULL)
31
+		return(NULL);
32
+	memset(conf,0,sizeof(cconfig));
33
+	mkdir("users",0700);
34
+	if((dir=opendir("users"))==NULL) {
35
+		free(conf),conf=NULL;
36
+		return(NULL);
37
+	}
38
+	for(pass=0,count=0;pass<=1;pass++,rewinddir(dir),total=count,count=0) {
39
+		while((e=readdir(dir))!=NULL) {
40
+			if(e->d_type!=DT_DIR)
41
+				continue;
42
+			if(!(e->d_name[0]>='a' && e->d_name[0]<='z') &&
43
+			   !(e->d_name[0]>='A' && e->d_name[0]<='Z'))
44
+				continue; /* usernames must start by a letter */
45
+			if(pass==0) {
46
+				count++;
47
+			} else if(count<total) {
48
+				if((conf->users[conf->numusers]=strdup(e->d_name))==NULL) {
49
+					cconfig_free(conf),conf=NULL;
50
+					return(NULL); /* insuf. mem. */
51
+				}
52
+				conf->numusers++;
53
+				count++;
54
+			}
55
+		}
56
+		if(pass==0) {
57
+			if((conf->users=malloc(sizeof(char *)*count))==NULL) {
58
+				closedir(dir),dir=NULL;
59
+				cconfig_free(conf),conf=NULL;
60
+				return(NULL); /* No users or insuf. mem. */
61
+			}
62
+			memset(conf->users,0,sizeof(char *)*count);
63
+		}
64
+	}
65
+	return(conf);
66
+}
67
+
68
+void
69
+cconfig_free(cconfig *conf)
70
+{
71
+	int i;
72
+	if(conf==NULL)
73
+		return;
74
+	if(conf->users!=NULL) {
75
+		for(i=0;i<conf->numusers;i++) {
76
+			if(conf->users[i]!=NULL)
77
+				free(conf->users[i]),conf->users[i]=NULL;
78
+		}
79
+		conf->numusers=0;
80
+		free(conf->users);
81
+	}
82
+	free(conf),conf=NULL;
83
+}
0 84
new file mode 100644
... ...
@@ -0,0 +1,16 @@
1
+/*
2
+ * sshchatd_config.h
3
+ *
4
+ * load/save the config information
5
+ *
6
+ * Author: Dario Rodriguez dario@softhome.net
7
+ * This program is licensed under the terms of the GNU GPL v2.1+
8
+ */
9
+
10
+typedef struct cconfig {
11
+	int numusers;
12
+	char **users;
13
+} cconfig;
14
+
15
+cconfig *cconfig_init(void);
16
+void cconfig_free(cconfig *conf);