2 * main.c: for rcpt500 (X.500 email query responder)
4 * 16 June 1992 by Mark C Smith
5 * Copyright (c) 1992 The Regents of The University of Michigan
15 #include <ac/string.h>
16 #include <ac/syslog.h>
17 #include <ac/unistd.h>
19 #include "ldapconfig.h"
23 #ifdef LDAP_CONNECTIONLESS
25 #endif /* LDAP_CONNECTIONLESS */
28 int sizelimit = RCPT500_SIZELIMIT;
29 int rdncount = RCPT500_RDNCOUNT;
31 char *ldaphost = NULL;
32 char *searchbase = NULL;
34 char *filterfile = FILTERFILE;
35 char *templatefile = TEMPLATEFILE;
36 static char reply[ MAXSIZE * RCPT500_LISTLIMIT ];
42 static int read_msg(FILE *fp, struct msginfo *msgp);
43 static char *read_hdr(FILE *fp, int off, char *buf, int MAXSIZEe, char **ln_p);
44 static int send_reply(struct msginfo *msgp, char *body);
45 static int find_command(char *text, char **argp);
48 * main is invoked by sendmail via the alias file
49 * the entire incoming message gets piped to our standard input
52 main( int argc, char **argv )
54 char *prog, *usage = "%s [-l] [-U] [-h ldaphost] [-p ldapport] [-b searchbase] [-a] [-z sizelimit] [-u dapuser] [-f filterfile] [-t templatefile] [-c rdncount]\n";
61 if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
62 prog = strdup( argv[ 0 ] );
64 prog = strdup( prog + 1 );
68 while (( c = getopt( argc, argv, "alUh:b:s:z:f:t:p:c:" )) != EOF ) {
77 #ifdef LDAP_CONNECTIONLESS
79 #else /* LDAP_CONNECTIONLESS */
81 "Compile with -DLDAP_CONNECTIONLESS for -U support\n" );
82 #endif /* LDAP_CONNECTIONLESS */
91 ldapport = atoi( optarg );
94 sizelimit = atoi( optarg );
103 templatefile = optarg;
106 rdncount = atoi( optarg );
112 if ( errflg || optind < argc ) {
113 fprintf( stderr, usage, prog );
119 * if syslogging requested, initialize
122 openlog( prog, OPENLOG_OPTIONS, LOG_DAEMON );
124 openlog( prog, OPENLOG_OPTIONS );
128 if ( read_msg( stdin, &msg ) < 0 ) {
130 syslog( LOG_INFO, "unparseable message ignored" );
132 exit( 0 ); /* so as not to give sendmail an error */
136 syslog( LOG_INFO, "processing command \"%s %s\" from %s",
137 ( msg.msg_command < 0 ) ? "Unknown" :
138 cmds[ msg.msg_command ].cmd_text,
139 ( msg.msg_arg == NULL ) ? "" : msg.msg_arg, msg.msg_replyto );
142 if ( msg.msg_command < 0 ) {
143 msg.msg_command = 0; /* unknown command == help command */
147 sprintf( reply, "Your request was interpreted as: %s %s\n\n",
148 cmds[ msg.msg_command ].cmd_text, msg.msg_arg );
151 (*cmds[ msg.msg_command ].cmd_handler)( &msg, reply );
153 if ( send_reply( &msg, reply ) < 0 ) {
155 syslog( LOG_INFO, "reply failed: %m" );
157 exit( 0 ); /* so as not to give sendmail an error */
161 syslog( LOG_INFO, "reply OK" );
169 read_msg( FILE *fp, struct msginfo *msgp )
171 char buf[ MAXSIZE ], *line;
174 msgp->msg_replyto = msgp->msg_date = msgp->msg_subject = NULL;
178 if ( line == NULL ) {
179 if (( line = fgets( buf, MAXSIZE, fp )) == NULL ) {
182 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
185 if ( *buf == '\0' ) { /* start of message body */
188 if ( strncasecmp( buf, "Reply-To:", 9 ) == 0 ) {
189 if ( msgp->msg_replyto != NULL ) {
190 free( msgp->msg_replyto );
192 msgp->msg_replyto = read_hdr( fp, 9, buf, MAXSIZE, &line );
193 } else if ( strncasecmp( buf, "From:", 5 ) == 0 &&
194 msgp->msg_replyto == NULL ) {
195 msgp->msg_replyto = read_hdr( fp, 5, buf, MAXSIZE, &line );
196 } else if ( strncasecmp( buf, "Date:", 5 ) == 0 ) {
197 msgp->msg_date = read_hdr( fp, 5, buf, MAXSIZE, &line );
198 } else if ( strncasecmp( buf, "Message-ID:", 5 ) == 0 ) {
199 msgp->msg_messageid = read_hdr( fp, 11, buf, MAXSIZE, &line );
200 } else if ( strncasecmp( buf, "Subject:", 8 ) == 0 ) {
201 if (( msgp->msg_subject =
202 read_hdr( fp, 8, buf, MAXSIZE, &line )) != NULL ) {
203 command = find_command( msgp->msg_subject, &msgp->msg_arg );
206 line = NULL; /* discard current line */
210 while ( command < 0 && line != NULL ) {
212 * read the body of the message, looking for commands
214 if (( line = fgets( buf, MAXSIZE, fp )) != NULL ) {
215 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
216 command = find_command( buf, &msgp->msg_arg );
220 if ( msgp->msg_replyto == NULL ) {
224 msgp->msg_command = command;
230 read_hdr( FILE *fp, int offset, char *buf, int MAXSIZEe, char **linep )
234 for ( hdr = buf + offset; isspace( *hdr ); ++hdr ) {
237 if (( hdr = strdup( hdr )) == NULL ) {
239 syslog( LOG_ERR, "strdup: %m" );
245 *linep = fgets( buf, MAXSIZE, fp );
246 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
247 if ( *linep == NULL || !isspace( **linep )) {
250 if (( hdr = realloc( hdr, strlen( hdr ) +
251 strlen( *linep ) + 3 )) == NULL) {
253 syslog( LOG_ERR, "realloc: %m" );
258 strcat( hdr, *linep );
266 send_reply( struct msginfo *msgp, char *body )
272 if (( cmdpipe = popen( RCPT500_PIPEMAILCMD, "w" )) == NULL ) {
274 syslog( LOG_ERR, "popen pipemailcmd failed: %m" );
283 sprintf( buf, "From: %s\n", RCPT500_FROM );
284 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
287 if ( msgp->msg_subject != NULL ) {
288 sprintf( buf, "Subject: Re: %s\n", msgp->msg_subject );
290 sprintf( buf, "Subject: query response\n" );
292 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
295 if ( rc == 1 && msgp->msg_date != NULL ) {
297 * add "In-reply-to:" header
299 if ( msgp->msg_messageid == NULL ) {
300 sprintf( buf, "In-reply-to: Your message of \"%s\"\n",
304 "In-reply-to: Your message of \"%s\"\n %s\n",
305 msgp->msg_date, msgp->msg_messageid );
307 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
311 sprintf( buf, "To: %s\n", msgp->msg_replyto );
312 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
316 * send the header/body separator (blank line)
319 rc = fwrite( "\n", 1, 1, cmdpipe );
326 rc = fwrite( body, strlen( body ), 1, cmdpipe );
329 if ( rc != 1 && dosyslog ) {
330 syslog( LOG_ERR, "write to binmail failed: %m" );
333 if ( pclose( cmdpipe ) < 0 ) {
335 syslog( LOG_ERR, "pclose binmail failed: %m" );
340 return( rc == 1 ? 0 : -1 );
345 find_command( char *text, char **argp )
349 static char argbuf[ MAXSIZE ];
352 for ( s = argbuf; *p != '\0'; ++p ) {
353 *s++ = TOLOWER( *p );
357 for ( i = 0; cmds[ i ].cmd_text != NULL; ++i ) {
358 if (( s = strstr( argbuf, cmds[ i ].cmd_text )) != NULL
359 && isspace( *(s + strlen( cmds[ i ].cmd_text )))) {
360 strcpy( argbuf, text + (s - argbuf) + strlen( cmds[ i ].cmd_text ));
362 while ( isspace( **argp )) {