#!/bin/sh
# $Id: sm.setup.sh.in,v 1.71 2007/11/18 16:44:27 ca Exp $
#
# Copyright (c) 2004-2006 Sendmail, Inc. and its suppliers.
#	All rights reserved.
#
# By using this file, you agree to the terms and conditions set
# forth in the LICENSE file which can be found at the top level of
# the sendmail distribution.
#

# -------------------------------------------------------
# Simple script to setup a runtime system for meta1
# This is used by make install, it is converted to sm.setup.sh
# during configuration to replace path names etc.
# -------------------------------------------------------

# queue directory
MTAQD=${MTAQDIR:-/var/spool/meta1}
D_MTAQD=${DESTDIR}${MTAQD}
# configuration directory
MTAETC=${MTACONFDIR:-/usr/local/etc/meta1}
D_MTAETC=${DESTDIR}${MTAETC}
# log directory (unless syslog(3) is used)
LOGDIR=${MTALOGDIR:-.}
D_LOGDIR=${DESTDIR}${LOGDIR}
MCP_LOG=""
if test ${LOGDIR} != "."
then
  MCP_LOG="-L ${LOGDIR}"
else
  D_LOGDIR=${DESTDIR}${MTAQD}
fi

# users and groups
# SMTP Server
MTAS=${MTAS:-meta1s}
# SMTP Client
MTAC=${MTAC:-meta1c}
# QMGR
MTAQ=${MTAQ:-meta1q}
# address resolver (misc)
MTAM=${MTAM:-meta1m}
# generic (configuration etc)
MTA=${MTA:-meta1}
# possible groups for logfiles (rw)
LGS=${MTALG:-"operator sysadmin root cannotfindgroupforlogfile"}
# path to build directory
MTAOBJ=`pwd`
S=${MTA_OBJ:-${MTAOBJ}}
# base name of configuration file
MTACNFB=${MTACONF:-meta1.conf}

RUNASPRG=${MTARUNAS:-/usr/local/bin/runas}
if ${RUNASPRG} ${MTAM} /usr/local/sbin/t-hostname
then
   RUNAS="${RUNASPRG} ${MTAM}"
else
   RUNAS=""
fi

# hostname
HOSTNAME=`${RUNAS} /usr/local/sbin/t-hostname -l -V 2>/dev/null`
if test "X${HOSTNAME}" = "X"
then
  HOSTNAME="unconfigured.host.name"
  echo "Warning: cannot determine host name"
fi
MTACGID=`${RUNAS} /usr/local/sbin/t-getgroup ${MTAC} 2>/dev/null`
if test "X${MTACGID}" = "X"
then
  echo "Error: cannot find gid for ${MTAC}"
  exit 1
fi

# get a group for root to use for logfiles
for LG in ${LGS}
do
  if ${RUNAS} /usr/local/sbin/t-getgroup ${LG} 2>/dev/null
  then
    break
  fi
done
if test "${LG}" = "cannotfindgroupforlogfile"
then
  echo "Cannot find group for logfile"
  exit 1
fi

#
SCRIPT=sm.awk
if test -f ${SCRIPT}
then
  echo "${SCRIPT} exists; please remove"
  exit 1
fi

# these values will be replaced by configure
srcdir=.
top_srcdir=..
prefix=
exec_prefix=${prefix}
bindir=${exec_prefix}/bin
sbindir=${exec_prefix}/sbin
libexecdir=${exec_prefix}/libexec
datadir=${prefix}/share
sysconfdir=${prefix}/etc
sharedstatedir=${prefix}/com
localstatedir=${prefix}/var
libdir=${exec_prefix}/lib
infodir=${prefix}/info
mandir=${prefix}/man
includedir=${prefix}/include
oldincludedir=/usr/include
pkgdatadir=${datadir}/meta1
pkglibdir=${libdir}/meta1
pkgincludedir=${includedir}/meta1
top_builddir=.
# ----

# some utilities
CHOWN=${MTACHOWN:-chown}
CHMOD=${MTACHMOD:-chmod}
MKDIR=${MTAMKDIR:-mkdir}
TOUCH=${MTATOUCH:-touch}
AWK=${MTAAWK:-awk}
SED=${MTASED:-sed}
CAT=${MTACAT:-cat}

# -- try to find name server
NS=""
RSLVC=/etc/resolv.conf
if test -s ${RSLVC}
then
NS=`grep '^nameserver' ${RSLVC} | head -1 | ${SED} -e 's/nameserver[	 ]*\([0-9][0-9\.]*\).*$/\1/'`
fi
if test -z "${NS}"
then
  echo "$0: no nameserver found"
  NS=127.0.0.1
fi

# create base directory and change to it
test -d ${D_MTAQD} || ${MKDIR} -p ${D_MTAQD}
if cd ${D_MTAQD}; then
  :
else
  echo "cannot chdir to ${D_MTAQD}"
  exit 1
fi
if ${CHMOD} 0755 .; then
  :
else
  echo "cannot chmod ${D_MTAQD}"
  exit 1
fi

# create queue directories (for CDB)
for i in 0 1 2 3 4 5 6 7 8 9 A B C D E F
do
  test -d ${i} || ${MKDIR} ${i}
  ${CHOWN} ${MTAS}:${MTAQ} ${i} || exit 3
  ${CHMOD} 0771 ${i}
done

# deferred envelope queue
DEFEDB=defedb
test -d ${DEFEDB} || ${MKDIR} ${DEFEDB} || exit 4
${CHOWN} ${MTAQ}:${MTAQ} ${DEFEDB} || exit 5
${CHMOD} 0700 ${DEFEDB} || exit 6

# create directories for sockets etc
for i in qmsmtps qmsmtpc qmsmar smtps ibdb ibdb/ibdb
do
  test -d ${i} || ${MKDIR} ${i}
  ${CHMOD} 02770 ${i} || exit 1
done
${CHMOD} 0700 ibdb ibdb/ibdb || exit 1
${CHMOD} 0750 smtps || exit 1

${CHOWN} ${MTAQ}:${MTAM} qmsmar || exit 1
${CHOWN} ${MTAQ}:${MTAC} qmsmtpc || exit 1
${CHOWN} ${MTAQ}:${MTAS} qmsmtps || exit 1
${CHOWN} ${MTAS}:${MTAS} smtps || exit 1
${CHOWN} ${MTAQ}:${MTAQ} ibdb || exit 1
${CHOWN} ${MTAQ}:${MTAQ} ibdb/ibdb || exit 1

# --
# create configuration directory
test -d ${D_MTAETC} || ${MKDIR} -p ${D_MTAETC}
if test ! -d ${D_MTAETC}; then
  echo "failed to mkdir ${D_MTAETC}"
  exit 1
fi

# create "mailertable"
D_MT=${D_MTAETC}/mt
if test ! -f ${D_MT}
then
   ${CAT} > ${D_MT} <<EOF
local.host	lmtp:
${HOSTNAME}	lmtp:
EOF

${CHOWN} ${MTAM}:${MTAM} ${D_MT} || exit 1
fi

MTACNF=${MTAETC}/${MTACNFB}
D_MTACNF=${D_MTAETC}/${MTACNFB}
if test -s ${D_MTACNF}
then
  MTACONFPRG=/usr/local/bin/smconf
  if test -x ${MTACONFPRG}
  then
    ${MTACONFPRG} ${D_MTACNF} >/dev/null 2>&1
    R=$?
  else
    R=0
  fi
  echo "${D_MTACNF} exists and will not be overwritten."
  if test ${R} != 0
  then
     echo "It is not syntactically correct for this version of meta1."
  fi
  echo "Please move it out of the way or upgrade it yourself."
else
${CAT} > ${D_MTACNF} <<EOF
# -------------------------------------------------------
# Sample configuration file for meta1
# can be used by mcp, qmgr, smar, smtps, and smtpc
# module -f ${MTACNF}
# -------------------------------------------------------
# Entries are started in reverse order by mcp
# -------------------------------------------------------

# default values
# SMTPS_socket = "qmsmtps/qmsmtps";
# SMTPC_socket = "qmsmtpc/qmsmtpc";
# SMAR_socket = "qmsmar/qmsmar";
# CDB_base_directory = "";

# activate this for local delivery using procmail and LMTP
# lmtp {
# 	listen_socket { type=unix;
#		path = lmtpsock; umask = 007;
# 		user = root; group = ${MTAC}; }
# 	start_action = nostartaccept;
# 	min_processes = 1;
# 	max_processes = 8;
# 	user = root;
# 	path = /usr/local/bin/procmail;
# 	arguments = "procmail -z+";
# }

smtps {
  log_level = 11;
  log { facility=mail; ident="smtps"; }
  CDB_gid = ${MTACGID};
  wait_for_server = 4;
  listen_socket { type=inet; port = 25; }
  start_action = pass;
  pass_fd_socket = smtps/smtpsfd;
  user = ${MTAS};
  path = "${libexecdir}/smtps";
  arguments = "smtps -f ${MTACNF}";
}

## To run two servers different names and different sockets must be used.
## Moreover, restart_dependencies for qmgr and smar must be changed too.
## Note: this does not use a configuration file and should be changed
#smtps MSA {
#	log_level = 11;
#	log { facility=mail; ident="MSA"; }
#	CDB_gid = ${MTACGID};
#	listen_socket { type=inet; port = 587; }
#	start_action = pass;
#	pass_fd_socket = smtps/msafd;
#	user = ${MTAS};
#	path = "${libexecdir}/smtps";
#	arguments = "smtps -I 1 -N MSA -f ${MTACNF}";
#}
#
#smtps MTA {
#	log_level = 11;
#	log { facility=mail; ident="MTA"; }
#	CDB_gid = ${MTACGID};
#	listen_socket { type=inet; port = 25; }
#	start_action = pass;
#	pass_fd_socket = smtps/mtafd;
#	user = ${MTAS};
#	path = "${libexecdir}/smtps";
#	arguments = "smtps -I 0 -N MTA -f ${MTACNF}";
#}

##
smtpc {
  log_level = 12;
  log { facility=mail; ident="smtpc"; }
  wait_for_server = 4;
  start_action = wait;
  user = ${MTAC};
  path = "${libexecdir}/smtpc";
  arguments = "smtpc -f ${MTACNF}";
}

# must be previous to last in the list: started after smar
qmgr {
  log_level = 12;
  log { facility=mail; ident="qmgr"; }
  wait_for_server = 4;
  wait_for_client = 3;
  start_action = wait;
  user = ${MTAQ};
  restart_dependencies = { smtps, smtpc, smar };
  path = "${libexecdir}/qmgr";
  arguments = "qmgr -f ${MTACNF}";
}

# must be last in the list: started first
smar {
  log_level = 12;
  log { facility=mail; ident="smar"; }
  nameserver = ${NS};
  start_action = wait;
  user = ${MTAM};
  restart_dependencies = { smtps, qmgr };
  path = "${libexecdir}/smar";
  arguments = "smar -f ${MTACNF}";
}
EOF
fi

${CHOWN} ${MTA}:${MTA} ${D_MTACNF} || exit 1

# create simple startup script
MCPSH=/usr/local/etc/rc.d/meta1-mcp.sh
if test ! -s ${MCPSH}
then
${CAT} > ${MCPSH} <<EOF
#!/bin/sh
# start MeTA1 via MCP

MCPPID=/var/run/mcp.pid
MCPOUT=mcp.out

start_mcp()
{
	${sbindir}/mcp ${MCP_LOG} -l -p \${MCPPID} ${MTACNF} > \${MCPOUT} 2>&1 &
}

stop_mcp()
{
	if test -s \${MCPPID}; then
		kill \`head -1 \${MCPPID}\`
	else
		echo "\$0: pid file \${MCPPID} does not exist or is empty"
	fi
}

if cd "${MTAQD}"; then
	:
else
	echo "\$0: cd ${MTAQD} failed"
	exit 1
fi

case "\$1" in
'start')
	start_mcp
	;;
'stop')
	stop_mcp
	;;
'restart')
	stop_mcp
	start_mcp
	;;
*)
	echo "Usage: \$0 { start | stop | restart }"
	exit 1
	;;
esac
exit 0
EOF
${CHMOD} 0700 ${MCPSH}
fi

LOGN=lognull.sh
if test ! -s ${LOGN}
then
${CAT} > ${LOGN} <<EOF
#!/bin/sh
for i in *.log
do
  cp /dev/null \${i}
done
EOF
${CHMOD} +x ${LOGN}
fi

${CAT} > ${SCRIPT} <<EOF
/^section=/ {NAME=\$2; next;}
/^title=/ {NAME=\$2; next;}
/^user=/ {printf("%s:%s\n",NAME,\$2); }
/ user=/ {printf("%s:%s\n",NAME,\$2); }
EOF

# try to use a scanner for configuration file
SCAN=/usr/local/sbin/tree
if test -x ${SCAN}
then
for i in `${SCAN} -e < ${D_MTACNF} | ${SED} -e 's/[	 ][	 ]*=[	 ][	 ]*/=/g' -e 's/;//'  -e 's/"//g' | ${AWK} -F= -f ${SCRIPT}`
do
  # i contains program:owner
  FILE=`echo ${i} | ${SED} 's;:.*;;'`
  D_LOGF=${D_LOGDIR}/${FILE}.log
  OWNER=`echo ${i} | ${SED} 's;.*:;;'`
  ${TOUCH} ${D_LOGF} || exit 1
  ${CHOWN} ${OWNER}:${LG} ${D_LOGF} || exit 1
done
else
  # hack
  echo "cannot find ${SCAN}"
  exit 1
fi

#
D_MF=${D_MTAETC}/Makefile
if test -f ${D_MF}
then
  echo "$0: ${D_MF} already exists; not changed"
else
${TOUCH} ${D_MTAETC}/sendusr1qmgr ${D_MTAETC}/sendusr1smar
${TOUCH} ${D_MTAETC}/sentusr1qmgr ${D_MTAETC}/sentusr1smar
PKILL=/usr/bin/pkill
test -x ${PKILL} || PKILL="echo kill"
${CAT} > ${D_MF} <<EOF
# Simple Makefile to create MeTA1 maps
MM=${bindir}/createmap
# all maps?
all: aliases.db access.db qmgr_conf.db sentusr1smar sentusr1qmgr
# or just aliases?
#all: aliases.db
aliases.db:aliases
	\${MM} -F aliases.db.new <aliases 
	mv aliases.db.new aliases.db
	chown ${MTAM} aliases.db
	chmod 664 aliases.db
	${TOUCH} sendusr1smar
access.db:access
	\${MM} -w -F access.db.new < access
	mv access.db.new access.db
	chown ${MTAM} access.db
	chmod 664 access.db
	${TOUCH} sendusr1smar
qmgr_conf.db:qmgr_conf
	\${MM} -w -F qmgr_conf.db.new < qmgr_conf
	mv qmgr_conf.db.new qmgr_conf.db
	chown ${MTAQ} qmgr_conf.db
	chmod 664 qmgr_conf.db
	${TOUCH} sendusr1qmgr

sentusr1smar:sendusr1smar
	touch sentusr1smar
	-${PKILL} -USR1 -U ${MTAM} smar
sentusr1qmgr:sendusr1qmgr
	touch sentusr1qmgr
	-${PKILL} -USR1 -U ${MTAQ} qmgr
EOF
fi

#
D_ALIASES=${D_MTAETC}/aliases
if test -f ${D_ALIASES}
then
  echo "$0: ${D_ALIASES} already exists; not changed"
else
${CAT} > ${D_ALIASES} <<EOF
# Replace user with a valid user who is responsible for this MTS
postmaster:	user
root:	user
user: local:
EOF
echo "please edit ${D_ALIASES} and run make afterwards"
fi

#
D_QMGRCONF=${D_MTAETC}/qmgr_conf
if test -f ${D_QMGRCONF}
then
  echo "$0: ${D_QMGRCONF} already exists; not changed"
else
${CAT} > ${D_QMGRCONF} <<EOF
# outgoing: local; this should match the number of lmtp processes
oci:127.0.0.255	1
ocm:127.0.0.255	8
# occ timeout (> smtpc timeout)
#octo:	600
# incoming: local limits
#icm:127.0.0.1	100
#icr:127.0.0.1	200
EOF
echo "please review ${D_QMGRCONF} and run make afterwards"
fi

# check first whether smtps/smtpc support STARTTLS?
echo "setup certs if STARTTLS should be used"

exit 0


syntax highlighted by Code2HTML, v. 0.9.1