1
/****************************************************************************
3
| Copyright (c) 2007 Novell, Inc.
6
| This program is free software; you can redistribute it and/or
7
| modify it under the terms of version 2 of the GNU General Public License as
8
| published by the Free Software Foundation.
10
| This program is distributed in the hope that it will be useful,
11
| but WITHOUT ANY WARRANTY; without even the implied warranty of
12
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
| GNU General Public License for more details.
15
| You should have received a copy of the GNU General Public License
16
| along with this program; if not, contact Novell, Inc.
18
| To contact Novell about this file by physical or electronic mail,
19
| you may find current contact information at www.novell.com
21
| Author: Russ Young ryoung@novell.com
22
|***************************************************************************/
27
using System.Web.Services;
28
using System.Web.Services.Protocols;
29
using System.Security.Cryptography;
30
using System.Threading;
36
//using Simias.Enterprise;
41
/// HostService - WebService class
43
public class HostService : WebService
45
/// <summary>Instance of the store.</summary>
46
static protected Store store = Store.GetStore();
47
/// <summary>Property Tag used to store the ID of the Home Server.</summary>
48
static protected string HomeServerTag = "HomeServer";
50
protected Domain domain
52
get { return store.GetDomain( store.DefaultDomain ); }
55
protected Domain hostDomain
57
get { return store.GetDomain( store.DefaultDomain ); }
61
/// Gets the member object in the domain for the specified user.
63
/// <param name="userID">The ID of the user.</param>
64
/// <returns>The member object, or null if not found.</returns>
65
protected Member GetDomainMemberByID( string userID )
69
return domain.GetMemberByID( userID );
75
protected Member GetDomainMemberByName( string userName )
79
return domain.GetMemberByName( userName );
85
protected Member GetAuthenticatedUser()
87
string userID = Thread.CurrentPrincipal.Identity.Name;
88
if (userID != null && userID.Length != 0)
90
return domain.GetMemberByID( userID );
97
/// HostLocation - WebService class
99
[WebService(Namespace = "http://novell.com/simias/host/location")]
100
public class HostLocation : HostService
105
public HostLocation()
110
/// Determins if the collection is on this Host.
112
/// <param name="collectionID">The ID of the collection to check for.</param>
113
/// <returns>True if hosted on this server, otherwise false.</returns>
114
[WebMethod(EnableSession=true)]
115
public bool IsCollectionOnHost( string collectionID )
117
// This call requires no authentication.
118
if ( store.GetCollectionByID( collectionID ) != null )
123
// The collection is not here return false.
128
/// Returns the associated host information for a given collection.
130
/// <param name="CollectionID">The id of a collection.</param>
131
/// <returns>HostInformation.</returns>
132
[WebMethod(EnableSession=true)]
133
public HostInformation GetCollectionLocation( string CollectionID )
135
HostInformation hostinformation = null;
136
CatalogEntry catEntry = Catalog.GetEntryByCollectionID( CollectionID );
137
if ( catEntry != null )
139
HostNode hostnode = HostNode.GetHostByID( store.DefaultDomain, catEntry.HostID );
140
if ( hostnode != null )
142
hostinformation = new HostInformation( hostnode );
146
return hostinformation;
150
/// Returns the home server for the specified user.
152
/// <param name="userName">The name of the user.</param>
153
/// <returns>The ID of the machine that is the home server for this user.</returns>
154
[WebMethod(EnableSession=true)]
155
public HostInformation GetHomeServer( string Username )
157
Member member = GetDomainMemberByName( Username );
158
if ( member != null )
160
HostNode host = member.HomeServer;
163
return new HostInformation( host );
166
// Call the provision service to provision this
168
HostInfo info = ProvisionService.ProvisionUser( Username );
171
return new HostInformation( info );
179
/// Method to get HostInfo for a specified host
181
[WebMethod(EnableSession=true)]
182
public HostInformation GetHostInfo( string CollectionID, string HostID )
184
HostInformation hostinfo = null;
185
if ( CollectionID == null || CollectionID == String.Empty )
187
HostNode hn = HostNode.GetHostByID( hostDomain.ID, HostID );
188
hostinfo = new HostInformation( hn );
192
Collection collection = store.GetCollectionByID( CollectionID );
193
if ( collection != null )
195
Member member = collection.GetMemberByID( HostID );
196
if ( member != null )
198
HostNode hn = HostNode.GetHostByID( hostDomain.ID, HostID );
199
hostinfo = new HostInformation( hn );
200
hostinfo.ID = member.ID;
209
/// Method to get all configured hosts
211
[WebMethod(EnableSession=true)]
212
public HostInformation[] GetHosts()
214
HostInformation[] infoList;
215
HostNode[] hosts = HostNode.GetHosts( hostDomain.ID );
216
if ( hosts.Length > 0 )
218
infoList = new HostInformation[ hosts.Length ];
220
foreach ( HostNode hn in hosts )
222
infoList[ i++ ] = new HostInformation( hn );
227
infoList = new HostInformation[ 0 ];
237
[WebService(Namespace = "http://novell.com/simias/host")]
238
public class HostAdmin : HostService
246
/// HostAdmin - default constructor
255
/// <param name="userID"></param>
256
/// <param name="server"></param>
257
/// <returns></returns>
258
[WebMethod(EnableSession=true)]
259
public bool SetHomeServer( string userID, string serverID )
263
Member member = GetDomainMemberByID( userID );
264
member.HomeServer = HostNode.GetHostByID( domain.ID, serverID );
265
domain.Commit( member );
275
/// <param name="userID"></param>
276
/// <returns></returns>
277
[WebMethod(EnableSession = true)]
278
public bool MigrateUser( string userID )
280
// Get the current home server for this user.
281
// Lock the collection on the old server so that it will not be modified.
282
// Syncronize the collection to this machine.
283
// Delete the collection from the old server.
290
/// <returns></returns>
291
[WebMethod(EnableSession = true)]
292
public bool ProvisionUser()
300
/// <param name="name"></param>
301
/// <param name="publicAddress"></param>
302
/// <param name="privateAddress"></param>
303
/// <param name="publicKey"></param>
304
/// <returns></returns>
305
[WebMethod(EnableSession = true)]
306
public string AddHost( string name, string publicAddress, string privateAddress, string publicKey, out bool created )
308
// Get the HostDomain
309
// If the domain does not exist create it and and this host to it.
310
// Add the host to the Host domain if it does not already exist.
311
// Check if the host already exists.
314
Member host = hostDomain.GetMemberByName( name );
317
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
318
rsa.FromXmlString( publicKey );
320
// Now add the new host.
321
host = new HostNode( name, System.Guid.NewGuid().ToString(), publicAddress, privateAddress, rsa );
322
domain.Commit( host );
326
return host.Properties.ToString( true );
332
/// <param name="id"></param>
333
[WebMethod(EnableSession=true)]
334
public void DeleteHost( string id )
336
Domain domain = this.domain;
337
HostNode host = domain.GetNodeByID( id ) as HostNode;
338
domain.Commit( domain.Delete( host ) );
342
/// Get the configuration from this server.
344
/// <returns>XML string that represents the configuration.</returns>
345
[WebMethod(EnableSession = true)]
346
public string GetConfiguration()
348
return Store.Config.ToXml();
354
/// <returns></returns>
355
[WebMethod(EnableSession = true)]
356
public string GetDomain()
358
// We need to add the hostID to this node.
360
d.Host = HostNode.GetLocalHost();
361
return d.Properties.ToString(false);
367
/// <returns></returns>
368
[WebMethod(EnableSession = true)]
369
public string GetDomainOwner()
371
return domain.Owner.Properties.ToString(true);
375
/// Method to set/reset public and private addresses
377
/// Note: The Host parameter can be represented as
378
/// the Host ID or the Host name. If the Host
379
/// is null local host is assumed.
381
/// <param name="Host"></param>
382
/// <param name="PublicAddress"></param>
383
/// <param name="PrivateAddress"></param>
384
/// <returns></returns>
385
[WebMethod( EnableSession = true )]
386
public void SetHostAddress( string Host, string PublicUrl, string PrivateUrl )
388
// Validate parameters
389
if ( PublicUrl == null && PrivateUrl == null )
391
throw new SimiasException( "Invalid parameter" );
394
HostNode host = null;
395
if ( Host == null || Host == String.Empty )
397
host = HostNode.GetLocalHost();
403
host = HostNode.GetHostByID( domain.ID, Host );
410
host = HostNode.GetHostByName( domain.ID, Host );
417
throw new SimiasException( String.Format( "Specified host {0} does not exist", Host ) );
421
if ( PrivateUrl != null && PrivateUrl != String.Empty )
423
host.PrivateUrl = PrivateUrl;
426
if ( PublicUrl != null && PublicUrl != String.Empty )
428
host.PublicUrl = PublicUrl;
432
domain.Commit( host );
437
/// HostInformation class
439
public class HostInformation
452
/// Host's user/member ID which is consistent
453
/// across all collections the host is
456
public string MemberID;
459
/// External facing address for clients
461
public string PublicAddress;
464
/// Internal facing address for server to
465
/// server communication.
467
public string PrivateAddress;
470
/// Public key for host to host authentication
472
public string PublicKey;
475
/// true = Master, false = Slave
480
/// HostInfo default constructor
482
public HostInformation()
486
internal HostInformation( HostNode node )
489
MemberID = node.UserID;
491
PublicAddress = node.PublicUrl;
492
PrivateAddress = node.PrivateUrl;
493
PublicKey = node.PublicKey.ToXmlString( false );
494
Master = node.IsMasterHost;
497
internal HostInformation( Simias.Host.HostInfo info )
500
MemberID = info.MemberID;
502
PublicAddress = info.PublicAddress;
503
PrivateAddress = info.PrivateAddress;
504
PublicKey = info.PublicKey;
505
Master = info.Master;