pcsc-lite  1.9.4
auth.c
Go to the documentation of this file.
1 /*
2  * MUSCLE SmartCard Development ( https://pcsclite.apdu.fr/ )
3  *
4  * Copyright (C) 2013 Red Hat
5  *
6  * All rights reserved.
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29  * DAMAGE.
30  *
31  * Author: Nikos Mavrogiannopoulos <nmav@redhat.com>
32  */
33 
42 #include "config.h"
43 #define _GNU_SOURCE
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47 #include <sys/un.h>
48 #include <stdio.h>
49 #include "debuglog.h"
50 #include "auth.h"
51 
52 #include <errno.h>
53 
54 #if defined(HAVE_POLKIT) && defined(SO_PEERCRED)
55 
56 #include <polkit/polkit.h>
57 
58 extern char disable_polkit;
59 
60 /* Returns non zero when the client is authorized */
61 unsigned IsClientAuthorized(int socket, const char* action, const char* reader)
62 {
63  struct ucred cr;
64  socklen_t cr_len;
65  int ret;
66  PolkitSubject *subject;
67  PolkitAuthority *authority;
68  PolkitAuthorizationResult *result;
69  PolkitDetails *details;
70  GError *error = NULL;
71  char action_name[128];
72 
73  if (disable_polkit)
74  return 1;
75 
76  snprintf(action_name, sizeof(action_name), "org.debian.pcsc-lite.%s", action);
77 
78  cr_len = sizeof(cr);
79  ret = getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len);
80  if (ret == -1)
81  {
82  int e = errno;
83  Log2(PCSC_LOG_CRITICAL,
84  "Error obtaining client process credentials: %s", strerror(e));
85  return 0;
86  }
87 
88  authority = polkit_authority_get_sync(NULL, &error);
89  if (authority == NULL)
90  {
91  Log2(PCSC_LOG_CRITICAL, "polkit_authority_get_sync failed: %s",
92  error->message);
93  g_error_free(error);
94  return 0;
95  }
96 
97  subject = polkit_unix_process_new_for_owner(cr.pid, 0, cr.uid);
98  if (subject == NULL)
99  {
100  Log1(PCSC_LOG_CRITICAL, "polkit_unix_process_new_for_owner failed");
101  ret = 0;
102  goto cleanup1;
103  }
104 
105  details = polkit_details_new();
106  if (details == NULL)
107  {
108  Log1(PCSC_LOG_CRITICAL, "polkit_details_new failed");
109  ret = 0;
110  goto cleanup0;
111  }
112 
113  if (reader != NULL)
114  polkit_details_insert(details, "reader", reader);
115 
116  result = polkit_authority_check_authorization_sync(authority, subject,
117  action_name, details,
118  POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
119  NULL,
120  &error);
121 
122  if (result == NULL)
123  {
124  Log2(PCSC_LOG_CRITICAL, "Error in authorization: %s", error->message);
125  g_error_free(error);
126  ret = 0;
127  }
128  else
129  {
130  if (polkit_authorization_result_get_is_authorized(result))
131  {
132  ret = 1;
133  }
134  else
135  {
136  ret = 0;
137  }
138  }
139 
140  if (ret == 0)
141  {
142  Log4(PCSC_LOG_CRITICAL,
143  "Process %u (user: %u) is NOT authorized for action: %s",
144  (unsigned)cr.pid, (unsigned)cr.uid, action);
145  }
146 
147  if (result)
148  g_object_unref(result);
149 
150  g_object_unref(subject);
151 cleanup0:
152  g_object_unref(details);
153 cleanup1:
154  g_object_unref(authority);
155 
156  return ret;
157 }
158 
159 #else
160 
161 unsigned IsClientAuthorized(int socket, const char* action, const char* reader)
162 {
163  (void)socket;
164  (void)action;
165  (void)reader;
166 
167  return 1;
168 }
169 
170 #endif
This handles debugging.