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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
|
/* external.cc: Interface to Cygwin internals from external programs.
Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
2008, 2009, 2010, 2011, 2012, 2014 Red Hat, Inc.
Written by Christopher Faylor <cgf@cygnus.com>
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include "sigproc.h"
#include "pinfo.h"
#include "shared_info.h"
#include "cygwin_version.h"
#include "cygerrno.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "heap.h"
#include "cygtls.h"
#include "child_info.h"
#include "environ.h"
#include "cygserver_setpwd.h"
#include "pwdgrp.h"
#include "exception.h"
#include <unistd.h>
#include <stdlib.h>
#include <wchar.h>
#include <iptypes.h>
child_info *get_cygwin_startup_info ();
static void exit_process (UINT, bool) __attribute__((noreturn));
static winpids pids;
static external_pinfo *
fillout_pinfo (pid_t pid, int winpid)
{
BOOL nextpid;
static external_pinfo ep;
static char ep_progname_long_buf[NT_MAX_PATH];
if ((nextpid = !!(pid & CW_NEXTPID)))
pid ^= CW_NEXTPID;
static unsigned int i;
if (!pids.npids || !nextpid)
{
pids.set (winpid);
i = 0;
}
if (!pid)
i = 0;
memset (&ep, 0, sizeof ep);
while (i < pids.npids)
{
DWORD thispid = pids.winpid (i);
_pinfo *p = pids[i];
i++;
if (!p)
{
if (!nextpid && thispid != (DWORD) pid)
continue;
ep.pid = cygwin_pid (thispid);
ep.dwProcessId = thispid;
ep.process_state = PID_IN_USE;
ep.ctty = -1;
break;
}
else if (nextpid || p->pid == pid || (winpid && thispid == (DWORD) pid))
{
ep.ctty = (p->ctty < 0 || iscons_dev (p->ctty)) ? p->ctty : device::minor (p->ctty);
ep.pid = p->pid;
ep.ppid = p->ppid;
ep.dwProcessId = p->dwProcessId;
ep.uid = p->uid;
ep.gid = p->gid;
ep.pgid = p->pgid;
ep.sid = p->sid;
ep.umask = 0;
ep.start_time = p->start_time;
ep.rusage_self = p->rusage_self;
ep.rusage_children = p->rusage_children;
ep.progname[0] = '\0';
sys_wcstombs(ep.progname, MAX_PATH, p->progname);
ep.strace_mask = 0;
ep.version = EXTERNAL_PINFO_VERSION;
ep.process_state = p->process_state;
ep.uid32 = p->uid;
ep.gid32 = p->gid;
ep.progname_long = ep_progname_long_buf;
mount_table->conv_to_posix_path (p->progname, ep.progname_long, 0);
break;
}
}
if (!ep.pid)
{
i = 0;
pids.reset ();
return NULL;
}
return &ep;
}
static inline uintptr_t
get_cygdrive_info (char *user, char *system, char *user_flags,
char *system_flags)
{
int res = mount_table->get_cygdrive_info (user, system, user_flags,
system_flags);
return (res == ERROR_SUCCESS) ? 1 : 0;
}
static bool
check_ntsec (const char *filename)
{
if (!filename)
return true;
path_conv pc (filename);
return pc.has_acls ();
}
/* Copy cygwin environment variables to the Windows environment. */
static PWCHAR
create_winenv (const char * const *env)
{
int unused_envc;
PWCHAR envblock = NULL;
char **envp = build_env (env ?: cur_environ (), envblock, unused_envc, false,
NULL);
PWCHAR p = envblock;
if (envp)
{
for (char **e = envp; *e; e++)
cfree (*e);
cfree (envp);
}
/* If we got an env block, just return pointer to win env. */
if (env)
return envblock;
/* Otherwise sync win env of current process with its posix env. */
if (!p)
return NULL;
while (*p)
{
PWCHAR eq = wcschr (p, L'=');
if (eq)
{
*eq = L'\0';
SetEnvironmentVariableW (p, ++eq);
p = eq;
}
p = wcschr (p, L'\0') + 1;
}
free (envblock);
return NULL;
}
/*
* Cygwin-specific wrapper for win32 ExitProcess and TerminateProcess.
* It ensures that the correct exit code, derived from the specified
* status value, will be made available to this process's parent (if
* that parent is also a cygwin process). If useTerminateProcess is
* true, then TerminateProcess(GetCurrentProcess(),...) will be used;
* otherwise, ExitProcess(...) is called.
*
* Used by startup code for cygwin processes which is linked statically
* into applications, and is not part of the cygwin DLL -- which is why
* this interface is exposed. "Normal" programs should use ANSI exit(),
* ANSI abort(), or POSIX _exit(), rather than this function -- because
* calling ExitProcess or TerminateProcess, even through this wrapper,
* skips much of the cygwin process cleanup code.
*/
static void
exit_process (UINT status, bool useTerminateProcess)
{
pid_t pid = getpid ();
external_pinfo *ep = fillout_pinfo (pid, 1);
DWORD dwpid = ep ? ep->dwProcessId : pid;
pinfo p (pid, PID_MAP_RW);
if (ep)
pid = ep->pid;
if ((dwpid == GetCurrentProcessId()) && (p->pid == pid))
p.set_exit_code ((DWORD)status);
if (useTerminateProcess)
TerminateProcess (GetCurrentProcess(), status);
/* avoid 'else' clause to silence warning */
ExitProcess (status);
}
extern "C" uintptr_t
cygwin_internal (cygwin_getinfo_types t, ...)
{
va_list arg;
uintptr_t res = (uintptr_t) -1;
va_start (arg, t);
switch (t)
{
case CW_LOCK_PINFO:
res = 1;
break;
case CW_UNLOCK_PINFO:
res = 1;
break;
case CW_GETTHREADNAME:
res = (uintptr_t) cygthread::name (va_arg (arg, DWORD));
break;
case CW_SETTHREADNAME:
{
set_errno (ENOSYS);
res = 0;
}
break;
case CW_GETPINFO:
res = (uintptr_t) fillout_pinfo (va_arg (arg, DWORD), 0);
break;
case CW_GETVERSIONINFO:
res = (uintptr_t) cygwin_version_strings;
break;
case CW_READ_V1_MOUNT_TABLES:
set_errno (ENOSYS);
res = 1;
break;
case CW_USER_DATA:
#ifndef __x86_64__
/* This is a kludge to work around a version of _cygwin_common_crt0
which overwrote the cxx_malloc field with the local DLL copy.
Hilarity ensues if the DLL is not loaded like while the process
is forking. */
__cygwin_user_data.cxx_malloc = &default_cygwin_cxx_malloc;
#endif
res = (uintptr_t) &__cygwin_user_data;
break;
case CW_PERFILE:
perfile_table = va_arg (arg, struct __cygwin_perfile *);
res = 0;
break;
case CW_GET_CYGDRIVE_PREFIXES:
{
char *user = va_arg (arg, char *);
char *system = va_arg (arg, char *);
res = get_cygdrive_info (user, system, NULL, NULL);
}
break;
case CW_GETPINFO_FULL:
res = (uintptr_t) fillout_pinfo (va_arg (arg, pid_t), 1);
break;
case CW_INIT_EXCEPTIONS:
/* noop */ /* init_exceptions (va_arg (arg, exception_list *)); */
res = 0;
break;
case CW_GET_CYGDRIVE_INFO:
{
char *user = va_arg (arg, char *);
char *system = va_arg (arg, char *);
char *user_flags = va_arg (arg, char *);
char *system_flags = va_arg (arg, char *);
res = get_cygdrive_info (user, system, user_flags, system_flags);
}
break;
case CW_SET_CYGWIN_REGISTRY_NAME:
case CW_GET_CYGWIN_REGISTRY_NAME:
res = 0;
break;
case CW_STRACE_TOGGLE:
{
pid_t pid = va_arg (arg, pid_t);
pinfo p (pid);
if (p)
{
sig_send (p, __SIGSTRACE);
res = 0;
}
else
{
set_errno (ESRCH);
res = (uintptr_t) -1;
}
}
break;
case CW_STRACE_ACTIVE:
{
res = strace.active ();
}
break;
case CW_CYGWIN_PID_TO_WINPID:
{
pinfo p (va_arg (arg, pid_t));
res = p ? p->dwProcessId : 0;
}
break;
case CW_EXTRACT_DOMAIN_AND_USER:
{
WCHAR nt_domain[MAX_DOMAIN_NAME_LEN + 1];
WCHAR nt_user[UNLEN + 1];
struct passwd *pw = va_arg (arg, struct passwd *);
char *domain = va_arg (arg, char *);
char *user = va_arg (arg, char *);
extract_nt_dom_user (pw, nt_domain, nt_user);
if (domain)
sys_wcstombs (domain, MAX_DOMAIN_NAME_LEN + 1, nt_domain);
if (user)
sys_wcstombs (user, UNLEN + 1, nt_user);
res = 0;
}
break;
case CW_CMDLINE:
{
size_t n;
pid_t pid = va_arg (arg, pid_t);
pinfo p (pid);
res = (uintptr_t) p->cmdline (n);
}
break;
case CW_CHECK_NTSEC:
{
char *filename = va_arg (arg, char *);
res = check_ntsec (filename);
}
break;
case CW_GET_ERRNO_FROM_WINERROR:
{
int error = va_arg (arg, int);
int deferrno = va_arg (arg, int);
res = geterrno_from_win_error (error, deferrno);
}
break;
case CW_GET_POSIX_SECURITY_ATTRIBUTE:
{
path_conv dummy;
security_descriptor sd;
int attribute = va_arg (arg, int);
PSECURITY_ATTRIBUTES psa = va_arg (arg, PSECURITY_ATTRIBUTES);
void *sd_buf = va_arg (arg, void *);
DWORD sd_buf_size = va_arg (arg, DWORD);
set_security_attribute (dummy, attribute, psa, sd);
if (!psa->lpSecurityDescriptor)
res = sd.size ();
else
{
psa->lpSecurityDescriptor = sd_buf;
res = sd.copy (sd_buf, sd_buf_size);
}
}
break;
case CW_GET_SHMLBA:
{
res = wincap.allocation_granularity ();
}
break;
case CW_GET_UID_FROM_SID:
{
cygpsid psid = va_arg (arg, PSID);
res = psid.get_uid (NULL);
}
break;
case CW_GET_GID_FROM_SID:
{
cygpsid psid = va_arg (arg, PSID);
res = psid.get_gid (NULL);
}
break;
case CW_GET_BINMODE:
{
const char *path = va_arg (arg, const char *);
path_conv p (path, PC_SYM_FOLLOW | PC_NULLEMPTY);
if (p.error)
{
set_errno (p.error);
res = (unsigned long) -1;
}
else
res = p.binmode ();
}
break;
case CW_HOOK:
{
const char *name = va_arg (arg, const char *);
const void *hookfn = va_arg (arg, const void *);
WORD subsys;
res = (uintptr_t) hook_or_detect_cygwin (name, hookfn, subsys);
}
break;
case CW_ARGV:
{
child_info_spawn *ci = (child_info_spawn *) get_cygwin_startup_info ();
res = (uintptr_t) (ci ? ci->moreinfo->argv : NULL);
}
break;
case CW_ENVP:
{
child_info_spawn *ci = (child_info_spawn *) get_cygwin_startup_info ();
res = (uintptr_t) (ci ? ci->moreinfo->envp : NULL);
}
break;
case CW_DEBUG_SELF:
error_start_init (va_arg (arg, const char *));
res = try_to_debug ();
break;
case CW_SYNC_WINENV:
create_winenv (NULL);
res = 0;
break;
case CW_CYGTLS_PADSIZE:
res = CYGTLS_PADSIZE;
break;
case CW_SET_DOS_FILE_WARNING:
{
dos_file_warning = va_arg (arg, int);
res = 0;
}
break;
case CW_SET_PRIV_KEY:
{
const char *passwd = va_arg (arg, const char *);
const char *username = va_arg (arg, const char *);
res = setlsapwd (passwd, username);
}
break;
case CW_SETERRNO:
{
const char *file = va_arg (arg, const char *);
int line = va_arg (arg, int);
seterrno(file, line);
res = 0;
}
break;
case CW_EXIT_PROCESS:
{
UINT status = va_arg (arg, UINT);
int useTerminateProcess = va_arg (arg, int);
exit_process (status, !!useTerminateProcess); /* no return */
}
case CW_SET_EXTERNAL_TOKEN:
{
HANDLE token = va_arg (arg, HANDLE);
int type = va_arg (arg, int);
set_imp_token (token, type);
res = 0;
}
break;
case CW_GET_INSTKEY:
{
PWCHAR dest = va_arg (arg, PWCHAR);
wcscpy (dest, cygheap->installation_key_buf);
res = 0;
}
break;
case CW_INT_SETLOCALE:
{
extern void internal_setlocale ();
internal_setlocale ();
res = 0;
}
break;
case CW_CVT_MNT_OPTS:
{
extern bool fstab_read_flags (char **, unsigned &, bool);
char **option_string = va_arg (arg, char **);
if (!option_string || !*option_string)
set_errno (EINVAL);
else
{
unsigned *pflags = va_arg (arg, unsigned *);
unsigned flags = pflags ? *pflags : 0;
if (fstab_read_flags (option_string, flags, true))
{
if (pflags)
*pflags = flags;
res = 0;
}
}
}
break;
case CW_LST_MNT_OPTS:
{
extern char *fstab_list_flags ();
char **option_string = va_arg (arg, char **);
if (!option_string)
set_errno (EINVAL);
else
{
*option_string = fstab_list_flags ();
if (*option_string)
res = 0;
}
}
break;
case CW_STRERROR:
{
int err = va_arg (arg, int);
res = (uintptr_t) strerror (err);
}
break;
case CW_CVT_ENV_TO_WINENV:
{
char **posix_env = va_arg (arg, char **);
res = (uintptr_t) create_winenv (posix_env);
}
break;
case CW_ALLOC_DRIVE_MAP:
{
dos_drive_mappings *ddm = new dos_drive_mappings ();
res = (uintptr_t) ddm;
}
break;
case CW_MAP_DRIVE_MAP:
{
dos_drive_mappings *ddm = va_arg (arg, dos_drive_mappings *);
wchar_t *pathbuf = va_arg (arg, wchar_t *);
if (ddm && pathbuf)
res = (uintptr_t) ddm->fixup_if_match (pathbuf);
}
break;
case CW_FREE_DRIVE_MAP:
{
dos_drive_mappings *ddm = va_arg (arg, dos_drive_mappings *);
if (ddm)
delete ddm;
res = 0;
}
break;
case CW_SETENT:
{
int group = va_arg (arg, int);
int enums = va_arg (arg, int);
PCWSTR enum_tdoms = va_arg (arg, PCWSTR);
if (group)
res = (uintptr_t) setgrent_filtered (enums, enum_tdoms);
else
res = (uintptr_t) setpwent_filtered (enums, enum_tdoms);
}
break;
case CW_GETENT:
{
int group = va_arg (arg, int);
void *obj = va_arg (arg, void *);
if (obj)
{
if (group)
res = (uintptr_t) getgrent_filtered (obj);
else
res = (uintptr_t) getpwent_filtered (obj);
}
}
break;
case CW_ENDENT:
{
int group = va_arg (arg, int);
void *obj = va_arg (arg, void *);
if (obj)
{
if (group)
endgrent_filtered (obj);
else
endpwent_filtered (obj);
res = 0;
}
}
break;
case CW_GETNSSSEP:
res = (uintptr_t) cygheap->pg.nss_separator ();
break;
case CW_GETNSS_PWD_SRC:
res = (uintptr_t) cygheap->pg.nss_pwd_src ();
break;
case CW_GETNSS_GRP_SRC:
res = (uintptr_t) cygheap->pg.nss_grp_src ();
break;
case CW_GETPWSID:
{
int db_only = va_arg (arg, int);
PSID psid = va_arg (arg, PSID);
cygpsid sid (psid);
res = (uintptr_t) (db_only ? internal_getpwsid_from_db (sid)
: internal_getpwsid (sid));
}
break;
case CW_GETGRSID:
{
int db_only = va_arg (arg, int);
PSID psid = va_arg (arg, PSID);
cygpsid sid (psid);
res = (uintptr_t) (db_only ? internal_getgrsid_from_db (sid)
: internal_getgrsid (sid));
}
break;
case CW_CYGNAME_FROM_WINNAME:
{
/* This functionality has been added mainly for sshd. Sshd
calls getpwnam() with the username of the non-privileged
user used for privilege separation. This is usually a
fixed string "sshd". However, when using usernames from
the Windows DBs, it's no safe bet anymore if the username
is "sshd", it could also be "DOMAIN+sshd". So what we do
here is this:
Sshd calls cygwin_internal (CW_CYGNAME_FROM_WINNAME,
"sshd",
username_buffer,
sizeof username_buffer);
If this call succeeds, sshd expects the correct Cygwin
username of the unprivileged sshd account in username_buffer.
The below code checks for a Windows username matching the
incoming username, and then fetches the Cygwin username with
the matching SID. This is our username used for privsep then.
Of course, other applications with similar needs can use the
same method. */
const char *winname = va_arg (arg, const char *);
char *buffer = va_arg (arg, char *);
size_t buflen = va_arg (arg, size_t);
if (!winname || !buffer || !buflen)
break;
WCHAR name[UNLEN + 1];
sys_mbstowcs (name, sizeof name, winname);
cygsid sid;
DWORD slen = SECURITY_MAX_SID_SIZE;
WCHAR dom[DNLEN + 1];
DWORD dlen = DNLEN + 1;
SID_NAME_USE acc_type;
if (!LookupAccountNameW (NULL, name, sid, &slen, dom, &dlen,
&acc_type))
break;
struct passwd *pw = internal_getpwsid (sid);
if (!pw)
break;
buffer[0] = '\0';
strncat (buffer, pw->pw_name, buflen - 1);
res = 0;
}
break;
case CW_FIXED_ATEXIT:
res = 0;
break;
case CW_EXCEPTION_RECORD_FROM_SIGINFO_T:
{
siginfo_t *si = va_arg(arg, siginfo_t *);
EXCEPTION_RECORD *er = va_arg(arg, EXCEPTION_RECORD *);
if (si && si->si_cyg && er)
{
memcpy(er, ((cygwin_exception *)si->si_cyg)->exception_record(),
sizeof(EXCEPTION_RECORD));
res = 0;
}
}
break;
default:
set_errno (ENOSYS);
}
va_end (arg);
return res;
}
|