main.c 8.15 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
3
4
5
6
7
8
/*
 * main.c: for rcpt500 (X.500 email query responder)
 *
 * 16 June 1992 by Mark C Smith
 * Copyright (c) 1992 The Regents of The University of Michigan
 * All Rights Reserved
 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
9
10
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
11
12
13
14
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

Kurt Zeilenga's avatar
Kurt Zeilenga committed
15
16
17
18
#include <ac/syslog.h>
#include <ac/string.h>


Kurt Zeilenga's avatar
Kurt Zeilenga committed
19
20
21
22
#include "ldapconfig.h"
#include "rcpt500.h"

int dosyslog = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
23
#ifdef LDAP_CONNECTIONLESS
Kurt Zeilenga's avatar
Kurt Zeilenga committed
24
int do_cldap = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
25
26
#endif /* LDAP_CONNECTIONLESS */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
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
int derefaliases = 1;
int sizelimit = RCPT500_SIZELIMIT;
int rdncount = RCPT500_RDNCOUNT;
int ldapport = 0;
char *ldaphost = LDAPHOST;
char *searchbase = RCPT500_BASE;
char *dapuser = RCPT500_BINDDN;
char *filterfile = FILTERFILE;
char *templatefile = TEMPLATEFILE;
char reply[ MAXSIZE * RCPT500_LISTLIMIT ];



/*
 * functions
 */
int	read_msg();
char	*read_hdr();
int 	send_reply();

/*
 * main is invoked by sendmail via the alias file
 * the entire incoming message gets piped to our standard input
 */
main( argc, argv )
    int		argc;
    char	**argv;
{
    char		*prog, *usage = "%s [-l] [-U] [-h ldaphost] [-p ldapport] [-b searchbase] [-a] [-z sizelimit] [-u dapuser] [-f filterfile] [-t templatefile] [-c rdncount]\n";
    struct msginfo	msg;
    int			c, errflg;
    char		*replytext;

    extern int		optind;
    extern char		*optarg;

    *reply = '\0';

    if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
	prog = strdup( argv[ 0 ] );
    } else {
	prog = strdup( prog + 1 );
    }

    errflg = 0;
    while (( c = getopt( argc, argv, "alUh:b:s:z:f:t:p:c:" )) != EOF ) {
	switch( c ) {
	case 'a':
	    derefaliases = 0;
	    break;
	case 'l':
	    dosyslog = 1;
	    break;
	case 'U':
Kurt Zeilenga's avatar
Kurt Zeilenga committed
81
#ifdef LDAP_CONNECTIONLESS
Kurt Zeilenga's avatar
Kurt Zeilenga committed
82
	    do_cldap = 1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
83
84
85
86
#else /* LDAP_CONNECTIONLESS */
	    fprintf( stderr,
			"Compile with -DLDAP_CONNECTIONLESS for -U support\n" );
#endif /* LDAP_CONNECTIONLESS */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
	    break;	
	case 'b':
	    searchbase = optarg;
	    break;
	case 'h':
	    ldaphost = optarg;
	    break;
	case 'p':
	    ldapport = atoi( optarg );
	    break;
	case 'z':
	    sizelimit = atoi( optarg );
	    break;
	case 'u':
	    dapuser = optarg;
	    break;
	case 'f':
	    filterfile = optarg;
	    break;
	case 't':
	    templatefile = optarg;
	    break;
	case 'c':
	    rdncount = atoi( optarg );
	    break;
	default:
	    ++errflg;
	}
    }
    if ( errflg || optind < argc ) {
	fprintf( stderr, usage, prog );
	exit( 1 );
    }

    if ( dosyslog ) {
	/*
	 * if syslogging requested, initialize
	 */
#ifdef LOG_DAEMON
	openlog( prog, OPENLOG_OPTIONS, LOG_DAEMON );
#else
	openlog( prog, OPENLOG_OPTIONS );
#endif
    }

    if ( read_msg( stdin, &msg ) < 0 ) {
	if ( dosyslog ) {
	    syslog( LOG_INFO, "unparseable message ignored" );
	}
	exit( 0 );	/* so as not to give sendmail an error */
    }

    if ( dosyslog ) {
	syslog( LOG_INFO, "processing command \"%s %s\" from %s",
		( msg.msg_command < 0 ) ? "Unknown" :
		cmds[ msg.msg_command ].cmd_text,
		( msg.msg_arg == NULL ) ? "" : msg.msg_arg, msg.msg_replyto );
    }

    if ( msg.msg_command < 0 ) {
	msg.msg_command = 0;	/* unknown command == help command */
    }

/*
    sprintf( reply, "Your request was interpreted as: %s %s\n\n",
	    cmds[ msg.msg_command ].cmd_text, msg.msg_arg );
*/

    (*cmds[ msg.msg_command ].cmd_handler)( &msg, reply );

    if ( send_reply( &msg, reply ) < 0 ) {
	if ( dosyslog ) {
	    syslog( LOG_INFO, "reply failed: %m" );
	}
	exit( 0 );	/* so as not to give sendmail an error */
    }

    if ( dosyslog ) {
	syslog( LOG_INFO, "reply OK" );
    }

    exit( 0 );
}


int
read_msg( fp, msgp )
    FILE		*fp;
    struct msginfo	*msgp;
{
    char	buf[ MAXSIZE ], *line;
    int		command = -1;

    msgp->msg_replyto = msgp->msg_date = msgp->msg_subject = NULL;

    line = NULL;
    while( 1 ) {
	if ( line == NULL ) {
	    if (( line = fgets( buf, MAXSIZE, fp )) == NULL ) {
		break;
	    }
	    buf[ strlen( buf ) - 1 ] = '\0';	/* remove trailing newline */
	}

	if ( *buf == '\0' ) {	/* start of message body */
	    break;
	}
	if ( strncasecmp( buf, "Reply-To:", 9 ) == 0 ) {
	    if ( msgp->msg_replyto != NULL ) {
		free( msgp->msg_replyto );
	    }
	     msgp->msg_replyto = read_hdr( fp, 9, buf, MAXSIZE, &line );
	} else if ( strncasecmp( buf, "From:", 5 ) == 0 &&
		    msgp->msg_replyto == NULL ) {
	     msgp->msg_replyto = read_hdr( fp, 5, buf, MAXSIZE, &line );
	} else if ( strncasecmp( buf, "Date:", 5 ) == 0 ) {
	     msgp->msg_date = read_hdr( fp, 5, buf, MAXSIZE, &line );
	} else if ( strncasecmp( buf, "Message-ID:", 5 ) == 0 ) {
	     msgp->msg_messageid = read_hdr( fp, 11, buf, MAXSIZE, &line );
	} else if ( strncasecmp( buf, "Subject:", 8 ) == 0 ) {
	     if (( msgp->msg_subject =
		    read_hdr( fp, 8, buf, MAXSIZE, &line )) != NULL ) {
		command = find_command( msgp->msg_subject, &msgp->msg_arg );
	    }
	} else {
	    line = NULL;	/* discard current line */
	}
    }

    while ( command < 0 && line != NULL ) {
	/*
	 * read the body of the message, looking for commands
	 */
	if (( line = fgets( buf, MAXSIZE, fp )) != NULL ) {
	    buf[ strlen( buf ) - 1 ] = '\0';	/* remove trailing newline */
	    command = find_command( buf, &msgp->msg_arg );
	}
    }

    if ( msgp->msg_replyto == NULL ) {
	return( -1 );
    }

    msgp->msg_command = command;
    return( 0 );
}


char *
read_hdr( fp, offset, buf, MAXSIZEe, linep )
    FILE	*fp;
    int		offset;
    char	*buf;
    int		MAXSIZEe;
    char	**linep;
{
    char	*hdr;

    for ( hdr = buf + offset; isspace( *hdr ); ++hdr ) {
	;
    }
    if (( hdr = strdup( hdr )) == NULL ) {
	if ( dosyslog ) {
	    syslog( LOG_ERR, "strdup: %m" );
	}
	exit( 1 );
    }

    while ( 1 ) {
	*linep = fgets( buf, MAXSIZE, fp );
	buf[ strlen( buf ) - 1 ] = '\0';	/* remove trailing newline */
	if ( *linep == NULL || !isspace( **linep )) {
	    break;
	}
	if (( hdr = realloc( hdr, strlen( hdr ) +
		    strlen( *linep ) + 3 )) == NULL) {
	    if ( dosyslog ) {
		syslog( LOG_ERR, "realloc: %m" );
	    }
	    exit( 1 );
	}
	strcat( hdr, "\n" );
	strcat( hdr, *linep );
    }

    return( hdr );
}


int
send_reply( msgp, body )
    struct msginfo	*msgp;
    char		*body;
{
    char	buf[ MAXSIZE ];
    FILE	*cmdpipe;
    int		rc;
    
    if (( cmdpipe = popen( RCPT500_PIPEMAILCMD, "w" )) == NULL ) {
	if ( dosyslog ) {
	    syslog( LOG_ERR, "popen pipemailcmd failed: %m" );
	}
	return( -1 );
    }

    /*
     * send the headers
     */

    sprintf( buf, "From: %s\n", RCPT500_FROM );
    rc = fwrite( buf, strlen( buf ), 1, cmdpipe );

    if ( rc == 1 ) {
	if ( msgp->msg_subject != NULL ) {
	    sprintf( buf, "Subject: Re: %s\n", msgp->msg_subject );
	} else {
	    sprintf( buf, "Subject: query response\n" );
	}
	rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
    }

    if ( rc == 1 && msgp->msg_date != NULL ) {
	/*
	 * add "In-reply-to:" header
	 */
	if ( msgp->msg_messageid == NULL ) {
	    sprintf( buf, "In-reply-to: Your message of \"%s\"\n",
		    msgp->msg_date );
	} else {
	    sprintf( buf,
		    "In-reply-to: Your message of \"%s\"\n             %s\n",
		    msgp->msg_date, msgp->msg_messageid );
	}
	rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
    }

    if ( rc == 1 ) {
	sprintf( buf, "To: %s\n", msgp->msg_replyto );
	rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
    }

    /*
     * send the header/body separator (blank line)
     */
    if ( rc == 1 ) {
	rc = fwrite( "\n", 1, 1, cmdpipe );
    }

    /*
     * send the body
     */
    if ( rc == 1 ) {
	rc = fwrite( body, strlen( body ), 1, cmdpipe );
    }

    if ( rc != 1 && dosyslog ) {
	syslog( LOG_ERR, "write to binmail failed: %m" );
    }

    if ( pclose( cmdpipe ) < 0 ) {
	if ( dosyslog ) {
	    syslog( LOG_ERR, "pclose binmail failed: %m" );
	}
	return( -1 );
    }

    return( rc == 1 ? 0 : -1 );
}


int
find_command( text, argp )
    char	*text;
    char	**argp;
{
    int		i;
    char	*s, *p;
    static char argbuf[ MAXSIZE ];

    p = text;
    for ( s = argbuf; *p != '\0'; ++p ) {
	*s++ = tolower( *p );
    }
    *s = '\0';

    for ( i = 0; cmds[ i ].cmd_text != NULL; ++i ) {
	if (( s = strstr( argbuf, cmds[ i ].cmd_text )) != NULL
		    && isspace( *(s + strlen( cmds[ i ].cmd_text )))) {
	    strcpy( argbuf, text + (s - argbuf) + strlen( cmds[ i ].cmd_text ));
	    *argp = argbuf;
	    while ( isspace( **argp )) {
		++(*argp);
	    }
	    return( i );
	}
    }

    return( -1 );
}