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