973edf
diff -up at-3.1.13/at.c.pam at-3.1.13/at.c
973edf
--- at-3.1.13/at.c.pam	2012-04-19 16:50:57.491000001 +0200
973edf
+++ at-3.1.13/at.c	2012-04-19 16:50:57.505000001 +0200
973edf
@@ -141,18 +141,13 @@ sigc(int signo)
973edf
 /* If the user presses ^C, remove the spool file and exit 
973edf
  */
973edf
     if (fcreated) {
973edf
-	/*
973edf
         PRIV_START
973edf
-
973edf
+        /*
973edf
         We need the unprivileged uid here since the file is owned by the real
973edf
         (not effective) uid.
973edf
         */
973edf
-        setregid(real_gid, effective_gid);
973edf
-	    unlink(atfile);
973edf
-        setregid(effective_gid, real_gid);
973edf
-        /*
973edf
+	unlink(atfile);
973edf
 	PRIV_END
973edf
-        */
973edf
     }
973edf
     exit(EXIT_FAILURE);
973edf
 }
973edf
@@ -318,26 +313,19 @@ writefile(time_t runtimer, char queue)
973edf
 	 * bit.  Yes, this is a kluge.
973edf
 	 */
973edf
 	cmask = umask(S_IRUSR | S_IWUSR | S_IXUSR);
973edf
-        seteuid(real_uid);
973edf
+       if ((seteuid(effective_uid)) < 0)
973edf
+	    perr("Error in seteuid: %s", errno);
973edf
 	if ((fd = open(atfile, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, S_IRUSR)) == -1)
973edf
 	    perr("Cannot create atjob file %.500s", atfile);
973edf
-        seteuid(effective_uid);
973edf
 
973edf
 	if ((fd2 = dup(fd)) < 0)
973edf
 	    perr("Error in dup() of job file");
973edf
 
973edf
-        /*
973edf
 	if (fchown(fd2, real_uid, real_gid) != 0)
973edf
-	    perr("Cannot give away file");
973edf
-        */
973edf
+	    perr("Cannot give real_uid and real_gid the file");
973edf
 
973edf
     PRIV_END
973edf
 
973edf
-    /* We no longer need suid root; now we just need to be able to write
973edf
-     * to the directory, if necessary.
973edf
-     */
973edf
-
973edf
-    REDUCE_PRIV(daemon_uid, daemon_gid)
973edf
     /* We've successfully created the file; let's set the flag so it 
973edf
      * gets removed in case of an interrupt or error.
973edf
      */
973edf
@@ -661,7 +649,7 @@ process_jobs(int argc, char **argv, int
973edf
                     We need the unprivileged uid here since the file is owned by the real
973edf
                     (not effective) uid.
973edf
                     */
973edf
-                    setregid(real_gid, effective_gid);
973edf
+		    PRIV_START
973edf
 
973edf
 		    if (queue == '=') {
973edf
 			fprintf(stderr, "Warning: deleting running job\n");
973edf
@@ -670,8 +658,8 @@ process_jobs(int argc, char **argv, int
973edf
 			perr("Cannot unlink %.500s", dirent->d_name);
973edf
 			rc = EXIT_FAILURE;
973edf
 		    }
973edf
+		    PRIV_END
973edf
 
973edf
-                    setregid(effective_gid, real_gid);
973edf
 		    done = 1;
973edf
 
973edf
 		    break;
973edf
@@ -681,7 +669,7 @@ process_jobs(int argc, char **argv, int
973edf
 			FILE *fp;
973edf
 			int ch;
973edf
 
973edf
-			setregid(real_gid, effective_gid);
973edf
+			PRIV_START
973edf
 			fp = fopen(dirent->d_name, "r");
973edf
 
973edf
 			if (fp) {
973edf
@@ -694,7 +682,7 @@ process_jobs(int argc, char **argv, int
973edf
 			    perr("Cannot open %.500s", dirent->d_name);
973edf
 			    rc = EXIT_FAILURE;
973edf
 			}
973edf
-			setregid(effective_gid, real_gid);
973edf
+			PRIV_END
973edf
 		    }
973edf
 		    break;
973edf
 
973edf
diff -up at-3.1.13/atd.c.pam at-3.1.13/atd.c
973edf
--- at-3.1.13/atd.c.pam	2012-04-19 16:50:57.498000001 +0200
973edf
+++ at-3.1.13/atd.c	2012-04-19 16:52:37.209000138 +0200
973edf
@@ -111,7 +111,7 @@ static int run_as_daemon = 0;
973edf
 
973edf
 static volatile sig_atomic_t term_signal = 0;
973edf
 
973edf
-#ifdef HAVE_PAM
973edf
+#ifdef WITH_PAM
973edf
 #include <security/pam_appl.h>
973edf
 
973edf
 static pam_handle_t *pamh = NULL;
973edf
@@ -120,15 +120,7 @@ static const struct pam_conv conv = {
973edf
 	NULL
973edf
 };
973edf
 
973edf
-#define PAM_FAIL_CHECK if (retcode != PAM_SUCCESS) { \
973edf
-	fprintf(stderr,"\n%s\n",pam_strerror(pamh, retcode)); \
973edf
-	syslog(LOG_ERR,"%s",pam_strerror(pamh, retcode)); \
973edf
-	pam_end(pamh, retcode); exit(1); \
973edf
-    }
973edf
-#define PAM_END { retcode = pam_close_session(pamh,0); \
973edf
-		pam_end(pamh,retcode); }
973edf
-
973edf
-#endif /* HAVE_PAM */
973edf
+#endif /* WITH_PAM */
973edf
 
973edf
 /* Signal handlers */
973edf
 RETSIGTYPE 
973edf
@@ -235,7 +227,7 @@ run_file(const char *filename, uid_t uid
973edf
     char fmt[64];
973edf
     unsigned long jobno;
973edf
     int rc;
973edf
-#ifdef HAVE_PAM
973edf
+#ifdef WITH_PAM
973edf
     int retcode;
973edf
 #endif
973edf
 
973edf
@@ -395,17 +387,11 @@ run_file(const char *filename, uid_t uid
973edf
     fstat(fd_out, &buf;;
973edf
     size = buf.st_size;
973edf
 
973edf
-#ifdef HAVE_PAM
973edf
-    PRIV_START
973edf
-    retcode = pam_start("atd", pentry->pw_name, &conv, &pamh);
973edf
-    PAM_FAIL_CHECK;
973edf
-    retcode = pam_acct_mgmt(pamh, PAM_SILENT);
973edf
-    PAM_FAIL_CHECK;
973edf
-    retcode = pam_open_session(pamh, PAM_SILENT);
973edf
-    PAM_FAIL_CHECK;
973edf
-    retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED | PAM_SILENT);
973edf
-    PAM_FAIL_CHECK;
973edf
-    PRIV_END
973edf
+#ifdef WITH_PAM
973edf
+    AT_START_PAM;
973edf
+    AT_OPEN_PAM_SESSION;
973edf
+    closelog(); 
973edf
+    openlog("atd", LOG_PID, LOG_ATD);
973edf
 #endif
973edf
 
973edf
     close(STDIN_FILENO);
973edf
@@ -419,7 +405,14 @@ run_file(const char *filename, uid_t uid
973edf
     else if (pid == 0) {
973edf
 	char *nul = NULL;
973edf
 	char **nenvp = &nul;
973edf
+	char **pam_envp=0L;
973edf
 
973edf
+	PRIV_START
973edf
+#ifdef WITH_PAM
973edf
+	pam_envp = pam_getenvlist(pamh);
973edf
+	if ( ( pam_envp != 0L ) && (pam_envp[0] != 0L) )
973edf
+		nenvp = pam_envp;
973edf
+#endif
973edf
 	/* Set up things for the child; we want standard input from the
973edf
 	 * input file, and standard output and error sent to our output file.
973edf
 	 */
973edf
@@ -438,8 +431,6 @@ run_file(const char *filename, uid_t uid
973edf
 	close(fd_in);
973edf
 	close(fd_out);
973edf
 
973edf
-	PRIV_START
973edf
-
973edf
 	    nice((tolower((int) queue) - 'a' + 1) * 2);
973edf
 
973edf
 	    if (initgroups(pentry->pw_name, pentry->pw_gid))
973edf
@@ -458,7 +449,16 @@ run_file(const char *filename, uid_t uid
973edf
 
973edf
 	    if (execle("/bin/sh", "sh", (char *) NULL, nenvp) != 0)
973edf
 		perr("Exec failed for /bin/sh");
973edf
-
973edf
+#ifdef  WITH_PAM
973edf
+	    if ( ( nenvp != &nul ) && (pam_envp != 0L)  && (*pam_envp != 0L))
973edf
+	    {
973edf
+		for( nenvp = pam_envp; *nenvp != 0L; nenvp++)
973edf
+			free(*nenvp);
973edf
+		free( pam_envp );
973edf
+		nenvp = &nul;
973edf
+		pam_envp=0L;
973edf
+	    }
973edf
+#endif
973edf
 	PRIV_END
973edf
     }
973edf
     /* We're the parent.  Let's wait.
973edf
@@ -471,14 +471,6 @@ run_file(const char *filename, uid_t uid
973edf
      */
973edf
     waitpid(pid, (int *) NULL, 0);
973edf
 
973edf
-#ifdef HAVE_PAM
973edf
-    PRIV_START
973edf
-	pam_setcred(pamh, PAM_DELETE_CRED | PAM_SILENT);
973edf
-	retcode = pam_close_session(pamh, PAM_SILENT);
973edf
-	pam_end(pamh, retcode);
973edf
-    PRIV_END
973edf
-#endif
973edf
-
973edf
     /* Send mail.  Unlink the output file after opening it, so it
973edf
      * doesn't hang around after the run.
973edf
      */
973edf
@@ -509,8 +501,20 @@ run_file(const char *filename, uid_t uid
973edf
     unlink(newname);
973edf
     free(newname);
973edf
 
973edf
+#ifdef ATD_MAIL_PROGRAM
973edf
     if (((send_mail != -1) && (buf.st_size != size)) || (send_mail == 1)) {
973edf
+       int mail_pid = -1;
973edf
+#ifdef WITH_PAM
973edf
+       AT_START_PAM;
973edf
+       AT_OPEN_PAM_SESSION;
973edf
+       closelog();
973edf
+       openlog("atd", LOG_PID, LOG_ATD);
973edf
+#endif
973edf
+
973edf
+   mail_pid = fork();
973edf
 
973edf
+   if ( mail_pid == 0 )
973edf
+   {
973edf
 	PRIV_START
973edf
 
973edf
 	    if (initgroups(pentry->pw_name, pentry->pw_gid))
973edf
@@ -535,7 +539,21 @@ run_file(const char *filename, uid_t uid
973edf
 	    perr("Exec failed for mail command");
973edf
 
973edf
 	PRIV_END
973edf
+   }
973edf
+   else if ( mail_pid == -1 ) {
973edf
+           perr("fork of mailer failed");
973edf
+   }
973edf
+   else {
973edf
+           /* Parent */
973edf
+           waitpid(mail_pid, (int *) NULL, 0);
973edf
+   }
973edf
+#ifdef WITH_PAM
973edf
+   AT_CLOSE_PAM;
973edf
+   closelog();
973edf
+   openlog("atd", LOG_PID, LOG_ATD);
973edf
+#endif
973edf
     }
973edf
+#endif
973edf
     exit(EXIT_SUCCESS);
973edf
 }
973edf
 
973edf
diff -up at-3.1.13/config.h.in.pam at-3.1.13/config.h.in
973edf
--- at-3.1.13/config.h.in.pam	2011-06-25 14:43:14.000000000 +0200
973edf
+++ at-3.1.13/config.h.in	2012-04-19 16:50:57.506000001 +0200
973edf
@@ -68,8 +68,8 @@
973edf
 /* Define to 1 if you have the <nlist.h> header file. */
973edf
 #undef HAVE_NLIST_H
973edf
 
973edf
-/* Define to 1 for PAM support */
973edf
-#undef HAVE_PAM
973edf
+/* Define if you are building with_pam */
973edf
+#undef WITH_PAM
973edf
 
973edf
 /* Define to 1 if you have the `pstat_getdynamic' function. */
973edf
 #undef HAVE_PSTAT_GETDYNAMIC
973edf
diff -up at-3.1.13/configure.ac.pam at-3.1.13/configure.ac
973edf
--- at-3.1.13/configure.ac.pam	2011-06-25 14:43:14.000000000 +0200
973edf
+++ at-3.1.13/configure.ac	2012-04-19 16:50:57.506000001 +0200
973edf
@@ -84,7 +84,7 @@ AC_FUNC_GETLOADAVG
973edf
 AC_CHECK_FUNCS(getcwd mktime strftime setreuid setresuid sigaction waitpid)
973edf
 AC_CHECK_HEADERS(security/pam_appl.h, [
973edf
   PAMLIB="-lpam"
973edf
-  AC_DEFINE(HAVE_PAM, 1, [Define to 1 for PAM support])
973edf
+  AC_DEFINE(WITH_PAM, 1, [Define to 1 for PAM support])
973edf
 ])
973edf
 
973edf
 dnl Checking for programs
973edf
@@ -238,6 +238,13 @@ AC_ARG_WITH(daemon_username,
973edf
 )
973edf
 AC_SUBST(DAEMON_USERNAME)
973edf
 
973edf
+AC_ARG_WITH(pam,
973edf
+[ --with-pam            Define to enable pam support ],
973edf
+AC_DEFINE(WITH_PAM),
973edf
+)
973edf
+AC_CHECK_LIB(pam, pam_start, PAMLIB='-lpam -lpam_misc')
973edf
+AC_SUBST(PAMLIB)
973edf
+
973edf
 AC_MSG_CHECKING(groupname to run under)
973edf
 AC_ARG_WITH(daemon_groupname,
973edf
 [ --with-daemon_groupname=DAEMON_GROUPNAME	Groupname to run under (default daemon) ],
973edf
diff -up at-3.1.13/perm.c.pam at-3.1.13/perm.c
973edf
--- at-3.1.13/perm.c.pam	2011-06-25 14:43:14.000000000 +0200
973edf
+++ at-3.1.13/perm.c	2012-04-19 16:53:09.192001742 +0200
973edf
@@ -51,6 +51,14 @@
973edf
 #define PRIV_END while(0)
973edf
 #endif
973edf
 
973edf
+#ifdef WITH_PAM
973edf
+#include <security/pam_appl.h>
973edf
+static pam_handle_t *pamh = NULL;
973edf
+static const struct pam_conv conv = {
973edf
+       NULL
973edf
+};
973edf
+#endif
973edf
+
973edf
 /* Structures and unions */
973edf
 
973edf
 
973edf
@@ -108,18 +116,45 @@ user_in_file(const char *path, const cha
973edf
 int
973edf
 check_permission()
973edf
 {
973edf
-  uid_t uid = geteuid();
973edf
+  uid_t euid = geteuid(), uid=getuid(), egid=getegid(), gid=getgid();
973edf
   struct passwd *pentry;
973edf
   int    allow = 0, deny = 1;
973edf
 
973edf
-  if (uid == 0)
973edf
+  int    retcode = 0;
973edf
+  if (euid == 0)
973edf
     return 1;
973edf
 
973edf
-  if ((pentry = getpwuid(uid)) == NULL) {
973edf
+  if ((pentry = getpwuid(euid)) == NULL) {
973edf
     perror("Cannot access user database");
973edf
     exit(EXIT_FAILURE);
973edf
   }
973edf
 
973edf
+#ifdef  WITH_PAM
973edf
+/*
973edf
+ *  We must check if the atd daemon userid will be allowed to gain the job owner user's
973edf
+ *  credentials with PAM . If not, the user has been denied at(1) usage, eg. with pam_access.
973edf
+ */
973edf
+  if (setreuid(daemon_uid, daemon_uid) != 0) {
973edf
+      fprintf(stderr, "cannot set egid: %s", strerror(errno));
973edf
+      exit(1);
973edf
+  }
973edf
+  if (setregid(daemon_gid, daemon_gid) != 0) {
973edf
+      fprintf(stderr, "cannot set euid: %s", strerror(errno));
973edf
+      exit(1);
973edf
+  }
973edf
+
973edf
+    AT_START_PAM;
973edf
+    AT_CLOSE_PAM;
973edf
+    if (setregid(gid,egid) != 0) {
973edf
+        fprintf(stderr, "cannot set egid: %s", strerror(errno));
973edf
+        exit(1);
973edf
+    }
973edf
+    if (setreuid(uid,euid) != 0) {
973edf
+        fprintf(stderr, "cannot set euid: %s", strerror(errno));
973edf
+        exit(1);
973edf
+    }
973edf
+#endif
973edf
+
973edf
   allow = user_in_file(ETCDIR "/at.allow", pentry->pw_name);
973edf
   if (allow==0 || allow==1)
973edf
     return allow;
973edf
diff -up at-3.1.13/privs.h.pam at-3.1.13/privs.h
973edf
--- at-3.1.13/privs.h.pam	2011-06-25 14:43:14.000000000 +0200
973edf
+++ at-3.1.13/privs.h	2012-04-19 16:53:46.296016675 +0200
973edf
@@ -144,3 +144,63 @@ extern gid_t real_gid, effective_gid, da
973edf
 #error "Cannot implement user ID swapping without setreuid or setresuid"
973edf
 #endif
973edf
 #endif
973edf
+
973edf
+#ifdef WITH_PAM
973edf
+/* PAM failed after session was open.  */
973edf
+#define PAM_SESSION_FAIL if (retcode != PAM_SUCCESS) \
973edf
+       pam_close_session(pamh,PAM_SILENT);
973edf
+
973edf
+/* syslog will be logging error messages */
973edf
+#ifdef HAVE_UNISTD_H
973edf
+#include <syslog.h>
973edf
+#endif
973edf
+
973edf
+/* PAM fail even before opening the session */
973edf
+#define PAM_FAIL_CHECK \
973edf
+       do { if (retcode != PAM_SUCCESS) { \
973edf
+               fprintf(stderr,"PAM failure: %s\n",pam_strerror(pamh, retcode)); \
973edf
+               syslog(LOG_ERR,"%s",pam_strerror(pamh, retcode)); \
973edf
+               if (pamh) \
973edf
+                   pam_end(pamh, retcode); \
973edf
+               if (setregid(getgid(),getegid()) != 0) { \
973edf
+                   fprintf(stderr, "cannot set egid: %s", strerror(errno)); \
973edf
+                   exit(1); \
973edf
+               } \
973edf
+               if (setreuid(getuid(),geteuid()) != 0) { \
973edf
+                   fprintf(stderr, "cannot set euid: %s", strerror(errno)); \
973edf
+                   exit(1); \
973edf
+               } \
973edf
+               exit(1); \
973edf
+           } \
973edf
+       } while (0) \
973edf
+
973edf
+static int pam_session_opened = 0;      //global for open session
973edf
+
973edf
+#define AT_START_PAM { \
973edf
+        retcode = pam_start("atd", pentry->pw_name, &conv, &pamh); \
973edf
+        PAM_FAIL_CHECK; \
973edf
+        retcode = pam_set_item(pamh, PAM_TTY, "atd"); \
973edf
+        PAM_FAIL_CHECK; \
973edf
+        retcode = pam_acct_mgmt(pamh, PAM_SILENT); \
973edf
+        PAM_FAIL_CHECK; \
973edf
+} 
973edf
+
973edf
+#define AT_OPEN_PAM_SESSION { \
973edf
+        retcode = pam_open_session(pamh, PAM_SILENT); \
973edf
+        PAM_FAIL_CHECK; \
973edf
+        retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED | PAM_SILENT); \
973edf
+        PAM_FAIL_CHECK; \
973edf
+        if (retcode == PAM_SUCCESS) \
973edf
+                pam_session_opened = 1; \
973edf
+}
973edf
+
973edf
+#define AT_CLOSE_PAM { \
973edf
+        if (pam_session_opened != 0) { \
973edf
+                pam_setcred(pamh, PAM_DELETE_CRED | PAM_SILENT); \
973edf
+                pam_close_session(pamh, PAM_SILENT); \
973edf
+        } \
973edf
+        pam_end(pamh, PAM_SUCCESS); \
973edf
+}
973edf
+
973edf
+#endif
973edf
+