master
c 120 lines 2.44 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 user.c
8
9 Abstract:
10
11 This file is the source for the user management.
12
13 --*/
14
15 #include "lxtcommon.h"
16 #include "unittests.h"
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <pwd.h>
21
22 #define LXT_NAME "user"
23
24 int ValidateUserTest(char* Username, uid_t Uid, uid_t Gid);
25
26 int UserTestEntry(int Argc, char* Argv[])
27
28 /*++
29 --*/
30
31 {
32
33 LXT_ARGS Args;
34 uid_t Gid;
35 int Result = LXT_RESULT_FAILURE;
36 uid_t Uid;
37 char* Username;
38
39 LxtCheckResult(LxtInitialize(Argc, Argv, &Args, LXT_NAME));
40
41 if (Argc < 4)
42 {
43 LxtLogError("User test requires three arguments: username, uid, gid");
44 goto ErrorExit;
45 }
46
47 Username = Argv[1];
48 Uid = atoi(Argv[2]);
49 Gid = atoi(Argv[3]);
50 LxtCheckResult(ValidateUserTest(Username, Uid, Gid));
51
52 ErrorExit:
53 LxtUninitialize();
54 return !LXT_SUCCESS(Result);
55 }
56
57 int ValidateUserTest(char* Username, uid_t Uid, uid_t Gid)
58
59 /*++
60 --*/
61
62 {
63
64 struct passwd* PasswordEntry;
65 uid_t RealGid;
66 uid_t RealUid;
67 int Result = LXT_RESULT_FAILURE;
68
69 RealUid = getuid();
70 if (Uid != RealUid)
71 {
72 LxtLogError("Uid %u does not match RealUid %u", Uid, RealUid);
73 goto ErrorExit;
74 }
75
76 RealGid = getgid();
77 if (Gid != RealGid)
78 {
79 LxtLogError("Gid %u does not match RealGid %u", Gid, RealGid);
80 goto ErrorExit;
81 }
82
83 //
84 // Compare passed-in values to the values stored in the password entry file.
85 //
86
87 PasswordEntry = getpwnam(Username);
88 if (PasswordEntry == NULL)
89 {
90 LxtLogError("getpwnam %s failed", Username);
91 goto ErrorExit;
92 }
93
94 if (Uid != PasswordEntry->pw_uid)
95 {
96 LxtLogError("Uid %u does not match PasswordEntry->pw_uid %u", Uid, PasswordEntry->pw_uid);
97
98 goto ErrorExit;
99 }
100
101 if (Gid != PasswordEntry->pw_gid)
102 {
103 LxtLogError("Gid %u does not match PasswordEntry->pw_gid %u", Gid, PasswordEntry->pw_gid);
104
105 goto ErrorExit;
106 }
107
108 if (strstr(PasswordEntry->pw_dir, Username) == NULL)
109 {
110 LxtLogError("Home path %s does not contain Username %s", PasswordEntry->pw_dir, Username);
111
112 goto ErrorExit;
113 }
114
115 Result = LXT_RESULT_SUCCESS;
116 LxtLogPassed("Username %s, Uid %u, Gid %u successfully validated!", Username, Uid, Gid);
117
118 ErrorExit:
119 return Result;
120 }