1 /* $Id: authsaslcram.c,v 1.6 2006/06/01 10:47:32 mrsam Exp $ */
4 ** Copyright 1998 - 2006 Double Precision, Inc. See COPYING for
5 ** distribution information.
8 #include "courier_auth_config.h"
9 #include "random128/random128.h"
10 #include "courierauthsasl.h"
20 extern char *strdupdefdomain(const char *userid
, const char *s1
,
21 const char *s2
, const char *s3
);
23 int authsasl_cram(const char *method
, const char *initresponse
,
24 char *(*getresp
)(const char *, void *),
29 const char *randtoken
;
30 char hostnamebuf
[256];
32 char *challenge_base64
;
38 if (initresponse
&& *initresponse
)
40 if (write(2, "authsasl_cram: invalid request.\n", 32) < 0)
41 ; /* ignore gcc warning */
42 return (AUTHSASL_ERROR
);
45 randtoken
=random128();
47 if (gethostname(hostnamebuf
, sizeof(hostnamebuf
)-1))
48 strcpy(hostnamebuf
, "cram");
50 challenge
=malloc(strlen(randtoken
)+strlen(hostnamebuf
)
55 return (AUTHSASL_ERROR
);
57 strcat(strcat(strcat(strcat(strcpy(challenge
, "<"),
58 randtoken
), "@"), hostnamebuf
), ">");
60 challenge_base64
=authsasl_tobase64(challenge
, -1);
62 if (!challenge_base64
)
65 return (AUTHSASL_ERROR
);
68 response
=getresp(challenge_base64
, callback_arg
);
71 free(challenge_base64
);
72 return (AUTHSASL_ERROR
);
77 free(challenge_base64
);
79 return (AUTHSASL_ABORTED
);
82 /* If DEFDOMAIN is set, pick apart the response and reassemble
83 * it, potentially with a default domain appended to the username */
84 q
=getenv("DEFDOMAIN");
88 if ( (plen
= authsasl_frombase64(response
)) > 0 &&
89 (response
[plen
]=0, (s
= strchr(response
, ' ')) != 0) &&
90 (*s
++ = 0, (t
= strdupdefdomain(response
, " ", s
, "")) != 0) )
92 r
= authsasl_tobase64(t
, -1);
96 if ((response
= r
) == 0)
98 free(challenge_base64
);
99 return (AUTHSASL_ERROR
);
103 chrsp
=malloc(strlen(challenge_base64
)+strlen(response
)+3);
106 free(challenge_base64
);
109 return (AUTHSASL_ERROR
);
112 strcat(strcat(strcat(strcpy(chrsp
, challenge_base64
), "\n"),
114 free(challenge_base64
);
117 if ( (*authtype
=malloc(strlen(method
)+1)) == 0)
121 return (AUTHSASL_ERROR
);
123 strcpy( *authtype
, method
);
126 for (chrsp
= *authtype
; *chrsp
; chrsp
++)
127 *chrsp
= tolower( (int)(unsigned char)*chrsp
);
129 return (AUTHSASL_OK
);