1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Text.RegularExpressions; 5 using System.Diagnostics; 6 using System.Globalization; 7 namespace System.Management.Instrumentation 8 { 9 #region CommonUMPAttributes 10 11 12 /// <summary> 13 /// This attribute declares a class to be exposed as a management 14 /// interface. 15 /// 16 /// It declares the noun to expose in Monad and 17 /// optionally the XML Namespace to expose the class 18 /// through WMI.NET and WS-Management. 19 /// 20 /// </summary> 21 /// 22 23 [AttributeUsage(AttributeTargets.Class, AllowMultiple = false,Inherited=false)] 24 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 25 sealed public class ManagementEntityAttribute : Attribute 26 { ManagementEntityAttribute()27 public ManagementEntityAttribute() 28 { 29 } 30 31 public string Name 32 { 33 get { return _nounName; } 34 set 35 { 36 _nounName = value; 37 } 38 } 39 40 public bool External 41 { 42 get { return _isExternalClass; } 43 set 44 { 45 _isExternalClass = value; 46 } 47 } 48 public bool Singleton 49 { 50 get { return _isSingleton; } 51 set 52 { 53 _isSingleton = value; 54 } 55 } 56 57 58 private string _nounName; 59 private bool _isExternalClass = false; 60 private bool _isSingleton = false; 61 62 /* 63 /// <summary> 64 /// Reference to the Type which acts as a factory for instances 65 /// of this class. 66 /// </summary> 67 /// 68 public Type Factory 69 { 70 get { return _factory; } 71 set { _factory = value; } 72 } 73 private Type _factory; 74 75 public Type FactoryFor 76 { 77 get { return _factoryfor; } 78 set { _factoryfor = value; } 79 } 80 private Type _factoryfor; 81 */ 82 } 83 84 85 86 #endregion CommonUMPAttributes 87 /// <remarks> 88 /// WMI is able to deal with Decoupled and Hosted providers. 89 /// UserHosted for component loaded inproc to the client is not allowed for .NET extension providers. 90 91 public enum ManagementHostingModel 92 { 93 Decoupled, 94 NetworkService, 95 LocalService, 96 LocalSystem 97 } 98 99 [AttributeUsage(AttributeTargets.Assembly)] 100 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 101 sealed public class WmiConfigurationAttribute : Attribute 102 { 103 private string _Scope = null; 104 private string _SecurityRestriction = null; 105 private string _NamespaceSecurity = null; 106 private ManagementHostingModel _HostingModel = ManagementHostingModel.Decoupled; 107 private string _HostingGroup = null; 108 private bool _IdentifyLevel = true; 109 WmiConfigurationAttribute(string scope)110 public WmiConfigurationAttribute(string scope) 111 { 112 string namespaceName = scope; 113 if (namespaceName != null) 114 namespaceName = namespaceName.Replace('/', '\\'); 115 116 if (namespaceName == null || namespaceName.Length == 0) 117 namespaceName = "root\\default"; 118 119 120 bool once = true; 121 foreach (string namespacePart in namespaceName.Split('\\')) 122 { 123 if (namespacePart.Length == 0 124 || (once && String.Compare(namespacePart, "root", StringComparison.OrdinalIgnoreCase) != 0) // Must start with 'root' 125 || !Regex.Match(namespacePart, @"^[a-z,A-Z]").Success // All parts must start with letter 126 || Regex.Match(namespacePart, @"_$").Success // Must not end with an underscore 127 || Regex.Match(namespacePart, @"[^a-z,A-Z,0-9,_,\u0080-\uFFFF]").Success) // Only letters, digits, or underscores 128 { 129 //ManagementException.ThrowWithExtendedInfo(ManagementStatus.InvalidNamespace); 130 } 131 once = false; 132 } 133 134 _Scope = namespaceName; 135 136 } 137 138 /// <remarks> 139 /// The security descriptor used by instrumentation to filter the providers 140 public string SecurityRestriction 141 { 142 get { return _SecurityRestriction; } 143 set { _SecurityRestriction = value; } 144 } 145 public string NamespaceSecurity 146 { 147 get { return _NamespaceSecurity; } 148 set { _NamespaceSecurity = value; } 149 } 150 public bool IdentifyLevel 151 { 152 get { return _IdentifyLevel; } 153 set { _IdentifyLevel = value; } 154 } 155 public ManagementHostingModel HostingModel 156 { 157 get { return _HostingModel; } 158 set { _HostingModel = value; } 159 } 160 161 /// <remarks> 162 /// To support provider separation 163 public string HostingGroup 164 { 165 get { return _HostingGroup; } 166 set { _HostingGroup = value; } 167 } 168 /// <remarks> 169 /// Scope of the assembly in the target instrumentation space 170 /// In WMI speak is the namespace 171 public string Scope 172 { 173 get { return _Scope; } 174 } 175 } 176 177 /// <summary> 178 /// This is the base class for all attribute which can be applied 179 /// to members of the Automation class. 180 /// </summary> 181 /// 182 /// <remarks> 183 /// The Exception member tells Monad which exception coming from the 184 /// member can be treated as non-fatal errors for the pipeline. 185 /// </remarks> 186 /// 187 [AttributeUsage(AttributeTargets.All)] 188 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 189 public abstract class ManagementMemberAttribute : Attribute 190 { 191 /// <summary> 192 /// The exceptions that can be thrown by the member. 193 /// </summary> 194 /// 195 public string Name 196 { 197 get { return _Name; } 198 set { _Name = value; } 199 } 200 private string _Name; 201 } 202 203 /// <summary> 204 /// This abstract attribute determines how one would get an instance of the class. 205 /// You can get an instance by: 206 /// 1) Binding to an instance [Bind] 207 /// 2) Creating an instance [Create] 208 /// 3) Using a factory to get an instance [Factory] 209 /// For any particular NOUN, there can only be ONE way to get instances. 210 /// </summary> 211 /// 212 [AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method, AllowMultiple = false)] 213 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 214 public abstract class ManagementNewInstanceAttribute : ManagementMemberAttribute 215 { 216 } 217 218 [AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method, AllowMultiple = false)] 219 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 220 sealed public class ManagementBindAttribute : ManagementNewInstanceAttribute 221 { 222 /// <summary> 223 /// Declares the type that the output should be 224 /// treated as even if the return value is of 225 /// type System.Object. 226 /// </summary> 227 /// ManagementBindAttribute()228 public ManagementBindAttribute() { } 229 230 public Type Schema 231 { 232 get { return _schema; } 233 set { _schema = value; } 234 } 235 private Type _schema; 236 } 237 238 [AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method, AllowMultiple = false)] 239 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 240 sealed public class ManagementCreateAttribute : ManagementNewInstanceAttribute 241 { 242 ///// <summary> 243 ///// Declares the type that the output should be 244 ///// treated as even if the return value is of 245 ///// type System.Object. 246 ///// </summary> 247 ///// 248 } 249 250 251 /// <summary> 252 /// This attribute determines how one would remove a real object 253 /// </summary> 254 /// 255 /// 256 [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] 257 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 258 sealed public class ManagementRemoveAttribute : ManagementMemberAttribute 259 { 260 /// <summary> 261 /// Declares the type that the output should be 262 /// treated as even if the return value is of 263 /// type System.Object. 264 /// </summary> 265 /// 266 public Type Schema 267 { 268 get { return _schema; } 269 set { _schema = value; } 270 } 271 private Type _schema; 272 } 273 274 /// <summary> 275 /// This attribute defines the enumerator of instances of the class 276 /// </summary> 277 /// 278 [AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method, AllowMultiple = false)] 279 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 280 sealed public class ManagementEnumeratorAttribute : ManagementNewInstanceAttribute 281 { 282 /* /// <summary> 283 /// Declares the member as an enumerator for other classes. The other 284 /// Type must specify the Factory property of the AutomationAttribute to 285 /// be this Type. 286 /// </summary> 287 /// 288 public Type FactoryFor 289 { 290 get { return _factoryFor; } 291 set { _factoryFor = value; } 292 } 293 private Type _factoryFor; 294 295 */ /// <summary> 296 /// Declares the type that the output should be 297 /// treated as even if the return value is of 298 /// type System.Object. 299 /// </summary> 300 /// 301 public Type Schema 302 { 303 get { return _schema; } 304 set { _schema = value; } 305 } 306 private Type _schema; 307 } 308 309 /// <summary> 310 /// Exposes a method or property as a Probe 311 /// </summary> 312 /// 313 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] 314 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 315 sealed public class ManagementProbeAttribute : ManagementMemberAttribute 316 { 317 /// <summary> 318 /// Declares the type that the output should be 319 /// treated as even if the return value is of 320 /// type System.Object. 321 /// </summary> 322 /// 323 public Type Schema 324 { 325 get { return _schema; } 326 set { _schema = value; } 327 } 328 private Type _schema; 329 } 330 331 #region Task 332 /// <summary> 333 /// Exposes a method as a task. 334 /// </summary> 335 /// 336 /// <remarks> 337 /// The TaskAttribute is placed on a method to expose it as a management task. 338 /// 339 /// If the task enumerates manageable objects, the task declaration should set 340 /// the Enumeration option to true. 341 /// 342 /// ISSUE-2005/06/08-jeffjon 343 /// Does the task need a Schema parameter or should we have a separate Probe 344 /// attribute? 345 /// </remarks> 346 /// 347 [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] 348 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 349 sealed public class ManagementTaskAttribute : ManagementMemberAttribute 350 { ManagementTaskAttribute()351 public ManagementTaskAttribute() 352 { 353 } 354 355 /// <summary> 356 /// Declares the type that the output should be 357 /// treated as even if the return value is of 358 /// type System.Object. 359 /// </summary> 360 /// 361 public Type Schema 362 { 363 get { return _schema; } 364 set { _schema = value; } 365 } 366 private Type _schema; 367 } 368 #endregion Task 369 370 #region Naming 371 372 /// <summary> 373 /// This attribute defines the ID (key) property of the class. 374 /// </summary> 375 /// 376 /// <remarks> 377 /// For Monad, this property is used to do filtering of enumerations. 378 /// 379 /// If used on a parameter, then the attribute must also exist on a property in 380 /// the class. 381 /// </remarks> 382 /// 383 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] 384 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 385 sealed public class ManagementKeyAttribute : ManagementMemberAttribute 386 { ManagementKeyAttribute()387 public ManagementKeyAttribute() 388 { 389 } 390 } 391 392 393 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] 394 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 395 sealed public class ManagementReferenceAttribute : Attribute 396 { ManagementReferenceAttribute()397 public ManagementReferenceAttribute() 398 { 399 } 400 public string Type 401 { 402 get { return _Type; } 403 set { _Type = value; } 404 } 405 private string _Type; 406 } 407 408 #endregion Naming 409 410 #region Configuration 411 412 /// <summary> 413 /// Defines a property as the storage for configuration data. 414 /// </summary> 415 /// 416 public enum ManagementConfigurationType { Apply, OnCommit }; 417 418 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] 419 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 420 sealed public class ManagementConfigurationAttribute : ManagementMemberAttribute 421 { 422 /// <summary> 423 /// Declares the type that the output should be 424 /// treated as even if the return value is of 425 /// type System.Object. 426 /// </summary> 427 /// 428 ManagementConfigurationAttribute()429 public ManagementConfigurationAttribute() 430 { 431 updateMode = ManagementConfigurationType.Apply; 432 } 433 434 public ManagementConfigurationType Mode 435 { 436 get { return updateMode; } 437 set { updateMode = value; } 438 } 439 440 public Type Schema 441 { 442 get { return _schema; } 443 set { _schema = value; } 444 } 445 private ManagementConfigurationType updateMode; 446 private Type _schema; 447 448 } 449 450 [AttributeUsage(AttributeTargets.Method)] 451 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 452 sealed public class ManagementCommitAttribute : ManagementMemberAttribute 453 { 454 } 455 /// <summary> 456 /// This attribute defines the naming (user friendly name) of method parameters 457 /// </summary> 458 /// 459 [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)] 460 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 461 sealed public class ManagementNameAttribute : Attribute 462 { 463 ManagementNameAttribute(string name)464 public ManagementNameAttribute(string name) 465 { 466 _Name = name; 467 468 } 469 public string Name 470 { 471 get { return _Name; } 472 } 473 private string _Name; 474 } 475 476 #endregion Configuration 477 478 /* 479 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] 480 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 481 public class FactoryAttribute : NewInstanceAttribute 482 { 483 /// <summary> 484 /// Declares the type that the output should be 485 /// treated as even if the return value is of 486 /// type System.Object. 487 /// </summary> 488 /// 489 public FactoryAttribute() { } 490 public FactoryAttribute(Type t) { } 491 492 public Type Schema 493 { 494 get { return _schema; } 495 set { _schema = value; } 496 } 497 private Type _schema; 498 } 499 500 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] 501 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 502 public class FactoryForAttribute : ManagementMemberAttribute 503 { 504 /// <summary> 505 /// Declares the type that the output should be 506 /// treated as even if the return value is of 507 /// type System.Object. 508 /// </summary> 509 /// 510 public FactoryForAttribute(Type t) { } 511 512 public Type Schema 513 { 514 get { return _schema; } 515 set { _schema = value; } 516 } 517 private Type _schema; 518 } 519 520 521 #region Constraints 522 523 /// <summary> 524 /// Constraints the member/option to a minimum and/or maximum length. 525 /// </summary> 526 /// 527 /// <remarks> 528 /// This can be used for strings or collections. 529 /// </remarks> 530 /// 531 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = true)] 532 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 533 public class ValidateLengthAttribute : Attribute 534 { 535 /// <summary> 536 /// The minimum length 537 /// </summary> 538 /// 539 public int Min 540 { 541 get { return _min; } 542 set { _min = value; } 543 } 544 private int _min = int.MinValue; 545 546 /// <summary> 547 /// The maximum length 548 /// </summary> 549 /// 550 public int Max 551 { 552 get { return _max; } 553 set { _max = value; } 554 } 555 private int _max = int.MaxValue; 556 } 557 558 /// <summary> 559 /// Constraints the member/option to a range of values. 560 /// </summary> 561 /// 562 /// <remarks> 563 /// This can be used for strings or collections. 564 /// </remarks> 565 /// 566 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = true)] 567 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 568 public class ValidateRangeAttribute : Attribute 569 { 570 /// <summary> 571 /// Defines the range for the constraint 572 /// </summary> 573 /// 574 /// <param name="lower"> 575 /// The minimum of the range. 576 /// </param> 577 /// 578 /// <param name="upper"> 579 /// The maximum of the range. 580 /// </param> 581 /// 582 public ValidateRangeAttribute(object lower, object upper) 583 { 584 this._lower = lower; 585 this._upper = upper; 586 } 587 588 /// <summary> 589 /// The lower bound for the range 590 /// </summary> 591 /// 592 public object Lower 593 { 594 get { return _lower; } 595 set { _lower = value; } 596 } 597 private object _lower; 598 599 /// <summary> 600 /// The upper bound for the range 601 /// </summary> 602 /// 603 public object Upper 604 { 605 get { return _upper; } 606 set { _upper = value; } 607 } 608 private object _upper; 609 } 610 611 /// <summary> 612 /// Constraints the member/option to a pattern represented by a regular expression 613 /// </summary> 614 /// 615 /// <remarks> 616 /// This can be used for strings or collections. 617 /// </remarks> 618 /// 619 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = true)] 620 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 621 public class ValidatePatternAttribute : Attribute 622 { 623 /// <summary> 624 /// Defines the pattern for the constraint 625 /// </summary> 626 /// 627 /// <param name="pattern"> 628 /// The minimum of the range. 629 /// </param> 630 /// 631 public ValidatePatternAttribute(string pattern) 632 { 633 this._pattern = pattern; 634 } 635 636 /// <summary> 637 /// The pattern which defines the constraint 638 /// </summary> 639 /// 640 public string Pattern 641 { 642 get { return _pattern; } 643 set { _pattern = value; } 644 } 645 private string _pattern; 646 647 /// <summary> 648 /// The options for the regular expression defined by the pattern. 649 /// </summary> 650 /// 651 public RegexOptions Options 652 { 653 get { return _options; } 654 set { _options = value; } 655 } 656 private RegexOptions _options = RegexOptions.IgnoreCase; 657 } 658 659 /// <summary> 660 /// Constraints the member/option to a number of values. 661 /// </summary> 662 /// 663 /// <remarks> 664 /// This can be used for strings or collections. 665 /// </remarks> 666 /// 667 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = true)] 668 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 669 public class ValidateCountAttribute : Attribute 670 { 671 /// <summary> 672 /// Defines the minimum and maximum number of elements. 673 /// </summary> 674 /// 675 /// <param name="minimum"> 676 /// The minimum minimum number of elements. 677 /// </param> 678 /// 679 /// <param name="maximum"> 680 /// The maximum number of elements. 681 /// </param> 682 /// 683 public ValidateCountAttribute(int minimum, int maximum) 684 { 685 } 686 687 /// <summary> 688 /// The minimum number of elements 689 /// </summary> 690 /// 691 public int Minimum; 692 693 /// <summary> 694 /// The maximum number of elements 695 /// </summary> 696 /// 697 public int Maximum; 698 } 699 700 /// <summary> 701 /// Constraints the member/option to a set of values. 702 /// </summary> 703 /// 704 705 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = true)] 706 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 707 public class ValidateSetAttribute : Attribute 708 { 709 /// <summary> 710 /// Defines the range for the constraint 711 /// </summary> 712 /// 713 /// <param name="validValues"> 714 /// The valid values for the set. 715 /// </param> 716 /// 717 public ValidateSetAttribute(params string[] validValues) 718 { 719 } 720 721 /// <summary> 722 /// The valid values for the set. 723 /// </summary> 724 /// 725 public string[] ValidValues 726 { 727 get { return null; } 728 set { } 729 } 730 731 /// <summary> 732 /// If true, the values are compared in a case-insensitive way. 733 /// If false, the set is constrained to exact matches. 734 /// </summary> 735 /// 736 public bool IgnoreCase 737 { 738 get { return _ignoreCase; } 739 set { _ignoreCase = value; } 740 } 741 private bool _ignoreCase = true; 742 } 743 744 #endregion Constraints 745 /// <summary> 746 /// Specifies the options for a task. 747 /// </summary> 748 /// 749 /// <remarks> 750 /// When placed on a parameter of a task method, this attribute 751 /// describes the options for the parameter. 752 /// </remarks> 753 /// 754 [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = true)] 755 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] 756 public class ManagementTaskOptionAttribute : Attribute 757 { 758 /// <summary> 759 /// If true, the option must be specified. 760 /// </summary> 761 /// 762 /// <remarks> 763 /// If false, and the InitialValue is not specified, then 764 /// an initial value will be deduced using the "default" 765 /// keyword in C#. 766 /// </remarks> 767 /// 768 public bool Mandatory 769 { 770 get { return _mandatory; } 771 set { _mandatory = value; } 772 } 773 private bool _mandatory = true; 774 775 /// <summary> 776 /// The initial value of the parameter. Used if Mandatory=false. 777 /// </summary> 778 /// 779 public object InitialValue 780 { 781 get { return _initialValue; } 782 set { _initialValue = value; } 783 } 784 private object _initialValue; 785 786 /// <summary> 787 /// Monad specific - provides mapping of the pipeline object 788 /// to the parameter value. 789 /// </summary> 790 /// 791 public bool ValueFromPipeline 792 { 793 get { return _valueFromPipeline; } 794 set { _valueFromPipeline = value; } 795 } 796 private bool _valueFromPipeline; 797 798 /// <summary> 799 /// Monad specific - provides mapping of the pipeline object's 800 /// property with the same name as the parameter to the parameter 801 /// value. 802 /// </summary> 803 public bool ValueFromPipelineByPropertyName 804 { 805 get { return _valueFromPipelineByPropertyName; } 806 set { _valueFromPipelineByPropertyName = value; } 807 } 808 private bool _valueFromPipelineByPropertyName; 809 810 } 811 */ 812 }; 813