summaryrefslogtreecommitdiff
path: root/src/rygel/rygel-acl.vala
blob: 8165e3a483cf72d77ebfbbc5c3600ab7a14491be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (C) 2014 Jens Georg <mail@jensge.org>
 *
 * Author: Jens Georg <mail@jensge.org>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Rygel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

internal class Rygel.Acl : GLib.Object, GUPnP.Acl
{
    private DBusAclProvider provider;
    private Configuration configuration;
    private bool fallback_policy;

    public override void constructed () {
        base.constructed ();

        Bus.watch_name (BusType.SESSION,
                        DBusAclProvider.SERVICE_NAME,
                        BusNameWatcherFlags.AUTO_START,
                        this.on_name_appeared,
                        this.on_name_vanished);

        this.configuration = MetaConfig.get_default ();
        this.fallback_policy = true;
        this.update_fallback_policy ();

        this.configuration.setting_changed.connect ( (s, k) => {
            if (s == "general" && k == "acl-fallback-policy") {
                this.update_fallback_policy ();
            }
        });
     }

    /**
     * Whether this provider supports sync access.
     *
     * If we do not have a DBus provider (yet) there is no need to
     * artificially delay the fall-back policy answer.
     */
    public bool can_sync () { return this.provider == null; }

    public bool is_allowed (GUPnP.Device? device,
                            GUPnP.Service? service,
                            string         path,
                            string         address,
                            string?        agent) {
        if (this.provider == null) {
            return this.fallback_policy;
        } else {
            assert_not_reached ();
        }
    }

    public async bool is_allowed_async (GUPnP.Device? device,
                                        GUPnP.Service? service,
                                        string path,
                                        string address,
                                        string? agent,
                                        GLib.Cancellable? cancellable)
                                        throws GLib.Error {
        if (this.provider == null) {
            Idle.add ( () => { is_allowed_async.callback (); return false; });
            yield;

            return this.fallback_policy;
        }

        debug ("Querying ACL for %s on %s by %s@%s",
               path,
               device != null ? device.udn : "none",
               agent ?? "Unknown",
               address);

        try {
            var device_hash = new HashTable<string, string> (str_hash, str_equal);

            if (device != null) {
                device_hash["FriendlyName"] = device.get_friendly_name ();
                device_hash["UDN"] = device.udn;
                device_hash["Type"] = device.device_type;
            }

            var service_hash = new HashTable<string, string> (str_hash, str_equal);
            if (service != null) {
                service_hash["Type"] = service.service_type;
            }

            var allowed = yield provider.is_allowed (device_hash,
                                                     service_hash,
                                                     path,
                                                     address,
                                                     agent ?? "");
            return allowed;
        } catch (Error error) {
            warning (_("Failed to query ACL: %s"), error.message);
        }

        return false;
    }

    private void on_name_appeared (DBusConnection connection,
                                   string         name,
                                   string         name_owner) {
        debug ("Found ACL provider %s (%s), creating object",
               name,
               name_owner);
        try {
            this.provider = Bus.get_proxy_sync (BusType.SESSION,
                                                name,
                                                DBusAclProvider.OBJECT_PATH);
        } catch (Error error) {
            warning (_("Error creating D-Bus proxy for ACL: %s"),
                     error.message);
        }
    }

    private void on_name_vanished (DBusConnection? connection, string name) {
        this.provider = null;
    }

    private void update_fallback_policy () {
        try {
            this.fallback_policy = this.configuration.get_bool
                                        ("general",
                                         "acl-fallback-policy");
            debug ("Found ACL fallback policy “%s”",
                   this.fallback_policy ? "allow" : "deny");
        } catch (Error error) {
            if (this.fallback_policy) {
                message (_("No ACL fallback policy found. Using “allow”"));
            } else {
                message (_("No ACL fallback policy found. Using “deny”"));
            }
        }
    }
}