| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of 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, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "proton/sasl.h" |
| 23 |
#include "proton/sasl_plugin.h" |
| 24 |
|
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
// SASL implementation interface |
| 29 |
static void default_sasl_prepare(pn_transport_t *transport); |
| 30 |
static void default_sasl_impl_free(pn_transport_t *transport); |
| 31 |
static const char *default_sasl_impl_list_mechs(pn_transport_t *transport); |
| 32 |
|
| 33 |
static bool default_sasl_init_server(pn_transport_t *transport); |
| 34 |
static void default_sasl_process_init(pn_transport_t *transport, const char *mechanism, const pn_bytes_t *recv); |
| 35 |
static void default_sasl_process_response(pn_transport_t *transport, const pn_bytes_t *recv); |
| 36 |
|
| 37 |
static bool default_sasl_init_client(pn_transport_t *transport); |
| 38 |
static bool default_sasl_process_mechanisms(pn_transport_t *transport, const char *mechs); |
| 39 |
static void default_sasl_process_challenge(pn_transport_t *transport, const pn_bytes_t *recv); |
| 40 |
static void default_sasl_process_outcome(pn_transport_t *transport, const pn_bytes_t *recv); |
| 41 |
|
| 42 |
static bool default_sasl_impl_can_encrypt(pn_transport_t *transport); |
| 43 |
static ssize_t default_sasl_impl_max_encrypt_size(pn_transport_t *transport); |
| 44 |
static ssize_t default_sasl_impl_encode(pn_transport_t *transport, pn_bytes_t in, pn_bytes_t *out); |
| 45 |
static ssize_t default_sasl_impl_decode(pn_transport_t *transport, pn_bytes_t in, pn_bytes_t *out); |
| 46 |
|
| 47 |
extern const pnx_sasl_implementation default_sasl_impl; |
| 48 |
const pnx_sasl_implementation default_sasl_impl = { |
| 49 |
default_sasl_impl_free, |
| 50 |
default_sasl_impl_list_mechs, |
| 51 |
|
| 52 |
default_sasl_init_server, |
| 53 |
default_sasl_init_client, |
| 54 |
|
| 55 |
default_sasl_prepare, |
| 56 |
|
| 57 |
default_sasl_process_init, |
| 58 |
default_sasl_process_response, |
| 59 |
|
| 60 |
default_sasl_process_mechanisms, |
| 61 |
default_sasl_process_challenge, |
| 62 |
default_sasl_process_outcome, |
| 63 |
|
| 64 |
default_sasl_impl_can_encrypt, |
| 65 |
default_sasl_impl_max_encrypt_size, |
| 66 |
default_sasl_impl_encode, |
| 67 |
default_sasl_impl_decode |
| 68 |
}; |
| 69 |
|
| 70 |
static const char ANONYMOUS[] = "ANONYMOUS"; |
| 71 |
static const char EXTERNAL[] = "EXTERNAL"; |
| 72 |
static const char PLAIN[] = "PLAIN"; |
| 73 |
|
| 74 |
void default_sasl_prepare(pn_transport_t* transport) |
| 75 |
{ |
| 76 |
} |
| 77 |
|
| 78 |
bool default_sasl_init_server(pn_transport_t* transport) |
| 79 |
{ |
| 80 |
// Setup to send SASL mechanisms frame |
| 81 |
pnx_sasl_set_desired_state(transport, SASL_POSTED_MECHANISMS); |
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
bool default_sasl_init_client(pn_transport_t* transport) |
| 86 |
{ |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
void default_sasl_impl_free(pn_transport_t *transport) |
| 91 |
{ |
| 92 |
free(pnx_sasl_get_context(transport)); |
| 93 |
} |
| 94 |
|
| 95 |
// Client handles ANONYMOUS or PLAIN mechanisms if offered |
| 96 |
// Offered mechanisms have already been filtered against the "allowed" list |
| 97 |
bool default_sasl_process_mechanisms(pn_transport_t *transport, const char *mechs) |
| 98 |
{ |
| 99 |
const char *username = pnx_sasl_get_username(transport); |
| 100 |
const char *password = pnx_sasl_get_password(transport); |
| 101 |
const char *authzid = pnx_sasl_get_authorization(transport); |
| 102 |
|
| 103 |
// Check whether offered EXTERNAL, PLAIN or ANONYMOUS |
| 104 |
// Look for "EXTERNAL" in mechs |
| 105 |
const char *found = strstr(mechs, EXTERNAL); |
| 106 |
// Make sure that string is separated and terminated |
| 107 |
if (found && (found==mechs || found[-1]==' ') && (found[8]==0 || found[8]==' ')) { |
| 108 |
pnx_sasl_set_selected_mechanism(transport, EXTERNAL); |
| 109 |
if (authzid) { |
| 110 |
size_t size = strlen(authzid); |
| 111 |
char *iresp = (char *) malloc(size); |
| 112 |
if (!iresp) return false; |
| 113 |
|
| 114 |
pnx_sasl_set_context(transport, iresp); |
| 115 |
|
| 116 |
memmove(iresp, authzid, size); |
| 117 |
pnx_sasl_set_bytes_out(transport, pn_bytes(size, iresp)); |
| 118 |
} else { |
| 119 |
static const char empty[] = ""; |
| 120 |
pnx_sasl_set_bytes_out(transport, pn_bytes(0, empty)); |
| 121 |
} |
| 122 |
pnx_sasl_set_desired_state(transport, SASL_POSTED_INIT); |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
// Look for "PLAIN" in mechs |
| 127 |
found = strstr(mechs, PLAIN); |
| 128 |
// Make sure that string is separated and terminated, allowed |
| 129 |
// and we have a username and password and connection is encrypted or we allow insecure |
| 130 |
if (found && (found==mechs || found[-1]==' ') && (found[5]==0 || found[5]==' ') && |
| 131 |
(pnx_sasl_is_transport_encrypted(transport) || pnx_sasl_get_allow_insecure_mechanisms(transport)) && |
| 132 |
username && password) { |
| 133 |
pnx_sasl_set_selected_mechanism(transport, PLAIN); |
| 134 |
size_t zsize = authzid ? strlen(authzid) : 0; |
| 135 |
size_t usize = strlen(username); |
| 136 |
size_t psize = strlen(password); |
| 137 |
size_t size = zsize + usize + psize + 2; |
| 138 |
char *iresp = (char *) malloc(size); |
| 139 |
if (!iresp) return false; |
| 140 |
|
| 141 |
pnx_sasl_set_context(transport, iresp); |
| 142 |
|
| 143 |
if (authzid) memmove(&iresp[0], authzid, zsize); |
| 144 |
iresp[zsize] = 0; |
| 145 |
memmove(&iresp[zsize + 1], username, usize); |
| 146 |
iresp[zsize + usize + 1] = 0; |
| 147 |
memmove(&iresp[zsize + usize + 2], password, psize); |
| 148 |
pnx_sasl_set_bytes_out(transport, pn_bytes(size, iresp)); |
| 149 |
|
| 150 |
// Zero out password and dealloc |
| 151 |
pnx_sasl_clear_password(transport); |
| 152 |
|
| 153 |
pnx_sasl_set_desired_state(transport, SASL_POSTED_INIT); |
| 154 |
return true; |
| 155 |
} |
| 156 |
|
| 157 |
// Look for "ANONYMOUS" in mechs |
| 158 |
found = strstr(mechs, ANONYMOUS); |
| 159 |
// Make sure that string is separated and terminated and allowed |
| 160 |
if (found && (found==mechs || found[-1]==' ') && (found[9]==0 || found[9]==' ')) { |
| 161 |
pnx_sasl_set_selected_mechanism(transport, ANONYMOUS); |
| 162 |
if (username) { |
| 163 |
size_t size = strlen(username); |
| 164 |
char *iresp = (char *) malloc(size); |
| 165 |
if (!iresp) return false; |
| 166 |
|
| 167 |
pnx_sasl_set_context(transport, iresp); |
| 168 |
|
| 169 |
memmove(iresp, username, size); |
| 170 |
pnx_sasl_set_bytes_out(transport, pn_bytes(size, iresp)); |
| 171 |
} else { |
| 172 |
static const char anon[] = "anonymous"; |
| 173 |
pnx_sasl_set_bytes_out(transport, pn_bytes(sizeof anon-1, anon)); |
| 174 |
} |
| 175 |
pnx_sasl_set_desired_state(transport, SASL_POSTED_INIT); |
| 176 |
return true; |
| 177 |
} |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
// Server will offer only ANONYMOUS and EXTERNAL if appropriate |
| 182 |
const char *default_sasl_impl_list_mechs(pn_transport_t *transport) |
| 183 |
{ |
| 184 |
// If we have an external authid then we can offer EXTERNAL |
| 185 |
if (pnx_sasl_get_external_username(transport)) { |
| 186 |
return "EXTERNAL ANONYMOUS"; |
| 187 |
} else { |
| 188 |
return "ANONYMOUS"; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
// The selected mechanism has already been verified against the "allowed" list |
| 193 |
void default_sasl_process_init(pn_transport_t *transport, const char *mechanism, const pn_bytes_t *recv) |
| 194 |
{ |
| 195 |
// Check that mechanism is ANONYMOUS |
| 196 |
if (strcmp(mechanism, ANONYMOUS)==0) { |
| 197 |
pnx_sasl_set_succeeded(transport, "anonymous", "anonymous"); |
| 198 |
pnx_sasl_set_desired_state(transport, SASL_POSTED_OUTCOME); |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
// Or maybe EXTERNAL |
| 203 |
const char *ext_username = pnx_sasl_get_external_username(transport); |
| 204 |
if (strcmp(mechanism, EXTERNAL)==0 && ext_username) { |
| 205 |
|
| 206 |
char *authzid = NULL; |
| 207 |
|
| 208 |
if (recv->size) { |
| 209 |
authzid = (char *) malloc(recv->size+1); |
| 210 |
pnx_sasl_set_context(transport, authzid); |
| 211 |
|
| 212 |
// If we didn't get memory pretend no authzid |
| 213 |
if (authzid) { |
| 214 |
memcpy(&authzid[0], recv->start, recv->size); |
| 215 |
authzid[recv->size] = 0; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
pnx_sasl_set_succeeded(transport, ext_username, authzid ? authzid : ext_username); |
| 220 |
pnx_sasl_set_desired_state(transport, SASL_POSTED_OUTCOME); |
| 221 |
return; |
| 222 |
} |
| 223 |
|
| 224 |
// Otherwise authentication failed |
| 225 |
pnx_sasl_set_failed(transport); |
| 226 |
pnx_sasl_set_desired_state(transport, SASL_POSTED_OUTCOME); |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
/* The default implementation neither sends nor receives challenges or responses */ |
| 231 |
void default_sasl_process_challenge(pn_transport_t *transport, const pn_bytes_t *recv) |
| 232 |
{ |
| 233 |
} |
| 234 |
|
| 235 |
void default_sasl_process_response(pn_transport_t *transport, const pn_bytes_t *recv) |
| 236 |
{ |
| 237 |
} |
| 238 |
|
| 239 |
void default_sasl_process_outcome(pn_transport_t* transport, const pn_bytes_t *recv) |
| 240 |
{ |
| 241 |
} |
| 242 |
|
| 243 |
bool default_sasl_impl_can_encrypt(pn_transport_t *transport) |
| 244 |
{ |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
ssize_t default_sasl_impl_max_encrypt_size(pn_transport_t *transport) |
| 249 |
{ |
| 250 |
return 0; |
| 251 |
} |
| 252 |
|
| 253 |
ssize_t default_sasl_impl_encode(pn_transport_t *transport, pn_bytes_t in, pn_bytes_t *out) |
| 254 |
{ |
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
ssize_t default_sasl_impl_decode(pn_transport_t *transport, pn_bytes_t in, pn_bytes_t *out) |
| 259 |
{ |
| 260 |
return 0; |
| 261 |
} |