|
Line 0
Link Here
|
|
|
1 |
/* $OpenBSD: auth-obc.c,v 0.5 2008/02/04 07:55:00 pgsery Exp $ */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2008 Paul Sery. All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 |
*/ |
| 25 |
|
| 26 |
/* This code was derived from Markus Friedl's auth-skey.c and |
| 27 |
Daniel B. Cid's Ossec/sendmail.c */ |
| 28 |
|
| 29 |
#include "includes.h" |
| 30 |
|
| 31 |
#include <sys/types.h> |
| 32 |
|
| 33 |
#include <string.h> |
| 34 |
#include <stdarg.h> |
| 35 |
|
| 36 |
#include "xmalloc.h" |
| 37 |
#include "packet.h" |
| 38 |
#include "log.h" |
| 39 |
#include "key.h" |
| 40 |
#include "hostfile.h" |
| 41 |
#include "auth.h" |
| 42 |
#include "buffer.h" |
| 43 |
#ifdef GSSAPI |
| 44 |
#include "ssh-gss.h" |
| 45 |
#endif |
| 46 |
#include "monitor_wrap.h" |
| 47 |
#include "servconf.h" |
| 48 |
#include "uidswap.h" |
| 49 |
|
| 50 |
#include <netinet/in.h> |
| 51 |
#include <netdb.h> |
| 52 |
#include <arpa/inet.h> |
| 53 |
#include <sys/socket.h> |
| 54 |
#include <sys/types.h> |
| 55 |
#include <string.h> |
| 56 |
#include <stdarg.h> |
| 57 |
#include <unistd.h> |
| 58 |
#include "includes.h" |
| 59 |
#include "log.h" |
| 60 |
|
| 61 |
/* import */ |
| 62 |
extern ServerOptions options; |
| 63 |
|
| 64 |
#define OS_SIZE_1024 1024 |
| 65 |
|
| 66 |
#define OS_SUCCESS 0 /* Success */ |
| 67 |
#define OS_INVALID -1 /* Invalid entry */ |
| 68 |
#define OS_NOTFOUND -2 /* Entry not found */ |
| 69 |
#define OS_FILERR -3 /* Error in the file */ |
| 70 |
#define OS_SIZELIM -4 /* Size limit problem */ |
| 71 |
#define OS_CFGERR -5 /* Configuration error */ |
| 72 |
#define OS_SOCKTERR -6 /* Socket error */ |
| 73 |
#define OS_MISVALUE -7 /* There are values missing */ |
| 74 |
#define OS_CONNERR -8 /* Connection failed */ |
| 75 |
#define OS_UNDEF -9 /* Uknown error */ |
| 76 |
#define OS_MEMERR -10 /* Memory Error */ |
| 77 |
#define OS_SOCKBUSY -11 /* Busy socket -- try again */ |
| 78 |
|
| 79 |
#define OS_ENDFILE -20 /* End of file */ |
| 80 |
#define OS_FINISH -21 /* Finished this task */ |
| 81 |
|
| 82 |
|
| 83 |
/* Return codes (from SMTP server) */ |
| 84 |
#define VALIDBANNER "220" |
| 85 |
#define VALIDMAIL "250" |
| 86 |
#define VALIDDATA "354" |
| 87 |
|
| 88 |
/* Default values use to connect */ |
| 89 |
#define SMTP_DEFAULT_PORT "25" |
| 90 |
#define HELOMSG "Helo openssh\r\n" |
| 91 |
#define MAILFROM "Mail From: <%s>\r\n" |
| 92 |
#define RCPTTO "Rcpt To: <%s>\r\n" |
| 93 |
#define DATAMSG "DATA\r\n" |
| 94 |
#define FROM "From: <%s>\r\n" |
| 95 |
#define TO "To: <%s>\r\n" |
| 96 |
#define CC "Cc: <%s>\r\n" |
| 97 |
#define SUBJECT "Subject: %s\r\n" |
| 98 |
#define ENDDATA "\r\n.\r\n" |
| 99 |
#define QUITMSG "QUIT\r\n" |
| 100 |
|
| 101 |
void |
| 102 |
display_ip(struct addrinfo *ip) |
| 103 |
{ |
| 104 |
struct sockaddr_in *sa = (struct sockaddr_in *) ip->ai_addr; |
| 105 |
struct in_addr *inadr = (struct in_addr *) &sa->sin_addr.s_addr; |
| 106 |
debug3("challenge smtp server ip: %-16s", inet_ntoa(*inadr)); |
| 107 |
if (ip->ai_canonname && *ip->ai_canonname) |
| 108 |
debug3("canonical name: %s", ip->ai_canonname); |
| 109 |
} |
| 110 |
|
| 111 |
char * |
| 112 |
obc_recv_tcp(int socket, int sizet) |
| 113 |
{ |
| 114 |
char *ret; |
| 115 |
int retsize=0; |
| 116 |
|
| 117 |
ret = (char *) calloc((sizet), sizeof(char)); |
| 118 |
if(ret == NULL) |
| 119 |
return(NULL); |
| 120 |
|
| 121 |
if((retsize = recv(socket, ret, sizet-1,0)) <= 0) |
| 122 |
return(NULL); |
| 123 |
|
| 124 |
return(ret); |
| 125 |
} |
| 126 |
|
| 127 |
void |
| 128 |
obc_send_tcp(int socket, char *type, char *msg, int size) |
| 129 |
{ |
| 130 |
int result; |
| 131 |
char snd_msg[128]; |
| 132 |
|
| 133 |
if (size) { |
| 134 |
memset(snd_msg,'\0',size); |
| 135 |
snprintf(snd_msg,size+1, type, msg); |
| 136 |
result = send(socket, snd_msg, strlen(snd_msg),0); |
| 137 |
} else { |
| 138 |
result = send(socket, type, strlen(type),0); |
| 139 |
} |
| 140 |
|
| 141 |
if (result == -1) |
| 142 |
error("obc send tcp: %d",OS_SOCKTERR); |
| 143 |
} |
| 144 |
|
| 145 |
int |
| 146 |
obc_connect_smtp(char *smtp_server, char *port) |
| 147 |
{ |
| 148 |
int err, sock, result; |
| 149 |
struct addrinfo hints, *ip_info, *ip; |
| 150 |
|
| 151 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 152 |
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ |
| 153 |
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ |
| 154 |
hints.ai_flags = 0; |
| 155 |
hints.ai_protocol = 0; /* Any protocol */ |
| 156 |
|
| 157 |
if ( (err = getaddrinfo(smtp_server, "25", &hints, &ip_info) != 0) ) { |
| 158 |
debug3("%s: getaddrinfo %s\n", __func__, gai_strerror(err)); |
| 159 |
return(-1); |
| 160 |
} |
| 161 |
|
| 162 |
for (ip = ip_info; ip != NULL; ip = ip->ai_next) { |
| 163 |
display_ip(ip); |
| 164 |
if ( (sock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0) { |
| 165 |
debug3("%s: can't open socket %d", __func__, sock); |
| 166 |
close(sock); |
| 167 |
continue; |
| 168 |
} |
| 169 |
if ( (result = connect(sock, ip->ai_addr, ip->ai_addrlen)) < 0) |
| 170 |
debug3("%s: %s", __func__, gai_strerror(result)); |
| 171 |
else |
| 172 |
break; |
| 173 |
} |
| 174 |
|
| 175 |
if (ip == NULL) { |
| 176 |
debug3("%s: Could not connect",__func__); |
| 177 |
return(-1); |
| 178 |
} |
| 179 |
|
| 180 |
return(sock); |
| 181 |
} |
| 182 |
|
| 183 |
void |
| 184 |
check_msg(int socket,char *msg,char *type, char *res) |
| 185 |
{ |
| 186 |
if((msg == NULL)||(!strcmp(res, msg))) |
| 187 |
{ |
| 188 |
if(msg) |
| 189 |
free(msg); |
| 190 |
close(socket); |
| 191 |
} |
| 192 |
free(msg); |
| 193 |
} |
| 194 |
|
| 195 |
int |
| 196 |
obc_send(char *obc, char *email_addr, char *smtp_server) |
| 197 |
{ |
| 198 |
int socket; |
| 199 |
char *msg; |
| 200 |
char final_to[512]; |
| 201 |
|
| 202 |
if(obc == NULL) |
| 203 |
debug3("%s: no message to send",__func__); |
| 204 |
|
| 205 |
/* Connecting to the smtp server */ |
| 206 |
socket = obc_connect_smtp(smtp_server, SMTP_DEFAULT_PORT); |
| 207 |
if(socket < 0) |
| 208 |
return(socket); |
| 209 |
|
| 210 |
msg = obc_recv_tcp(socket, OS_SIZE_1024); |
| 211 |
check_msg(socket,msg,"returned banner",VALIDBANNER); |
| 212 |
|
| 213 |
obc_send_tcp(socket,HELOMSG,NULL,0); |
| 214 |
msg = obc_recv_tcp(socket, OS_SIZE_1024); |
| 215 |
check_msg(socket,msg,HELOMSG,VALIDMAIL); |
| 216 |
|
| 217 |
obc_send_tcp(socket, MAILFROM, email_addr, 128); |
| 218 |
msg = obc_recv_tcp(socket, OS_SIZE_1024); |
| 219 |
check_msg(socket,msg,MAILFROM,VALIDMAIL); |
| 220 |
|
| 221 |
obc_send_tcp(socket,RCPTTO,email_addr,128); |
| 222 |
msg = obc_recv_tcp(socket, OS_SIZE_1024); |
| 223 |
check_msg(socket,msg,RCPTTO,VALIDMAIL); |
| 224 |
|
| 225 |
obc_send_tcp(socket,DATAMSG,NULL,0); |
| 226 |
msg = obc_recv_tcp(socket, OS_SIZE_1024); |
| 227 |
check_msg(socket,msg,DATAMSG,VALIDMAIL); |
| 228 |
|
| 229 |
/* Building "From" and "To" in the e-mail header */ |
| 230 |
final_to[0] = '\0'; |
| 231 |
obc_send_tcp(socket,final_to,NULL,0); |
| 232 |
obc_send_tcp(socket, TO,email_addr,128); |
| 233 |
obc_send_tcp(socket, FROM, email_addr, 128); |
| 234 |
obc_send_tcp(socket, SUBJECT,"Out-of-band challenge",128); |
| 235 |
obc_send_tcp(socket, obc, NULL, 0); |
| 236 |
|
| 237 |
/* Sending end of data \r\n.\r\n */ |
| 238 |
obc_send_tcp(socket,ENDDATA,NULL,0); |
| 239 |
msg = obc_recv_tcp(socket, OS_SIZE_1024); |
| 240 |
|
| 241 |
/* quitting and closing socket */ |
| 242 |
obc_send_tcp(socket,QUITMSG,NULL,0); |
| 243 |
msg = obc_recv_tcp(socket, OS_SIZE_1024); |
| 244 |
|
| 245 |
/* Returning 0 (success) */ |
| 246 |
close(socket); |
| 247 |
|
| 248 |
return(0); |
| 249 |
} |
| 250 |
|
| 251 |
int |
| 252 |
obc_gen(struct Authctxt *authctxt) |
| 253 |
{ |
| 254 |
int i,ran,obc_length=4; |
| 255 |
char *obc,cpool[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 256 |
size_t nchars = sizeof(cpool) - 1; |
| 257 |
|
| 258 |
/* generate out-of-bound challenge (obc) */ |
| 259 |
obc=malloc(obc_length+1); |
| 260 |
if (obc == NULL) |
| 261 |
exit(-1); |
| 262 |
|
| 263 |
for (i=0;i<obc_length;i++) { |
| 264 |
ran = arc4random(); |
| 265 |
obc[i] = cpool[ran%nchars]; |
| 266 |
} |
| 267 |
obc[obc_length] = '\0'; |
| 268 |
|
| 269 |
authctxt->obc = obc; |
| 270 |
|
| 271 |
return 1; |
| 272 |
} |
| 273 |
|
| 274 |
int |
| 275 |
obc_challenge(Authctxt *authctxt) |
| 276 |
{ |
| 277 |
int i, result=0; |
| 278 |
char *alias=NULL,*email_addr=NULL; |
| 279 |
|
| 280 |
for (i=0; options.challenge_users[i] != NULL; i++) { |
| 281 |
alias=strtok(options.challenge_users[i],":"); |
| 282 |
email_addr=strtok(NULL,":"); |
| 283 |
if (strcmp(authctxt->user,alias) == 0) |
| 284 |
obc_gen(authctxt); |
| 285 |
result = obc_send(authctxt->obc,email_addr, |
| 286 |
options.challengesmtpserver); |
| 287 |
} |
| 288 |
debug2("%s: challenge sent to %s at %s via %s", |
| 289 |
__func__, alias ,email_addr,options.challengesmtpserver); |
| 290 |
|
| 291 |
return(result); |
| 292 |
} |
| 293 |
|
| 294 |
int |
| 295 |
obc_haskey(char *pw_name) |
| 296 |
{ |
| 297 |
if (pw_name != NULL) |
| 298 |
return 0; |
| 299 |
else |
| 300 |
return 1; |
| 301 |
} |
| 302 |
|
| 303 |
int |
| 304 |
obc_passcheck(char *obc_local, char *obc_remote) |
| 305 |
{ |
| 306 |
if (strcmp(obc_local, obc_remote) == 0) |
| 307 |
return 1; |
| 308 |
|
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
static void * |
| 313 |
obc_init_ctx(Authctxt *authctxt) |
| 314 |
{ |
| 315 |
int result; |
| 316 |
result=obc_challenge(authctxt); |
| 317 |
return authctxt; |
| 318 |
} |
| 319 |
|
| 320 |
int |
| 321 |
obc_query(void *ctx, char **name, char **infotxt, |
| 322 |
u_int* numprompts, char ***prompts, u_int **echo_on) |
| 323 |
{ |
| 324 |
*name = xstrdup(""); |
| 325 |
*infotxt = xstrdup(""); |
| 326 |
*numprompts = 1; |
| 327 |
*prompts = xcalloc(*numprompts, sizeof(char *)); |
| 328 |
*echo_on = xcalloc(*numprompts, sizeof(u_int)); |
| 329 |
|
| 330 |
xasprintf(*prompts, "Enter out-of-band challenge:"); |
| 331 |
|
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
int |
| 336 |
obc_respond(void *ctx, u_int numresponses, char **responses) |
| 337 |
{ |
| 338 |
Authctxt *authctxt = ctx; |
| 339 |
|
| 340 |
if (authctxt->valid && |
| 341 |
numresponses == 1 && |
| 342 |
obc_haskey(authctxt->obc) == 0 && |
| 343 |
obc_passcheck(authctxt->obc, responses[0]) == 1) |
| 344 |
return 0; |
| 345 |
return -1; |
| 346 |
} |
| 347 |
|
| 348 |
static void |
| 349 |
obc_free_ctx(void *ctx) |
| 350 |
{ |
| 351 |
Authctxt *authctx = ctx; |
| 352 |
|
| 353 |
authctx->obc = NULL; |
| 354 |
} |
| 355 |
|
| 356 |
KbdintDevice obc_device = { |
| 357 |
"obc", |
| 358 |
obc_init_ctx, |
| 359 |
obc_query, |
| 360 |
obc_respond, |
| 361 |
obc_free_ctx |
| 362 |
}; |
| 363 |
|
| 364 |
KbdintDevice mm_obc_device = { |
| 365 |
"obc", |
| 366 |
obc_init_ctx, |
| 367 |
mm_obc_query, |
| 368 |
mm_obc_respond, |
| 369 |
obc_free_ctx |
| 370 |
}; |