FreeRDP
Loading...
Searching...
No Matches
lsa_transport.c
1
17
18#include <winpr/config.h>
19
20#ifndef _WIN32
21
22#include <winpr/crt.h>
23#include <winpr/assert.h>
24#include <winpr/wlog.h>
25#include <winpr/nt.h>
26#include <winpr/environment.h>
27#include <winpr/path.h>
28
29#include <errno.h>
30#include <string.h>
31#include <unistd.h>
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/socket.h>
35#include <sys/un.h>
36
37#include "lsa_transport.h"
38
39#define TAG "com.winpr.sspicli.lsa"
40
41/* Return a malloc'd copy of an environment variable's value, or nullptr when unset
42 * or empty. Uses the WinPR environment API, which is thread safe unlike getenv. */
43WINPR_ATTR_MALLOC(free, 1)
44static char* dup_env(const char* name)
45{
46 const DWORD n = GetEnvironmentVariableX(name, nullptr, 0);
47 if (n <= 1) /* unset or empty: the reported length-plus-null is 1 */
48 return nullptr;
49
50 char* value = malloc(n);
51 if (!value)
52 return nullptr;
53
54 if (GetEnvironmentVariableX(name, value, n) != (n - 1))
55 {
56 free(value);
57 return nullptr;
58 }
59 return value;
60}
61
62/* Must match default_socket_path() in winpr/tools/lsa4posix/main.c. */
63WINPR_ATTR_MALLOC(free, 1)
64static char* lsa_socket_path(void)
65{
66 char* env = dup_env("LSA4POSIX_SOCKET");
67 if (env)
68 return env;
69
70 /* $XDG_RUNTIME_DIR, or the temporary directory when it is not set. That one is shared between
71 * the users, so the directory name carries the uid. */
72 return GetKnownSubPathV(KNOWN_PATH_XDG_RUNTIME_DIR, "lsa4posix-%u/lsa.sock",
73 (unsigned)geteuid());
74}
75
76/* The socket directory must be owned by this user and not accessible to group or others. */
77static BOOL lsa_dir_is_safe(const char* sockPath)
78{
79 char* copy = _strdup(sockPath);
80 if (!copy)
81 return FALSE;
82
83 char* slash = strrchr(copy, '/');
84 if (!slash || (slash == copy))
85 {
86 free(copy);
87 return FALSE;
88 }
89 *slash = '\0';
90
91 struct stat st = WINPR_C_ARRAY_INIT;
92 const BOOL rc = (stat(copy, &st) == 0) && S_ISDIR(st.st_mode) && (st.st_uid == geteuid()) &&
93 ((st.st_mode & (S_IRWXG | S_IRWXO)) == 0);
94 if (!rc)
95 WLog_WARN(TAG, "lsa4posix socket directory is missing or not private");
96 free(copy);
97 return rc;
98}
99
100int lsa4posix_client_connect(void)
101{
102 char* path = lsa_socket_path();
103 if (!path)
104 return -1;
105
106 if (!lsa_dir_is_safe(path))
107 {
108 free(path);
109 return -1;
110 }
111
112 struct sockaddr_un addr = WINPR_C_ARRAY_INIT;
113 addr.sun_family = AF_UNIX;
114 if (strlen(path) >= sizeof(addr.sun_path))
115 {
116 WLog_ERR(TAG, "lsa4posix socket path too long");
117 free(path);
118 return -1;
119 }
120 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
121 free(path);
122
123 const int fd = socket(AF_UNIX, SOCK_STREAM, 0);
124 if (fd < 0)
125 return -1;
126
127 if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) != 0)
128 {
129 close(fd);
130 return -1;
131 }
132 return fd;
133}
134
135void lsa4posix_client_close(int fd)
136{
137 if (fd >= 0)
138 close(fd);
139}
140
141static BOOL lsa_write_all(int fd, const BYTE* data, size_t len)
142{
143 size_t off = 0;
144 while (off < len)
145 {
146 const ssize_t n = write(fd, &data[off], len - off);
147 if (n > 0)
148 off += (size_t)n;
149 else if ((n < 0) && (errno == EINTR))
150 continue;
151 else
152 return FALSE;
153 }
154 return TRUE;
155}
156
157static BOOL lsa_read_all(int fd, BYTE* data, size_t len)
158{
159 size_t off = 0;
160 while (off < len)
161 {
162 const ssize_t n = read(fd, &data[off], len - off);
163 if (n > 0)
164 off += (size_t)n;
165 else if ((n < 0) && (errno == EINTR))
166 continue;
167 else
168 return FALSE;
169 }
170 return TRUE;
171}
172
173static BOOL lsa_send(int fd, wStream* payload)
174{
175 const size_t len = Stream_Length(payload);
176 if ((len == 0) || (len > LSA_PROTO_MAX_FRAME))
177 return FALSE;
178
179 /* the frame length, big-endian */
180 BYTE prefix[4] = WINPR_C_ARRAY_INIT;
181 wStream pbuffer = WINPR_C_ARRAY_INIT;
182 wStream* ps = Stream_StaticInit(&pbuffer, prefix, sizeof(prefix));
183 Stream_Write_UINT32_BE(ps, (UINT32)len);
184
185 return lsa_write_all(fd, prefix, sizeof(prefix)) &&
186 lsa_write_all(fd, Stream_Buffer(payload), len);
187}
188
189WINPR_ATTR_MALLOC(Stream_Free, 1)
190static wStream* lsa_recv(int fd)
191{
192 BYTE prefix[4] = WINPR_C_ARRAY_INIT;
193 if (!lsa_read_all(fd, prefix, sizeof(prefix)))
194 return nullptr;
195
196 /* the frame length, big-endian */
197 wStream pbuffer = WINPR_C_ARRAY_INIT;
198 wStream* ps = Stream_StaticConstInit(&pbuffer, prefix, sizeof(prefix));
199 UINT32 len = 0;
200 Stream_Read_UINT32_BE(ps, len);
201 if ((len == 0) || (len > LSA_PROTO_MAX_FRAME))
202 return nullptr;
203
204 wStream* s = Stream_New(nullptr, len);
205 if (!s)
206 return nullptr;
207
208 if (!lsa_read_all(fd, Stream_Buffer(s), len))
209 {
210 Stream_Free(s, TRUE);
211 return nullptr;
212 }
213 if (!Stream_SetLength(s, len) || !Stream_SetPosition(s, 0))
214 {
215 Stream_Free(s, TRUE);
216 return nullptr;
217 }
218 return s;
219}
220
221NTSTATUS lsa4posix_transact(int fd, UINT16 expectType, wStream* request, UINT32 requestId,
222 LsaProtoReader* reader, LSA_MSG_HDR* respHdr, wStream** response)
223{
224 WINPR_ASSERT(response);
225 *response = nullptr;
226
227 const BOOL sent = lsa_send(fd, request);
228 Stream_Free(request, TRUE);
229 if (!sent)
230 return STATUS_UNSUCCESSFUL;
231
232 wStream* resp = lsa_recv(fd);
233 if (!resp)
234 return STATUS_UNSUCCESSFUL;
235
236 if (!lsa_proto_reader_init(reader, resp, respHdr))
237 {
238 Stream_Free(resp, TRUE);
239 return STATUS_UNSUCCESSFUL;
240 }
241
242 if ((respHdr->type != expectType) || !(respHdr->flags & LSA_MSG_FLAG_RESPONSE) ||
243 (respHdr->requestId != requestId))
244 {
245 lsa_proto_reader_done(reader);
246 Stream_Free(resp, TRUE);
247 return STATUS_UNSUCCESSFUL;
248 }
249
250 *response = resp;
251 return STATUS_SUCCESS;
252}
253
254#endif /* _WIN32 */