Import Debian package 0.61.0-1+lenny1
[hcoop/debian/courier-authlib.git] / authsasl.c
CommitLineData
0fde1ce3 1/* $Id: authsasl.c,v 1.5 2008/07/10 02:43:55 mrsam Exp $ */
d9898ee8 2
3/*
0fde1ce3 4** Copyright 1998 - 2008 Double Precision, Inc. See COPYING for
d9898ee8 5** distribution information.
6*/
7
8#include "courier_auth_config.h"
9#include "courierauthsasl.h"
0fde1ce3 10#include "authsaslclient.h"
d9898ee8 11#include <stdlib.h>
12#include <ctype.h>
13#include <string.h>
14#include <errno.h>
15
16/* Use the SASL_LIST macro to build authsasl_list */
17
0fde1ce3 18#define NO_SERVER_FUNC()
19
20#define SERVER_FUNC(b) int b(const char *, const char *, \
21 char *(*)(const char *, void *), \
22 void *, \
23 char **, \
24 char **);
25
26#define SASL(a,b,c) b
d9898ee8 27SASL_LIST
28
29#undef SASL
30
0fde1ce3 31#undef SERVER_FUNC
32#define SERVER_FUNC(n) n
33
34#undef NO_SERVER_FUNC
35#define NO_SERVER_FUNC() 0
36
d9898ee8 37#define SASL(a,b,c) {a, b},
38
39struct authsasl_info authsasl_list[] = {
40
41SASL_LIST
42
43 { 0, 0}};
44
45int auth_sasl(const char *method,
46 const char *initreply,
47 char *(*callback_func)(const char *, void *),
48 void *callback_arg,
49 char **authtype_ptr, /* Returned - AUTHTYPE */
50 char **authdata_ptr)
51{
52int i;
53char *p, *q;
54
55 if ((p=malloc(strlen(method)+1)) == 0)
56 return (0);
57 strcpy(p, method);
58 for (q=p; *q; q++)
59 *q=toupper((int)(unsigned char)*q);
60
61 for (i=0; authsasl_list[i].sasl_method; i++)
62 {
0fde1ce3 63 if (strcmp(p, authsasl_list[i].sasl_method) == 0 &&
64 authsasl_list[i].sasl_func)
d9898ee8 65 {
66 free(p);
67 return ( (*authsasl_list[i].sasl_func)
68 (method,
69 initreply, callback_func,
70 callback_arg,
71 authtype_ptr, authdata_ptr));
72 }
73 }
74 free(p);
75 errno=ENOENT;
76 return (AUTHSASL_ERROR);
77}
0fde1ce3 78
79int auth_sasl_ex(const char *method,
80 const char *initresponse,
81 const char *externalauth,
82 char *(*callback_func)(const char *, void *),
83 void *callback_arg,
84 char **authtype_ptr, /* Returned - AUTHTYPE */
85 char **authdata_ptr)
86{
87 char *uid;
88 int n;
89
90 if (strcmp(method, "EXTERNAL"))
91 return auth_sasl(method, initresponse, callback_func,
92 callback_arg,
93 authtype_ptr,
94 authdata_ptr);
95
96 if (initresponse && *initresponse)
97 return AUTHSASL_ERROR;
98
99 if (!externalauth || !*externalauth)
100 return AUTHSASL_ERROR;
101
102 if (!initresponse)
103 {
104 uid=callback_func("", callback_arg);
105
106 if (*uid == '*')
107 {
108 free(uid);
109 return (AUTHSASL_ABORTED);
110 }
111
112 n=authsasl_frombase64(uid);
113
114 if (n < 0)
115 {
116 free(uid);
117 return AUTHSASL_ABORTED;
118 }
119 uid[n]=0;
120
121 if (uid[0])
122 {
123 free(uid);
124 return AUTHSASL_ABORTED;
125 }
126 free(uid);
127 }
128
129 if ((*authtype_ptr=strdup("EXTERNAL")) == NULL)
130 return AUTHSASL_ABORTED;
131
132 if ((*authdata_ptr=strdup(externalauth)) == NULL)
133 {
134 free(authtype_ptr);
135 return AUTHSASL_ABORTED;
136 }
137
138 return 0;
139}