Debian Codegraph @

pam/pam.c

raw
1 /*
2 * pam.c - Functions to let us call into libpam from Go.
3 *
4 * Copyright 2017 Google Inc.
5 * Author: Joe Richey ([email protected])
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8 * use this file except in compliance with the License. You may obtain a copy of
9 * the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations under
17 * the License.
18 */
19
20 #include "pam.h"
21
22 #include <security/pam_appl.h>
23 #include <security/pam_ext.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/mman.h> // mlock/munlock
28
29 #include "_cgo_export.h" // for input callbacks
30
31 static int conversation(int num_msg, const struct pam_message** msg,
32 struct pam_response** resp, void* appdata_ptr) {
33 if (num_msg <= 0 || num_msg > PAM_MAX_NUM_MSG) {
34 return PAM_CONV_ERR;
35 }
36
37 // Allocate the response table with num_msg entries.
38 *resp = calloc(num_msg, sizeof **resp);
39 if (!*resp) {
40 return PAM_BUF_ERR;
41 }
42
43 // Check each message to see if we need to run a callback.
44 char* callback_msg = NULL;
45 char* callback_resp = NULL;
46 int i;
47 for (i = 0; i < num_msg; ++i) {
48 callback_msg = (char*)msg[i]->msg;
49
50 // We run our input callback if the style tells us we need data. Otherwise,
51 // we just print the error messages or text info to standard output.
52 switch (msg[i]->msg_style) {
53 case PAM_PROMPT_ECHO_OFF:
54 callback_resp = passphraseInput(callback_msg);
55 break;
56 case PAM_PROMPT_ECHO_ON:
57 callback_resp = userInput(callback_msg);
58 break;
59 case PAM_ERROR_MSG:
60 case PAM_TEXT_INFO:
61 fprintf(stderr, "%s\n", callback_msg);
62 continue;
63 }
64
65 if (!callback_resp) {
66 // If the callback failed, free each nonempty response in the response
67 // table and the response table itself.
68 while (--i >= 0) {
69 free((*resp)[i].resp);
70 }
71 free(*resp);
72 *resp = NULL;
73 return PAM_CONV_ERR;
74 }
75
76 (*resp)[i].resp = callback_resp;
77 }
78
79 return PAM_SUCCESS;
80 }
81
82 static const struct pam_conv conv = {conversation, NULL};
83 const struct pam_conv* goConv = &conv;
84
85 void freeData(pam_handle_t* pamh, void* data, int error_status) { free(data); }
86
87 void freeArray(pam_handle_t* pamh, void** array, int error_status) {
88 int i;
89 for (i = 0; array[i]; ++i) {
90 free(array[i]);
91 }
92 free(array);
93 }
94
95 void* copyIntoSecret(void* data) {
96 size_t size = strlen(data) + 1; // include null terminator
97 void* copy = calloc(1, size); // initialize to avoid a compiler warning
98 mlock(copy, size);
99 memcpy(copy, data, size);
100 return copy;
101 }
102
103 void freeSecret(pam_handle_t* pamh, char* data, int error_status) {
104 size_t size = strlen(data) + 1; // Include null terminator
105 // Use volatile function pointer to actually clear the memory.
106 static void* (*const volatile memset_sec)(void*, int, size_t) = &memset;
107 memset_sec(data, 0, size);
108 munlock(data, size);
109 free(data);
110 }
111
112 void infoMessage(pam_handle_t* pamh, const char* message) {
113 pam_info(pamh, "%s", message);
114 }