-
Notifications
You must be signed in to change notification settings - Fork 32
/
Configuration.cs
251 lines (232 loc) · 5.86 KB
/
Configuration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
* Copyright (C) 2006 Alex Pedenko
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace NetSync
{
public class Configuration
{
private string confFile;
public ArrayList Modules = null;
public string logFile;
public string port;
public string address;
public Configuration(string cFile)
{
confFile = Path.Combine(Environment.SystemDirectory,Path.GetFileName(cFile));
}
public int GetNumberModule(string nameModule)
{
lock(this)
{
for(int i=0; i < Modules.Count; i++)
if(((Module)Modules[i]).Name == nameModule)
return i;
}
return -1;
}
public Module GetModule(int numberModule)
{
lock(this)
{
if(numberModule < 0 || numberModule > Modules.Count)
return null;
return (Module)Modules[numberModule];
}
}
public string GetModuleName(int numberModule){
lock(this)
return GetModule(numberModule).Name;
}
public bool ModuleIsReadOnly(int numberModule)
{
lock(this)
return GetModule(numberModule).ReadOnly;
}
public bool ModuleIsWriteOnly(int numberModule)
{
lock(this)
return GetModule(numberModule).WriteOnly;
}
public string GetHostsAllow(int numberModule)
{
lock(this)
return GetModule(numberModule).HostsAllow;
}
public string GetHostsDeny(int numberModule)
{
lock(this)
return GetModule(numberModule).HostsDeny;
}
public string GetAuthUsers(int numberModule)
{
lock(this)
return GetModule(numberModule).AuthUsers;
}
public string GetSecretsFile(int numberModule)
{
lock(this)
return GetModule(numberModule).SecretsFile;
}
public bool LoadParm(Options options)
{
lock(this)
{
TextReader cf;
// TODO: path length
if(confFile == null || confFile.CompareTo("") == 0 || !FileSystem.File.Exists(confFile))
{
MainClass.Exit("Can't find .conf file: " + confFile, null);
return false;
}
try
{
cf = new System.IO.StreamReader(confFile);
}
catch
{
MainClass.Exit("failed to open: " + confFile, null);
return false;
}
Module mod = null;
if(Modules == null)
Modules = new ArrayList();
lock(cf)
{
while(true)
{
string line = cf.ReadLine();
if(line == null)
break;
line = line.Trim();
if(line.CompareTo("") != 0 && line[0] != ';' && line[0] != '#')
{
if(line[0] == '[' && line[line.Length - 1] == ']')
{
line = line.TrimStart('[').TrimEnd(']');
int numberModule = -1;
if((numberModule = GetNumberModule(line)) >= 0)
{
mod = GetModule(numberModule);
}
else
{
mod = new Module(line);
Modules.Add(mod);
}
}
else
{
if(mod != null)
{
string[] parm = line.Split('=');
if(parm.Length > 2)
continue;
parm[0] = parm[0].Trim().ToLower();
parm[1] = parm[1].Trim();
switch(parm[0])
{
case "path":
mod.Path = parm[1].Replace(@"\","/");
break;
case "comment":
mod.Comment = parm[1];
break;
case "read only":
mod.ReadOnly = (parm[1].CompareTo("false") == 0) ? false : true;
break;
case "write only":
mod.WriteOnly = (parm[1].CompareTo("true") == 0) ? true : false;
break;
case "hosts allow":
mod.HostsAllow = parm[1];
break;
case "hosts deny":
mod.HostsDeny = parm[1];
break;
case "auth users":
mod.AuthUsers = parm[1];
break;
case "secrets file":
mod.SecretsFile = Path.GetFileName(parm[1]);
break;
default:
continue;
}
}
else
{
string[] parm = line.Split('=');
if(parm.Length > 2)
continue;
parm[0] = parm[0].Trim();
parm[1] = parm[1].Trim();
switch(parm[0])
{
case "log file":
string logFile = parm[1];
try
{
options.logFile = new FileStream(logFile, FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write);
}
catch(Exception e)
{
Log.Write(e.Message);
}
break;
case "port":
port = parm[1];
options.rsyncPort = Convert.ToInt32(port);
break;
case "address":
options.bindAddress = address = parm[1];
break;
default:
continue;
}
}
}
}
}
cf.Close();
}
}
return true;
}
}
public class Module
{
public string Name;
public string Path = "";
public string Comment = "";
public bool ReadOnly = true;
public bool WriteOnly = false;
public string HostsAllow = "";
public string HostsDeny = "";
public string AuthUsers = "";
public string SecretsFile = "";
public Module(string name)
{
Name = name;
}
}
}