Skip to content
API Documentation
BastionGuard™
SystemUser.vala
Go to the documentation of this file.
1
2/*
3 * SystemUser.vala
4 *
5 * Copyright 2012-2018 Tony George <teejeetech@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
21 *
22 *
23 */
24
25using TeeJee.Logging;
28
29public class SystemUser : GLib.Object {
30
31 public string name = "";
32 public string password = "";
33 public int uid = -1;
34 public int gid = -1;
35 public string user_info = "";
36 public string home_path = "";
37 public string shell_path = "";
38
39 public string full_name = "";
40 public string room_num = "";
41 public string phone_work = "";
42 public string phone_home = "";
43 public string other_info = "";
44
45 public bool has_encrypted_home = false;
46 public bool has_encrypted_private_dirs = false;
47 public Gee.ArrayList<string> encrypted_dirs = new Gee.ArrayList<string>();
48 public Gee.ArrayList<string> encrypted_private_dirs = new Gee.ArrayList<string>();
49
50 public bool is_selected = false;
51
52 public static Gee.HashMap<string,SystemUser> all_users;
53
54 public SystemUser(string name){
55 this.name = name;
56 }
57
58 public static void query_users(){
59
60 all_users = read_users_from_file("/etc/passwd");
61 }
62
63 public static Gee.ArrayList<SystemUser> all_users_sorted {
64 owned get {
65 var list = new Gee.ArrayList<SystemUser>();
66 foreach(var user in all_users.values) {
67 list.add(user);
68 }
69 list.sort((a,b) => { return strcmp(a.name, b.name); });
70 return list;
71 }
72 }
73
74 public static Gee.HashMap<string,SystemUser> read_users_from_file(string passwd_file){
75
76 var list = new Gee.HashMap<string,SystemUser>();
77
78 // read 'passwd' file ---------------------------------
79
80 string txt = file_read(passwd_file);
81
82 if (txt.length == 0){
83 return list;
84 }
85
86 foreach(string line in txt.split("\n")){
87 if ((line == null) || (line.length == 0)){
88 continue;
89 }
90 var user = parse_line_passwd(line);
91 if (user != null){
92 list[user.name] = user;
93 }
94 }
95
96 return list;
97 }
98
99 private static SystemUser? parse_line_passwd(string line){
100
101 if ((line == null) || (line.length == 0)){
102 return null;
103 }
104
105 SystemUser user = null;
106
107 //teejee:x:504:504:Tony George:/home/teejee:/bin/bash
108 string[] fields = line.split(":");
109
110 if (fields.length == 7){
111 user = new SystemUser(fields[0].strip());
112 user.password = fields[1].strip();
113 user.uid = int.parse(fields[2].strip());
114 user.gid = int.parse(fields[3].strip());
115 user.user_info = fields[4].strip();
116 user.home_path = fields[5].strip();
117 user.shell_path = fields[6].strip();
118
119 string[] arr = user.user_info.split(",");
120 if (arr.length >= 1){
121 user.full_name = arr[0];
122 }
123 if (arr.length >= 2){
124 user.room_num = arr[1];
125 }
126 if (arr.length >= 3){
127 user.phone_work = arr[2];
128 }
129 if (arr.length >= 4){
130 user.phone_home = arr[3];
131 }
132 if (arr.length >= 5){
133 user.other_info = arr[4];
134 }
135
137 }
138 else{
139 log_error("'passwd' file contains a record with non-standard fields" + ": %d".printf(fields.length));
140 return null;
141 }
142
143 return user;
144 }
145
146 public void check_encrypted_dirs() {
147
148 // check encrypted home ------------------------------
149
150 string ecryptfs_mount_file = "/home/.ecryptfs/%s/.ecryptfs/Private.mnt".printf(name);
151
152 if (file_exists(ecryptfs_mount_file)){
153
154 string txt = file_read(ecryptfs_mount_file);
155
156 foreach(string line in txt.split("\n")){
157
158 string path = line.strip();
159
160 if (path.length == 0){ continue; }
161
162 if (path == home_path){
163 has_encrypted_home = true;
164 }
165
166 encrypted_dirs.add(path);
167 }
168 }
169
170 // check encrypted Private dirs --------------------------
171
172 ecryptfs_mount_file = "%s/.ecryptfs/Private.mnt".printf(home_path);
173
174 if (file_exists(ecryptfs_mount_file)){
175
176 string txt = file_read(ecryptfs_mount_file);
177
178 foreach(string line in txt.split("\n")){
179
180 string path = line.strip();
181
182 if (path.length == 0){ continue; }
183
184 if (path != home_path){
186 encrypted_private_dirs.add(path);
187 }
188
189 encrypted_dirs.add(path);
190 }
191 }
192 }
193
194 public bool is_system{
195 get {
196 return ((uid != 0) && (uid < 1000)) || (uid == 65534) || (name == "PinguyBuilder"); // 65534 - nobody
197 }
198 }
199
200 public string group_names{
201 owned get {
202 return "";
203 }
204 }
205}
206
string user_info
SystemUser(string name)
static Gee.ArrayList< SystemUser > all_users_sorted
string full_name
static ? SystemUser parse_line_passwd(string line)
string phone_home
string name
bool has_encrypted_private_dirs
void check_encrypted_dirs()
string shell_path
bool has_encrypted_home
string group_names
string password
string room_num
string phone_work
bool is_selected
Gee.ArrayList< string > encrypted_dirs
Gee.ArrayList< string > encrypted_private_dirs
static Gee.HashMap< string, SystemUser > all_users
static Gee.HashMap< string, SystemUser > read_users_from_file(string passwd_file)
string home_path
string other_info
static void query_users()
bool file_exists(string file_path)
string? file_read(string file_path)
void log_error(string message, bool highlight=false, bool is_warning=false)