362ff4a84a38ecb9918c7fcd490ad26430d52a10
[users/kov/gksu-polkit.git] / common / gksu-environment.vala
1 /*
2  * Copyright (C) 2008 Gustavo Noronha Silva
3  *
4  * This file is part of the Gksu PolicyKit library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 3 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.  You should have received
15  * a copy of the GNU General Public License along with this program;
16  * if not, write to the Free Software Foundation, Inc., 59 Temple
17  * Place, Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 using GLib;
21 using Gee;
22
23 namespace Gksu {
24         class Variable: Object {
25                 public string name;
26                 public string regex;
27         }
28
29         public class Environment: Object {
30                 HashMap<string,Variable> variables;
31
32                 construct {
33                         weak string[] search_path = GLib.Environment.get_system_data_dirs();
34                         variables = new HashMap<string,Variable>(GLib.str_hash, GLib.str_equal);
35
36                         foreach(string path in search_path) {
37                                 string full_path = path.concat("gksu-polkit-1/environment/");
38                                 read_variables_from_path(full_path);
39                         }
40                 }
41
42                 public HashTable<string,string>? get_variables() {
43                         Set<string> keysset = variables.keys;
44                         HashTable<string,string> envpairs = new HashTable<string,string>(str_hash, str_equal);
45
46                         foreach(string variable in keysset) {
47                                 string value = GLib.Environment.get_variable(variable);
48                                 envpairs.insert(variable, value);
49                         }
50
51                         return envpairs;
52                 }
53
54                 public bool validate_hash_table(HashTable<string,string> hash_table) {
55                         GLib.List<weak string> varnames = hash_table.get_keys();
56
57                         foreach(string name in varnames) {
58                                 weak string value = hash_table.lookup(name);
59                                 if(!is_variable_valid(name, value))
60                                         return false;
61                         }
62
63                         return true;
64                 }
65
66                 /* this verifies that the variable should be passed through,
67                  * and that it contains a valid value
68                  */
69                 public bool is_variable_valid(string name, string value) {
70                         /* first we verify that the variable is specified */
71                         if(variables.get(name) == null)
72                                 return false;
73
74                         /* then we verify that the variable regular expression matches */
75                         Variable variable = variables.get(name);
76                         if((variable.regex != null) && (variable.regex != "") ) {
77                                 try {
78                                         Regex regex = new Regex(variable.regex);
79                                         return regex.match(value);
80                                 } catch (RegexError error) {
81                                         warning("bad regular expression for variable %s", name);
82                                         return false;
83                                 }
84                         }
85
86                         /* the variable looks OK */
87                         return true;
88                 }
89
90                 private void read_variables_from_path(string path) {
91                         Dir directory;
92
93                         try {
94                                 directory = Dir.open(path, 0);
95                         } catch (FileError error) {
96                                 return;
97                         }
98
99                         weak string entry;
100                         while((entry = directory.read_name()) != null) {
101                                 if(entry.has_suffix(".variables")) {
102                                         string full_path = path.concat(entry);
103                                         read_variables_from_file(full_path);
104                                 }
105                         }
106                 }
107
108                 private void read_variables_from_file(string path) {
109                         KeyFile file = new KeyFile();
110                         string[] variable_names;
111
112                         try {
113                                 file.load_from_file(path, 0);
114                         } catch (KeyFileError error) {
115                                 warning("%s", error.message);
116                                 return;
117                         } catch (FileError error) {
118                                 warning("%s", error.message);
119                                 return;
120                         }
121
122                         variable_names = file.get_groups();
123                         foreach(string name in variable_names) {
124                                 string policy;
125                                 try {
126                                         policy = file.get_value(name, "Policy");
127                                 } catch (KeyFileError error) {
128                                         policy = null;
129                                 }
130
131                                 if(policy != "send")
132                                         continue;
133
134                                 Variable variable = new Variable();
135                                 variable.name = name;
136                                 try {
137                                         variable.regex = file.get_value(name, "Regex");
138                                 } catch (KeyFileError error) {}
139
140                                 variables.set(name, variable);
141                         }
142                 }
143         }
144 }