-
Notifications
You must be signed in to change notification settings - Fork 45
/
userInfoService.c
106 lines (86 loc) · 3.32 KB
/
userInfoService.c
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
/*
This program and the accompanying materials are
made available under the terms of the Eclipse Public License v2.0 which accompanies
this distribution, and is available at https://www.eclipse.org/legal/epl-v20.html
SPDX-License-Identifier: EPL-2.0
Copyright Contributors to the Zowe Project.
*/
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <errno.h>
#include "zowetypes.h"
#include "alloc.h"
#include "utils.h"
#include "charsets.h"
#include "httpserver.h"
#include "json.h"
#include "zssLogging.h"
#include "serviceUtils.h"
#include "userInfoService.h"
static int serveUserInfo(HttpService *service, HttpResponse *response);
static void respondWithUserInfo(HttpResponse *response, struct passwd *pw);
static void respondWithUserInfoError(HttpResponse *response, int code, const char *status, const char *message);
int installUserInfoService(HttpServer *server) {
HttpService *httpService = makeGeneratedService("User info service", "/user-info");
httpService->authType = SERVICE_AUTH_NATIVE_WITH_SESSION_TOKEN;
httpService->serviceFunction = serveUserInfo;
httpService->runInSubtask = TRUE;
httpService->doImpersonation = TRUE;
registerHttpService(server, httpService);
return 0;
}
static int serveUserInfo(HttpService *service, HttpResponse *response) {
HttpRequest *request = response->request;
const char *userId = request->username;
if (0 != strcmp(request->method, methodGET)) {
respondWithUserInfoError(response, HTTP_STATUS_METHOD_NOT_FOUND, "Method Not Allowed", "Only GET method allowed");
return 0;
}
struct passwd pw = {0};
struct passwd *pwPtr = NULL;
char buf[1024] = {0};
int rc = getpwnam_r(userId, &pw, buf, sizeof(buf), &pwPtr);
if (rc != 0) {
zowelog(NULL, LOG_COMP_ID_MVD_SERVER, ZOWE_LOG_DEBUG, "failed to get user info for %s - %s\n", userId, strerror(errno));
respondWithUserInfoError(response, HTTP_STATUS_INTERNAL_SERVER_ERROR, "Internal Server Error", "Failed to get user info");
return 0;
}
respondWithUserInfo(response, &pw);
return 0;
}
static void respondWithUserInfo(HttpResponse *response, struct passwd *pw) {
HttpRequest *request = response->request;
jsonPrinter *p = respondWithJsonPrinter(response);
setResponseStatus(response, 200, "OK");
setDefaultJSONRESTHeaders(response);
writeHeader(response);
jsonStart(p);
jsonAddString(p, "userId", pw->pw_name);
jsonAddUInt(p, "uid", pw->pw_uid);
jsonAddUInt(p, "gid", pw->pw_gid);
jsonAddString(p, "home", pw->pw_dir);
jsonAddString(p, "shell", pw->pw_shell);
jsonEnd(p);
finishResponse(response);
}
static void respondWithUserInfoError(HttpResponse *response, int code, const char *status, const char *message) {
HttpRequest *request = response->request;
jsonPrinter *p = respondWithJsonPrinter(response);
setResponseStatus(response, code, (char*)status);
setDefaultJSONRESTHeaders(response);
writeHeader(response);
jsonStart(p);
jsonAddString(p, "error", (char*)message);
jsonEnd(p);
finishResponse(response);
}
/*
This program and the accompanying materials are
made available under the terms of the Eclipse Public License v2.0 which accompanies
this distribution, and is available at https://www.eclipse.org/legal/epl-v20.html
SPDX-License-Identifier: EPL-2.0
Copyright Contributors to the Zowe Project.
*/