#include "V.h"
#include <grp.h>
#include <pwd.h>
#include "sys_types.h"
/*
*   mailfile uid file...
#
# DESCRIPTION
#   Change a mail-related file's ownership.
*
*   This program is intended to be run  setuid-root.   When  run,  it
*   changes  the  owner  and  group  of  the  files to the uid on the
*   command line, and also changes the group to "mail".
*
*   The main use of this program is within email scripts, so they can
*   "give  away"  files  to  the recipients.  Everything in the email
*   system should really be owned by  "mail"  and  be  in  the  group
*   "mail".  But in reality, things are rarely set up this way.  So I
*   wrote this program to correct the problems.
*
* AUTHOR:
*   John Chambers <jc@trillian.mit.edu>
*/

char* usr = 0;
char* grp = "mail";

struct passwd * pw = 0;
struct group  * gp = 0;

int mailuid = 12;
int mailgid = 12;
extern exitstat;

main(ac,av)
	int    ac;
	char** av;
{	int    a;
	ac = Vinit(ac,av);

	usr = av[1];
	if (isdigit(usr[0])) {
		sscanf(usr,"%d",&mailuid);
		V2 "Got mailuid %d from \"%s\"",mailuid,usr D;
	} else if (pw = getpwnam(usr)) {
		mailuid = pw->pw_uid;
		V2 "Got mailuid %d for \"%s\"",mailuid,usr D;
	} else {
		V1 "### Can't get uid for \"%s\"",usr D;
	}
	if (gp = getgrnam(grp)) {
		mailgid = gp->gr_gid;
		V2 "Got mailgid %d from \"%s\"",mailuid,grp D;
	} else {
		V1 "### Can't get gid for \"%s\"",grp D;
	}
	V2 "mailuid=%d mailgid=%d.",mailuid,mailgid D;

	for (a=2; a<ac; a++) {
		V3 "chown(\"%s\",%d,%d)",av[a],mailuid,mailgid D;
		if (chown(av[a],mailuid,mailgid)) {
			V1 "### Can't chown(\"%s\",%d,%d) [Err %d=%d=%s]",av[a],mailuid,mailgid,Errinfo D;
			exitstat = errno;;
		}
	}
	Exit(exitstat);
}
