File: authpipe.c

package info (click to toggle)
courier-authlib 0.71.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 14,416 kB
  • sloc: ansic: 15,778; sh: 4,565; cpp: 4,205; makefile: 793; perl: 739
file content (188 lines) | stat: -rw-r--r-- 3,966 bytes parent folder | download | duplicates (5)
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
/*
** Copyright 1998 - 2005 Double Precision, Inc.  See COPYING for
** distribution information.
*/

#if HAVE_CONFIG_H
#include "courier_auth_config.h"
#endif
#include	<stdio.h>
#include	<stdlib.h>
#include	<ctype.h>
#include	<string.h>
#include	<errno.h>
#include	<unistd.h>
#include	"numlib/numlib.h"

#include	"courierauth.h"
#include	"courierauthstaticlist.h"
#include	"courierauthdebug.h"

#include	"authpipelib.h"
#include	"authpiperc.h"

extern int _authdaemondopasswd(int wrfd, int rdfd, char *buffer, int bufsiz);
extern int _authdaemondo(int wrfd, int rdfd, const char *authreq,
			 int (*func)(struct authinfo *, void *), void *arg);
extern int _auth_enumerate(int wrfd, int rdfd,
			   void(*cb_func)(const char *name,
					  uid_t uid,
					  gid_t gid,
					  const char *homedir,
					  const char *maildir,
					  const char *options,
					  void *void_arg),
			   void *void_arg);

static int disabled_flag;

/* modelled on auth_generic() in authdaemon.c */
int auth_pipe(const char *service, const char *authtype, char *authdata,
		int (*callback_func)(struct authinfo *, void *),
		void *callback_arg)
{
	char	tbuf[NUMBUFSIZE];
	size_t	l=strlen(service)+strlen(authtype)+strlen(authdata)+2;
	char	*n=libmail_str_size_t(l, tbuf);
	char	*buf=malloc(strlen(n)+l+20);
	int	rdfd, wrfd, rc;

	if (!buf)
		return 1;

	if (disabled_flag)
	{
		free(buf);
		return -1;
	}

	strcat(strcat(strcpy(buf, "AUTH "), n), "\n");
	strcat(strcat(buf, service), "\n");
	strcat(strcat(buf, authtype), "\n");
	strcat(buf, authdata);
	
	if (getPipe(&rdfd, &wrfd))
	{
		free(buf);
		return 1;
	}
	rc=_authdaemondo(wrfd, rdfd, buf, callback_func, callback_arg);
	free(buf);
	if (rc > 0) closePipe();
	return rc;
}


/* modelled on auth_getuserinfo() in preauthdaemon.c (but note first
 * two args are reversed) */
int auth_pipe_pre(const char *uid, const char *service,
		int (*callback)(struct authinfo *, void *),
		void *arg)
{
	char *buf;
	int rdfd, wrfd, rc;

	if (disabled_flag)
		return -1;

	buf=malloc(strlen(service)+strlen(uid)+20);
	if (!buf)
		return 1;
	
	strcat(strcat(strcat(strcat(strcpy(buf, "PRE . "), service), " "),
		uid), "\n");
	
	if (getPipe(&rdfd, &wrfd))
	{
		free(buf);
		return 1;
	}
	rc=_authdaemondo(wrfd, rdfd, buf, callback, arg);
	free(buf);
	if (rc > 0) closePipe();
	return (rc);
}


/* modelled on auth_passwd() in authmoduser2.c */
int auth_pipe_chgpwd(const char *service,
		const char *uid,
		const char *opwd,
		const char *npwd)
{
	char *buf;
	int rdfd, wrfd, rc;

	if (disabled_flag)
		return -1;

	buf=malloc(strlen(service)+strlen(uid)+strlen(opwd)+
			strlen(npwd)+20);
	if (!buf)
		return 1;

	sprintf(buf, "PASSWD %s\t%s\t%s\t%s\n",
		service, uid, opwd, npwd);

	if (getPipe(&rdfd, &wrfd))
	{
		free(buf);
		return 1;
	}
	rc = _authdaemondopasswd(wrfd, rdfd, buf, strlen(buf));
	free(buf);
	if (rc > 0) closePipe();
	return (rc);
}


void auth_pipe_idle()
{
	/* don't need to do anything when idle */
}


void auth_pipe_close()
{
	closePipe();
}

void auth_pipe_enumerate(void(*cb_func)(const char *name,
	                                      uid_t uid,
	                                      gid_t gid,
	                                const char *homedir,
	                                const char *maildir,
					const char *options,
	                                      void *void_arg),
	                         void *void_arg)
{
	int rdfd, wrfd, rc;

	if (disabled_flag)
		return;

	if (getPipe(&rdfd, &wrfd))
		return;
	rc = _auth_enumerate(wrfd, rdfd, cb_func, void_arg);
	if (rc > 0) closePipe();
}

static struct authstaticinfo authpipe_info={
	"authpipe",
	auth_pipe,
	auth_pipe_pre,
	auth_pipe_close,
	auth_pipe_chgpwd,
	auth_pipe_idle,
	auth_pipe_enumerate};

struct authstaticinfo *courier_authpipe_init()
{
	disabled_flag=access(PIPE_PROGRAM, X_OK);
	if (disabled_flag)
	{
		DPRINTF("authpipe: disabled: failed to stat pipe program %s: %s",
			PIPE_PROGRAM, strerror(errno));
	}
	return &authpipe_info;
}