OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "remoting/host/user_authenticator_pam.h" | |
6 | |
7 #include <security/pam_appl.h> | |
8 #include <stdlib.h> | |
awong
2011/02/15 16:36:06
http://google-styleguide.googlecode.com/svn/trunk/
Lambros
2011/02/15 18:15:51
Done.
| |
9 #include <string> | |
10 | |
11 namespace remoting { | |
12 | |
13 UserAuthenticatorPam::UserAuthenticatorPam() { | |
14 } | |
15 | |
16 UserAuthenticatorPam::~UserAuthenticatorPam() { | |
17 } | |
18 | |
19 bool UserAuthenticatorPam::Authenticate(const std::string& username, | |
20 const std::string& password) { | |
21 username_ = username; | |
22 password_ = password; | |
23 pam_conv conversation; | |
24 conversation.conv = ConvFunction; | |
25 conversation.appdata_ptr = static_cast<void*>(this); | |
awong
2011/02/15 16:36:06
Do you need a static_cast in order to upcast to vo
Lambros
2011/02/15 18:15:51
You don't need it. But since void* is a "strange"
| |
26 // TODO(lambroslambrou): Allow PAM service name to be configurable. | |
27 pam_handle_t* pam_h; | |
awong
2011/02/15 16:36:06
pam_h -> pam_handle.
Lambros
2011/02/15 18:15:51
Strange! That's the name I originally picked, but
| |
28 if (pam_start("chromoting", username_.c_str(), | |
awong
2011/02/15 16:36:06
Pull this out into a file constant. kPamServiceNa
Lambros
2011/02/15 18:15:51
Done.
| |
29 &conversation, &pam_h) != PAM_SUCCESS) { | |
30 return false; | |
31 } | |
32 | |
33 // TODO(lambroslambrou): Move to separate thread. | |
34 int pam_status = pam_authenticate(pam_h, 0); | |
35 pam_end(pam_h, pam_status); | |
36 return pam_status == PAM_SUCCESS; | |
37 } | |
38 | |
39 // static | |
40 int UserAuthenticatorPam::ConvFunction(int num_msg, | |
awong
2011/02/15 16:36:06
num_msg -> num_messages
Lambros
2011/02/15 18:15:51
I didn't pick those names. They come from a PAM c
awong
2011/02/15 19:45:21
Ah, I see. Yeah, in these APIs that interface wit
| |
41 const pam_message** msg, | |
awong
2011/02/15 16:36:06
msg -> message
| |
42 pam_response** resp, | |
awong
2011/02/15 16:36:06
resp -> response
| |
43 void* appdata_ptr) { | |
awong
2011/02/15 16:36:06
Usually we don't type-tag things with _ptr. Just
Lambros
2011/02/15 18:15:51
Specifically, "appdata_ptr" is also a field-name o
| |
44 if (num_msg <= 0) | |
45 return PAM_CONV_ERR; | |
46 UserAuthenticatorPam* user_auth = | |
47 static_cast<UserAuthenticatorPam*>(appdata_ptr); | |
48 // Must allocate with malloc(), as the calling PAM module will | |
49 // release the memory with free(). | |
50 pam_response* resp_tmp = static_cast<pam_response*>( | |
51 malloc(num_msg * sizeof(pam_response))); | |
52 if (resp_tmp == NULL) | |
53 return PAM_CONV_ERR; | |
awong
2011/02/15 16:36:06
Add a newline here? This block of code is a bit t
Lambros
2011/02/15 18:15:51
Done.
| |
54 bool raise_error = false; | |
55 // On exit from the loop, 'count' will hold the number of initialised items | |
awong
2011/02/15 16:36:06
initialised -> initialized.
(kidding...ignore th
Lambros
2011/02/15 18:15:51
Hehe, never noticed that one! I'm just giving the
| |
56 // that the cleanup code needs to look at, in case of error. | |
57 int count; | |
58 for (count = 0; count < num_msg; count++) { | |
59 // Alias for readability. | |
60 pam_response* resp_item = &resp_tmp[count]; | |
61 resp_item->resp_retcode = 0; | |
62 resp_item->resp = NULL; | |
63 switch (msg[count]->msg_style) { | |
64 case PAM_PROMPT_ECHO_ON: | |
65 resp_item->resp = strdup(user_auth->username_.c_str()); | |
66 if (resp_item->resp == NULL) | |
67 raise_error = true; | |
68 break; | |
69 case PAM_PROMPT_ECHO_OFF: | |
70 resp_item->resp = strdup(user_auth->password_.c_str()); | |
71 if (resp_item->resp == NULL) | |
72 raise_error = true; | |
73 break; | |
74 case PAM_TEXT_INFO: | |
75 // No response needed, as this instructs the PAM client to display | |
76 // text to the user. Leave as NULL and continue with next prompt. | |
77 break; | |
78 default: | |
79 // Unexpected style code, so abort. | |
80 raise_error = true; | |
81 } | |
82 if (raise_error) | |
83 break; | |
84 } | |
85 | |
86 if (raise_error) { | |
87 // Not passing the response back, so free up any memory used. | |
88 for (int n = 0; n < count; n++) { | |
89 if (resp_tmp[n].resp) { | |
90 free(resp_tmp[n].resp); | |
awong
2011/02/15 16:36:06
Let's NULL out the pointer as well just to be OCD
Lambros
2011/02/15 18:15:51
Is there any point, when we free() the whole resp_
awong
2011/02/15 19:45:21
No, not really. You're right.
| |
91 } | |
92 } | |
93 free(resp_tmp); | |
94 return PAM_CONV_ERR; | |
95 } else { | |
96 *resp = resp_tmp; | |
97 return PAM_SUCCESS; | |
98 } | |
99 } | |
100 | |
101 } // namespace remoting | |
OLD | NEW |