File: address.c

package info (click to toggle)
sendfile 2.1b.20080616-5.2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,568 kB
  • sloc: ansic: 13,128; sh: 4,043; perl: 844; makefile: 145; java: 36; csh: 3
file content (882 lines) | stat: -rw-r--r-- 23,563 bytes parent folder | download | duplicates (7)
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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/*
 * File:	address.c
 *
 * Author:	Ulli Horlacher (framstag@rus.uni-stuttgart.de)
 * 
 * Contrib.:	Stefan Zehl (sec@42.org)
 *
 * History:	
 * 
 *   1995-08-12  Framstag	initial version
 *   1995-11-07  Framstag	added URL addressing
 *   1995-11-15  Framstag	added sendfile alias file
 *   1995-12-13  Framstag	correct bug when reading alias file
 *   1996-04-30  Framstag	checking elm alias only if configured
 *   1997-01-04  Framstag	renamed from destination.c to address.c
 * 				added check_forward()
 *   1997-01-22  Framstag	added connect-test to generic saft address
 *   1997-02-23  Framstag	modified str_* function names
 *   1997-02-24  Framstag	sprintf() -> snprintf()
 *   1997-03-18  Framstag	better URL parsing
 *   1997-11-22  Framstag	added saft2rfc822()
 * 				better SAFT URL parsing in check_forward()
 *   1997-11-27  Framstag	accept also saft.domain/user pseudo URLs 
 *				(idea by Eumel, thanx!)
 *   1998-01-04  Framstag	fixed bug in saft2rfc822()
 *   1998-01-13  Sec		look for generic saft.domain address
 *   1998-01-17  Framstag	check SAFT-URL for alternative tcp port
 *   1998-01-20  Framstag	check_forward() works now better with sendfiled
 *   1998-01-25  Framstag	better check_forward() 
 *				(accept user name without host)
 *   1998-03-07  Framstag	added finger_saft_port()
 *   1998-03-11  Framstag	aliases is now in $HOME/.sendfile/
 *   1998-07-11  Framstag	aliases may have an entry for CLI arguments
 *   1998-08-26  Framstag	allow port names, too (framstag@bofh:saft)
 *   1998-12-13	 Framstag	fixed port determination bug
 *   1999-08-07	 Framstag	fixed redirect message collecting bug
 * 				"forward address found" is now a Info
 *   2005-06-06  Maide          added multiprotocol cababilities
 *   2005-06-06  Maide          replaced numeric ports with service string
 *   2005-06-06  Maide          made finger_saft_port return port or service
 *				string instead of numeric port value
 *   2005-06-06  Maide          added out of memory error message
 *
 * Various address routines for sendfile.c and sendmsg.c
 *
 * Copyright  1995-1999 Ulli Horlacher
 * This file is covered by the GNU General Public License
 */

#include "config.h"		/* various #defines */

#include <ctype.h>
#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netdb.h>
#include <netinet/in.h>

#include "string.h"	/* Extended string functions */
#include "io.h"		/* misc IO routines */
#include "net.h"	/* the network routines */
#include "utf7.h"	/* UTF-7 coding */
#include "message.h"	/* information, warning and error messages */
#include "address.h"	/* address routines */

#if defined(SOLARIS2)
  int gethostname(char *, int);
#endif

#if defined(LINUX)
  int gethostname(char *, size_t);
  int symlink(const char *, const char *);
#endif

/* check an alias file */
int check_alias(char *, char *, char *, char *);

/* test if there is a forward address set */
int check_forward(int, char *, char *, char *);

/* finger user@host and look for user SAFT port */
#ifndef ENABLE_MULTIPROTOCOL
int finger_saft_port(char *, char *);
#else
char* finger_saft_port(char *, char *);
#endif


extern int 
  client,		/* flag to determine client or server */
  verbose,		/* flag for verbose mode */
  quiet;		/* quiet mode flag */

extern char *prg;	/* name of the game */


/*
 * destination  - get recipient user and host
 *
 * INPUT:  argc		- shell argument count
 *         argv		- the shell arguments
 *
 * OUTPUT: user		- own user login and real name
 *         recipient	- recipient user name
 *         host		- recipient host name
 * 	   aopt		- alias options
 */
void destination(int argc, char **argv,
		 char *user, char *recipient, char *host, char *aopt) {
  char *cp,			/* simple char pointer */
       *at, 			/* @ character in recepient@host */
       *larg,			/* last argument */
       gecos[FLEN],		/* user real name */
       tmp[MAXLEN],		/* tmp string */
       line[MAXLEN],		/* one text line */
       localhost[FLEN], 	/* name of the local host */
       userconfig[MAXLEN],	/* user configuration directory */
       aliasfile[MAXLEN];	/* the alias file */
  FILE *inf;			/* input file */
  struct passwd *pwe;		/* password entry */
  struct stat finfo;		/* file information */
  
  /* get the own user name */
  if ((pwe=getpwuid(getuid())) == NULL)
    message(prg,'F',"could not determine own user name");

  /* translate the real name to UTF-7 and add it */
  iso2utf(gecos,pwe->pw_gecos);
  if ((cp=strchr(gecos,','))) *cp=0;
  snprintf(user,FLEN-1,"%s %s",pwe->pw_name,gecos);
  
  /* check user configuration directory */
  snprintf(MAXS(userconfig),"%s/.sendfile",pwe->pw_dir);
  snprintf(MAXS(tmp),SPOOL"/%s/config",pwe->pw_name);
  if (stat(userconfig,&finfo)<0 && stat(tmp,&finfo)==0)
    symlink(tmp,userconfig);
  
  /* trick: argc == 0, when message reply mode */
  if (argc==0) {
    if (gethostname(localhost,FLEN-1)<0) strcpy(localhost,"localhost");
    snprintf(MAXS(tmp),"%s/msg@%s",userconfig,localhost);
    if ((inf=rfopen(tmp,"r")) && fgetl(line,inf)) {
      if ((cp=strchr(line,'\n'))) *cp=0;
      if ((cp=strchr(line,'@'))) {
	*cp=0;
	snprintf(host,FLEN-1,"%s",cp+1);
      }
      snprintf(recipient,FLEN-1,"%s",line);
    }
    fclose(inf);
    return;
  }
  
  /* trick: argc < 0, when called from quak */
  if (argc<0)
    larg=argv[-argc];
  else
    larg=argv[argc-1];
  
  *host=0;
  
  /* user@host specified? */
  if ((at=strchr(larg,'@'))) {
   
   /* store recipient name and host */
    *recipient=0;
    strncat(recipient,larg,at-larg);
    strcpy(host,at+1);

  /* SAFT-URL specified? */
  } else if (str_neq_nocase(larg,"saft",4) && strchr(larg,'/')) {
    if (str_neq_nocase(larg,"saft://",7)) larg+=7;
    cp=strrchr(larg,'/');
    if (!cp || strchr(larg,'@')) message(prg,'F',"illegal SAFT-URL");
    strcpy(recipient,cp+1);
    *cp=0;
    while ((cp=strchr(larg,'/'))) *cp='.';
    strcpy(host,larg);

  /* local user or alias specified */
  } else {
   
    strcpy(recipient,larg);

    /* check the sendfile alias file */
    snprintf(MAXS(aliasfile),"%s/aliases",userconfig);
    if (check_alias(aliasfile,recipient,host,aopt)<0) {
     
#ifdef RESPECT_MAIL_ALIASES
      /* check the elm alias file */
      snprintf(MAXS(aliasfile),"%s/.elm/aliases.text",pwe->pw_dir);
      if (check_alias(aliasfile,recipient,host,aopt)<0) {
       
#endif
	/* store local recipient name and local host */
	/* trick: argc <= 0, when called from quak */
	if (argc<=0)
	  strcpy(recipient,argv[-argc]);
	else
	  strcpy(recipient,argv[argc-1]);
	strcpy(host,"127.0.0.1");

#ifdef RESPECT_MAIL_ALIASES
      }
#endif
    }
  }
  /*if (!*port) *port=SAFT;*/
}


/*
 * check_alias  - check an alias file
 *
 * INPUT:  aliasfile	- the alias file
 *         recipient	- recipient alias name
 *
 * OUTPUT: recipient	- recipient user name
 *         host		- recipient host name
 * 	   aopt		- alias options
 */
int check_alias(char *aliasfile, char *recipient, char *host, char *aopt) {
  char *cp, 			/* a character pointer */
       line[MAXLEN],		/* one line of the alias file */
       address[MAXLEN];		/* address from the alias */
  FILE *inf;			/* input file to read */

  /* if there is an alias file, open it (what else? :-) ) */
  inf=rfopen(aliasfile,"r");
  if (inf==NULL) return(-1);

  *address=0;
  if (aopt) *aopt=0;
  
  /* loop over all lines */
  while (fgetl(line,inf)) {
   
    /* trim line */
    if ((cp=strchr(line,'\n'))) *cp=0;
    if ((cp=strchr(line,'#'))) *cp=0;
    str_trim(line);
    if (!*line) continue;

    /* save the address and options */
    cp=strchr(line,' ');
    if (cp) 
      *cp=0;
    else
      continue;
    strcpy(address,cp+1);
    if ((cp=strchr(address,' '))) {
      *cp=0;
      if (aopt) snprintf(aopt,FLEN-1,"%s",cp+1);
    }

    /* is it the correct alias, we are ready */
    if (str_eq(recipient,line)) break;
    
    *address=0;
    if (aopt) *aopt=0;
  }
  
  fclose(inf);

  /* alias found? */
  if (*address) {

    /* convert SAFT to mail address */
    if (str_beq_nocase(address,"saft://")) saft2rfc822(address);
   
    /* store recipient name and host */
    cp=strchr(address,'@');
    if (cp) {
      *cp=0;
      strcpy(host,cp+1);
    } else
      strcpy(host,"0");
      
    strcpy(recipient,address);
    return(0);

  }
  return(-1);
}


/*
 * check_forward  - test if there is a forward address set
 *
 * INPUT:  sockfd	- socket file descriptor
 *         recipient	- recipient user name
 *	   host		- host to connect
 * 	   redirect	- redirect comment 
 *
 * OUTPUT: recipient	- new recipient user name
 *	   host		- new host to connect
 * 	   redirect	- new redirect comment
 *
 * RETURN: 1 if a forward is set, 0 if not, -1 on error
 */
int check_forward(int sockfd, char *recipient, char *host, char *redirect) {
  char
    *cp,*at,		/* simple string pointer */
    *adr,		/* address in forwarding reply string */
    *reply,		/* reply string from server */
    tmp[MAXLEN];	/* temporary string */

  /* get reply from server */
  reply=getreply(sockfd);
  str_trim(reply);
  
  /* forward address set? */
  if (str_beq(reply,"510 ")) {
    
    adr=strrchr(reply,' ')+1;
    
    /* convert SAFT to mail address */
    if (str_beq_nocase(adr,"saft://")) saft2rfc822(adr);

    if (quiet<2) message(prg,'I',"forward address found");
    if (*redirect) {
      strcpy(tmp,redirect);
      snprintf(redirect,MAXLEN-1,"%s\r\nredirected by %s@%s",
	       tmp,recipient,host);
    } else {
      snprintf(redirect,MAXLEN-1,"redirected by %s@%s",recipient,host);
    }

    /* save new recipient and host name */
    if ((at=strchr(adr,'@'))) {
      *at=0;
      strcpy(host,at+1);
    }
    strcpy(recipient,adr);

    return(1);
  }

  /* illegal answer */
  if (!str_beq(reply,"200 ")) {
    snprintf(MAXS(tmp),"server error: %s",reply+4);
    errno=0;
    if (client) message(prg,'F',tmp);
    strcpy(redirect,reply+4);
    if ((cp=strrchr(redirect,'.'))) *cp=0;
    return(-1);
  }

  return(0);
}


#ifndef ENABLE_MULTIPROTOCOL

/*
 * saft_connect  - look for correct SAFT server and open connection
 *
 * INPUT:  type		- file or message
 *         recipient	- recipient user name
 *	   user		- local user name
 *	   host		- host to connect
 * 	   redirect	- redirect comment
 *
 * OUTPUT: recipient	- new recipient user name
 *	   host		- new host to connect
 * 	   redirect	- redirect comment
 * 
 * RETURN: socket file descriptor; -1 on error
 */
int saft_connect(const char *type, 
		 char *recipient, char *user, char *host, char *redirect) {
  int
    port,		/* tcp port to connect */
    sockfd,     	/* socket file descriptor */
    hopcount;		/* count for forwarding addresses */
  char
    *cp,		/* simple character pointer */
    answer[FLEN],	/* answer string */
    tmp[MAXLEN],	/* temporary string */
    ahost[MAXLEN],	/* alternate host */
    line[MAXLEN]; 	/* one line of text */
  extern int client;	/* flag to determine client or server mode */
  struct servent *sinfo;/* service info */
      
  port=SAFT;
  hopcount=0;
  *answer=0;
  
  /* get port number */
  if ((cp=strchr(host,':'))) {
    cp++;
    if (*cp>'9') {
      if ((sinfo=getservbyname(cp,"tcp"))) port=ntohs(sinfo->s_port);
    } else
      port=atoi(cp);
  }
  
  /* try to connect to the recipient's server */
  for (hopcount=1; hopcount<11; hopcount++) {
     
    /* if the finger-port is specified get real port from there */
    if (port==79 || !port) {
      if ((cp=strchr(host,':'))) *cp=0;
      snprintf(MAXS(tmp),"opening connection to finger://%s/%s",host,recipient);
      if (quiet<2) message(prg,'I',tmp);
      port=finger_saft_port(recipient,host);
      if (port<1) {
	errno=0;
	message(prg,'F',"no SAFT port information");
      }
      snprintf(tmp,FLEN-1,"%s:%d",host,port);
      strcpy(host,tmp);
    }
      
    /* initiate the SAFT-connection to the server */
    snprintf(MAXS(tmp),"opening connection to saft://%s/%s",host,recipient);
    if (quiet<2) message(prg,'I',tmp);
    sockfd=open_connection(host,port);

    if (port==SAFT) {
      
      /* host has no ip-address, but we can try more ... */
      if (sockfd==-3 && str_eq(type,"file")) {
	if (client) {
	  snprintf(MAXS(tmp),"%s has no internet-address",host);
	  if (quiet<2) message(prg,'W',tmp);
	}
    
	/* try generic saft-address for this host/domain */
	if (port==SAFT) {
	  snprintf(MAXS(ahost),"saft.%s",host);
	  if (client) {
	    if(gethostbyname(ahost)){
	      if (!quiet) {
		printf("try sending to %s@%s ? ",recipient,ahost);
		fgetl(answer,stdin);
	      } else
		*answer='y';
	    } else
	      *answer='n';
	  }
	  if (tolower(*answer)!='n' || !client) {
	    strcpy(host,ahost);
	    if (client) {
	      snprintf(MAXS(tmp),"opening connection to %s@%s",recipient,host);
	      if (quiet<2) message(prg,'I',tmp);
	    }
	    sockfd=open_connection(host,port);
	  }
	}
	
      }
  
      /* try user SAFT port on connection failure */
      if (sockfd==-2 && str_eq(type,"file")) {
	if (verbose) {
	  snprintf(MAXS(tmp),"cannot connect to SAFT port %d on %s",
		   port,host);
	  message(prg,'E',tmp);
	}
	port=finger_saft_port(recipient,host);
	if (port>0 && port!=SAFT) {
	  snprintf(MAXS(tmp),"%s has no system SAFT server, "
		   "trying user SAFT server on port %d",host,port);
	  if (quiet<2) message(prg,'W',tmp);
	  sockfd=open_connection(host,port);
	} else
	  port=SAFT;
      }
      
    }
    
    if (sockfd==-1) snprintf(MAXS(tmp),"cannot create a network socket");
    if (sockfd==-2) snprintf(MAXS(tmp),"cannot open connection to %s",host);
    if (sockfd==-3) snprintf(MAXS(tmp),"%s is unknown",host);
    if (sockfd<0) {
      if (client) {
	errno=0;
	message(prg,'F',tmp);
      } else
	return(-1);
    }

    /* no remote server or protocol error? */
    sock_getline(sockfd,line);
    if (!str_beq(line,"220 ") || !strstr(line,"SAFT")) {
      snprintf(MAXS(tmp),"No SAFT server on port %d at %s",port,host);
      if ((cp=strrchr(tmp,':'))) *cp=0;
      if (client) {
	errno=0;
	message(prg,'F',tmp);
      } else {
	strcpy(redirect,tmp);
	return(-1);
      }
    }

    /* send constant header lines */
    snprintf(MAXS(tmp),"FROM %s",user);
    sendheader(sockfd,tmp);
    snprintf(MAXS(tmp),"TO %s",recipient);
    sock_putline(sockfd,tmp);

    /* is there a forward set? */
    if (check_forward(sockfd,recipient,host,redirect)) {
      
      /* close current connection */
      sock_putline(sockfd,"QUIT");
      getreply(sockfd);
      shutdown(sockfd,2);
      continue;
    }

    /* sendfile connection? */
    if (str_eq(type,"file")) {

      if (verbose) message(prg,'I',"testing remote server");
      
      /* test if server can receive files */
      sock_putline(sockfd,"FILE test");
      if (check_forward(sockfd,recipient,host,redirect)) {
      
	/* close current connection */
	sock_putline(sockfd,"QUIT");
	getreply(sockfd);
	shutdown(sockfd,2);
	continue;
      }
      
    }

    /* connection is successfull */
    break;

  }

  if (hopcount>10) {
    strcpy(tmp,"maximum hopcount reached (forward loop?)");
    if (client) {
      errno=0;
      message(prg,'F',tmp);
    } else {
      strcpy(redirect,tmp);
      return(-1);
    }
  }
  
  return(sockfd);
}


/* 
 * finger_saft_port  - finger user@host and look for user SAFT port
 * 
 * INPUT:  user	- user login name
 * 
 * OUTPUT: host	- host to connect
 * 
 * RETURN: user saft port on success, -1 on failure
 */
int finger_saft_port(char *user, char *host) {
  int
    port,		/* user SAFT tcp port */
    sockfd;     	/* socket file descriptor */
  char
    *cp,		/* character pointer */
    line[MAXLEN];	/* one line of text */
  
  port=-1;
  *line=0;
  
  /* connect to the finger port of the remote host */
  sockfd=open_connection(host,79);
  if (sockfd>0) {
    sock_putline(sockfd,user);
    while (sock_getline(sockfd,line)>=0) {
      str_trim(line);
      if (str_beq_nocase(line,"saftport") && (cp=strchr(line,'='))) {
	port=atoi(cp+1);
	break;
      }
    }
    shutdown(sockfd,2);
  }
  
  return(port);
}


#else /* ENABLE_MULTIPROTOCOL */


/*
 * saft_connect  - look for correct SAFT server and open connection
 *
 * INPUT:  type		- file or message
 *         recipient	- recipient user name
 *	   user		- local user name
 *	   host		- host to connect
 * 	   redirect	- redirect comment
 *
 * OUTPUT: recipient	- new recipient user name
 *	   host		- new host to connect
 * 	   redirect	- redirect comment
 * 
 * RETURN: socket file descriptor; -1 on error
 */
int saft_connect(const char *type, 
		 char *recipient, char *user, char *host, char *redirect) {
  int
    sockfd,     	/* socket file descriptor */
    hopcount;		/* count for forwarding addresses */
  char* service = NULL;	/* service or port to connect to */
  int needsFree = 0;	/* do we need to call free(service)? */
  char
    *cp,		/* simple character pointer */
    answer[FLEN],	/* answer string */
    tmp[MAXLEN],	/* temporary string */
    ahost[MAXLEN],	/* alternate host */
    line[MAXLEN]; 	/* one line of text */
  extern int client;	/* flag to determine client or server mode */
      
  service=SERVICE;
  hopcount=0;
  *answer=0;
  
  /* get port number */
  if (*host == '[') {
    if ((cp=strchr(host,']'))) {
      cp++;
      if (*cp == ':') {
        cp++;
        service = cp;
      }
    }
  } else {
    if ((cp=strchr(host,':'))) {
      cp++;
      service = cp;
    }
  }
  
  /* try to connect to the recipient's server */
  for (hopcount=1; hopcount<11; hopcount++) {
     
    /* if the finger-port is specified get real port from there */
    if (strcasecmp(service, "finger") == 0 || strcmp(service, "79") == 0) {
      if (*host == '[') {
        if ((cp=strchr(host,']'))) {
          cp++;
          if (*cp == ':') {
             *cp=0;
          }
        }
      } else {
        if ((cp=strchr(host,':'))) *cp=0;
      }
      snprintf(MAXS(tmp),"opening connection to finger://%s/%s",host,recipient);
      if (quiet<2) message(prg,'I',tmp);
      service=finger_saft_port(recipient,host);
      if (service==NULL) {
	errno=0;
	message(prg,'F',"no SAFT port information");
      } else needsFree = 1;
      snprintf(tmp,FLEN-1,"%s:%s",host,service);
      strcpy(host,tmp);
    }
      
    /* initiate the SAFT-connection to the server */
    snprintf(MAXS(tmp),"opening connection to saft://%s/%s",host,recipient);
    if (quiet<2) message(prg,'I',tmp);
    sockfd=open_connection(host,service);

    if (strcasecmp(service, SERVICE) == 0 || strcmp(service, PORT_STRING) == 0) {
      
      /* host has no ip-address, but we can try more ... */
      if (sockfd==-3 && str_eq(type,"file")) {
	if (client) {
	  snprintf(MAXS(tmp),"%s has no internet-address",host);
	  if (quiet<2) message(prg,'W',tmp);
	}
    
	/* try generic saft-address for this host/domain */
	if (strcasecmp(service, SERVICE) == 0 || strcmp(service, PORT_STRING) == 0) {
	  snprintf(MAXS(ahost),"saft.%s",host);
	  if (client) {
	    if(gethostbyname2(ahost, addressFamily)){
	      if (!quiet) {
		printf("try sending to %s@%s ? ",recipient,ahost);
		fgetl(answer,stdin);
	      } else
		*answer='y';
	    } else
	      *answer='n';
	  }
	  if (tolower(*answer)!='n' || !client) {
	    strcpy(host,ahost);
	    if (client) {
	      snprintf(MAXS(tmp),"opening connection to %s@%s",recipient,host);
	      if (quiet<2) message(prg,'I',tmp);
	    }
	    sockfd=open_connection(host,service);
	  }
	}
	
      }
  
      /* try user SAFT port on connection failure */
      if (sockfd==-2 && str_eq(type,"file")) {
	if (verbose) {
	  snprintf(MAXS(tmp),"cannot connect to SAFT port %s on %s",
		   service,host);
	  message(prg,'E',tmp);
	}
	service=finger_saft_port(recipient,host);
        if (service != NULL) needsFree = 1;
	if (service != NULL && strcasecmp(service, SERVICE) != 0 && strcmp(service, PORT_STRING) != 0) {
	  snprintf(MAXS(tmp),"%s has no system SAFT server, "
		   "trying user SAFT server on port %s",host,service);
	  if (quiet<2) message(prg,'W',tmp);
	  sockfd=open_connection(host,service);
	} else
	  service=SERVICE;
	  needsFree = 0;
      }
      
    }
    
    if (sockfd==-1) snprintf(MAXS(tmp),"cannot create a network socket");
    if (sockfd==-2) snprintf(MAXS(tmp),"cannot open connection to %s",host);
    if (sockfd==-3) snprintf(MAXS(tmp),"%s is unknown",host);
    if (sockfd==-4) snprintf(MAXS(tmp),"out of memory");
    if (sockfd<0) {
      if (client) {
	errno=0;
	message(prg,'F',tmp);
      } else {
        if (needsFree) free(service);
	return(-1);
      }
    }

    /* no remote server or protocol error? */
    sock_getline(sockfd,line);
    if (!str_beq(line,"220 ") || !strstr(line,"SAFT")) {
      snprintf(MAXS(tmp),"No SAFT server on port %s at %s",service,host);
      if ((cp=strrchr(tmp,':'))) *cp=0;
      if (client) {
	errno=0;
	message(prg,'F',tmp);
      } else {
	strcpy(redirect,tmp);
        if (needsFree) free(service);
	return(-1);
      }
    }

    /* send constant header lines */
    snprintf(MAXS(tmp),"FROM %s",user);
    sendheader(sockfd,tmp);
    snprintf(MAXS(tmp),"TO %s",recipient);
    sock_putline(sockfd,tmp);

    /* is there a forward set? */
    if (check_forward(sockfd,recipient,host,redirect)) {
      
      /* close current connection */
      sock_putline(sockfd,"QUIT");
      getreply(sockfd);
      shutdown(sockfd,2);
      continue;
    }

    /* sendfile connection? */
    if (str_eq(type,"file")) {

      if (verbose) message(prg,'I',"testing remote server");
      
      /* test if server can receive files */
      sock_putline(sockfd,"FILE test");
      if (check_forward(sockfd,recipient,host,redirect)) {
      
	/* close current connection */
	sock_putline(sockfd,"QUIT");
	getreply(sockfd);
	shutdown(sockfd,2);
	continue;
      }
      
    }

    /* connection is successfull */
    break;

  }

  if (hopcount>10) {
    strcpy(tmp,"maximum hopcount reached (forward loop?)");
    if (client) {
      errno=0;
      message(prg,'F',tmp);
    } else {
      strcpy(redirect,tmp);
      if (needsFree) free(service);
      return(-1);
    }
  }
  
  if (needsFree) free(service);
  return(sockfd);
}


/* 
 * finger_saft_port  - finger user@host and look for user SAFT port
 * 
 * INPUT:  user	- user login name
 * 
 * OUTPUT: host	- host to connect
 * 
 * RETURN: user saft port on success, -1 on failure
 */
char* finger_saft_port(char *user, char *host) {
  int
    sockfd;     	/* socket file descriptor */
  char
    *cp,		/* character pointer */
    *port,		/* user SAFT tcp port */
    line[MAXLEN];	/* one line of text */
  
  port=NULL;
  *line=0;
  
  /* connect to the finger port of the remote host */
  sockfd=open_connection(host,"finger");
  if (sockfd>0) {
    sock_putline(sockfd,user);
    while (sock_getline(sockfd,line)>=0) {
      str_trim(line);
      if (str_beq_nocase(line,"saftport") && (cp=strchr(line,'='))) {
	port=strdup(cp+1);
	break;
      }
    }
    shutdown(sockfd,2);
  }
  
  return(port);
}


#endif /* ENABLE_MULTIPROTOCOL */


/*
 * saft2rfc822  - SAFT URL to RFC822 mail address translation
 * 
 * INPUT:  adr	- SAFT URL
 * 
 * OUTPUT: adr  - SAFT address in RFC822 format
 * 
 * RETURN: 0 on success, -1 on failure
 */
int saft2rfc822(char *adr) {
  char 
    *cp,		/* simple char pointer */
    *user,		/* user name */
    host[MAXLEN];	/* host name */
  
  if (!str_beq_nocase(adr,"saft://")) return(-1);
  strcpy(host,adr+7);
  cp=strchr(host,'/');
  if (!cp) return(-1);
  *cp=0;
  user=cp+1;
  sprintf(adr,"%s@%s",user,host);
  return(0);
}