~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/server/Simias.HostService/.svn/text-base/HostService.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
 |
3
 
 | Copyright (c) 2007 Novell, Inc.
4
 
 | All Rights Reserved.
5
 
 |
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.
9
 
 |
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.
14
 
 |
15
 
 | You should have received a copy of the GNU General Public License
16
 
 | along with this program; if not, contact Novell, Inc.
17
 
 |
18
 
 | To contact Novell about this file by physical or electronic mail,
19
 
 | you may find current contact information at www.novell.com
20
 
 |
21
 
 |  Author: Russ Young ryoung@novell.com
22
 
 |***************************************************************************/
23
 
 
24
 
using System;
25
 
using System.Net;
26
 
using System.Web;
27
 
using System.Web.Services;
28
 
using System.Web.Services.Protocols;
29
 
using System.Security.Cryptography;
30
 
using System.Threading;
31
 
 
32
 
using Simias;
33
 
using Simias.Storage;
34
 
using Simias.Client;
35
 
using Simias.Server;
36
 
//using Simias.Enterprise;
37
 
 
38
 
namespace Simias.Host
39
 
{
40
 
        /// <summary>
41
 
        /// HostService - WebService class
42
 
        /// </summary>
43
 
        public class HostService : WebService
44
 
        {
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";
49
 
 
50
 
                protected Domain domain
51
 
                {
52
 
                        get { return store.GetDomain( store.DefaultDomain ); }
53
 
                }
54
 
 
55
 
                protected Domain hostDomain
56
 
                {
57
 
                        get { return store.GetDomain( store.DefaultDomain ); }
58
 
                }
59
 
 
60
 
                /// <summary>
61
 
                /// Gets the member object in the domain for the specified user.
62
 
                /// </summary>
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 )
66
 
                {
67
 
                        try
68
 
                        {
69
 
                                return domain.GetMemberByID( userID );
70
 
                        }
71
 
                        catch { }
72
 
                        return null;
73
 
                }
74
 
 
75
 
                protected Member GetDomainMemberByName( string userName )
76
 
                {
77
 
                        try
78
 
                        {
79
 
                                return domain.GetMemberByName( userName );
80
 
                        }
81
 
                        catch { }
82
 
                        return null;
83
 
                }
84
 
 
85
 
                protected Member GetAuthenticatedUser()
86
 
                {
87
 
                        string userID = Thread.CurrentPrincipal.Identity.Name;
88
 
                        if (userID != null && userID.Length != 0)
89
 
                        {
90
 
                                return domain.GetMemberByID( userID );
91
 
                        }
92
 
                        return null;
93
 
                }
94
 
        }
95
 
 
96
 
        /// <summary>
97
 
        /// HostLocation - WebService class
98
 
        /// </summary>
99
 
        [WebService(Namespace = "http://novell.com/simias/host/location")]
100
 
        public class HostLocation : HostService
101
 
        {
102
 
                /// <summary>
103
 
                /// 
104
 
                /// </summary>
105
 
                public HostLocation()
106
 
                {
107
 
                }
108
 
 
109
 
                /// <summary>
110
 
                /// Determins if the collection is on this Host.
111
 
                /// </summary>
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 )
116
 
                {
117
 
                        // This call requires no authentication.
118
 
                        if ( store.GetCollectionByID( collectionID ) != null )
119
 
                        {
120
 
                                return true;
121
 
                        }
122
 
        
123
 
                        // The collection is not here return false.
124
 
                        return false;
125
 
                }
126
 
 
127
 
                /// <summary>
128
 
                /// Returns the associated host information for a given collection.
129
 
                /// </summary>
130
 
                /// <param name="CollectionID">The id of a collection.</param>
131
 
                /// <returns>HostInformation.</returns>
132
 
                [WebMethod(EnableSession=true)]
133
 
                public HostInformation GetCollectionLocation( string CollectionID )
134
 
                {
135
 
                        HostInformation hostinformation = null;
136
 
                        CatalogEntry catEntry = Catalog.GetEntryByCollectionID( CollectionID );
137
 
                        if ( catEntry != null )
138
 
                        {
139
 
                                HostNode hostnode = HostNode.GetHostByID( store.DefaultDomain, catEntry.HostID );
140
 
                                if ( hostnode != null )
141
 
                                {
142
 
                                        hostinformation = new HostInformation( hostnode );
143
 
                                }
144
 
                        }
145
 
 
146
 
                        return hostinformation;
147
 
                }
148
 
 
149
 
                /// <summary>
150
 
                /// Returns the home server for the specified user.
151
 
                /// </summary>
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 )
156
 
                {
157
 
                        Member member = GetDomainMemberByName( Username );
158
 
                        if ( member != null )
159
 
                        {
160
 
                                HostNode host = member.HomeServer;
161
 
                                if ( host != null )
162
 
                                {
163
 
                                        return new HostInformation( host );
164
 
                                }
165
 
 
166
 
                                // Call the provision service to provision this
167
 
                                // user to a host
168
 
                                HostInfo info = ProvisionService.ProvisionUser( Username );
169
 
                                if ( info != null )
170
 
                                {
171
 
                                        return new HostInformation( info );
172
 
                                }
173
 
                        }
174
 
 
175
 
                        return null;
176
 
                }
177
 
 
178
 
                /// <summary>
179
 
                ///  Method to get HostInfo for a specified host
180
 
                /// </summary>
181
 
                [WebMethod(EnableSession=true)]
182
 
                public HostInformation GetHostInfo( string CollectionID, string HostID )
183
 
                {
184
 
                        HostInformation hostinfo = null;
185
 
                        if ( CollectionID == null || CollectionID == String.Empty )
186
 
                        {
187
 
                                HostNode hn = HostNode.GetHostByID( hostDomain.ID, HostID );
188
 
                                hostinfo = new HostInformation( hn );
189
 
                        }
190
 
                        else
191
 
                        {
192
 
                                Collection collection = store.GetCollectionByID( CollectionID );
193
 
                                if ( collection != null )
194
 
                                {
195
 
                                        Member member = collection.GetMemberByID( HostID );
196
 
                                        if ( member != null )
197
 
                                        {
198
 
                                                HostNode hn = HostNode.GetHostByID( hostDomain.ID, HostID );
199
 
                                                hostinfo = new HostInformation( hn );
200
 
                                                hostinfo.ID = member.ID;
201
 
                                        }
202
 
                                }
203
 
                        }
204
 
 
205
 
                        return hostinfo;
206
 
                }
207
 
 
208
 
                /// <summary>
209
 
                ///  Method to get all configured hosts
210
 
                /// </summary>
211
 
                [WebMethod(EnableSession=true)]
212
 
                public HostInformation[] GetHosts()
213
 
                {
214
 
                        HostInformation[] infoList;
215
 
                        HostNode[] hosts = HostNode.GetHosts( hostDomain.ID );
216
 
                        if ( hosts.Length > 0 )
217
 
                        {
218
 
                                infoList = new HostInformation[ hosts.Length ];
219
 
                                int i = 0;
220
 
                                foreach ( HostNode hn in hosts )
221
 
                                {
222
 
                                        infoList[ i++ ] = new HostInformation( hn );
223
 
                                }
224
 
                        }
225
 
                        else
226
 
                        {
227
 
                                infoList = new HostInformation[ 0 ];
228
 
                        }
229
 
 
230
 
                        return infoList;
231
 
                }
232
 
        }
233
 
 
234
 
        /// <summary>
235
 
        /// HostAdmin - 
236
 
        /// </summary>
237
 
        [WebService(Namespace = "http://novell.com/simias/host")]
238
 
        public class HostAdmin : HostService
239
 
        {
240
 
                private bool IsAdmin
241
 
                {
242
 
                        get { return true; }
243
 
                }
244
 
 
245
 
                /// <summary>
246
 
                /// HostAdmin - default constructor 
247
 
                /// </summary>
248
 
                public HostAdmin() 
249
 
                {
250
 
                }
251
 
 
252
 
                /// <summary>
253
 
                /// 
254
 
                /// </summary>
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 )
260
 
                {
261
 
                        try
262
 
                        {
263
 
                                Member member = GetDomainMemberByID( userID );
264
 
                                member.HomeServer = HostNode.GetHostByID( domain.ID, serverID );
265
 
                                domain.Commit( member );
266
 
                                return true;
267
 
                        }
268
 
                        catch { }
269
 
                        return false;
270
 
                }
271
 
 
272
 
                /// <summary>
273
 
                /// 
274
 
                /// </summary>
275
 
                /// <param name="userID"></param>
276
 
                /// <returns></returns>
277
 
                [WebMethod(EnableSession = true)]
278
 
                public bool MigrateUser( string userID )
279
 
                {
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.
284
 
                        return false;
285
 
                }
286
 
 
287
 
                /// <summary>
288
 
                /// 
289
 
                /// </summary>
290
 
                /// <returns></returns>
291
 
                [WebMethod(EnableSession = true)]
292
 
                public bool ProvisionUser()
293
 
                {
294
 
                        return false;
295
 
                }
296
 
 
297
 
                /// <summary>
298
 
                /// 
299
 
                /// </summary>
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 )
307
 
                {
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.
312
 
                        created = false;
313
 
 
314
 
                        Member host = hostDomain.GetMemberByName( name );
315
 
                        if (host == null)
316
 
                        {
317
 
                                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
318
 
                                rsa.FromXmlString( publicKey );
319
 
 
320
 
                                // Now add the new host.
321
 
                                host = new HostNode( name, System.Guid.NewGuid().ToString(), publicAddress, privateAddress, rsa );
322
 
                                domain.Commit( host );
323
 
                                created = true;
324
 
                        }
325
 
 
326
 
                        return host.Properties.ToString( true );
327
 
                }
328
 
 
329
 
                /// <summary>
330
 
                /// 
331
 
                /// </summary>
332
 
                /// <param name="id"></param>
333
 
                [WebMethod(EnableSession=true)]
334
 
                public void DeleteHost( string id )
335
 
                {
336
 
                        Domain domain = this.domain;
337
 
                        HostNode host = domain.GetNodeByID( id ) as HostNode;
338
 
                        domain.Commit( domain.Delete( host ) );
339
 
                }
340
 
 
341
 
                /// <summary>
342
 
                /// Get the configuration from this server.
343
 
                /// </summary>
344
 
                /// <returns>XML string that represents the configuration.</returns>
345
 
                [WebMethod(EnableSession = true)]
346
 
                public string GetConfiguration()
347
 
                {
348
 
                        return Store.Config.ToXml();
349
 
                }
350
 
 
351
 
                /// <summary>
352
 
                /// 
353
 
                /// </summary>
354
 
                /// <returns></returns>
355
 
                [WebMethod(EnableSession = true)]
356
 
                public string GetDomain()
357
 
                {
358
 
                        // We need to add the hostID to this node.
359
 
                        Domain d = domain;
360
 
                        d.Host = HostNode.GetLocalHost();
361
 
                        return d.Properties.ToString(false);
362
 
                }
363
 
 
364
 
                /// <summary>
365
 
                /// 
366
 
                /// </summary>
367
 
                /// <returns></returns>
368
 
                [WebMethod(EnableSession = true)]
369
 
                public string GetDomainOwner()
370
 
                {
371
 
                        return domain.Owner.Properties.ToString(true);
372
 
                }
373
 
 
374
 
                /// <summary>
375
 
                /// Method to set/reset public and private addresses
376
 
                /// of a host
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.
380
 
                /// </summary>
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 )
387
 
                {
388
 
                        // Validate parameters
389
 
                        if ( PublicUrl == null && PrivateUrl == null )
390
 
                        {
391
 
                                throw new SimiasException( "Invalid parameter" );
392
 
                        }
393
 
 
394
 
                        HostNode host = null;
395
 
                        if ( Host == null || Host == String.Empty )
396
 
                        {
397
 
                                host = HostNode.GetLocalHost();
398
 
                        }
399
 
                        else
400
 
                        {
401
 
                                try
402
 
                                {
403
 
                                        host = HostNode.GetHostByID( domain.ID, Host );
404
 
                                }
405
 
                                catch{}
406
 
                                if ( host == null )
407
 
                                {
408
 
                                        try
409
 
                                        {
410
 
                                                host = HostNode.GetHostByName( domain.ID, Host );
411
 
                                        } 
412
 
                                        catch{}
413
 
                                }
414
 
 
415
 
                                if ( host == null )
416
 
                                {
417
 
                                        throw new SimiasException( String.Format( "Specified host {0} does not exist", Host ) );
418
 
                                }
419
 
                        }
420
 
 
421
 
                        if ( PrivateUrl != null && PrivateUrl != String.Empty )
422
 
                        {
423
 
                                host.PrivateUrl = PrivateUrl;
424
 
                        }
425
 
 
426
 
                        if ( PublicUrl != null && PublicUrl != String.Empty )
427
 
                        {
428
 
                                host.PublicUrl = PublicUrl;
429
 
                        }
430
 
 
431
 
                        // Save the changes
432
 
                        domain.Commit( host );
433
 
                }
434
 
        }
435
 
 
436
 
        /// <summary>
437
 
        /// HostInformation class
438
 
        /// </summary>
439
 
        public class HostInformation
440
 
        {
441
 
                /// <summary>
442
 
                /// Host's unique ID
443
 
                /// </summary>
444
 
                public string ID;
445
 
 
446
 
                /// <summary>
447
 
                /// Host's name
448
 
                /// </summary>
449
 
                public string Name;
450
 
 
451
 
                /// <summary>
452
 
                /// Host's user/member ID which is consistent
453
 
                /// across all collections the host is 
454
 
                /// a member of.
455
 
                /// </summary>
456
 
                public string MemberID;
457
 
 
458
 
                /// <summary>
459
 
                /// External facing address for clients
460
 
                /// </summary>
461
 
                public string PublicAddress;
462
 
 
463
 
                /// <summary>
464
 
                /// Internal facing address for server to 
465
 
                /// server communication.
466
 
                /// </summary>
467
 
                public string PrivateAddress;
468
 
 
469
 
                /// <summary>
470
 
                /// Public key for host to host authentication
471
 
                /// </summary>
472
 
                public string PublicKey;
473
 
 
474
 
                /// <summary>
475
 
                /// true = Master, false = Slave
476
 
                /// </summary>
477
 
                public bool Master;
478
 
 
479
 
                /// <summary>
480
 
                /// HostInfo default constructor
481
 
                /// </summary>
482
 
                public HostInformation()
483
 
                {
484
 
                }
485
 
 
486
 
                internal HostInformation( HostNode node )
487
 
                {
488
 
                        ID = node.ID;
489
 
                        MemberID = node.UserID;
490
 
                        Name = node.Name;
491
 
                        PublicAddress = node.PublicUrl;
492
 
                        PrivateAddress = node.PrivateUrl;
493
 
                        PublicKey = node.PublicKey.ToXmlString( false );
494
 
                        Master = node.IsMasterHost;
495
 
                }
496
 
 
497
 
                internal HostInformation( Simias.Host.HostInfo info )
498
 
                {
499
 
                        ID = info.ID;
500
 
                        MemberID = info.MemberID;
501
 
                        Name = info.Name;
502
 
                        PublicAddress = info.PublicAddress;
503
 
                        PrivateAddress = info.PrivateAddress;
504
 
                        PublicKey = info.PublicKey;
505
 
                        Master = info.Master;
506
 
                }
507
 
        }
508
 
}