1 
2 /*
3 ** Copyright 1998 - 2005 Double Precision, Inc.  See COPYING for
4 ** distribution information.
5 */
6 
7 #include	"courier_auth_config.h"
8 #include	"auth.h"
9 #include	"random128/random128.h"
10 #include	"courierauthsasl.h"
11 #include	<stdlib.h>
12 #include	<string.h>
13 #if	HAVE_UNISTD_H
14 #include	<unistd.h>
15 #endif
16 #include	<ctype.h>
17 #include	<stdio.h>
18 #include	<errno.h>
19 
20 extern char *strdupdefdomain(const char *userid, const char *s1,
21 			     const char *s2, const char *s3);
22 
authsasl_login(const char * method,const char * initresponse,char * (* getresp)(const char *,void *),void * callback_arg,char ** authtype,char ** authdata)23 int authsasl_login(const char *method, const char *initresponse,
24 		   char *(*getresp)(const char *, void *),
25 		   void *callback_arg,
26 		   char **authtype,
27 		   char **authdata)
28 {
29 char	*uid;
30 char	*pw;
31 char	*p;
32 int	n;
33 
34 	if (initresponse)
35 	{
36 		uid=malloc(strlen(initresponse)+1);
37 		if (!uid)
38 		{
39 			perror("malloc");
40 			return (AUTHSASL_ERROR);
41 		}
42 		strcpy(uid, initresponse);
43 	}
44 	else
45 	{
46 		p=authsasl_tobase64("Username:", -1);
47 		if (!p)
48 		{
49 			perror("malloc");
50 			return (AUTHSASL_ERROR);
51 		}
52 		uid=getresp(p, callback_arg);
53 		free(p);
54 		if (!uid)
55 		{
56 			perror("malloc");
57 			return (AUTHSASL_ERROR);
58 		}
59 
60 		if (*uid == '*')
61 		{
62 			free(uid);
63 			return (AUTHSASL_ABORTED);
64 		}
65 	}
66 
67 	p=authsasl_tobase64("Password:", -1);
68 	if (!p)
69 	{
70 		free(uid);
71 		perror("malloc");
72 		return (AUTHSASL_ERROR);
73 	}
74 
75 	pw=getresp(p, callback_arg);
76 	free(p);
77 	if (!pw)
78 	{
79 		free(uid);
80 		perror("malloc");
81 		return (AUTHSASL_ERROR);
82 	}
83 
84 	if (*pw == '*')
85 	{
86 		free(pw);
87 		free(uid);
88 		return (AUTHSASL_ABORTED);
89 	}
90 
91 	if ((n=authsasl_frombase64(uid)) < 0 ||
92 		(uid[n]=0, n=authsasl_frombase64(pw)) < 0)
93 	{
94 		free(uid);
95 		free(pw);
96 		return (AUTHSASL_ABORTED);
97 	}
98 	pw[n]=0;
99 
100 	if ( (*authtype=malloc(sizeof(AUTHTYPE_LOGIN))) == 0)
101 	{
102 		free(uid);
103 		free(pw);
104 		perror("malloc");
105 		return (AUTHSASL_ERROR);
106 	}
107 
108 	strcpy( *authtype, AUTHTYPE_LOGIN);
109 
110 	if ( (*authdata=strdupdefdomain(uid,"\n",pw,"\n")) == 0)
111 	{
112 		free( *authtype );
113 		free(uid);
114 		free(pw);
115 		perror("malloc");
116 		return (AUTHSASL_ERROR);
117 	}
118 
119 	free(uid);
120 	free(pw);
121 
122 	return (AUTHSASL_OK);
123 }
124