Allow schema files that are missing checksums on the !!SCHEMAMATIC line.
[versaplex.git] / wvdbus-sharp / auth.cs
blob4761c60217c0787d51b3c970b8919aba6a1e0a8a
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
5 //
6 using System;
7 using Wv;
8 using Wv.Extensions;
9 using Mono.Unix;
11 namespace Wv.Dbus
13 enum ClientState
15 WaitingForData,
16 WaitingForOK,
17 WaitingForReject,
18 Terminate
21 enum ServerState
23 WaitingForAuth,
24 WaitingForData,
25 WaitingForBegin,
26 Terminate,
27 DoneAuth
30 public class ExternalAuthClient : SaslProcess
32 public override bool SupportNonBlocking { get { return false; } }
34 bool done = false;
35 public override bool Done { get { return done; } }
36 bool ok = false;
37 public override bool OK { get { return ok; } }
39 WvBufStream s;
41 public ExternalAuthClient(WvDbus conn, WvBufStream s) : base(conn)
43 this.s = s;
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"; }
55 string AuthString()
57 if (Wv.wv.IsMono())
58 return MonoAuthString();
59 else
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") {
74 done = true;
75 throw new Exception
76 (wv.fmt("Authentication error: AUTH EXTERNAL "
77 + "was not OK: \"{0}\"", ok_rep));
80 s.print("BEGIN\r\n");
81 done = true;
82 ok = true;
86 public enum SaslMechResponse {
87 Continue,
88 OK,
89 Reject
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)
112 this.conn = 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)
153 if (hex == null)
154 return null;
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];
159 int n = 0;
160 int i = 0;
161 while (n < result.Length) {
162 result [n] = (byte) (FromHexChar (hex [i++]) << 4);
163 result [n++] += FromHexChar (hex [i++]);
165 return result;
169 public abstract class SaslAuthCtx
171 protected WvDbus conn;
173 protected SaslAuthCtx (WvDbus conn)
175 this.conn = 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);