File: authsasl.c

package info (click to toggle)
courier-authlib 0.72.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,824 kB
  • sloc: ansic: 15,828; sh: 5,047; cpp: 4,205; makefile: 853; perl: 761
file content (206 lines) | stat: -rw-r--r-- 3,414 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

/*
** Copyright 1998 - 2013 Double Precision, Inc.  See COPYING for
** distribution information.
*/

#include	"courier_auth_config.h"
#include	"courierauth.h"
#include	"courierauthsasl.h"
#include	"authsaslclient.h"
#include	<stdlib.h>
#include	<ctype.h>
#include	<string.h>
#include	<errno.h>

/* Use the SASL_LIST macro to build authsasl_list */

#define NO_SERVER_FUNC()

#define	SERVER_FUNC(b) int b(const char *, const char *,		\
				 char *(*)(const char *, void *),	\
				 void *,				\
				 char **,				\
				 char **);

#define SASL(a,b,c) b
SASL_LIST

#undef	SASL

#undef  SERVER_FUNC
#define SERVER_FUNC(n) n

#undef  NO_SERVER_FUNC
#define NO_SERVER_FUNC() 0

#define	SASL(a,b,c) {a, b},

struct authsasl_info authsasl_list[] = {

SASL_LIST

	{ 0, 0}};

int auth_sasl(const char *method,
	      const char *initreply,
	      char *(*callback_func)(const char *, void *),
	      void *callback_arg,
	      char **authtype_ptr,		/* Returned - AUTHTYPE */
	      char **authdata_ptr)
{
int	i;
char	*p, *q;

	if ((p=malloc(strlen(method)+1)) == 0)
		return (0);
	strcpy(p, method);
	for (q=p; *q; q++)
		*q=toupper((int)(unsigned char)*q);

	for (i=0; authsasl_list[i].sasl_method; i++)
	{
		if (strcmp(p, authsasl_list[i].sasl_method) == 0 &&
		    authsasl_list[i].sasl_func)
		{
			free(p);
			return ( (*authsasl_list[i].sasl_func)
				 (method,
				  initreply, callback_func,
				  callback_arg,
				  authtype_ptr, authdata_ptr));
		}
	}
	free(p);
	errno=ENOENT;
	return (AUTHSASL_ERROR);
}

int auth_sasl_ex(const char *method,
		 const char *initresponse,
		 const char *externalauth,
		 char *(*callback_func)(const char *, void *),
		 void *callback_arg,
		 char **authtype_ptr,		/* Returned - AUTHTYPE */
		 char **authdata_ptr)
{
	char	*uid;
	int n;

	if (strcmp(method, "EXTERNAL"))
		return auth_sasl(method, initresponse, callback_func,
				 callback_arg,
				 authtype_ptr,
				 authdata_ptr);

	if (!externalauth || !*externalauth)
		return AUTHSASL_ERROR;

	if (initresponse && !*initresponse)
		initresponse=NULL;

	if (initresponse)
	{
		uid=strdup(initresponse);

		if (!uid)
			return AUTHSASL_ERROR;

		n=authsasl_frombase64(uid);

		if (n < 0)
		{
			free(uid);
			return AUTHSASL_ABORTED;
		}
		uid[n]=0;

		if (strcmp(uid, externalauth))
		{
			free(uid);
			return AUTHSASL_ERROR;
		}
		free(uid);
	}
	else
	{
		uid=callback_func("", callback_arg);

		if (*uid == '*')
		{
			free(uid);
			return (AUTHSASL_ABORTED);
		}

		n=authsasl_frombase64(uid);

		if (n < 0)
		{
			free(uid);
			return AUTHSASL_ABORTED;
		}
		uid[n]=0;

		if (uid[0])
		{
			free(uid);
			return AUTHSASL_ABORTED;
		}
		free(uid);
	}

	if ((*authtype_ptr=strdup("EXTERNAL")) == NULL)
		return AUTHSASL_ABORTED;

	if ((*authdata_ptr=strdup(externalauth)) == NULL)
	{
		free(*authtype_ptr);
		return AUTHSASL_ABORTED;
	}

	return 0;
}

char *auth_sasl_extract_userid(const char *authtype,
			       const char *authdata)
{
	struct	cram_callback_info	cci;
	char *p;
	char *t;
	char *d;

	if (strcmp(authtype, AUTHTYPE_LOGIN) == 0)
	{
		char *q;

		p=strdup(authdata);

		if (!p)
			return NULL;

		q=strchr(p, '\n');
		if (q) *q=0;
		return p;
	}

	if ((t=strdup(authtype)) == NULL)
		return NULL;

	if ((d=strdup(authdata)) == NULL)
	{
		free(t);
		return NULL;
	}

	if (auth_get_cram_silent(t, d, &cci))
	{
		free(d);
		free(t);
		return (NULL);
	}

	p=strdup(cci.user);
	free(d);
	free(t);
	return p;
}