~ubuntu-branches/ubuntu/utopic/libesmtp/utopic

« back to all changes in this revision

Viewing changes to ntlm/client-ntlm.c

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy T. Bouse
  • Date: 2002-03-06 08:37:48 UTC
  • Revision ID: james.westby@ubuntu.com-20020306083748-ihmt32mddsslvg5i
Tags: upstream-0.8.11
ImportĀ upstreamĀ versionĀ 0.8.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Authentication module for the Micr$oft NTLM mechanism.
 
3
 *
 
4
 *  This file is part of libESMTP, a library for submission of RFC 2822
 
5
 *  formatted electronic mail messages using the SMTP protocol described
 
6
 *  in RFC 2821.
 
7
 *
 
8
 *  Copyright (C) 2002  Brian Stafford  <brian@stafford.uklinux.net>
 
9
 *
 
10
 *  This library is free software; you can redistribute it and/or
 
11
 *  modify it under the terms of the GNU Lesser General Public
 
12
 *  License as published by the Free Software Foundation; either
 
13
 *  version 2.1 of the License, or (at your option) any later version.
 
14
 *
 
15
 *  This library is distributed in the hope that it will be useful,
 
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
18
 *  Lesser General Public License for more details.
 
19
 *
 
20
 *  You should have received a copy of the GNU Lesser General Public
 
21
 *  License along with this library; if not, write to the Free Software
 
22
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
23
 */
 
24
#define _XOPEN_SOURCE   500
 
25
#ifdef HAVE_CONFIG_H
 
26
#include <config.h>
 
27
#endif
 
28
 
 
29
#include <assert.h>
 
30
 
 
31
#include <string.h>
 
32
#include <stdlib.h>
 
33
#include <unistd.h>
 
34
 
 
35
#include <sys/types.h>
 
36
#include "ntlm.h"
 
37
#include "auth-client.h"
 
38
#include "auth-plugin.h"
 
39
 
 
40
#define NELT(x)         (sizeof x / sizeof x[0])
 
41
 
 
42
static int ntlm_init (void *pctx);
 
43
static void ntlm_destroy (void *ctx);
 
44
static const char *ntlm_response (void *ctx,
 
45
                                  const char *challenge, int *len,
 
46
                                  auth_interact_t interact, void *arg);
 
47
 
 
48
const struct auth_client_plugin sasl_client =
 
49
  {
 
50
  /* Plugin information */
 
51
    "NTLM",
 
52
    "NTLM Authentication Mechanism (Microsoft)",
 
53
  /* Plugin instance */
 
54
    ntlm_init,
 
55
    ntlm_destroy,
 
56
  /* Authentication */
 
57
    ntlm_response,
 
58
    0,
 
59
  /* Security Layer */
 
60
    0,
 
61
    NULL,
 
62
    NULL,
 
63
  };
 
64
 
 
65
static const struct auth_client_request client_request[] =
 
66
  {
 
67
    { "domain",         AUTH_CLEARTEXT | AUTH_REALM,    "Domain",       0, },
 
68
    { "user",           AUTH_CLEARTEXT | AUTH_USER,     "User Name",    0, },
 
69
    { "passphrase",     AUTH_PASS,                      "Pass Phrase",  0, },
 
70
  };
 
71
 
 
72
struct ntlm_context
 
73
  {
 
74
    int state;
 
75
    char *result[NELT (client_request)];
 
76
    char host[64];
 
77
    char buf[256];
 
78
  };
 
79
 
 
80
static int 
 
81
ntlm_init (void *pctx)
 
82
{
 
83
  struct ntlm_context *context;
 
84
 
 
85
  context = malloc (sizeof (struct ntlm_context));
 
86
  memset (context, 0, sizeof (struct ntlm_context));
 
87
 
 
88
  *(void **) pctx = context;
 
89
  return 1;
 
90
}
 
91
 
 
92
static void
 
93
ntlm_destroy (void *ctx)
 
94
{
 
95
  struct ntlm_context *context = ctx;
 
96
 
 
97
  memset (context, 0, sizeof (struct ntlm_context));
 
98
  free (context);
 
99
}
 
100
 
 
101
static const char *
 
102
ntlm_response (void *ctx, const char *challenge, int *len,
 
103
               auth_interact_t interact, void *arg)
 
104
{
 
105
  struct ntlm_context *context = ctx;
 
106
  unsigned char nonce[8];
 
107
  unsigned char lm_resp[24], nt_resp[24];
 
108
  char *domain = NULL;
 
109
  char *p;
 
110
 
 
111
  switch (context->state)
 
112
    {
 
113
    case 0:     /* build the authentication request */
 
114
      context->state = 1;
 
115
      if (!(*interact) (client_request, context->result, NELT (client_request),
 
116
                        arg))
 
117
        break;
 
118
      gethostname (context->host, sizeof context->host);
 
119
      if ((p = strchr (context->host, '.')) != NULL)
 
120
        *p = '\0';
 
121
      *len = ntlm_build_type_1 (context->buf, sizeof context->buf,
 
122
                                context->result[0], context->host);
 
123
      return context->buf;
 
124
 
 
125
    case 1:     /* compute a response based om the challenge */
 
126
      context->state = 2;
 
127
      if (!ntlm_parse_type_2 (challenge, *len, nonce, &domain))
 
128
        break;
 
129
      ntlm_responses (lm_resp, nt_resp, nonce, context->result[2]);
 
130
      *len = ntlm_build_type_3 (context->buf, sizeof context->buf,
 
131
                                lm_resp, nt_resp,
 
132
                                context->result[0], context->result[1],
 
133
                                context->host);
 
134
      if (domain != NULL)
 
135
        free (domain);
 
136
      return context->buf;
 
137
    }
 
138
  *len = 0;
 
139
  return NULL;
 
140
}
 
141