This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
forked from MatthewSteeples/rsync.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Configuration.cs
333 lines (316 loc) · 13.1 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* 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.Generic;
using System.IO;
namespace NetSync
{
/// <summary>
///
/// </summary>
public class Configuration
{
private string confFile;
public List<Module> Modules = null;
public string logFile;
public string port;
public string address;
/// <summary>
/// Creates new instance and sets confFile to system.directory + cFile
/// </summary>
/// <param name="cFile"></param>
public Configuration(string cFile)
{
confFile = Path.Combine(Environment.SystemDirectory, Path.GetFileName(cFile));
}
/// <summary>
/// Finds number of module using its name
/// </summary>
/// <param name="nameModule"></param>
/// <returns></returns>
public int GetNumberModule(string nameModule)
{
lock (this)
{
for (int i = 0; i < Modules.Count; i++)
{
if (Modules[i].Name == nameModule)
{
return i;
}
}
}
return -1;
}
/// <summary>
/// Get Module by its number from list od Modules
/// </summary>
/// <param name="numberModule"></param>
/// <returns></returns>
public Module GetModule(int numberModule)
{
lock (this)
{
if (numberModule < 0 || numberModule > Modules.Count)
{
return null;
}
return (Module)Modules[numberModule];
}
}
/// <summary>
/// Finds name of module using its number
/// </summary>
/// <param name="numberModule"></param>
/// <returns></returns>
public string GetModuleName(int numberModule)
{
lock (this)
{
return GetModule(numberModule).Name;
}
}
/// <summary>
/// Checks whether Module, located at given number, is ReadOnly
/// </summary>
/// <param name="numberModule"></param>
/// <returns></returns>
public bool ModuleIsReadOnly(int numberModule)
{
lock (this)
{
return GetModule(numberModule).ReadOnly; //@todo GetModule may return null and thus nullref exception
}
}
/// <summary>
/// Checks whether Module, located at given number, is WriteOnly
/// </summary>
/// <param name="numberModule"></param>
/// <returns></returns>
public bool ModuleIsWriteOnly(int numberModule)
{
lock (this)
{
return GetModule(numberModule).WriteOnly; //@todo GetModule may return null and thus nullref exception
}
}
/// <summary>
/// Gets string, which contains allowed hosts for Module, located at given number
/// </summary>
/// <param name="numberModule"></param>
/// <returns></returns>
public string GetHostsAllow(int numberModule)
{
lock (this)
{
return GetModule(numberModule).HostsAllow; //@todo GetModule may return null and thus nullref exception
}
}
/// <summary>
/// Gets string, which contains denied hosts for Module, located at given number
/// </summary>
/// <param name="numberModule"></param>
/// <returns></returns>
public string GetHostsDeny(int numberModule)
{
lock (this)
{
return GetModule(numberModule).HostsDeny; //@todo GetModule may return null and thus nullref exception
}
}
/// <summary>
/// Gets string of allowed users for Module, located at given number
/// </summary>
/// <param name="numberModule"></param>
/// <returns></returns>
public string GetAuthUsers(int numberModule)
{
lock (this)
{
return GetModule(numberModule).AuthUsers; //@todo GetModule may return null and thus nullref exception
}
}
/// <summary>
/// Get name of secret file for Module, located at given number
/// </summary>
/// <param name="numberModule"></param>
/// <returns></returns>
public string GetSecretsFile(int numberModule)
{
lock (this)
{
return GetModule(numberModule).SecretsFile; //@todo GetModule may return null and thus nullref exception
}
}
/// <summary>
///
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
public bool LoadParm(Options options)
{
lock (this)
{
// TODO: path length
if (confFile == null || confFile.CompareTo(String.Empty) == 0 || !File.Exists(confFile))
{
MainClass.Exit("Can't find .conf file: " + confFile, null);
return false;
}
try
{
using (var cf = new System.IO.StreamReader(confFile))
{
Module mod = null;
if (Modules == null)
{
Modules = new List<Module>();
}
lock (cf)
{
while (true)
{
string line = cf.ReadLine();
if (line == null)
{
break;
}
line = line.Trim();
if (line.CompareTo(String.Empty) != 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();
}
}
}
catch
{
MainClass.Exit("failed to open: " + confFile, null);
return false;
}
}
return true;
}
}
public class Module
{
public string Name;
public string Path = String.Empty;
public string Comment = String.Empty;
public bool ReadOnly = true;
public bool WriteOnly = false;
public string HostsAllow = String.Empty;
public string HostsDeny = String.Empty;
public string AuthUsers = String.Empty;
public string SecretsFile = String.Empty;
public Module(string name)
{
Name = name;
}
}
}