2003-10-14 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / bus / policy.h
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* policy.h  Bus security policy
3  *
4  * Copyright (C) 2003  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #ifndef BUS_POLICY_H
25 #define BUS_POLICY_H
26
27 #include <dbus/dbus.h>
28 #include <dbus/dbus-string.h>
29 #include <dbus/dbus-sysdeps.h>
30 #include "bus.h"
31
32 typedef enum
33 {
34   BUS_POLICY_RULE_SEND,
35   BUS_POLICY_RULE_RECEIVE,
36   BUS_POLICY_RULE_OWN,
37   BUS_POLICY_RULE_USER,
38   BUS_POLICY_RULE_GROUP
39 } BusPolicyRuleType;
40
41 /** determines whether the rule affects a connection, or some global item */
42 #define BUS_POLICY_RULE_IS_PER_CLIENT(rule) (!((rule)->type == BUS_POLICY_RULE_USER || \
43                                                (rule)->type == BUS_POLICY_RULE_GROUP))
44
45 struct BusPolicyRule
46 {
47   int refcount;
48   
49   BusPolicyRuleType type;
50
51   unsigned int allow : 1; /**< #TRUE if this allows, #FALSE if it denies */
52   
53   union
54   {
55     struct
56     {
57       /* message type can be DBUS_MESSAGE_TYPE_INVALID meaning "any" */
58       int   message_type;
59       /* any of these can be NULL meaning "any" */
60       char *path;
61       char *interface;
62       char *member;
63       char *error;
64       char *destination;      
65     } send;
66
67     struct
68     {
69       /* message type can be DBUS_MESSAGE_TYPE_INVALID meaning "any" */
70       int   message_type;
71       /* any of these can be NULL meaning "any" */
72       char *path;
73       char *interface;
74       char *member;
75       char *error;
76       char *origin;
77       unsigned int eavesdrop : 1;
78       unsigned int requested_reply : 1;
79     } receive;
80
81     struct
82     {
83       /* can be NULL meaning "any" */
84       char *service_name;
85     } own;
86
87     struct
88     {
89       /* can be DBUS_UID_UNSET meaning "any" */
90       dbus_uid_t uid;
91     } user;
92
93     struct
94     {
95       /* can be DBUS_GID_UNSET meaning "any" */
96       dbus_gid_t gid;
97     } group;
98     
99   } d;
100 };
101
102 BusPolicyRule* bus_policy_rule_new   (BusPolicyRuleType type,
103                                       dbus_bool_t       allow);
104 void           bus_policy_rule_ref   (BusPolicyRule    *rule);
105 void           bus_policy_rule_unref (BusPolicyRule    *rule);
106
107 BusPolicy*       bus_policy_new                   (void);
108 void             bus_policy_ref                   (BusPolicy        *policy);
109 void             bus_policy_unref                 (BusPolicy        *policy);
110 BusClientPolicy* bus_policy_create_client_policy  (BusPolicy        *policy,
111                                                    DBusConnection   *connection,
112                                                    DBusError        *error);
113 dbus_bool_t      bus_policy_allow_user            (BusPolicy        *policy,
114                                                    DBusUserDatabase *user_database,
115                                                    unsigned long     uid);
116 dbus_bool_t      bus_policy_append_default_rule   (BusPolicy        *policy,
117                                                    BusPolicyRule    *rule);
118 dbus_bool_t      bus_policy_append_mandatory_rule (BusPolicy        *policy,
119                                                    BusPolicyRule    *rule);
120 dbus_bool_t      bus_policy_append_user_rule      (BusPolicy        *policy,
121                                                    dbus_uid_t        uid,
122                                                    BusPolicyRule    *rule);
123 dbus_bool_t      bus_policy_append_group_rule     (BusPolicy        *policy,
124                                                    dbus_gid_t        gid,
125                                                    BusPolicyRule    *rule);
126 dbus_bool_t      bus_policy_merge                 (BusPolicy        *policy,
127                                                    BusPolicy        *to_absorb);
128
129 BusClientPolicy* bus_client_policy_new               (void);
130 void             bus_client_policy_ref               (BusClientPolicy  *policy);
131 void             bus_client_policy_unref             (BusClientPolicy  *policy);
132 dbus_bool_t      bus_client_policy_check_can_send    (BusClientPolicy  *policy,
133                                                       BusRegistry      *registry,
134                                                       DBusConnection   *receiver,
135                                                       DBusMessage      *message);
136 dbus_bool_t      bus_client_policy_check_can_receive (BusClientPolicy  *policy,
137                                                       BusRegistry      *registry,
138                                                       dbus_bool_t       requested_reply,
139                                                       DBusConnection   *sender,
140                                                       DBusConnection   *addressed_recipient,
141                                                       DBusConnection   *proposed_recipient,
142                                                       DBusMessage      *message);
143 dbus_bool_t      bus_client_policy_check_can_own     (BusClientPolicy  *policy,
144                                                       DBusConnection   *connection,
145                                                       const DBusString *service_name);
146 dbus_bool_t      bus_client_policy_append_rule       (BusClientPolicy  *policy,
147                                                       BusPolicyRule    *rule);
148 void             bus_client_policy_optimize          (BusClientPolicy  *policy);
149
150
151 #endif /* BUS_POLICY_H */