File: mq2auth.c

package info (click to toggle)
zmailer 2.99.55-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,516 kB
  • ctags: 9,694
  • sloc: ansic: 120,953; sh: 3,862; makefile: 3,166; perl: 2,695; python: 115; awk: 22
file content (334 lines) | stat: -rw-r--r-- 7,361 bytes parent folder | download
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 *	ZMailer 2.99.53+ Scheduler "mailq2" routines
 *
 *	Copyright Matti Aarnio <mea@nic.funet.fi> 1999-2000
 *
 */

#include "scheduler.h"
#include "prototypes.h"
#include <ctype.h>
#include <unistd.h>
#include "zsyslog.h"
#include <stdlib.h>
#include <errno.h>

#include "ta.h"
#include "libz.h"
#include "md5.h"

#include <arpa/inet.h>

/*
 *  MAILQv2 autentication database info content:
 *
 *  - username (search key)
 *  - cleartext password (for AUTH hash to work)
 *  - controlling attributes
 *  - IP ACL per tcp-wrapper (how?)
 *
 *
 *  Field separator classical double-colon (':'), meaning that
 *  the cleartext password shall *not* contain that character.
 */

struct mq2pw {
	char *user;
	char *plain;
	char *attrs;
	int auth;
};

struct mq2keys {
  long value;
  char *name;
};
static struct mq2keys keys[] =
{
  { 0,			"NONE"	},
  { MQ2MODE_SNMP,	"SNMP"	},
  { MQ2MODE_QQ,		"QQ"	},
  { MQ2MODE_FULL,	"TT"	},
  { MQ2MODE_ETRN,	"ETRN"	},
  { MQ2MODE_KILL,	"KILL"	},
  /* other modes ? */
  { 0x7fffffff,		"ALL"	},
  { 0, NULL },
};


static long mq2authtokens(s)
     char *s;
{
  char *p = s;
  long rc = 0;
  struct mq2keys *m;

  while (p && *p) {
    s = p;
    while (*p && *p != ' ') ++p;
    if (*p) *p = 0; else p = NULL;
    for (m = keys;m->name;++m) {
      if (strcmp(m->name,s)==0) {
	rc |= m->value;
	break;
      }
    }
    if (p) *p++ = ' ';
  }
  return rc;
}

static int parseaddrlit __((const char **, Usockaddr *));
static int
parseaddrlit(hostp, au)
	const char **hostp;
	Usockaddr *au;
{
	int rc = 0, err;
	const char *host = *hostp;
	char *hh = (void *) host;

	memset(au, 0, sizeof(*au));

	hh = strchr(hh, ']');
	if (hh) *hh = 0;

#if defined(AF_INET6) && defined(INET6)
	if (CISTREQN(host,"[IPv6:",6) ||
	    CISTREQN(host,"[IPv6.",6)) {
	  au->v6.sin6_family = AF_INET6;
	  err = inet_pton(AF_INET6, host+6, &au->v6.sin6_addr);
	  if (err > 0) rc = 128;
	} else
#endif
	  if (*host == '[') {
	    au->v4.sin_family = AF_INET;
	    err = inet_pton(AF_INET, host+1, &au->v4.sin_addr);
	    if (err > 0) rc = 32;
	  } else
	    err = -1;

	if (hh) *hh = ']';

	while (*host && *host != ']') ++host;
	if (*host == ']') ++host;

	if (*host == '/') {
	  ++host;
	  rc = -1;
	  while ('0' <= *host && *host <= '9') {
	    if (rc < 0) rc = 0;
	    rc = rc * 10 + (*host) - '0';
	    ++host;
	  }
	}

	*hostp = host;

	if (err < 0) rc = -1;
	return rc;
}

static void mask_ip_bits __((void *, int, int));
static void mask_ip_bits(ipnump, width, maxwidth)
     void *ipnump;
     int width, maxwidth;
{
    unsigned char *ipnum = ipnump;
    int i, bytewidth, bytemaxwidth;

    bytemaxwidth = maxwidth >> 3;	/* multiple of 8 */
    bytewidth = (width + 7) >> 3;

    /* All full zero bytes... */
    for (i = bytewidth; i < bytemaxwidth; ++i)
	ipnum[i] = 0;

    /* Now the remaining byte */
    i = 8 - (width & 7);	/* Modulo 8 */

    bytewidth = width >> 3;
    if (i != 8) {
	/* Not exactly multiple-of-byte-width operand to be masked    */
	/* For 'width=31' we get now 'bytewidth=3', and 'i=1'         */
	/* For 'width=25' we get now 'bytewidth=3', and 'i=7'         */
	ipnum[bytewidth] &= (0xFF << i);
    }
}


static int mq2amaskcompare(mq, width, ua)
     struct mailq *mq;
     int width;
     Usockaddr *ua;
{
  Usockaddr qa = mq->qaddr;
  unsigned char ipbuf1[16], ipbuf2[16];

#ifdef INET6
  if (qa.v6.sin6_family == AF_INET6) {
    const u_char zv4mapprefix[16] = 
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0};

    if (memcmp((void *)&qa.v6.sin6_addr, zv4mapprefix, 12) == 0) {
      /* Is a IPv4 address mapped via IPv6 prefix. */
      qa.v4.sin_family = AF_INET;
      memcpy((void*)&qa.v4.sin_addr, 12+((char*)&qa.v6.sin6_addr), 4);
    }
  }
  if (qa.v6.sin6_family == AF_INET6) {
    if (ua->v6.sin6_family != AF_INET6) return 0; /* No match! */
    memcpy(ipbuf1, &qa.v6.sin6_addr, 16);
    memcpy(ipbuf2, &ua->v6.sin6_addr, 16);
    mask_ip_bits(ipbuf1, width, 128);
    mask_ip_bits(ipbuf2, width, 128);
    if (memcmp(ipbuf1, ipbuf2, 16) == 0)
      return 1; /* Match! */
    return 0; /* No match */

  } else
#endif
    if (qa.v4.sin_family == AF_INET) {
      if (ua->v4.sin_family != AF_INET) return 0; /* No match! */
      memcpy(ipbuf1, &qa.v4.sin_addr,  4);
      memcpy(ipbuf2, &ua->v4.sin_addr, 4);
      mask_ip_bits(ipbuf1, width, 32);
      mask_ip_bits(ipbuf2, width, 32);
      if (memcmp(ipbuf1, ipbuf2, 4) == 0)
	return 1; /* Match! */
      return 0; /* No match */

    } else {
      return 0; /* NO MATCH! */

    }
}

static int mq2amaskverify(mq, s)
     struct mailq *mq;
     const char *s;
{
  /* TO BE WRITTEN!
     Verify that  mq->qaddr  stored address is ok
     for this user/authenticator to use us.        */
  int rc;
  Usockaddr ua;
  int not = 0;

  while (*s == ' ') ++s;
  if (*s == 0) return 0; /* Empty -> Any OK */

  for ( ; *s; ++s) {
    if (*s == ',') { not = 0; continue; }
    if (*s == ' ') { not = 0; continue; }
    if (*s == '!') { not = 1; continue; }
    if (*s == '[') {
      rc = parseaddrlit( &s, &ua );
      if (rc >= 0 && mq2amaskcompare(mq, rc, &ua))
	return not;
    }
  }

  return -1;  /* Non-empty -> no match -> not ok */
}


static struct mq2pw * authuser(mq, user)
     struct mailq *mq;
     char *user;
{
  static char linebuf[2000];
  static struct mq2pw mpw;
  char *s;
  Sfio_t *fp;
  int ulen = strlen(user)+1;

  if (!mq2authfile) return NULL; /* D'uh! */

  fp = sfopen(NULL, mq2authfile, "r");
  if (!fp) return NULL; /* D'uh! */

  mpw.user = linebuf;
  while (csfgets(linebuf, sizeof(linebuf)-1, fp) >= 0) {
    if (*linebuf == '#' || *linebuf == '*' || *linebuf == '\n')
      continue;
    s = strchr(linebuf,'\n');
    if (s) *s = 0;
    s = strchr(linebuf,':');
    if (!s) continue; /* Bad syntax! */
    *s++ = '\000';
    if (memcmp(linebuf,user,ulen) == 0) {
      /* FOUND! */
      mpw.plain = s;
      s = strchr(s, ':');
      if (!s) continue; /* Bad syntax! */
      *s++ = '\000';
      mpw.attrs = s;

      s = strchr(s, ':');
      if (!s) continue; /* Bad syntax! */
      *s++ = '\000';
      if (mq2amaskverify(mq, s)) continue; /* BAD! */
      mpw.auth = mq2authtokens(mpw.attrs);
      sfclose(fp);
      return & mpw;
    }
  }

  sfclose(fp);
  return NULL; /* nothing found */
}



void mq2auth(mq,str)
     struct mailq *mq;
     char *str;
{
  char *p = str;
  struct mq2pw *pw;
  MD5_CTX CTX;
  unsigned char digest[16];
  char authbuf[32+1];
  int i;

  mq->auth = 0;

  while (*p && (*p != ' ') && (*p != '\t')) ++p;
  if (*p) *p++ = '\000';
  while (*p == ' ' || *p == '\t') ++p;

  /* Now 'str' points to username, and from 'p' onwards
     there is the HEX-encoded MD5 authenticator.. */

  pw = authuser(mq, str);

  if (!pw) {
    mq2_puts(mq,"-BAD USER OR AUTHENTICATOR OR CONTACT ADDRESS\n");
    return;
  }

  MD5Init(&CTX);
  MD5Update(&CTX, (const void *)(mq->challenge), strlen(mq->challenge));
  MD5Update(&CTX, (const void *)(pw->plain),     strlen(pw->plain));
  MD5Final(digest, &CTX);

  for (i = 0; i < 16; ++i)
    sprintf(authbuf+i+i, "%02x", digest[i]);

  if (strcmp(authbuf,p) != 0) {
    mq2_puts(mq,"-BAD USER OR PASSWORD");
#if 0 /* used to debug MD5 code... */
    mq2_puts(mq,"; real auth:");
    mq2_puts(mq,authbuf);
#endif
    mq2_puts(mq,"\n");
    return;
  }

  /* Right, authenticator is ok */
  mq->auth = pw->auth;

  mq2_puts(mq,"+OK\n");
}