home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume36 / msend / part01 / misc.c < prev    next >
C/C++ Source or Header  |  1993-03-22  |  3KB  |  155 lines

  1. /* misc.c:
  2.  *
  3.  * miscellaneous functions
  4.  *
  5.  * (c) Copyright 1988, 1989, 1990 Jim Frost.  All Rights Reserved.  Please see
  6.  * the accompanying file "Copyright" for more information.
  7.  */
  8.  
  9. #include "Copyright"
  10. #include "config.h"
  11. #include "msend.h"
  12. #ifdef M_SYSV
  13. #include <unistd.h>                     /* Unix Standard definitions */
  14. #endif
  15. #if defined(_AIX) && defined(USE_LOCKF)
  16. #include <sys/lockf.h>
  17. #endif
  18.  
  19. void error();
  20.  
  21. /* easy way to build error messages
  22.  */
  23.  
  24. void blderr(ri,errno,msg)
  25. struct rimsg *ri;
  26. int    errno;
  27. char   *msg;
  28. { ri->h.errno= errno;
  29.   ri->h.msglen= strlen(msg);
  30.   strcpy(ri->msg,msg);
  31. }
  32.  
  33. void die(i)
  34. int i;
  35. { error("md terminated");
  36.   exit(i);
  37. }
  38.  
  39. /* when we have a problem, call this
  40.  */
  41.  
  42. void error(s)
  43. char *s;
  44. { int  uid;
  45.   long t;
  46.   char when[30];
  47.   FILE *f;
  48.  
  49.   time(&t);
  50.   strcpy(when,ctime(&t));
  51.   when[strlen(when)-1]= '\0';
  52.   if (getuid() == ROOTUID) {
  53.     uid= geteuid();
  54.     seteuid(ROOTUID);
  55.   }
  56.   f= fopen(LOGFILE,"a");
  57.   if (getuid() == ROOTUID)
  58.     seteuid(uid);
  59.   if (f != NULL) {
  60. #ifndef USE_LOCKF
  61.     flock(fileno(f),LOCK_EX);
  62. #else
  63.     lockf(fileno(f),F_TLOCK, 0);
  64. #endif
  65.     fprintf(f,"%s: %s\n",when,s);
  66. #ifndef USE_LOCKF
  67.     flock(fileno(f),LOCK_UN);
  68. #else
  69.     lockf(fileno(f),F_ULOCK, 0);
  70. #endif
  71.     fclose(f);
  72.   }
  73.   else
  74.     printf("%s: %s\n",when,s);
  75. }
  76.  
  77. /* this returns the port number to use for communication
  78.  */
  79.  
  80. int portnum()
  81. { struct servent *se;
  82.   int    p;
  83.  
  84.   /* if possible, return the port number in /etc/services; if not,
  85.    * use hardcoded default
  86.    */
  87.  
  88.   if ((se= getservbyname("msend","tcp")) == NULL)
  89.     p= PORTNUM;
  90.   else
  91.     p= ntohs(se->s_port);
  92.  
  93.   /* oops, someone forgot to make me setuid
  94.    */
  95.  
  96.   if ((p < 1024) && geteuid()) {
  97.     printf("portnum: not setuid\n");
  98.     exit(1);
  99.   }
  100.   return(p);
  101. }
  102.  
  103. /* find the host name within an address, put it in an array, and truncate
  104.  * the address at the hostname.
  105.  */
  106.  
  107. char *striphost(addr,host)
  108. char addr[];
  109. char *host;
  110. { int a;
  111.  
  112.   for (a= strlen(addr); (a >= 0) && (addr[a] != '@'); a--)
  113.     ;
  114.   if (a >= 0) {
  115.     strcpy(host,&addr[a+1]);
  116.     addr[a]= '\0';
  117.     return(host);
  118.   }
  119.   host[0]= '\0';
  120.   return(NULL);
  121. }
  122.  
  123. char *gethome(user)
  124.      char *user;
  125. { struct passwd *pw;
  126.  
  127.   if (! (pw= getpwnam(user)))
  128.     return(NULL);
  129.   return(pw->pw_dir);
  130. }
  131.  
  132. int getid(user)
  133.      char *user;
  134. { struct passwd *pw;
  135.  
  136.   if (! (pw= getpwnam(user)))
  137.     return(-1);
  138.   return(pw->pw_uid);
  139. }
  140.  
  141. #ifdef NEEDS_LOCK
  142. /* if the system needs flock, put the correct locking function in here.  if
  143.  * you leave it like this it's possible that you'll get some conflict in
  144.  * writing to spool files and such, but it's not likely and it won't hurt
  145.  * anything much.
  146.  */
  147.  
  148. int flock(fd, how)
  149.      int fd, how;
  150. {
  151.   return(0);
  152. }
  153. #endif
  154.  
  155.