File: authsaslplain.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 (117 lines) | stat: -rw-r--r-- 1,812 bytes parent folder | download | duplicates (7)
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

/*
** Copyright 2000-2005 Double Precision, Inc.  See COPYING for
** distribution information.
*/

#include	"courier_auth_config.h"
#include	"auth.h"
#include	"random128/random128.h"
#include	"courierauthsasl.h"
#include	<stdlib.h>
#include	<string.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif
#include	<ctype.h>
#include	<stdio.h>
#include	<errno.h>

extern char *strdupdefdomain(const char *userid, const char *s1,
			     const char *s2, const char *s3);

int authsasl_plain(const char *method, const char *initresponse,
		   char *(*getresp)(const char *, void *),
		   void *callback_arg,
		   char **authtype,
		   char **authdata)
{
char	*uid;
char	*pw;
char	*p;
int	n;
int	i;

	if (initresponse)
	{
		p=malloc(strlen(initresponse)+1);
		if (!p)
		{
			perror("malloc");
			return (AUTHSASL_ERROR);
		}
		strcpy(p, initresponse);
	}
	else
	{
		p=authsasl_tobase64("", -1);
		if (!p)
		{
			perror("malloc");
			return (AUTHSASL_ERROR);
		}
		uid=getresp(p, callback_arg);
		free(p);
		p=uid;
		if (!p)
		{
			perror("malloc");
			return (AUTHSASL_ERROR);
		}

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

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

	uid=pw=0;

	for (i=0; i<n; i++)
	{
		if (p[i] == 0)
		{
			++i;
			for (uid=p+i; i<n; i++)
				if (p[i] == 0)
				{
					pw=p+i+1;
					break;
				}
		}
	}

	if (pw == 0)
	{
		free(p);
		return (AUTHSASL_ABORTED);	/* Bad message */
	}

	if ( (*authtype=malloc(sizeof(AUTHTYPE_LOGIN))) == 0)
	{
		free(p);
		perror("malloc");
		return (AUTHSASL_ERROR);
	}

	strcpy( *authtype, AUTHTYPE_LOGIN);

	if ( (*authdata=strdupdefdomain(uid, "\n", pw, "\n")) == 0)
	{
		free( *authtype );
		free(p);
		perror("malloc");
		return (AUTHSASL_ERROR);
	}

	free(p);
	return (AUTHSASL_OK);
}