This repository has been archived by the owner on Dec 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
184 lines (162 loc) · 5.96 KB
/
main.cpp
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
/* Copyright (C) 2015, Tim Cooper <tim.cooper@layeh.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of the Mumble Developers nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include <cstdlib>
#include <cstring>
#define BOOST_LOG_DYN_LINK 1
#include <boost/log/trivial.hpp>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <security/pam_appl.h>
#include <grpc/grpc.h>
#include <grpc++/channel_arguments.h>
#include <grpc++/client_context.h>
#include <grpc++/create_channel.h>
#include <grpc++/credentials.h>
#include "MurmurRPC/MurmurRPC.grpc.pb.h"
#define SERVICE_NAME "murmur-auth-pam"
bool valid_user(const MurmurRPC::Authenticator_Request &req, MurmurRPC::Authenticator_Response &resp);
int main(int argc, char *argv[]) {
if (argc <= 1) {
std::cerr << "usage: " << argv[0] << " <server IP and port>" << std::endl;
return 1;
}
auto channel = grpc::CreateChannel(argv[1], grpc::InsecureCredentials(), grpc::ChannelArguments());
grpc::ClientContext ctx;
auto client = MurmurRPC::V1::NewStub(channel);
BOOST_LOG_TRIVIAL(info) << "Starting murmur-auth-pam";
auto stream = client->AuthenticatorStream(&ctx);
MurmurRPC::Authenticator_Response init;
init.mutable_initialize()->mutable_server()->set_id(1);
if (!stream->Write(init)) {
BOOST_LOG_TRIVIAL(error) << "write error";
return 2;
}
while (true) {
MurmurRPC::Authenticator_Response resp;
MurmurRPC::Authenticator_Request req;
if (!stream->Read(&req)) {
BOOST_LOG_TRIVIAL(error) << "read error";
return 2;
}
if (req.has_authenticate()) {
if (!req.authenticate().has_name() || !req.authenticate().has_password()) {
resp.mutable_authenticate()->set_status(MurmurRPC::Authenticator_Response_Status_Failure);
} else {
BOOST_LOG_TRIVIAL(info) << "starting authenticating " << req.authenticate().name();
if (valid_user(req, resp)) {
resp.mutable_authenticate()->set_status(MurmurRPC::Authenticator_Response_Status_Success);
BOOST_LOG_TRIVIAL(info) << "successfully authenticated";
} else {
resp.mutable_authenticate()->set_status(MurmurRPC::Authenticator_Response_Status_Failure);
BOOST_LOG_TRIVIAL(info) << "failure authenticating";
}
}
} else if (req.has_find()) {
struct passwd *info = nullptr;
if (req.find().has_id()) {
BOOST_LOG_TRIVIAL(info) << "starting find (id) " << req.find().id();
info = getpwuid(req.find().id());
} else if (req.find().has_name()) {
BOOST_LOG_TRIVIAL(info) << "starting find (name) " << req.find().name();
info = getpwnam(req.find().name().c_str());
}
resp.mutable_find();
if (info) {
BOOST_LOG_TRIVIAL(info) << "successfully found";
auto user = resp.mutable_find()->mutable_user();
user->set_id(info->pw_uid);
user->set_name(info->pw_name);
} else {
BOOST_LOG_TRIVIAL(info) << "failure finding ";
}
}
if (!stream->Write(resp)) {
BOOST_LOG_TRIVIAL(error) << "write error";
return 2;
}
}
return 0;
}
int auth_callback(int num_msg, const struct pam_message *msg[], struct pam_response *resp[], void *appdata_ptr) {
if (num_msg <= 0) {
return PAM_CONV_ERR;
}
struct pam_response *r = (struct pam_response *)calloc(num_msg, sizeof(struct pam_response));
int i;
const MurmurRPC::Authenticator_Request *req = (MurmurRPC::Authenticator_Request *) appdata_ptr;
for (i = 0; i < num_msg; i++) {
switch (msg[i]->msg_style) {
case PAM_PROMPT_ECHO_OFF:
r[i].resp_retcode = 0;
r[i].resp = strdup(req->authenticate().password().c_str());
break;
case PAM_PROMPT_ECHO_ON:
r[i].resp_retcode = 0;
r[i].resp = strdup(req->authenticate().name().c_str());
break;
}
}
*resp = r;
return PAM_SUCCESS;
}
bool valid_user(const MurmurRPC::Authenticator_Request &req, MurmurRPC::Authenticator_Response &resp) {
struct pam_conv pam_conversation;
pam_conversation.conv = auth_callback;
pam_conversation.appdata_ptr = (void *) &req;
pam_handle_t *pamh;
int ret = pam_start(SERVICE_NAME, NULL, &pam_conversation, &pamh);
if (ret != PAM_SUCCESS) {
return false;
}
ret = pam_authenticate(pamh, 0);
if (ret != PAM_SUCCESS) {
pam_end(pamh, ret);
return false;
}
ret = pam_acct_mgmt(pamh, PAM_SILENT);
if (ret != PAM_SUCCESS) {
pam_end(pamh, ret);
return false;
}
const void *item = 0;
ret = pam_get_item(pamh, PAM_USER, &item);
if (ret != PAM_SUCCESS) {
pam_end(pamh, ret);
return false;
}
char *username = (char *) item;
struct passwd *info = getpwnam(username);
if (info == NULL) {
pam_end(pamh, ret);
return false;
}
resp.mutable_authenticate()->set_id(info->pw_uid);
resp.mutable_authenticate()->set_name(username);
pam_end(pamh, ret);
return true;
}