2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 systemd 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.
16 You should have received a copy of the GNU General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 [CCode (cheader_filename = "time.h")]
28 extern int clock_gettime(int id, out timespec ts);
30 public class PasswordDialog : Dialog {
34 public PasswordDialog(string message, string icon) {
35 set_title("System Password");
36 set_has_separator(false);
38 set_default_response(ResponseType.OK);
41 add_button(Stock.CANCEL, ResponseType.CANCEL);
42 add_button(Stock.OK, ResponseType.OK);
44 Container content = (Container) get_content_area();
46 Box hbox = new HBox(false, 16);
47 hbox.set_border_width(8);
50 Image image = new Image.from_icon_name(icon, IconSize.DIALOG);
51 hbox.pack_start(image, false, false);
53 Box vbox = new VBox(false, 8);
54 hbox.pack_start(vbox, true, true);
56 Label label = new Label(message);
57 vbox.pack_start(label, false, false);
60 entry.set_visibility(false);
61 entry.set_activates_default(true);
62 vbox.pack_start(entry, false, false);
64 entry.activate.connect(on_entry_activated);
69 public void on_entry_activated() {
70 response(ResponseType.OK);
74 public class MyStatusIcon : StatusIcon {
78 FileMonitor file_monitor;
84 PasswordDialog password_dialog;
86 public MyStatusIcon() throws GLib.Error {
87 GLib.Object(icon_name : "dialog-password");
88 set_title("System Password");
90 directory = File.new_for_path("/dev/.systemd/ask-password/");
91 file_monitor = directory.monitor_directory(0);
92 file_monitor.changed.connect(file_monitor_changed);
97 activate.connect(status_icon_activate);
100 void file_monitor_changed(GLib.File file, GLib.File? other_file, GLib.FileMonitorEvent event_type) throws GLib.Error {
102 if (!file.get_basename().has_prefix("ask."))
105 if (event_type == FileMonitorEvent.CREATED ||
106 event_type == FileMonitorEvent.DELETED)
110 void look_for_password() throws GLib.Error {
112 if (current != null) {
113 if (!current.query_exists()) {
115 if (password_dialog != null)
116 password_dialog.response(ResponseType.REJECT);
120 if (current == null) {
121 FileEnumerator enumerator = directory.enumerate_children("standard::name", FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
124 while ((i = enumerator.next_file()) != null) {
125 if (!i.get_name().has_prefix("ask."))
128 current = directory.get_child(i.get_name());
142 bool load_password() throws GLib.Error {
144 KeyFile key_file = new KeyFile();
149 key_file.load_from_file(current.get_path(), KeyFileFlags.NONE);
151 string not_after_as_string = key_file.get_string("Ask", "NotAfter");
153 clock_gettime(1, out ts);
154 uint64 now = (ts.tv_sec * 1000000) + (ts.tv_nsec / 1000);
157 if (not_after_as_string.scanf("%llu", out not_after) != 1)
163 socket = key_file.get_string("Ask", "Socket");
164 } catch (GLib.Error e) {
169 message = key_file.get_string("Ask", "Message").compress();
170 } catch (GLib.Error e) {
171 message = "Please Enter System Password!";
173 set_tooltip_text(message);
176 icon = key_file.get_string("Ask", "Icon");
177 } catch (GLib.Error e) {
178 icon = "dialog-password";
180 set_from_icon_name(icon);
184 Notification n = new Notification(title, message, icon);
191 void status_icon_activate() throws GLib.Error {
196 if (password_dialog != null) {
197 password_dialog.present();
201 password_dialog = new PasswordDialog(message, icon);
203 int result = password_dialog.run();
204 string password = password_dialog.entry.get_text();
206 password_dialog.destroy();
207 password_dialog = null;
209 if (result == ResponseType.REJECT ||
210 result == ResponseType.DELETE_EVENT)
215 Process.spawn_async_with_pipes(
217 { "/usr/bin/pkexec", "/lib/systemd/systemd-reply-password", result == ResponseType.OK ? "1" : "0", socket },
226 OutputStream stream = new UnixOutputStream(to_process, true);
228 stream.write(password.data, null);
232 static const OptionEntry entries[] = {
236 void show_error(string e) {
237 var m = new MessageDialog(null, 0, MessageType.ERROR, ButtonsType.CLOSE, "%s", e);
242 int main(string[] args) {
244 Gtk.init_with_args(ref args, "[OPTION...]", entries, "systemd-ask-password-agent");
245 Notify.init("Password Agent");
247 MyStatusIcon i = new MyStatusIcon();
250 } catch (DBus.Error e) {
251 show_error(e.message);
252 } catch (GLib.Error e) {
253 show_error(e.message);