11using System ;
22using System . Collections . Generic ;
33using System . Configuration ;
4+ using System . Text . RegularExpressions ;
45
56public class GetAuthorization
67{
78 private static List < String [ ] > ReadPriviledges ( )
89 {
910 string file = System . IO . File . ReadAllText ( ConfigurationManager . AppSettings . Get ( "sudoers" ) ) ;
1011 string [ ] readPriviledges = file . Split ( new [ ] { "\r \n " , "\r " , "\n " } , StringSplitOptions . None ) ;
12+ string sudoersPattern = @"^(u|g);(.*?);(.*);($|NOPASSWD$|PASSWD$)" ;
1113
1214 List < String [ ] > privilegesList = new List < String [ ] > ( ) ;
1315 for ( int i = 0 ; i < readPriviledges . Length ; i ++ )
1416 {
15- if ( readPriviledges [ i ] . Split ( ";" ) . Length >= 3 && readPriviledges [ i ] . Split ( ";" ) . Length <= 4 )
17+ if ( Regex . IsMatch ( readPriviledges [ i ] , sudoersPattern ) )
1618 {
17- privilegesList . Add ( readPriviledges [ i ] . Split ( ";" ) ) ;
19+ Match matchSyntax = Regex . Match ( readPriviledges [ i ] , sudoersPattern ) ;
20+ privilegesList . Add ( new [ ] { matchSyntax . Groups [ 1 ] . Value , matchSyntax . Groups [ 2 ] . Value , matchSyntax . Groups [ 3 ] . Value , matchSyntax . Groups [ 4 ] . Value } ) ;
1821 }
1922 }
2023
@@ -29,21 +32,25 @@ public static int IsUserGranted(String username, String commandLine)
2932 foreach ( String [ ] priviledge in privileges )
3033 {
3134 if ( priviledge [ 0 ] == "u" && priviledge [ 1 ] == username )
32- {
35+ {
3336 if ( priviledge [ 2 ] == commandLine )
34- {
37+ {
3538 if ( priviledge [ 3 ] == "NOPASSWD" )
36- {
39+ {
3740 return 0 ;
3841 }
39- else
40- {
42+ else if ( priviledge [ 3 ] == "PASSWD" || priviledge [ 3 ] == "" )
43+ {
4144 return 1 ;
4245 }
43- }
44- }
45- else if ( priviledge [ 0 ] == "g" )
46- {
46+ else
47+ {
48+ return 2 ;
49+ }
50+ }
51+ }
52+ else if ( priviledge [ 0 ] == "g" )
53+ {
4754 bool isAMember = GetUserInfo . IsUserGroupMember ( username , priviledge [ 1 ] ) ;
4855
4956 if ( priviledge [ 2 ] == commandLine && isAMember )
0 commit comments