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
13 #include <ac/stdlib.h>
16 #include <ac/signal.h>
17 #include <ac/string.h>
18 #include <ac/syslog.h>
19 #include <ac/unistd.h>
21 #include "ldap_defaults.h"
25 #ifdef LDAP_CONNECTIONLESS
27 #endif /* LDAP_CONNECTIONLESS */
30 int sizelimit = RCPT500_SIZELIMIT;
31 int rdncount = RCPT500_RDNCOUNT;
33 char *ldaphost = NULL;
34 char *searchbase = NULL;
36 char *filterfile = FILTERFILE;
37 char *templatefile = TEMPLATEFILE;
38 static char reply[ MAXSIZE * RCPT500_LISTLIMIT ];
44 static int read_msg(FILE *fp, struct msginfo *msgp);
45 static char *read_hdr(FILE *fp, int off, char *buf, int MAXSIZEe, char **ln_p);
46 static int send_reply(struct msginfo *msgp, char *body);
47 static int find_command(char *text, char **argp);
50 * main is invoked by sendmail via the alias file
51 * the entire incoming message gets piped to our standard input
54 main( int argc, char **argv )
56 char *prog, *usage = "%s [-l] [-U] [-h ldaphost] [-p ldapport] [-b searchbase] [-a] [-z sizelimit] [-u dapuser] [-f filterfile] [-t templatefile] [-c rdncount]\n";
62 if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
63 prog = strdup( argv[ 0 ] );
65 prog = strdup( prog + 1 );
69 while (( c = getopt( argc, argv, "alUh:b:s:z:f:t:p:c:" )) != EOF ) {
78 #ifdef LDAP_CONNECTIONLESS
80 #else /* LDAP_CONNECTIONLESS */
82 "Compile with -DLDAP_CONNECTIONLESS for -U support\n" );
83 #endif /* LDAP_CONNECTIONLESS */
92 ldapport = atoi( optarg );
95 sizelimit = atoi( optarg );
104 templatefile = optarg;
107 rdncount = atoi( optarg );
113 if ( errflg || optind < argc ) {
114 fprintf( stderr, usage, prog );
119 (void) SIGNAL( SIGPIPE, SIG_IGN );
124 * if syslogging requested, initialize
127 openlog( prog, OPENLOG_OPTIONS, LOG_DAEMON );
129 openlog( prog, OPENLOG_OPTIONS );
133 if ( read_msg( stdin, &msg ) < 0 ) {
135 syslog( LOG_INFO, "unparseable message ignored" );
137 exit( 0 ); /* so as not to give sendmail an error */
141 syslog( LOG_INFO, "processing command \"%s %s\" from %s",
142 ( msg.msg_command < 0 ) ? "Unknown" :
143 cmds[ msg.msg_command ].cmd_text,
144 ( msg.msg_arg == NULL ) ? "" : msg.msg_arg, msg.msg_replyto );
147 if ( msg.msg_command < 0 ) {
148 msg.msg_command = 0; /* unknown command == help command */
152 sprintf( reply, "Your request was interpreted as: %s %s\n\n",
153 cmds[ msg.msg_command ].cmd_text, msg.msg_arg );
156 (*cmds[ msg.msg_command ].cmd_handler)( &msg, reply );
158 if ( send_reply( &msg, reply ) < 0 ) {
160 syslog( LOG_INFO, "reply failed: %m" );
162 exit( 0 ); /* so as not to give sendmail an error */
166 syslog( LOG_INFO, "reply OK" );
174 read_msg( FILE *fp, struct msginfo *msgp )
176 char buf[ MAXSIZE ], *line;
179 msgp->msg_replyto = msgp->msg_date = msgp->msg_subject = NULL;
183 if ( line == NULL ) {
184 if (( line = fgets( buf, MAXSIZE, fp )) == NULL ) {
187 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
190 if ( *buf == '\0' ) { /* start of message body */
193 if ( strncasecmp( buf, "Reply-To:", 9 ) == 0 ) {
194 if ( msgp->msg_replyto != NULL ) {
195 free( msgp->msg_replyto );
197 msgp->msg_replyto = read_hdr( fp, 9, buf, MAXSIZE, &line );
198 } else if ( strncasecmp( buf, "From:", 5 ) == 0 &&
199 msgp->msg_replyto == NULL ) {
200 msgp->msg_replyto = read_hdr( fp, 5, buf, MAXSIZE, &line );
201 } else if ( strncasecmp( buf, "Date:", 5 ) == 0 ) {
202 msgp->msg_date = read_hdr( fp, 5, buf, MAXSIZE, &line );
203 } else if ( strncasecmp( buf, "Message-ID:", 5 ) == 0 ) {
204 msgp->msg_messageid = read_hdr( fp, 11, buf, MAXSIZE, &line );
205 } else if ( strncasecmp( buf, "Subject:", 8 ) == 0 ) {
206 if (( msgp->msg_subject =
207 read_hdr( fp, 8, buf, MAXSIZE, &line )) != NULL ) {
208 command = find_command( msgp->msg_subject, &msgp->msg_arg );
211 line = NULL; /* discard current line */
215 while ( command < 0 && line != NULL ) {
217 * read the body of the message, looking for commands
219 if (( line = fgets( buf, MAXSIZE, fp )) != NULL ) {
220 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
221 command = find_command( buf, &msgp->msg_arg );
225 if ( msgp->msg_replyto == NULL ) {
229 msgp->msg_command = command;
235 read_hdr( FILE *fp, int offset, char *buf, int MAXSIZEe, char **linep )
239 for ( hdr = buf + offset; isspace( (unsigned char) *hdr ); ++hdr ) {
242 if (( hdr = strdup( hdr )) == NULL ) {
244 syslog( LOG_ERR, "strdup: %m" );
250 *linep = fgets( buf, MAXSIZE, fp );
251 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
252 if ( *linep == NULL || !isspace( (unsigned char) **linep )) {
255 if (( hdr = realloc( hdr, strlen( hdr ) +
256 strlen( *linep ) + 3 )) == NULL) {
258 syslog( LOG_ERR, "realloc: %m" );
263 strcat( hdr, *linep );
271 send_reply( struct msginfo *msgp, char *body )
277 if (( cmdpipe = popen( RCPT500_PIPEMAILCMD, "w" )) == NULL ) {
279 syslog( LOG_ERR, "popen pipemailcmd failed: %m" );
288 sprintf( buf, "From: %s\n", RCPT500_FROM );
289 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
292 if ( msgp->msg_subject != NULL ) {
293 sprintf( buf, "Subject: Re: %s\n", msgp->msg_subject );
295 sprintf( buf, "Subject: query response\n" );
297 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
300 if ( rc == 1 && msgp->msg_date != NULL ) {
302 * add "In-reply-to:" header
304 if ( msgp->msg_messageid == NULL ) {
305 sprintf( buf, "In-reply-to: Your message of \"%s\"\n",
309 "In-reply-to: Your message of \"%s\"\n %s\n",
310 msgp->msg_date, msgp->msg_messageid );
312 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
316 sprintf( buf, "To: %s\n", msgp->msg_replyto );
317 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
321 * send the header/body separator (blank line)
324 rc = fwrite( "\n", 1, 1, cmdpipe );
331 rc = fwrite( body, strlen( body ), 1, cmdpipe );
334 if ( rc != 1 && dosyslog ) {
335 syslog( LOG_ERR, "write to binmail failed: %m" );
338 if ( pclose( cmdpipe ) < 0 ) {
340 syslog( LOG_ERR, "pclose binmail failed: %m" );
345 return( rc == 1 ? 0 : -1 );
350 find_command( char *text, char **argp )
354 static char argbuf[ MAXSIZE ];
357 for ( s = argbuf; *p != '\0'; ++p ) {
358 *s++ = TOLOWER( (unsigned char) *p );
362 for ( i = 0; cmds[ i ].cmd_text != NULL; ++i ) {
363 if (( s = strstr( argbuf, cmds[ i ].cmd_text )) != NULL
364 && isspace( (unsigned char) s[ strlen( cmds[ i ].cmd_text ) ] )) {
365 strcpy( argbuf, text + (s - argbuf) + strlen( cmds[ i ].cmd_text ));
367 while ( isspace( (unsigned char) **argp )) {