121{
122 static int _is_service = -1;
123 BOOL IsMember;
124 PSID ServiceSid;
125 PSID LocalSystemSid;
126 SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
127 HANDLE stderr_handle;
128
129
130 if (_is_service != -1)
131 return _is_service;
132
133
134 stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
135 if (stderr_handle != INVALID_HANDLE_VALUE && stderr_handle != NULL)
136 {
137 _is_service = 0;
138 return _is_service;
139 }
140
141
142 if (!AllocateAndInitializeSid(&NtAuthority, 1,
143 SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0,
144 &LocalSystemSid))
145 {
146 fprintf(stderr,
"could not get SID for local system account\n");
147 return -1;
148 }
149
150 if (!CheckTokenMembership(NULL, LocalSystemSid, &IsMember))
151 {
152 fprintf(stderr,
"could not check access token membership: error code %lu\n",
153 GetLastError());
154 FreeSid(LocalSystemSid);
155 return -1;
156 }
157 FreeSid(LocalSystemSid);
158
159 if (IsMember)
160 {
161 _is_service = 1;
162 return _is_service;
163 }
164
165
166 if (!AllocateAndInitializeSid(&NtAuthority, 1,
167 SECURITY_SERVICE_RID, 0, 0, 0, 0, 0, 0, 0,
168 &ServiceSid))
169 {
170 fprintf(stderr,
"could not get SID for service group: error code %lu\n",
171 GetLastError());
172 return -1;
173 }
174
175 if (!CheckTokenMembership(NULL, ServiceSid, &IsMember))
176 {
177 fprintf(stderr,
"could not check access token membership: error code %lu\n",
178 GetLastError());
179 FreeSid(ServiceSid);
180 return -1;
181 }
182 FreeSid(ServiceSid);
183
184 if (IsMember)
185 _is_service = 1;
186 else
187 _is_service = 0;
188
189 return _is_service;
190}