| ... | ... |
@@ -20,64 +20,71 @@ |
| 20 | 20 |
|
| 21 | 21 |
#include "sshchatd_config.h" |
| 22 | 22 |
|
| 23 |
+#define USERSBLOCK 128 |
|
| 24 |
+ |
|
| 23 | 25 |
cconfig * |
| 24 | 26 |
cconfig_init(void) |
| 25 | 27 |
{
|
| 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); |
|
| 28 |
+ cconfig *conf; |
|
| 29 |
+ DIR *dir; |
|
| 30 |
+ struct dirent *e; |
|
| 31 |
+ int pass,count,total; |
|
| 32 |
+ int nblocks; |
|
| 33 |
+ if((conf=malloc(sizeof(cconfig)))==NULL) |
|
| 34 |
+ return(NULL); |
|
| 35 |
+ memset(conf,0,sizeof(cconfig)); |
|
| 36 |
+ mkdir("users",0700);
|
|
| 37 |
+ if((dir=opendir("users"))==NULL) {
|
|
| 38 |
+ free(conf),conf=NULL; |
|
| 39 |
+ return(NULL); |
|
| 40 |
+ } |
|
| 41 |
+ for(pass=0,count=0;pass<=1;pass++,rewinddir(dir),total=count,count=0) {
|
|
| 42 |
+ while((e=readdir(dir))!=NULL) {
|
|
| 43 |
+ if(e->d_type!=DT_DIR) |
|
| 44 |
+ continue; |
|
| 45 |
+ if(!(e->d_name[0]>='a' && e->d_name[0]<='z') && |
|
| 46 |
+ !(e->d_name[0]>='A' && e->d_name[0]<='Z')) |
|
| 47 |
+ continue; /* usernames must start by a letter */ |
|
| 48 |
+ if(pass==0) {
|
|
| 49 |
+ count++; |
|
| 50 |
+ } else if(count<total) {
|
|
| 51 |
+ if((conf->users[conf->numusers]=strdup(e->d_name))==NULL) {
|
|
| 52 |
+ cconfig_free(conf),conf=NULL; |
|
| 53 |
+ return(NULL); /* insuf. mem. */ |
|
| 54 |
+ } |
|
| 55 |
+ conf->numusers++; |
|
| 56 |
+ count++; |
|
| 57 |
+ } |
|
| 58 |
+ } |
|
| 59 |
+ if(pass==0) {
|
|
| 60 |
+ nblocks=((count+USERSBLOCK-1))/USERSBLOCK; |
|
| 61 |
+ if(count>0 && (conf->users=malloc(sizeof(char *)*USERSBLOCK*nblocks))==NULL) {
|
|
| 62 |
+ closedir(dir),dir=NULL; |
|
| 63 |
+ cconfig_free(conf),conf=NULL; |
|
| 64 |
+ return(NULL); /* No users or insuf. mem. */ |
|
| 65 |
+ } |
|
| 66 |
+ memset(conf->users,0,sizeof(char *)*USERSBLOCK*nblocks); |
|
| 67 |
+ conf->sizeusers=USERSBLOCK*nblocks; |
|
| 68 |
+ } |
|
| 69 |
+ } |
|
| 70 |
+ return(conf); |
|
| 66 | 71 |
} |
| 67 | 72 |
|
| 68 | 73 |
void |
| 69 | 74 |
cconfig_free(cconfig *conf) |
| 70 | 75 |
{
|
| 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; |
|
| 76 |
+ int i; |
|
| 77 |
+ if(conf==NULL) |
|
| 78 |
+ return; |
|
| 79 |
+ if(conf->users!=NULL) {
|
|
| 80 |
+ for(i=0;i<conf->numusers;i++) {
|
|
| 81 |
+ if(conf->users[i]!=NULL) |
|
| 82 |
+ free(conf->users[i]),conf->users[i]=NULL; |
|
| 83 |
+ } |
|
| 84 |
+ conf->numusers=0; |
|
| 85 |
+ conf->sizeusers=0; |
|
| 86 |
+ free(conf->users),conf->users=NULL; |
|
| 87 |
+ } |
|
| 88 |
+ free(conf),conf=NULL; |
|
| 83 | 89 |
} |
| 90 |
+ |
| 1 | 1 |
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 |
+} |