/* mbxchown.c
*
* Change ownership of mail box.  Due to mail not running setuid root,
* it is not able to chown a file in /usr/spool/mail when it creates one.
* This program runs as root and is used to have this done.
*
* The program will only set the owner of a file in /usr/mail to the
* user whose name matches the file name.  The file must be a regular
* file, cannot be a symbolic link, and must be mode 0660.
*
* Usage: mbxchown mbox_path_name
*/

#ident	"$Header: mbxchown.c 1.2 89/04/04 $"

/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

#include <stdio.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>

#define streq(s1, s2)	(strcmp((s1), (s2)) == 0)
#define MYNAME		"/usr/lib/mbxchown"

main(argc, argv)
	int argc;
	char *argv[];
{
	uid_t mail_uid;		/* user id for user given */
	gid_t mail_gid;		/* group id for mail */
	char path[MAXPATHLEN];
	char *p, *p1, *p2, *p3;
	struct passwd *pwd;
	struct group *grp;
	struct stat sb;

	/* Allow only two args, prog name and mbox name. */

	if (argc != 2)
		exit(1);

	/* Must be invoked as name we expect. */
	
	if (!streq(argv[0], MYNAME))
		exit(1);

	if (chdir("/usr/mail") != 0)
		exit(1);

	/*
	 * Set up pointer to 3 expected path components.
	 *   p1 -- usr
	 *   p2 -- mail
	 *   p3 -- owner
	 */

	strcpy(path, argv[1]);

	p = path;
	if (*p != '/')
		exit(1);
	p++;
	if (*p == '\0')
		exit(1);
	p1 = p;				/* should be "usr"	*/
	while (*p && (*p != '/'))	/* scan to "/"		*/
		p++;
	if (*p == '\0')
		exit(1);
	*p = '\0';
	p++;
	p2 = p;				/* should be "mail"	*/
	if (*p == '\0')
		exit(1);
	while (*p && (*p != '/'))	/* scan to "/"		*/
		p++;
	if (*p == '\0')
		exit(1);
	*p = '\0';
	p++;
	if (*p == '\0')
		exit(1);
	p3 = p;				/* should be user name */
	while (*p && (*p != '/'))	/* scan to "/"		*/
		p++;
	if (*p)
		exit(1);
	
	if (!streq(p1, "usr"))
		exit(1);
	if (!streq(p2, "mail"))
		exit(1);

	if ((pwd = getpwnam(p3)) == NULL)
		exit(1);
	mail_uid = pwd->pw_uid;

	if ((grp = getgrnam("mail")) == NULL)
		exit(1);
	mail_gid = grp->gr_gid;

	/*
	 * Now we have found user and group ID for the file. 
	 * We have also verified that user name exists in the password
	 * file.  Next step is to verify that the file exists in the
	 * /usr/mail directory, is not a symbolic link, and has the
	 * expected mode.
	 */

	if (lstat(p3, &sb) != 0)
		exit(1);

	if ((sb.st_mode & S_IFMT) != S_IFREG)
		exit(1);

	if ((sb.st_mode & ~S_IFMT) != 0660)
		exit(1);

	if (chown(p3, mail_uid, mail_gid) == 0)
		exit(0);
	 else
		exit(1);
}
