2 * Copyright (c) 2007,西安邮电学院Linux兴趣小组
5 * 文件名称:read_configure.c
22 #define CONFIG_NUM 13 /*item number of the configue file*/
23 struct run_env run_env
;
25 int _read_line(int fd
,char *buf
);
26 int _analyze_para(char *buf
,int *array
);
27 int _check_configure(int *array
);
33 printf("run_env.anonymous_enable = %d\n",run_env
.anonymous_enable
);
34 printf("run_env.ftp_port = %d\n",run_env
.ftp_port
);
35 printf("run_env.local_umask = %d\n",run_env
.local_umask
);
36 printf("run_env.log_file_enable = %d\n",run_env
.log_file_enable
);
37 printf("run_env.log_file = %s\n",run_env
.log_file
);
38 printf("run_env.idle_session_timeout = %d\n",run_env
.idle_session_timeout
);
39 printf("run_env.data_connection_timeout = %d \n",run_env
.data_connection_timeout
);
40 printf("run_env.ftpd_banner = %s\n",run_env
.ftpd_banner
);
41 printf("run_env.max_clients = %d\n",run_env
.max_clients
);
42 printf("run_env.max_links = %d\n",run_env
.max_links
);
43 printf("run_env.passive_port = %d,%d\n",run_env
.passive_port_min
,run_env
.passive_port_max
);
44 printf("run_env.ftp_dir = %s\n",run_env
.ftp_dir
);
45 printf("run_env.user_pass_file = %s\n",run_env
.user_pass_file
);
49 int _analyze_para(char *buf
,int *array
)
52 char *option
= NULL
,*value
= NULL
; /*used to store the option strings and value strings*/
53 char *saveptr1
,*saveptr2
; /*used for the function strtok_r*/
54 if ((tmp
= strtok_r(buf
,"=",&saveptr1
)) == NULL
) {
55 fprintf(stderr
,"error in configure file\n");
57 } /*get the option string*/
58 if ((option
= malloc(sizeof(char) * (strlen(tmp
) + 1))) == NULL
) {
59 fprintf(stderr
,"Not enough memory!\n");
62 strcpy(option
,buf
); /*store option*/
63 if((tmp
= strtok_r(NULL
,"=",&saveptr1
)) == NULL
) {
64 fprintf(stderr
,"error in configure file\n");
67 if ((value
= malloc(sizeof(char) * ((strlen(tmp
) + 1)))) == NULL
) {
68 fprintf(stderr
,"Not enough memory!\n");
71 strcpy(value
,tmp
); /*store value*/
73 /*codes below are used to value the parameters of the struct run_env*/
74 if (!strcmp(option
,"Anonymous_enable")) {
75 if (!strcmp(value
,"YES")) {
76 run_env
.anonymous_enable
= TRUE
;
78 run_env
.anonymous_enable
= FALSE
;
81 } else if (!strcmp(option
,"FTP_port")) {
82 run_env
.ftp_port
= (unsigned int)(atoi(value
));
84 } else if (!strcmp(option
,"Local_umask")) {
85 run_env
.local_umask
= (unsigned int)(atoi(value
));
87 } else if (!strcmp(option
,"Log_file_enable")) {
88 if (!strcmp(value
,"YES")) {
89 run_env
.log_file_enable
= 1;
91 run_env
.log_file_enable
= 0;
94 } else if (!strcmp(option
,"Log_file")) {
95 if ((run_env
.log_file
= malloc((strlen(value
) + 1) * sizeof (char))) == NULL
) {
96 fprintf(stderr
,"Not enough memory!\n");
99 strcpy(run_env
.log_file
,value
);
101 } else if (!strcmp(option
,"Idle_session_timeout")) {
102 run_env
.idle_session_timeout
= (unsigned int)(atoi(value
));
104 } else if (!strcmp(option
,"Data_connection_timeout")) {
105 run_env
.data_connection_timeout
= (unsigned int)(atoi(value
));
107 } else if (!strcmp(option
,"Ftpd_banner")) {
108 if ((run_env
.ftpd_banner
= malloc((strlen(value
) + 1) * sizeof (char))) == NULL
) {
109 fprintf(stderr
,"Not enough memory!\n");
112 strcpy(run_env
.ftpd_banner
,value
);
114 } else if (!strcmp(option
,"Max_clients")) {
115 run_env
.max_clients
= (unsigned int)(atoi(value
));
117 } else if (!strcmp(option
,"Max_links")) {
118 run_env
.max_links
= (unsigned int)(atoi(value
));
120 } else if (!strcmp(option
,"Passive_port")) {
121 if ((tmp
= strtok_r(value
,",",&saveptr2
)) == NULL
) {
122 fprintf(stderr
,"error in configure file\n");
125 run_env
.passive_port_min
= (unsigned int)(atoi(tmp
)); /*get the min_port and store it*/
126 if ((tmp
= strtok_r(NULL
,",",&saveptr2
)) == NULL
) {
127 fprintf(stderr
,"error in configure file\n");
129 } /*get the max_port and store it*/
130 run_env
.passive_port_max
= (unsigned int)(atoi(tmp
));
132 } else if (!strcmp(option
,"FTP_dir")) {
133 if ((strlen(value
) + 1) > PATH_NAME_LEN
) {
134 fprintf(stderr
, "Path name is too long!\n");
137 strcpy(run_env
.ftp_dir
,value
);
139 } else if (!strcmp(option
,"User_pass_file")) {
140 if ((run_env
.user_pass_file
= malloc((strlen(value
) + 1) * sizeof (char))) == NULL
) {
141 fprintf(stderr
,"Not enough memory!\n");
144 strcpy(run_env
.user_pass_file
,value
);
147 fprintf(stderr
,"Parameter can't be analyzed!\n");
150 /*codes above are used to value the parameters of the struct run_env*/
154 free(run_env
.user_pass_file
); /*free the memory*/
155 free(run_env
.log_file
); /*free the memory*/
156 free(value
); /*free the memory*/
157 free(option
); /*free the memory*/
165 char buffer
[_BUFFER_LENGTH
];
167 int config_array
[CONFIG_NUM
];
169 for (i
= 0;i
< CONFIG_NUM
;i
++) {
174 if ((fd
= open(CONFIG_FILE
,O_RDONLY
)) == -1) { /*open the configure file error*/
175 fprintf(stderr
,"can't open the configure file,please use the tool xylftppass to solve this problem!\n");
178 i
= _read_line(fd
,buffer
); /*get an valuable line from the configure file*/
183 printf("%s\n",buffer
);
185 _analyze_para(buffer
,config_array
); /*analyze the line got from the file*/
187 i
= _read_line(fd
,buffer
);
194 if (_check_configure(config_array
) == -1) {
195 /*write_log("configure file error!",0);*/
196 fprintf(stderr
,"configure file error!");
202 int _read_line(int fd
,char *buf
)
207 if ((j
= read(fd
,&ch
,1)) == 0) { /*end of the file*/
213 while (ch
!= '\n' && j
!= 0 ){ /*if the character we got is not an end of a line or the end of the file*/
214 if (ch
== '#' ) { /*if it is a comment line,skip it over*/
215 while (ch
!= '\n' ) {
216 if ((j
= read(fd
,&ch
,1)) == 0) {
221 } else if (ch
== ' ') { /*if it is a space,skip to the next character*/
226 } else if (j
!= 0){ /*a valuable character,store it*/
232 if ( j
== 0 ) { /*end of the line,also the end of the file,return EOF*/
235 } else if (i
!= 0) { /*end of an valuable line*/
242 int _check_configure(int *array
)
246 fprintf(stderr
,"no Anonymous_enable item or error\n");
249 fprintf(stderr
,"no FTP_port item or error\n");
252 fprintf(stderr
,"no Local_umask item or error\n");
255 fprintf(stderr
,"no Log_file_enable item or error\n");
258 fprintf(stderr
,"no Log_file item or error\n");
261 fprintf(stderr
,"no Idle_session_timeout item or error\n");
264 fprintf(stderr
,"no Data_connection_timeout item or error\n");
267 fprintf(stderr
,"no Ftpd_banner item or error\n");
270 fprintf(stderr
,"no Max_clients item or error\n");
273 fprintf(stderr
,"no Max_links item or error\n");
276 fprintf(stderr
,"no Passive_port item or error\n");
279 fprintf(stderr
,"no FTP_dir item or error\n");
282 fprintf(stderr
,"no User_pass_file item or error\n");
285 for (i
= 0;i
< CONFIG_NUM
;i
++) {