1 // Copyright 2006 Alp Toker <alp@atoker.com>
2 // Copyright 2007 Versabanq (Adrian Dewhurst <adewhurst@versabanq.com>)
3 // This software is made available under the MIT License
4 // See COPYING for details
30 public class ExternalAuthClient
: SaslProcess
32 public override bool SupportNonBlocking { get { return false; }
}
35 public override bool Done { get { return done; }
}
37 public override bool OK { get { return ok; }
}
41 public ExternalAuthClient(WvDbus conn
, WvBufStream s
) : base(conn
)
46 // This has to be a separate function so we can delay JITting it until
47 // we're sure it's mono.
48 string MonoAuthString()
50 try { //will work in Mono on Linux.
51 return UnixUserInfo
.GetRealUserId().ToString();
52 } catch { return "WIN32"; }
58 return MonoAuthString();
60 return "WIN32"; // FIXME do something better?
63 public override void Run()
65 byte[] bs
= AuthString().ToUTF8();
66 string authStr
= ToHex(bs
);
68 s
.print("AUTH EXTERNAL {0}\r\n", authStr
);
70 string ok_rep
= s
.getline(-1, '\n').or("");
71 string[] parts
= ok_rep
.Split(' ');
73 if (parts
.Length
< 1 || parts
[0] != "OK") {
76 (wv
.fmt("Authentication error: AUTH EXTERNAL "
77 + "was not OK: \"{0}\"", ok_rep
));
86 public enum SaslMechResponse
{
92 public abstract class SaslProcess
{
93 protected WvDbus conn
;
95 public abstract bool SupportNonBlocking { get; }
96 public abstract bool Done { get; }
97 public abstract bool OK { get; }
99 public virtual void Run()
101 while (ProcessLine()) {
105 public virtual bool ProcessLine()
107 throw new NotImplementedException();
110 protected SaslProcess(WvDbus conn
)
115 //From Mono.Unix.Native.NativeConvert
116 //should these methods use long or (u)int?
117 public static DateTime
UnixToDateTime (long time
)
119 DateTime LocalUnixEpoch
= new DateTime (1970, 1, 1);
120 TimeSpan LocalUtcOffset
= TimeZone
.CurrentTimeZone
.GetUtcOffset (DateTime
.UtcNow
);
121 return LocalUnixEpoch
.AddSeconds ((double) time
+ LocalUtcOffset
.TotalSeconds
);
124 public static long DateTimeToUnix (DateTime time
)
126 DateTime LocalUnixEpoch
= new DateTime (1970, 1, 1);
127 TimeSpan LocalUtcOffset
= TimeZone
.CurrentTimeZone
.GetUtcOffset (DateTime
.UtcNow
);
128 TimeSpan unixTime
= time
.Subtract (LocalUnixEpoch
) - LocalUtcOffset
;
130 return (long) unixTime
.TotalSeconds
;
133 static public string ToHex(byte[] input
)
135 return input
.ToHex().ToLowerInvariant();
138 //From Mono.Security.Cryptography
139 static private byte FromHexChar (char c
)
141 if ((c
>= 'a') && (c
<= 'f'))
142 return (byte) (c
- 'a' + 10);
143 if ((c
>= 'A') && (c
<= 'F'))
144 return (byte) (c
- 'A' + 10);
145 if ((c
>= '0') && (c
<= '9'))
146 return (byte) (c
- '0');
147 throw new ArgumentException ("Invalid hex char");
150 //From Mono.Security.Cryptography
151 static public byte[] FromHex(string hex
)
155 if ((hex
.Length
& 0x1) == 0x1)
156 throw new ArgumentException ("Length must be a multiple of 2");
158 byte[] result
= new byte [hex
.Length
>> 1];
161 while (n
< result
.Length
) {
162 result
[n
] = (byte) (FromHexChar (hex
[i
++]) << 4);
163 result
[n
++] += FromHexChar (hex
[i
++]);
169 public abstract class SaslAuthCtx
171 protected WvDbus conn
;
173 protected SaslAuthCtx (WvDbus conn
)
178 public abstract SaslMechResponse
Data (byte[] response
,
179 out byte[] challenge
);
181 public abstract bool Accepted { get; }
183 public virtual void Aborted()
187 public virtual void Completed()
192 public delegate SaslMechResponse
SaslAuthCtxFactory (byte[] initialData
,
193 out SaslAuthCtx ctx
, out byte[] challenge
);