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
|
%% options
copyright owner = Dirk Krause
copyright year = 2015-xxxx
SPDX-License-Identifier: BSD-3-Clause
%% header
/** @file
Get information about user
(login name and home directory).
*/
#ifndef DK4CONF_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4conf.h"
#else
#include <dktools-4/dk4conf.h>
#endif
#endif
#ifndef DK4TYPES_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4types.h"
#else
#include <dktools-4/dk4types.h>
#endif
#endif
#ifndef DK4ERROR_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4error.h"
#else
#include <dktools-4/dk4error.h>
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/** Get users login name.
@param pdst Pointer to destination buffer.
@param szdst Size of destination buffer (number of characters).
@param eff Flag: Retrieve information about effective user.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error.
CRT on Windows: Not used.
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if pdst is NULL or szdst is 0,
- DK4_E_NOT_FOUND<br>
if the login name was not found,
- DK4_E_BUFFER_TOO_SMALL<br>
if the buffer is too small for the result.
*/
int
dk4user_get_logname(dkChar *pdst, size_t szdst, int eff, dk4_er_t *erp);
/** Get users home directory.
@param pdst Pointer to destination buffer.
@param szdst Size of destination buffer (number of characters).
@param eff Flag: Retrieve information about effective user.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error.
CRT on Windows: Optional.
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if pdst is NULL or szdst is 0,
- DK4_E_NOT_FOUND<br>
if the login name, the user entry or the home
directory was not found,
- DK4_E_MEMORY<br>
if an internal memory allocation failed,
- DK4_E_SYNTAX<br>
if the retrieved registry entry on
Windows is not a string,
- DK4_E_BUFFER_TOO_SMALL<br>
if the pdst buffer or one of the internal buffers
is too small for the result.
*/
int
dk4user_get_homedir(dkChar *pdst, size_t szdst, int eff, dk4_er_t *erp);
#ifdef __cplusplus
}
#endif
%% module
#include "dk4conf.h"
#include "dk4user.h"
#include "dk4str8.h"
#include "dk4strw.h"
#include "dk4strd.h"
#include "dk4wregw.h"
#include "dk4wregc.h"
#include "dk4mem.h"
#include "dk4error.h"
#include "dk4masz.h"
#include "dk4mpl.h"
#include "dk4unused.h"
#if DK4_ON_WINDOWS
#ifndef WINDOWS_H_INCLUDED
#include <windows.h>
#define WINDOWS_H_INCLUDED 1
#endif
#ifndef LM_H_INCLUDED
#include <lm.h>
#define LM_H_INCLUDED 1
#endif
#ifndef SDDL_H_INCLUDED
#include <sddl.h>
#define SDDL_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_SYS_TYPES_H
#ifndef SYS_TYPES_H_INCLUDED
#include <sys/types.h>
#define SYS_TYPES_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_UNISTD_H
#ifndef UNISTD_H_INCLUDED
#include <unistd.h>
#define UNISTD_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_PWD_H
#ifndef PWD_H_INCLUDED
#include <pwd.h>
#define PWD_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_ASSERT_H
#ifndef ASSERT_H_INCLUDED
#include <assert.h>
#define ASSERT_H_INCLUDED 1
#endif
#endif
$!trace-include
#if DK4_ON_WINDOWS
static const wchar_t * const dk4user_kw[] = {
$!string-table prefix=L
#
# 0 ... 2 Registry key name and value name for profile dir
#
SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList
\\
ProfileImagePath
#
# 3 4 5 6
#
LOGONSERVER
USERPROFILE
HOMEDRIVE
HOMEPATH
$!end
};
#endif
int
dk4user_get_logname(
dkChar *pdst,
size_t szdst,
#if DK4_ON_WINDOWS
int DK4_ARG_UNUSED(eff),
#else
int eff,
#endif
dk4_er_t *erp
)
{
#if DK4_ON_WINDOWS
DWORD sz;
int back = 0;
$? "+ dk4user_get_logname (Windows)"
DK4_UNUSED_ARG(eff)
#if DK4_USE_ASSERT
assert(NULL != pdst);
assert(0 < szdst);
#endif
if ((NULL != pdst) && (0 < szdst)) {
sz = (DWORD)szdst;
#if DK4_CHAR_SIZE > 1
if(GetUserNameW(pdst, &sz))
#else
if(GetUserNameA(pdst, &sz))
#endif
{
if (sz < szdst) {
pdst[sz] = dkT('\0');
back = 1;
} else {
dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
}
}
else
{
dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
} $? "- dk4user_get_logname (Windows) %d", back
return back;
#else
#if DK4_CHAR_SIZE > 1
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
return 0;
#else
struct passwd *pwent;
uid_t uid;
int back = 0;
$? "+ dk4user_get_logname (non-Windows, char)"
#if DK4_USE_ASSERT
assert(NULL != pdst);
assert(0 < szdst);
#endif
if ((NULL != pdst) && (0 < szdst)) {
if (0 != eff) { uid = geteuid(); } else { uid = getuid(); }
pwent = getpwuid(uid);
if (NULL != pwent) {
if (NULL != pwent->pw_name) {
if (0 != dk4str_cpy_s(pdst, szdst, pwent->pw_name, erp)) {
back = 1;
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
} $? "- dk4user_get_logname (non-Windows, char) %d", back
return back;
#endif
#endif
}
#if DK4_ON_WINDOWS
/** Obtain home directory information using one logon server.
@param pdst Destination buffer pointer.
@param szdst Destination buffer size (number of wchar_t).
@param lsn Logon server name, may be NULL.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error.
*/
static
int
dk4user_get_i_ls_homedir(
wchar_t *pdst, size_t szdst, const wchar_t *lsn, dk4_er_t *erp
)
{
wchar_t b1[DK4_MAX_PATH+16]; /* Buffer RegQueryValue() */
wchar_t b2[DK4_MAX_PATH+16]; /* Buffer Expand...() */
wchar_t logname[256]; /* Login name */
dk4_er_t er; /* Error report */
USER_INFO_4 *pbuf4 = NULL; /* User information */
wchar_t *pStringSid = NULL; /* String SID */
wchar_t *keyname = NULL; /* Registry key name */
void *wrkey = NULL; /* Registry key */
DWORD szlog = DK4_SIZEOF(logname,wchar_t);
size_t sz = 0; /* Key name length */
int back = 0; /* Result */
NET_API_STATUS nStatus = 0; /* Network op result */
$? "+ dk4user_get_i_ls_homedir lsn=\"%!ws\"", TR_LSTR(lsn)
#if DK4_USE_ASSERT
assert(NULL != pdst);
assert(0 < szdst);
#endif
dk4error_init(&er);
if (GetUserNameW(logname, &szlog)) { $? ". user name found"
if (DK4_SIZEOF(logname,wchar_t) >= szlog) { $? ". name size ok"
logname[szlog] = L'\0';
nStatus = NetUserGetInfo(lsn, logname, (DWORD)4, (LPBYTE *)(&pbuf4));
if ((NERR_Success == nStatus) && (NULL != pbuf4)) { $? ". user info"
if (ConvertSidToStringSidW(pbuf4->usri4_user_sid, &pStringSid)) {
$? ". sid"
sz = dk4ma_size_t_add(
dk4ma_size_t_add(
1,
dk4strw_len(dk4user_kw[0]),
&er
),
dk4ma_size_t_add(
dk4strw_len(dk4user_kw[1]),
dk4strw_len(pStringSid),
&er
),
&er
);
if (DK4_E_NONE == er.ec) { $? ". size calc 1 ok"
(void)dk4ma_size_t_mul(sizeof(wchar_t), sz, &er);
if (DK4_E_NONE == er.ec) { $? ". size calc 2 ok"
keyname = dk4mem_calloc(sizeof(wchar_t),sz,&er);
if (NULL != keyname) { $? ". calloc ok"
(void)dk4strw_cpy_s(keyname, sz, dk4user_kw[0], NULL);
(void)dk4strw_cat_s(keyname, sz, dk4user_kw[1], NULL);
(void)dk4strw_cat_s(keyname, sz, pStringSid, NULL);
wrkey = dk4wreg_open_key_wc(DK4_WREGKEY_HKLM, keyname, 0, &er);
if (NULL != wrkey) { $? ". key opened"
back = dk4wreg_get_expanded_string_wc(
wrkey,
dk4user_kw[2],
pdst, szdst,
b1, DK4_SIZEOF(b1,wchar_t),
b2, (DK4_SIZEOF(b2,wchar_t)-2),
&er
); $? ". back = %d", back
dk4wreg_close_key(wrkey);
} else { $? "! registry key"
/* ERROR: Failed to open key (reported) */
}
dk4mem_free(keyname);
} else { $? "! memory"
/* ERROR: Memory (reported) */
}
} else { $? "! size calc overflow"
/* ERROR: Overflow in size calculation (reported) */
}
} else { $? "! size calc overflow"
/* ERROR: Overflow in size calculation (reported) */
}
} else { $? "! failed to convert sid"
/* ERROR: Failed to convert SID */
dk4error_set_simple_error_code(&er, DK4_E_NOT_FOUND);
}
if (NULL != pStringSid) {
LocalFree(pStringSid);
}
} else { $? "! failed to obtain info"
/* ERROR: No user info */
}
if (NULL != pbuf4) {
NetApiBufferFree(pbuf4);
}
} else { $? "! user name too long"
/* ERROR: User name too long */
dk4error_set_simple_error_code(&er, DK4_E_BUFFER_TOO_SMALL);
}
} else { $? "! user name not found"
/* ERROR: User name not found */
}
if (0 == back) {
dk4error_copy(erp, &er);
}
$? "- dk4user_get_i_ls_homedir %d", back
return back;
}
/** Find Windows home directory.
@param pdst Destination buffer pointer.
@param szdst Destination buffer size (number of wchar_t).
@param erp Error report, may be NULL.
@return 1 on success, 0 on error.
*/
static
int
dk4user_get_i_homedir(wchar_t *pdst, size_t szdst, dk4_er_t *erp)
{
dk4_er_t myerp;
wchar_t *lsn = NULL;
wchar_t *hop = NULL;
int back = 0;
$? "+ dk4user_get_i_homedir"
#if DK4_USE_ASSERT
assert(NULL != pdst);
assert(0 < szdst);
#endif
dk4error_init(&myerp);
back = dk4user_get_i_ls_homedir(pdst, szdst, NULL, &myerp);
$? ". back = %d", back
if ((0 == back) && (DK4_E_NONE == myerp.ec)) { $? ". attempt"
lsn = _wgetenv(dk4user_kw[4]); $? ". userprofile"
if (NULL != lsn) {
back = dk4strw_cpy_s(pdst, szdst, lsn, &myerp);
$? ". back = %d", back
} else { $? "! userprofile"
}
}
if ((0 == back) && (DK4_E_NONE == myerp.ec)) { $? ". attempt"
lsn = _wgetenv(dk4user_kw[5]);
hop = _wgetenv(dk4user_kw[6]);
if ((NULL != lsn) && (NULL != hop)) { $? ". drive / path"
if (0 != dk4strw_cpy_s(pdst, szdst, lsn, &myerp)) {
back = dk4strw_cat_s(pdst, szdst, hop, &myerp);
$? ". back = %d", back
} else { $? "! too long"
}
} else { $? "! drive / path"
}
}
if ((0 == back) && (DK4_E_NONE == myerp.ec)) { $? ". attempt"
lsn = _wgetenv(dk4user_kw[3]);
if (NULL != lsn) { $? ". env ok"
back = dk4user_get_i_ls_homedir(pdst, szdst, lsn, &myerp);
$? ". back = %d", back
}
}
if ((0 == back) && (DK4_E_NONE == myerp.ec)) { $? ". attempt"
if (NULL != lsn) { $? ". lsn"
if (L'\\' == lsn[0]) {
if (L'\\' == lsn[1]) {
back = dk4user_get_i_ls_homedir(pdst, szdst, &(lsn[2]), &myerp);
$? ". back = %d", back
} else { $? "! lsn[1]"
}
} else { $? "! lsn[0]"
}
} else { $? "! lsn"
}
}
if ((0 == back) && (DK4_E_NONE == myerp.ec)) {
dk4error_set_simple_error_code(&myerp, DK4_E_NOT_FOUND);
}
if (0 == back) {
dk4error_copy(erp, &myerp);
} $? "- dk4user_get_i_homedir %d", back
return back;
}
#endif
int
dk4user_get_homedir(
dkChar *pdst,
size_t szdst,
#if DK4_ON_WINDOWS
int DK4_ARG_UNUSED(eff),
#else
int eff,
#endif
dk4_er_t *erp
)
{
#if DK4_ON_WINDOWS
/* +++++ Windows +++++ */
#if DK4_CHAR_SIZE == 1
wchar_t homew[DK4_MAX_PATH];
#endif
int back = 0;
$? "+ dk4user_get_homedir"
DK4_UNUSED_ARG(eff)
#if DK4_USE_ASSERT
assert(NULL != pdst);
assert(0 < szdst);
#endif
if ((NULL != pdst) && (0 < szdst)) { $? ". arguments"
*pdst = dkT('\0');
#if DK4_CHAR_SIZE > 1
$? ". wchar_t"
back = dk4user_get_i_homedir(pdst, szdst, erp);
#else
$? ". char"
if (0 != dk4user_get_i_homedir(homew, DK4_SIZEOF(homew,wchar_t), erp)) {
back = dk4recode_utf16_to_ansi(pdst, szdst, homew, erp);
}
#endif
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
} $? "- dk4user_get_homedir %d", back
return back;
/* ----- Windows ----- */
#else
/* +++++ non-Windows +++++ */
#if DK4_CHAR_SIZE > 1
/* +++++ non-Windows wchar_t +++++ */
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
return 0;
/* ----- non-Windows wchar_t ----- */
#else
/* +++++ non-Windows char +++++ */
struct passwd *pwent;
uid_t uid;
int back = 0;
$? "+ dk4user_get_homedir (non-windows, char)"
#if DK4_USE_ASSERT
assert(NULL != pdst);
assert(0 < szdst);
#endif
if ((NULL != pdst) && (0 < szdst)) {
if (0 != eff) { uid = geteuid(); } else { uid = getuid(); }
pwent = getpwuid(uid);
if (NULL != pwent) {
if (NULL != pwent->pw_dir) {
if (0 != dk4str8_cpy_s(pdst, szdst, pwent->pw_dir, erp)) {
back = 1;
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
} $? "- dk4user_get_homedir (non-windows, char) %d", back
return back;
/* ----- non-Windows char ----- */
#endif
/* ----- non-Windows ----- */
#endif
}
|