-
Notifications
You must be signed in to change notification settings - Fork 32
/
Authentication.cs
191 lines (160 loc) · 4.92 KB
/
Authentication.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
/**
* 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.IO;
using System.Text;
namespace NetSync
{
class Authentication
{
public static string password_file = "";
public static string base64_encode(string message)
{
Encoding asciiEncoding = Encoding.ASCII;
byte[] byteArray = new byte[asciiEncoding.GetByteCount(message)];
byteArray = asciiEncoding.GetBytes(message);
return Convert.ToBase64String(byteArray);
}
public static string gen_challenge(string addr, Options opt)
{
string challenge = "";
byte[] input = new byte[32];
DateTime tv = DateTime.Now;
for(int i=0; i < addr.Length; i++)
input[i] = Convert.ToByte(addr[i]);
CheckSum.SIVAL(ref input, 16, (UInt32)tv.Second);
CheckSum.SIVAL(ref input, 20, (UInt32)tv.Hour);
CheckSum.SIVAL(ref input, 24, (UInt32)tv.Day);
Sum sum = new Sum(opt);
sum.Init(0);
sum.Update(input,0,input.Length);
challenge = Encoding.ASCII.GetString(sum.End());
return challenge;
}
public static string generate_hash(string indata, string challenge, Options options)
{
Sum sum = new Sum(options);
sum.Init(0);
sum.Update(Encoding.ASCII.GetBytes(indata), 0, indata.Length);
sum.Update(Encoding.ASCII.GetBytes(challenge), 0, challenge.Length);
byte[] buf = sum.End();
string hash = Convert.ToBase64String(buf);
return hash.Substring(0, (buf.Length * 8 + 5)/6);
}
public static string auth_client( string user, string pass, string challenge, Options options)
{
if(String.Compare(user, "") == 0)
user = "nobody";
if(pass == null || String.Compare(pass, "") == 0)
pass = getpassf(password_file);
if(String.Compare(pass, "") == 0)
pass = System.Environment.GetEnvironmentVariable("RSYNC_PASSWORD");
if(pass == null || String.Compare(pass, "") == 0)
pass = getpass();
string pass2 = generate_hash(pass, challenge, options);
Log.WriteLine(user + " " + pass2);
return pass2;
}
public static string getpass()
{
Console.Write("Password: ");
return Console.ReadLine();
}
public static string getpassf(string filename)
{
return "";
}
public static bool AuthServer(ClientInfo cInfo, int moduleNumber, string addr, string leader)
{
string users = Daemon.config.GetAuthUsers(moduleNumber).Trim();
string challenge;
string b64_challenge;
IOStream f = cInfo.IoStream;
string line;
string user = "";
string secret = "";
string pass = "";
string pass2 = "";
string[] listUsers;
string tok = "";
/* if no auth list then allow anyone in! */
if (users == null || users.CompareTo("") == 0)
return true;
challenge = gen_challenge(addr, cInfo.Options);
b64_challenge = base64_encode(challenge);
f.IOPrintf(leader + b64_challenge + "\n");
line = f.ReadLine();
if(line.IndexOf(' ') > 0)
{
user = line.Substring(0,line.IndexOf(' '));
pass = line.Substring(line.IndexOf(' ')).Trim('\n').Trim();
} else
return false;
listUsers = users.Split(',');
for (int i = 0; i < listUsers.Length; i++)
{
tok = listUsers[i];
if (user.CompareTo(tok) == 0)
break;
tok = null;
}
if (tok == null || tok.CompareTo("") == 0)
return false;
if ((secret = GetSecret(moduleNumber, user)) == null)
{
return false;
}
pass2 = generate_hash(secret, b64_challenge, cInfo.Options);
if (pass.CompareTo(pass2) == 0)
return true;
return false;
}
static string GetSecret(int moduleNumber, string user)
{
string fname = Path.Combine(Environment.SystemDirectory,Daemon.config.GetSecretsFile(moduleNumber));
string secret = null;
TextReader fd;
if (fname == null || fname.CompareTo("") == 0)
return null;
try{
fd = new System.IO.StreamReader(fname);
} catch(Exception) {
return null;
}
while(true)
{
string line = fd.ReadLine();
if(line == null)
break;
line.Trim();
if(line.CompareTo("") != 0 && (line[0] != ';' && line[0] != '#'))
{
string[] userp = line.Split(':');
if(userp[0].Trim().CompareTo(user) == 0)
{
secret = userp[1].Trim();
break;
}
}
}
fd.Close();
return secret;
}
}
}