File: authtest.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 (213 lines) | stat: -rw-r--r-- 4,310 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
/*
** Copyright 1998 - 2004 Double Precision, Inc.  See COPYING for
** distribution information.
*/

#include	"auth.h"
#include	"authmod.h"
#include	"authstaticlist.h"
#include	"authsasl.h"
#include	"debug.h"
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<errno.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif

static const char rcsid[]="$Id: authtest.c,v 1.11 2004/04/20 01:34:44 mrsam Exp $";

static void usage()
{
int i;

	fprintf(stderr, "Usage: authtest [-s service] [ -m module ] userid [ password [ newpassword ] ]\n"
		"Modules available:");
	for (i=0; authstaticmodulelist[i]; i++)
		fprintf(stderr, " %s", authstaticmodulelist[i]->auth_name);
	fprintf(stderr, "\n");	        
	exit(1);
}

static int callback_pre(struct authinfo *a, void *dummy)
{
        authsuccess(a->homedir, a->sysuserid ? 0:a->sysusername,
		a->sysuserid, &a->sysgroupid,
                a->address, a->fullname);

	if (a->maildir)
	{
	char	*p=malloc(sizeof("MAILDIR=")+strlen(a->maildir));

		strcat(strcpy(p, "MAILDIR="), a->maildir);
		putenv(p);
	}
	else	putenv("MAILDIR=");

	if (a->options)
	{
		char	*p=malloc(sizeof("OPTIONS=")+strlen(a->options));

		strcat(strcpy(p, "OPTIONS="), a->options);
		putenv(p);
	}
	else	putenv("OPTIONS=");

	return (0);
}

int main(int argc, char **argv)
{
int	argn;
const char *service="login";
const char *module="";
int	i;
int	found=0;
char	*p;

	if (getuid())
	{
		printf("I must be run as root!\n");
		exit(0);
	}

	for (argn=1; argn<argc; argn++)
	{
	const char *argp;

		if (argv[argn][0] != '-')	break;
		if (argv[argn][1] == 0)
		{
			++argn;
			break;
		}

		argp=argv[argn]+2;

		switch (argv[argn][1])	{
		case 's':
			if (!*argp && argn+1 < argc)
				argp=argv[++argn];
			service=argp;
			break;
		case 'm':
			if (!*argp && argn+1 < argc)
				argp=argv[++argn];
			module=argp;
			break;
		default:
			usage();
		}
	}
	if (argc - argn <= 0)
		usage();

	auth_debug_login_level = 2;

	for (i=0; authstaticmodulelist[i]; i++)
	{
	char	*authdata;
	char	buf[1024];

		if (*module && strcmp(module,
				      authstaticmodulelist[i]->auth_name))
			continue;

		found = 1;
		fprintf(stderr, "Trying %s...\n",
			authstaticmodulelist[i]->auth_name);
		if (argc - argn > 2)
		{
			if (authstaticmodulelist[i]->auth_changepwd == 0)
				continue;

			if ((*authstaticmodulelist[i]->auth_changepwd)
			    (service, argv[argn], argv[argn+1],
			     argv[argn+2]) == 0)
			{
				printf("Password CHANGED!\n");
				exit (0);
			}
			break;
		}

		if (argc - argn > 1)
		{
			authdata=malloc(strlen(argv[argn])+strlen(argv[argn+1])+3);
			if (!authdata)
			{
				perror("malloc");
				exit(1);
			}
			sprintf(authdata, "%s\n%s\n",
				argv[argn], argv[argn+1]);

			p= (*authstaticmodulelist[i]->auth_func)(service,
				AUTHTYPE_LOGIN, authdata, 0, 0, 0);
			free(authdata);
			if (p == 0)
			{
				if (errno != EPERM)
				{
					fprintf(stderr,
						"Temporary authentication failure from "
						"module %s\n",
						authstaticmodulelist[i]->auth_name);
					break;
				}
				continue;
			}
		}
		else
		{
		int	rc;

			if ((rc=(*authstaticmodulelist[i]->auth_prefunc)
			     (argv[argn], service ? service:"",
				&callback_pre, 0)) != 0)
			{
				(*authstaticmodulelist[i]->auth_cleanupfunc)();
				if (rc < 0)
					continue;
				fprintf(stderr,
					"Temporary authentication failure from "
					"module %s\n",
					authstaticmodulelist[i]->auth_name);
				break;
			}
			(*authstaticmodulelist[i]->auth_cleanupfunc)();
		}

		printf("Authenticated: module %s\n",
		       authstaticmodulelist[i]->auth_name);
		if ( getcwd(buf, sizeof(buf)))
			printf("Home directory: %s\n", buf);
		else
			printf("Unable to determine home directory!!\n");

		printf("UID/GID: %lu/%lu\n",
			(unsigned long)getuid(), (unsigned long)getgid());

		p=getenv("MAILDIR");
		if (p && *p)
			printf("Maildir: %s\n", p);

		p=getenv("MAILDIRQUOTA");
		if (p && *p)
			printf("Maildir quota: %s\n", p);

		p=getenv("AUTHADDR");
		printf("AUTHADDR=%s\n", p && *p ? p:"<none>");
		p=getenv("AUTHFULLNAME");
		printf("AUTHFULLNAME=%s\n", p && *p ? p:"<none>");
		p=getenv("OPTIONS");
		printf("OPTIONS=%s\n", p && *p ? p:"<none>");
		exit(0);
	}
	if (!found)
		fprintf(stderr, "No matching module found!\n");
	fprintf(stderr, "Authentication FAILED!\n");
	exit(1);
	return (0);
}