@@ -11,174 +11,182 @@ class Program
11
11
{
12
12
static int Main ( string [ ] args )
13
13
{
14
- if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
15
- {
16
- Console . Error . WriteLine ( "AuthenticodeLint is only supported on Windows." ) ;
17
- return ExitCodes . PlatformNotSupported ;
18
- }
19
- var cli = Environment . CommandLine ;
20
- List < CommandLineParameter > ? parsedCommandLine ;
21
14
try
22
15
{
23
- var commandLine = CommandLineParser . LexCommandLine ( cli ) . Skip ( 1 ) ;
24
- parsedCommandLine = CommandLineParser . CreateCommandLineParametersWithValues ( commandLine ) . ToList ( ) ;
25
- }
26
- catch ( InvalidOperationException )
27
- {
28
- parsedCommandLine = null ;
29
- }
16
+ if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
17
+ {
18
+ Console . Error . WriteLine ( "AuthenticodeLint is only supported on Windows." ) ;
19
+ return ExitCodes . PlatformNotSupported ;
20
+ }
21
+ var cli = Environment . CommandLine ;
22
+ List < CommandLineParameter > ? parsedCommandLine ;
23
+ try
24
+ {
25
+ var commandLine = CommandLineParser . LexCommandLine ( cli ) . Skip ( 1 ) ;
26
+ parsedCommandLine = CommandLineParser . CreateCommandLineParametersWithValues ( commandLine ) . ToList ( ) ;
27
+ }
28
+ catch ( InvalidOperationException )
29
+ {
30
+ parsedCommandLine = null ;
31
+ }
30
32
31
- if ( parsedCommandLine == null || parsedCommandLine . Count == 0 || parsedCommandLine . Any ( cl => cl . Name == "help" ) )
32
- {
33
- ShowHelp ( ) ;
34
- //Avoid returning success for printing help so that automated build systems do not interpret "show the help"
35
- //As a successful build incase the build system is incorrectly passing arguments.
36
- return ExitCodes . InvalidInputOrConfig ;
37
- }
38
- var inputs = new List < string > ( ) ;
39
- var suppress = new HashSet < int > ( ) ;
40
- bool quiet = false ;
41
- bool verbose = false ;
42
- string ? report = null ;
43
- string ? extract = null ;
44
- var revocation = RevocationChecking . None ;
45
- var ruleSet = RuleSet . Modern ;
46
- foreach ( var parameter in parsedCommandLine )
47
- {
48
- if ( parameter . Name == "in" )
33
+ if ( parsedCommandLine == null || parsedCommandLine . Count == 0 || parsedCommandLine . Any ( cl => cl . Name == "help" ) )
34
+ {
35
+ ShowHelp ( ) ;
36
+ //Avoid returning success for printing help so that automated build systems do not interpret "show the help"
37
+ //As a successful build incase the build system is incorrectly passing arguments.
38
+ return ExitCodes . InvalidInputOrConfig ;
39
+ }
40
+ var inputs = new List < string > ( ) ;
41
+ var suppress = new HashSet < int > ( ) ;
42
+ bool quiet = false ;
43
+ bool verbose = false ;
44
+ string ? report = null ;
45
+ string ? extract = null ;
46
+ var revocation = RevocationChecking . None ;
47
+ var ruleSet = RuleSet . Modern ;
48
+ foreach ( var parameter in parsedCommandLine )
49
49
{
50
- if ( string . IsNullOrWhiteSpace ( parameter . Value ) )
50
+ if ( parameter . Name == "in" )
51
51
{
52
- Console . Error . WriteLine ( "A value is required for input." ) ;
53
- return ExitCodes . InvalidInputOrConfig ;
52
+ if ( string . IsNullOrWhiteSpace ( parameter . Value ) )
53
+ {
54
+ Console . Error . WriteLine ( "A value is required for input." ) ;
55
+ return ExitCodes . InvalidInputOrConfig ;
56
+ }
57
+ var filePattern = Path . GetFileName ( parameter . Value ) ;
58
+ //The value contains a pattern.
59
+ if ( filePattern . Contains ( '*' ) || filePattern . Contains ( '?' ) )
60
+ {
61
+ var directory = Path . GetDirectoryName ( parameter . Value ) ;
62
+ if ( Directory . Exists ( directory ) )
63
+ {
64
+ var files = Directory . GetFiles ( directory , filePattern , SearchOption . TopDirectoryOnly ) ;
65
+ inputs . AddRange ( files ) ;
66
+ }
67
+ }
68
+ else
69
+ {
70
+ inputs . Add ( parameter . Value ) ;
71
+ }
54
72
}
55
- var filePattern = Path . GetFileName ( parameter . Value ) ;
56
- //The value contains a pattern.
57
- if ( filePattern . Contains ( '*' ) || filePattern . Contains ( '?' ) )
73
+ else if ( parameter . Name == "suppress" )
58
74
{
59
- var directory = Path . GetDirectoryName ( parameter . Value ) ;
60
- if ( Directory . Exists ( directory ) )
75
+ if ( string . IsNullOrWhiteSpace ( parameter . Value ) )
61
76
{
62
- var files = Directory . GetFiles ( directory , filePattern , SearchOption . TopDirectoryOnly ) ;
63
- inputs . AddRange ( files ) ;
77
+ ShowInvalidSuppression ( ) ;
78
+ return ExitCodes . InvalidInputOrConfig ;
79
+ }
80
+ foreach ( var idString in parameter . Value . Split ( ',' ) . Select ( p => p . Trim ( ) ) )
81
+ {
82
+ int id ;
83
+ if ( int . TryParse ( idString , out id ) )
84
+ {
85
+ suppress . Add ( id ) ;
86
+ }
87
+ else
88
+ {
89
+ Console . Error . WriteLine ( $ "{ idString } is not a valid error ID.") ;
90
+ return ExitCodes . InvalidInputOrConfig ;
91
+ }
64
92
}
65
93
}
66
- else
94
+ else if ( parameter . Name == "q" || parameter . Name == "quiet" )
67
95
{
68
- inputs . Add ( parameter . Value ) ;
96
+ if ( ! string . IsNullOrWhiteSpace ( parameter . Value ) )
97
+ {
98
+ Console . Error . WriteLine ( $ "-{ parameter . Name } does not expect a value.") ;
99
+ return ExitCodes . InvalidInputOrConfig ;
100
+ }
101
+ quiet = true ;
69
102
}
70
- }
71
- else if ( parameter . Name == "suppress" )
72
- {
73
- if ( string . IsNullOrWhiteSpace ( parameter . Value ) )
103
+ else if ( parameter . Name == "verbose" )
74
104
{
75
- ShowInvalidSuppression ( ) ;
76
- return ExitCodes . InvalidInputOrConfig ;
105
+ if ( ! string . IsNullOrWhiteSpace ( parameter . Value ) )
106
+ {
107
+ Console . Error . WriteLine ( $ "-{ parameter . Name } does not expect a value.") ;
108
+ return ExitCodes . InvalidInputOrConfig ;
109
+ }
110
+ verbose = true ;
111
+ }
112
+
113
+ else if ( parameter . Name == "report" )
114
+ {
115
+ report = parameter . Value ;
116
+ }
117
+ else if ( parameter . Name == "extract" )
118
+ {
119
+ extract = parameter . Value ;
77
120
}
78
- foreach ( var idString in parameter . Value . Split ( ',' ) . Select ( p => p . Trim ( ) ) )
121
+ else if ( parameter . Name == "revocation" )
79
122
{
80
- int id ;
81
- if ( int . TryParse ( idString , out id ) )
123
+ if ( string . IsNullOrWhiteSpace ( parameter . Value ) )
82
124
{
83
- suppress . Add ( id ) ;
125
+ Console . Error . WriteLine ( $ "-{ parameter . Name } requires a value if specified.") ;
126
+ return ExitCodes . InvalidInputOrConfig ;
84
127
}
85
- else
128
+ if ( ! Enum . TryParse ( parameter . Value , true , out revocation ) )
86
129
{
87
- Console . Error . WriteLine ( $ "{ idString } is not a valid error ID .") ;
130
+ Console . Error . WriteLine ( $ "- { parameter . Value } is an unrecognized revocation mode .") ;
88
131
return ExitCodes . InvalidInputOrConfig ;
89
132
}
90
133
}
91
- }
92
- else if ( parameter . Name == "q" || parameter . Name == "quiet" )
93
- {
94
- if ( ! string . IsNullOrWhiteSpace ( parameter . Value ) )
134
+ else if ( parameter . Name == "ruleset" )
95
135
{
96
- Console . Error . WriteLine ( $ "-{ parameter . Name } does not expect a value.") ;
97
- return ExitCodes . InvalidInputOrConfig ;
136
+ if ( string . IsNullOrWhiteSpace ( parameter . Value ) )
137
+ {
138
+ Console . Error . WriteLine ( $ "-{ parameter . Name } requires a value if specified.") ;
139
+ return ExitCodes . InvalidInputOrConfig ;
140
+ }
141
+ if ( ! Enum . TryParse ( parameter . Value , true , out ruleSet ) || parameter . Value . Equals ( "all" , StringComparison . OrdinalIgnoreCase ) )
142
+ {
143
+ Console . Error . WriteLine ( $ "-{ parameter . Value } is an unrecognized ruleset.") ;
144
+ return ExitCodes . InvalidInputOrConfig ;
145
+ }
98
146
}
99
- quiet = true ;
100
- }
101
- else if ( parameter . Name == "verbose" )
102
- {
103
- if ( ! string . IsNullOrWhiteSpace ( parameter . Value ) )
147
+ else
104
148
{
105
- Console . Error . WriteLine ( $ "-{ parameter . Name } does not expect a value .") ;
149
+ Console . Error . WriteLine ( $ "-{ parameter . Name } is an unknown parameter .") ;
106
150
return ExitCodes . InvalidInputOrConfig ;
107
151
}
108
- verbose = true ;
109
152
}
153
+ if ( inputs . Count == 0 )
154
+ {
155
+ Console . Error . WriteLine ( "Input is expected. See -help for usage." ) ;
156
+ return ExitCodes . InvalidInputOrConfig ;
157
+ }
158
+ var configuration = new CheckConfiguration ( inputs , report , quiet , suppress , verbose , revocation , extract , ruleSet ) ;
110
159
111
- else if ( parameter . Name == "report" )
160
+ if ( ! ConfigurationValidator . ValidateAndPrint ( configuration , Console . Error ) )
112
161
{
113
- report = parameter . Value ;
162
+ return ExitCodes . InvalidInputOrConfig ;
114
163
}
115
- else if ( parameter . Name == "extract" )
164
+ var collectors = new List < IRuleResultCollector > ( ) ;
165
+ if ( ! quiet )
116
166
{
117
- extract = parameter . Value ;
167
+ collectors . Add ( new StdOutRuleResultCollector ( ) ) ;
118
168
}
119
- else if ( parameter . Name == "revocation" )
169
+ if ( ! string . IsNullOrWhiteSpace ( report ) )
120
170
{
121
- if ( string . IsNullOrWhiteSpace ( parameter . Value ) )
122
- {
123
- Console . Error . WriteLine ( $ "-{ parameter . Name } requires a value if specified.") ;
124
- return ExitCodes . InvalidInputOrConfig ;
125
- }
126
- if ( ! Enum . TryParse ( parameter . Value , true , out revocation ) )
127
- {
128
- Console . Error . WriteLine ( $ "-{ parameter . Value } is an unrecognized revocation mode.") ;
129
- return ExitCodes . InvalidInputOrConfig ;
130
- }
171
+ collectors . Add ( new XmlRuleResultCollector ( report ) ) ;
131
172
}
132
- else if ( parameter . Name == "ruleset" )
173
+ var result = ExitCodes . Success ;
174
+ foreach ( var file in inputs )
133
175
{
134
- if ( string . IsNullOrWhiteSpace ( parameter . Value ) )
176
+ var signatures = SignatureTreeInspector . Extract ( file ) ;
177
+ if ( CheckEngine . Instance . RunAllRules ( file , signatures , collectors , configuration ) != RuleEngineResult . AllPass )
135
178
{
136
- Console . Error . WriteLine ( $ "-{ parameter . Name } requires a value if specified.") ;
137
- return ExitCodes . InvalidInputOrConfig ;
138
- }
139
- if ( ! Enum . TryParse ( parameter . Value , true , out ruleSet ) || parameter . Value . Equals ( "all" , StringComparison . OrdinalIgnoreCase ) )
140
- {
141
- Console . Error . WriteLine ( $ "-{ parameter . Value } is an unrecognized ruleset.") ;
142
- return ExitCodes . InvalidInputOrConfig ;
179
+ result = ExitCodes . ChecksFailed ;
143
180
}
144
181
}
145
- else
146
- {
147
- Console . Error . WriteLine ( $ "-{ parameter . Name } is an unknown parameter.") ;
148
- return ExitCodes . InvalidInputOrConfig ;
149
- }
182
+ collectors . ForEach ( c => c . Flush ( ) ) ;
183
+ return result ;
150
184
}
151
- if ( inputs . Count == 0 )
185
+ catch ( Exception ex )
152
186
{
153
- Console . Error . WriteLine ( "Input is expected. See -help for usage." ) ;
154
- return ExitCodes . InvalidInputOrConfig ;
155
- }
156
- var configuration = new CheckConfiguration ( inputs , report , quiet , suppress , verbose , revocation , extract , ruleSet ) ;
157
-
158
- if ( ! ConfigurationValidator . ValidateAndPrint ( configuration , Console . Error ) )
159
- {
160
- return ExitCodes . InvalidInputOrConfig ;
161
- }
162
- var collectors = new List < IRuleResultCollector > ( ) ;
163
- if ( ! quiet )
164
- {
165
- collectors . Add ( new StdOutRuleResultCollector ( ) ) ;
166
- }
167
- if ( ! string . IsNullOrWhiteSpace ( report ) )
168
- {
169
- collectors . Add ( new XmlRuleResultCollector ( report ) ) ;
170
- }
171
- var result = ExitCodes . Success ;
172
- foreach ( var file in inputs )
173
- {
174
- var signatures = SignatureTreeInspector . Extract ( file ) ;
175
- if ( CheckEngine . Instance . RunAllRules ( file , signatures , collectors , configuration ) != RuleEngineResult . AllPass )
176
- {
177
- result = ExitCodes . ChecksFailed ;
178
- }
187
+ Console . Error . WriteLine ( ex . ToString ( ) ) ;
188
+ return ExitCodes . UnknownResults ;
179
189
}
180
- collectors . ForEach ( c => c . Flush ( ) ) ;
181
- return result ;
182
190
}
183
191
184
192
static void ShowInvalidSuppression ( ) => Console . Error . WriteLine ( "Invalid list of errors to suppress. Use -help for more information." ) ;
0 commit comments