File: authcram.c

package info (click to toggle)
courier 0.47-4sarge5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 32,680 kB
  • ctags: 12,265
  • sloc: ansic: 164,045; cpp: 23,863; sh: 19,569; perl: 7,225; makefile: 4,192; yacc: 316; sed: 16
file content (180 lines) | stat: -rw-r--r-- 3,589 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
/*
** Copyright 1998 - 2000 Double Precision, Inc.  See COPYING for
** distribution information.
*/

#if	HAVE_CONFIG_H
#include	"config.h"
#endif
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<errno.h>
#include	<pwd.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif

#include	"auth.h"
#include	"authsasl.h"
#include	"authmod.h"
#include	"authstaticlist.h"
#include	"debug.h"
#include	"userdb/userdb.h"
#include	"../libhmac/hmac.h"
#include	"cramlib.h"

static const char rcsid[]="$Id: authcram.c,v 1.17 2004/04/18 15:54:38 mrsam Exp $";

extern void auth_userdb_cleanup();

char *auth_cram(const char *service, const char *authtype, char *authdata,
	int issession,
	void (*callback_func)(struct authinfo *, void *), void *callback_arg)
{
char	*u;
char	*udbs;
char	*passwords;
char	*services;
struct	userdbs *udb;
char *challenge, *response;
char	*user;
struct hmac_hashinfo	*hmacptr;

	if (auth_get_cram(authtype, authdata, &hmacptr,
		&user, &challenge, &response))
		return (0);

	userdb_set_debug(auth_debug_login_level);
	userdb_init(USERDB ".dat");
        if ( (u=userdb(user)) == 0)
	{
		userdb_close();
		errno=EPERM;
		return (0);
	}

	if ( (udbs=userdbshadow(USERDB "shadow.dat", user)) == 0)
	{
		free(u);
		userdb_close();
		errno=EPERM;
		return (0);
	}

	if ((services=malloc(strlen(service)+strlen(hmacptr->hh_name)
		+sizeof("-hmac-pw"))) == 0)
	{
		free(udbs);
		free(u);
		userdb_close();
		errno=ENOSPC;
		return (0);
	}

	strcat(strcat(strcat(strcpy(services, service), "-hmac-"),
		hmacptr->hh_name), "pw");

	passwords=userdb_gets(udbs, services);
	if (passwords == 0)
	{
		strcat(strcat(strcpy(services, "hmac-"),
			hmacptr->hh_name), "pw");
		passwords=userdb_gets(udbs, services);
	}
	if (passwords == 0)
		dprintf("authcram: no %s-%s or %s value found",
			service, services, services);
	free(services);

	if (passwords == 0)
	{
		errno=EPERM;
		free(udbs);
		free(u);
		userdb_close();
		return (0);
	}

	if (auth_verify_cram(hmacptr, challenge, response, passwords))
	{
		free(passwords);
		errno=EPERM;  /* other modules can do cram too */
		free(udbs);
		free(u);
		userdb_close();
		return (0);
	}

	free(passwords);
	free(udbs);
        if ((udb=userdb_creates(u)) == 0)
        {
		free(u);
		userdb_close();
		errno=EACCES;
                return (0);
        }

	if (callback_func == 0)
		authsuccess(udb->udb_dir, 0, &udb->udb_uid, &udb->udb_gid,
			user, udb->udb_gecos);
	else
	{
	struct	authinfo	aa;

		memset(&aa, 0, sizeof(aa));

		/*aa.sysusername=user;*/
		aa.sysuserid= &udb->udb_uid;
		aa.sysgroupid= udb->udb_gid;
		aa.homedir=udb->udb_dir;
		aa.address=user;
		aa.maildir=udb->udb_mailbox;
		aa.options=udb->udb_options;
		(*callback_func)(&aa, callback_arg);
	}

        free(u);
	userdb_close();

        if (callback_func == 0)
		putenv("MAILDIR=");

        if (callback_func == 0 && udb->udb_mailbox && *udb->udb_mailbox)
        {
        char    *p=malloc(sizeof("MAILDIR=")+strlen(udb->udb_mailbox));
	static	char *prevp=0;

                if (!p)
		{
			userdb_frees(udb);
			perror("malloc");
			authexit(1);
		}
                strcat(strcpy(p, "MAILDIR="), udb->udb_mailbox);
                putenv(p);

		if (prevp)	free(prevp);
		prevp=p;
        }
	userdb_frees(udb);
	if  ((u=strdup(user)) == 0)
	{
		perror("malloc");
		authexit(1);
	}
	return (u);
}

extern int auth_cram_pre(const char *userid, const char *service,
        int (*callback)(struct authinfo *, void *),
		  void *arg);

struct authstaticinfo authcram_info={
	"authcram",
	auth_cram,
	auth_cram_pre,
	auth_userdb_cleanup,
	NULL,
	NULL};