home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / msgd / Common / savemsg.c < prev   
Encoding:
C/C++ Source or Header  |  1992-03-09  |  3.1 KB  |  123 lines

  1. #ifndef lint
  2. static char *RCSid = "$Header: /source/users/msg/Common/RCS/savemsg.c,v 1.8 91/08/13 23:57:35 jmsellens Exp $";
  3. #endif
  4.  
  5. #if 0
  6. /*
  7.  * $Log:    savemsg.c,v $
  8.  * Revision 1.8  91/08/13  23:57:35  jmsellens
  9.  * s/eprintf/errprintf/ to avoid libuw clash
  10.  * 
  11.  * Revision 1.7  91/08/13  23:57:05  jmsellens
  12.  * realuser stuff
  13.  * 
  14.  * Revision 1.6  90/12/29  22:01:03  jmsellen
  15.  * Avoid PMBECOME, and handle DUMBSHORT machines
  16.  * 
  17.  * Revision 1.5  88/09/25  03:03:29  jmsellens
  18.  * call syserr() is pmbecome() fails
  19.  * 
  20.  * Revision 1.4  87/10/19  18:29:11  sahayman
  21.  * Use pmbecome() to become different users, since the simple
  22.  * seteuid( old_euid ) after you seteuid (someone) won't work.
  23.  * 
  24.  * Revision 1.2  87/08/06  20:17:31  sahayman
  25.  * Write /usr/tmp/msg.user file by doing a seteuid(user) first, so that
  26.  * it has a chance of working when /usr/tmp is an NFS partition.
  27.  * No longer writes ~/.msg.
  28.  * 
  29.  * Revision 1.1  87/08/06  19:06:40  sahayman
  30.  * Initial revision
  31.  * 
  32.  */
  33. #endif
  34.  
  35. #include "msg.h"
  36. #include <pwd.h>
  37. #ifdef REALUSER
  38. #include <mfcf/libuw/standard.h>
  39. #endif
  40.  
  41.  
  42. savemsg( uid, recipient, sender, msg )
  43. int uid;
  44. char *recipient, *sender, *msg;
  45. {
  46.     char fname[100];    /* lots o' room */
  47.     FILE *fp;
  48.     struct passwd *pwd;
  49. #ifdef PMBECOME
  50.     int old_euid = geteuid();
  51. #endif /* PMBECOME */
  52.  
  53.     /*
  54.      * Original code used to write into ~/.msg.  No more.
  55.      */
  56.  
  57.     /* truncate the recipient for lookup */
  58.     if ( strlen(recipient) > sizeof(((struct utmp *)NULL)->ut_name) )
  59.     recipient[sizeof(((struct utmp *)NULL)->ut_name)] = '\0';
  60.     if ((pwd = getpwnam(recipient)) == (struct passwd *) NULL) {
  61. #ifdef DUMBSHORT
  62.     /* loop through the whole passwd file looking for a
  63.        truncated match */
  64.     struct utmp *ut;    /* just for sizeof() */
  65.     setpwent();
  66.     while ( (pwd = getpwent()) != (struct passwd *) NULL ) {
  67.         if ( strncmp(recipient,pwd->pw_name,sizeof(ut->ut_name)) == 0 )
  68.         break;
  69.     }
  70.     if ( pwd == (struct passwd *) NULL )
  71.         return;
  72.     recipient = pwd->pw_name;
  73. #else
  74.     return;
  75. #endif
  76.     }
  77. #ifdef REALUSER
  78.     recipient = realuser( pwd );
  79. #endif
  80.     
  81.  
  82.     /* now, put it where it really belongs, in MSGFILE */
  83.  
  84. #ifdef PMBECOME
  85.     /*
  86.      * This seteuid() business is so that this can work
  87.      * when /usr/tmp is an NFS thingy, and root on a client
  88.      * can only creat things there owned by -2, so the old
  89.      * scheme of opening the file and then chowning it wouldn't work.
  90.      */
  91.  
  92. #ifdef old
  93.     if ( seteuid( pwd->pw_uid ) == -1 ) {
  94.     errprintf("savemsg: Cannot seteuid(%d)", pwd->pw_uid);
  95. #else
  96.     if ( pmbecome( pwd->pw_uid ) == -1 ) {
  97.     errprintf("savemsg: Cannot pmbecome(%d): %s", pwd->pw_uid, syserr());
  98. #endif
  99.  
  100.     /*
  101.      * Keep going, it might work...
  102.      */
  103.     }
  104. #endif /* PMBECOME */
  105.  
  106.     strcpy( fname, MSGFILE );
  107.     strcat( fname, recipient );
  108.  
  109.     (void) unlink( fname );    /* in case it already exists */
  110.  
  111.     if ( (fp = fopen( fname, "w" ) ) != NULL ) {
  112.     (void) fchmod( fileno(fp), 0600 );
  113.     (void) fchown( fileno(fp), pwd->pw_uid, -1 );
  114.     fprintf( fp, "%s: %s\n", sender, msg );
  115.     (void) fclose( fp );
  116.     }
  117.  
  118. #ifdef PMBECOME
  119.     if ( pmbecome(0) == -1 )
  120.     errprintf("savemsg: Cannot pmbecome(0): %s", syserr());
  121. #endif /* PMBECOME */
  122. }
  123.