-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathcygwin_idmapper.ksh
338 lines (301 loc) · 7.37 KB
/
cygwin_idmapper.ksh
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
#!/bin/ksh93
set -o nounset
typeset IFS=''
export PATH='/bin:/usr/bin'
export LC_ALL='en_US.UTF-8'
#
# global variables for this script
# (stored in compound variable so we
# can do a $ print -u2 -v c # for debugging)
#
compound c=(
mode="$1"
name="${2-}"
)
#
# Windows uses localised user and group names,
# e.g. on a German Windows the group "None" is called "Kein" etc.
#
# Cygwin allows to lookup users&&groups by their SID with
# /usr/bin/getent, so we use that to get the localised user/group
# name.
# Alternatively powershell can be used like this:
# ---- snip ----
# (New-Object System.Security.Principal.SecurityIdentifier \
# "S-1-5-21-3286904461-661230000-4220857270-513").Translate( [System.Security.Principal.NTAccount]).Value
# ---- snip ----
#
# FIXME: This still means we send a localised group name
# to the NFSv4 server, and it needs /etc/group entries for all
# localised variations of group "None". In the future the idmapper
# should do the mapping in both directions to avoid this.
#
typeset stdout
typeset -A c.localised_usernames
typeset -A c.localised_groupnames
# fixme: Different Windows versions use different machine SIDs
# Windows 10+Windows Server 2019 use
# "S-1-5-21-3286904461-661230000-4220857270", but other Windows
# versions use different values
typeset machine_sid="$(mkgroup -l | sed -n 's/[^:]*:\(S-[-0-9]*\)-513:.*$/\1/p')"
if [[ "$machine_sid" != ~(El)S-1-5-21- ]] ; then
print -u2 -f "%s: Unexpected machine SID '%q'\n" \
"$0" "$machine_sid"
exit 1
fi
# User "SYSTEM": de_DE: "SYSTEM" ...
stdout="$(getent passwd 'S-1-5-18')"
if (( $? == 0 )) && [[ "$stdout" != ~(El)Unknown\+User: ]] ; then
c.localised_usernames['SYSTEM']="${stdout%%:*}"
fi
# User "Adminstrator": fr_FR: "Administrateur" ...
stdout="$(getent passwd "${machine_sid}-500")"
if (( $? == 0 )) && [[ "$stdout" != ~(El)Unknown\+User: ]] ; then
c.localised_usernames['Administrator']="${stdout%%:*}"
fi
# Group "None": de_DE: "Kein", fr_FR: "Aucun" ...
stdout="$(getent group "${machine_sid}-513")"
if (( $? == 0 )) && [[ "$stdout" != ~(El)Unknown\+Group: ]] ; then
c.localised_groupnames['None']="${stdout%%:*}"
fi
compound -A localusers=(
#
# System accounts
#
#
# Site-specific users
#
["roland_mainz"]=(
localaccountname='roland_mainz'
localuid=197608
localgid=197121
)
["siegfried_wulsch"]=(
localaccountname='siegfried_wulsch'
localuid=197609
localgid=197121
)
["rmainz"]=(
localaccountname='rmainz'
localuid=1616
localgid=1616
)
["swulsch"]=(
localaccountname='swulsch'
localuid=1818
localgid=1818
)
["root"]=(
localaccountname='root'
localuid=0
localgid=0
)
["nobody"]=(
localaccountname='nobody'
localuid=65534
localgid=65534
)
)
if [[ -v c.localised_usernames['Administrator'] ]] ; then
localusers+=(
["${c.localised_usernames['Administrator']}"]=(
localaccountname="${c.localised_usernames['Administrator']}"
localuid=197108
localgid=197121
)
['Administrator']=(
localaccountname="${c.localised_usernames['Administrator']}"
localuid=197108
localgid=197121
)
# French user "Administrator"
['Administrateur']=(
localaccountname="${c.localised_usernames['Administrator']}"
localuid=197108
localgid=197121
)
)
fi
if [[ -v c.localised_usernames['SYSTEM'] ]] ; then
localusers+=(
["${c.localised_usernames['SYSTEM']}"]=(
localaccountname="${c.localised_usernames['SYSTEM']}"
localuid=18
localgid=18
)
["SYSTEM"]=(
localaccountname="${c.localised_usernames['SYSTEM']}"
localuid=18
localgid=18
)
# French user "SYSTEM"
# FIXME: This should be $'Syst\u[e8]me', but ksh93 1.0.10
# doesn't work
[$'Syst\xc3\xa8me']=(
localaccountname="${c.localised_usernames['SYSTEM']}"
localuid=18
localgid=18
)
)
fi
compound -A localgroups=(
#
# System accounts
#
#
# Site-specific users
#
["rmainz"]=(
localgroupname='rmainz'
localgid=1616
)
["swulsch"]=(
localgroupname='swulsch'
localgid=1818
)
["root"]=(
localgroupname='root'
localgid=0
)
["nogroup"]=(
localgroupname='nogroup'
localgid=65534
)
)
function getent_local_domain_passwd
{
integer res
typeset passwdname="$1"
#
# first try local accounts and if getent does
# not find anything do a (normal) domain lookup
#
# Cygwin getent uses '+' prefix to search for local
# accounts only
#
getent passwd "+${passwdname}"
(( res=$? ))
if (( res == 2 )) ; then
getent passwd "${passwdname}"
(( res=$? ))
fi
return $res
}
function getent_local_domain_group
{
integer res
typeset groupname="$1"
#
# first try local accounts and if getent does
# not find anything do a (normal) domain lookup
#
# Cygwin getent uses '+' prefix to search for local
# accounts only
#
getent group "+${groupname}"
(( res=$? ))
if (( res == 2 )) ; then
getent group "${groupname}"
(( res=$? ))
fi
return $res
}
if [[ -v c.localised_groupnames['None'] ]] ; then
localgroups+=(
["${c.localised_groupnames['None']}"]=(
localgroupname="${c.localised_groupnames['None']}"
localgid=197121
)
["None"]=(
localgroupname="${c.localised_groupnames['None']}"
localgid=197121
)
# French Windows localised group name for "None"
['Aucun']=(
localgroupname="${c.localised_groupnames['None']}"
localgid=197121
)
# German Windows localised group name for "None"
["Kein"]=(
localgroupname="${c.localised_groupnames['None']}"
localgid=197121
)
)
fi
case "${c.mode}" in
'nfsserver_owner2localaccount')
#
# Try static info
#
if [[ "${c.name}" == ~(Elr)[[:digit:]]+ ]] ; then
for s in "${!localusers[@]}" ; do
if (( localusers[$s].localuid == c.name )) ; then
print -v localusers[$s]
exit 0
fi
done
# getent passwd accepts numeric uids too, so continue below
fi
if [[ -v localusers["${c.name}"] ]] ; then
print -v localusers["${c.name}"]
exit 0
fi
#
# try getent passwd
#
compound gec # getent compound var
typeset dummy1 dummy2
getent_local_domain_passwd "${c.name}" | \
IFS=':' read gec.localaccountname dummy1 gec.localuid gec.localgid dummy2
if [[ "${gec.localaccountname-}" != '' ]] ; then
if [[ "${gec.localuid-}" == ~(Elr)[[:digit:]]+ && "${gec.localgid-}" == ~(Elr)[[:digit:]]+ ]] ; then
print -v gec
exit 0
else
print -u2 -f "cygwin_idmapper.ksh: getent passwd %q returned garbage.\n" "${c.name}"
fi
fi
print -u2 -f "cygwin_idmapper.ksh: Account '%q' not found.\n" "${c.name}"
exit 1
;;
'nfsserver_owner_group2localgroup')
#
# Try static info
#
if [[ "${c.name}" == ~(Elr)[[:digit:]]+ ]] ; then
for s in "${!localgroups[@]}" ; do
if (( localgroups[$s].localgid == c.name )) ; then
print -v localgroups[$s]
exit 0
fi
done
# getent group accepts numeric gids too, so continue below
fi
if [[ -v localgroups["${c.name}"] ]] ; then
print -v localgroups["${c.name}"]
exit 0
fi
#
# try getent group
#
compound gec # getent compound var
typeset dummy1 dummy2
getent_local_domain_group "${c.name}" | \
IFS=':' read gec.localgroupname dummy1 gec.localgid dummy2
if [[ "${gec.localgroupname-}" != '' ]] ; then
if [[ "${gec.localgid-}" == ~(Elr)[[:digit:]]+ ]] ; then
print -v gec
exit 0
else
print -u2 -f "cygwin_idmapper.ksh: getent group %q returned garbage.\n" "${c.name}"
fi
fi
print -u2 -f "cygwin_idmapper.ksh: Group '%q' not found.\n" "${c.name}"
exit 1
;;
*)
print -u2 -f "cygwin_idmapper.ksh: Unknown mode %q.\n" "${c.mode}"
exit 1
;;
esac
# EOF.