annotate src/c-client/auth_pla.c @ 0:ada5e610ab86

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1 /* ========================================================================
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2 * Copyright 1988-2006 University of Washington
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
3 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
4 * Licensed under the Apache License, Version 2.0 (the "License");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
5 * you may not use this file except in compliance with the License.
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
6 * You may obtain a copy of the License at
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
7 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
8 * http://www.apache.org/licenses/LICENSE-2.0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
9 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
10 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
11 * ========================================================================
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
12 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
13
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
14 /*
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
15 * Program: Plain authenticator
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
16 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
17 * Author: Mark Crispin
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
18 * Networks and Distributed Computing
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
19 * Computing & Communications
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
20 * University of Washington
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
21 * Administration Building, AG-44
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
22 * Seattle, WA 98195
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
23 * Internet: MRC@CAC.Washington.EDU
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
24 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
25 * Date: 22 September 1998
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
26 * Last Edited: 30 August 2006
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
27 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
28
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
29 long auth_plain_client (authchallenge_t challenger,authrespond_t responder,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
30 char *service,NETMBX *mb,void *stream,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
31 unsigned long *trial,char *user);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
32 char *auth_plain_server (authresponse_t responder,int argc,char *argv[]);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
33
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
34 AUTHENTICATOR auth_pla = {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
35 AU_AUTHUSER | AU_HIDE, /* allow authuser, hidden */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
36 "PLAIN", /* authenticator name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
37 NIL, /* always valid */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
38 auth_plain_client, /* client method */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
39 auth_plain_server, /* server method */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
40 NIL /* next authenticator */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
41 };
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
42
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
43 /* Client authenticator
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
44 * Accepts: challenger function
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
45 * responder function
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
46 * SASL service name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
47 * parsed network mailbox structure
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
48 * stream argument for functions
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
49 * pointer to current trial count
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
50 * returned user name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
51 * Returns: T if success, NIL otherwise, number of trials incremented if retry
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
52 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
53
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
54 long auth_plain_client (authchallenge_t challenger,authrespond_t responder,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
55 char *service,NETMBX *mb,void *stream,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
56 unsigned long *trial,char *user)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
57 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
58 char *u,pwd[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
59 void *challenge;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
60 unsigned long clen;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
61 long ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
62 /* snarl if not SSL/TLS session */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
63 if (!mb->sslflag && !mb->tlsflag)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
64 mm_log ("SECURITY PROBLEM: insecure server advertised AUTH=PLAIN",WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
65 /* get initial (empty) challenge */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
66 if (challenge = (*challenger) (stream,&clen)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
67 fs_give ((void **) &challenge);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
68 if (clen) { /* abort if challenge non-empty */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
69 mm_log ("Server bug: non-empty initial PLAIN challenge",WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
70 (*responder) (stream,NIL,0);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
71 ret = LONGT; /* will get a BAD response back */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
72 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
73 pwd[0] = NIL; /* prompt user if empty challenge */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
74 mm_login (mb,user,pwd,*trial);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
75 if (!pwd[0]) { /* empty challenge or user requested abort */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
76 (*responder) (stream,NIL,0);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
77 *trial = 0; /* cancel subsequent attempts */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
78 ret = LONGT; /* will get a BAD response back */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
79 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
80 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
81 unsigned long rlen =
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
82 strlen (mb->authuser) + strlen (user) + strlen (pwd) + 2;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
83 char *response = (char *) fs_get (rlen);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
84 char *t = response; /* copy authorization id */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
85 if (mb->authuser[0]) for (u = user; *u; *t++ = *u++);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
86 *t++ = '\0'; /* delimiting NUL */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
87 /* copy authentication id */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
88 for (u = mb->authuser[0] ? mb->authuser : user; *u; *t++ = *u++);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
89 *t++ = '\0'; /* delimiting NUL */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
90 /* copy password */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
91 for (u = pwd; *u; *t++ = *u++);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
92 /* send credentials */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
93 if ((*responder) (stream,response,rlen)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
94 if (challenge = (*challenger) (stream,&clen))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
95 fs_give ((void **) &challenge);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
96 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
97 ++*trial; /* can try again if necessary */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
98 ret = LONGT; /* check the authentication */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
99 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
100 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
101 memset (response,0,rlen); /* erase credentials */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
102 fs_give ((void **) &response);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
103 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
104 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
105 memset (pwd,0,MAILTMPLEN); /* erase password */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
106 if (!ret) *trial = 65535; /* don't retry if bad protocol */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
107 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
108 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
109
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
110 /* Server authenticator
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
111 * Accepts: responder function
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
112 * argument count
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
113 * argument vector
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
114 * Returns: authenticated user name or NIL
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
115 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
116
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
117 char *auth_plain_server (authresponse_t responder,int argc,char *argv[])
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
118 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
119 char *ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
120 char *user,*aid,*pass;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
121 unsigned long len;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
122 /* get user name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
123 if (aid = (*responder) ("",0,&len)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
124 /* note: responders null-terminate */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
125 if ((((unsigned long) ((user = aid + strlen (aid) + 1) - aid)) < len) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
126 (((unsigned long) ((pass = user + strlen (user) + 1) - aid)) < len) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
127 (((unsigned long) ((pass + strlen (pass)) - aid)) == len) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
128 (*aid ? server_login (aid,pass,user,argc,argv) :
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
129 server_login (user,pass,NIL,argc,argv))) ret = myusername ();
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
130 fs_give ((void **) &aid);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
131 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
132 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
133 }

yatex.org