76a39a
diff -up at-3.1.20/at.c.pam at-3.1.20/at.c
76a39a
--- at-3.1.20/at.c.pam	2016-06-28 22:18:00.000000000 +0200
76a39a
+++ at-3.1.20/at.c	2016-07-01 09:44:22.251683924 +0200
76a39a
@@ -144,18 +144,13 @@ sigc(int signo)
76a39a
 /* If the user presses ^C, remove the spool file and exit 
76a39a
  */
76a39a
     if (fcreated) {
76a39a
-	/*
76a39a
         PRIV_START
76a39a
-
76a39a
+        /*
76a39a
         We need the unprivileged uid here since the file is owned by the real
76a39a
         (not effective) uid.
76a39a
         */
76a39a
-        setregid(real_gid, effective_gid);
76a39a
-	    unlink(atfile);
76a39a
-        setregid(effective_gid, real_gid);
76a39a
-        /*
76a39a
+	unlink(atfile);
76a39a
 	PRIV_END
76a39a
-        */
76a39a
     }
76a39a
     exit(EXIT_FAILURE);
76a39a
 }
76a39a
@@ -315,26 +310,19 @@ writefile(time_t runtimer, char queue)
76a39a
 	 * bit.  Yes, this is a kluge.
76a39a
 	 */
76a39a
 	cmask = umask(S_IRUSR | S_IWUSR | S_IXUSR);
76a39a
-        seteuid(real_uid);
76a39a
+       if ((seteuid(effective_uid)) < 0)
76a39a
+	    perr("Error in seteuid: %s", errno);
76a39a
 	if ((fd = open(atfile, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, S_IRUSR)) == -1)
76a39a
 	    perr("Cannot create atjob file %.500s", atfile);
76a39a
-        seteuid(effective_uid);
76a39a
 
76a39a
 	if ((fd2 = dup(fd)) < 0)
76a39a
 	    perr("Error in dup() of job file");
76a39a
 
76a39a
-        /*
76a39a
 	if (fchown(fd2, real_uid, real_gid) != 0)
76a39a
-	    perr("Cannot give away file");
76a39a
-        */
76a39a
+	    perr("Cannot give real_uid and real_gid the file");
76a39a
 
76a39a
     PRIV_END
76a39a
 
76a39a
-    /* We no longer need suid root; now we just need to be able to write
76a39a
-     * to the directory, if necessary.
76a39a
-     */
76a39a
-
76a39a
-    REDUCE_PRIV(daemon_uid, daemon_gid)
76a39a
     /* We've successfully created the file; let's set the flag so it 
76a39a
      * gets removed in case of an interrupt or error.
76a39a
      */
76a39a
@@ -673,7 +661,7 @@ process_jobs(int argc, char **argv, int
76a39a
                     We need the unprivileged uid here since the file is owned by the real
76a39a
                     (not effective) uid.
76a39a
                     */
76a39a
-                    setregid(real_gid, effective_gid);
76a39a
+		    PRIV_START
76a39a
 
76a39a
 		    if (queue == '=') {
76a39a
 			fprintf(stderr, "Warning: deleting running job\n");
76a39a
@@ -682,8 +670,8 @@ process_jobs(int argc, char **argv, int
76a39a
 			perr("Cannot unlink %.500s", dirent->d_name);
76a39a
 			rc = EXIT_FAILURE;
76a39a
 		    }
76a39a
+		    PRIV_END
76a39a
 
76a39a
-                    setregid(effective_gid, real_gid);
76a39a
 		    done = 1;
76a39a
 
76a39a
 		    break;
76a39a
@@ -693,7 +681,7 @@ process_jobs(int argc, char **argv, int
76a39a
 			FILE *fp;
76a39a
 			int ch;
76a39a
 
76a39a
-			setregid(real_gid, effective_gid);
76a39a
+			PRIV_START
76a39a
 			fp = fopen(dirent->d_name, "r");
76a39a
 
76a39a
 			if (fp) {
76a39a
@@ -706,7 +694,7 @@ process_jobs(int argc, char **argv, int
76a39a
 			    perr("Cannot open %.500s", dirent->d_name);
76a39a
 			    rc = EXIT_FAILURE;
76a39a
 			}
76a39a
-			setregid(effective_gid, real_gid);
76a39a
+			PRIV_END
76a39a
 		    }
76a39a
 		    break;
76a39a
 
76a39a
diff -up at-3.1.20/atd.c.pam at-3.1.20/atd.c
76a39a
--- at-3.1.20/atd.c.pam	2016-06-28 22:14:39.000000000 +0200
76a39a
+++ at-3.1.20/atd.c	2016-07-01 09:44:22.251683924 +0200
76a39a
@@ -91,6 +91,10 @@ int selinux_enabled = 0;
76a39a
 
76a39a
 /* Macros */
76a39a
 
76a39a
+#ifndef LOG_ATD
76a39a
+#define LOG_ATD        LOG_DAEMON
76a39a
+#endif
76a39a
+
76a39a
 #define BATCH_INTERVAL_DEFAULT 60
76a39a
 #define CHECK_INTERVAL 3600
76a39a
 
76a39a
@@ -114,7 +118,7 @@ static int run_as_daemon = 0;
76a39a
 
76a39a
 static volatile sig_atomic_t term_signal = 0;
76a39a
 
76a39a
-#ifdef HAVE_PAM
76a39a
+#ifdef WITH_PAM
76a39a
 #include <security/pam_appl.h>
76a39a
 
76a39a
 static pam_handle_t *pamh = NULL;
76a39a
@@ -123,15 +127,7 @@ static const struct pam_conv conv = {
76a39a
 	NULL
76a39a
 };
76a39a
 
76a39a
-#define PAM_FAIL_CHECK if (retcode != PAM_SUCCESS) { \
76a39a
-	fprintf(stderr,"\n%s\n",pam_strerror(pamh, retcode)); \
76a39a
-	syslog(LOG_ERR,"%s",pam_strerror(pamh, retcode)); \
76a39a
-	pam_end(pamh, retcode); exit(1); \
76a39a
-    }
76a39a
-#define PAM_END { retcode = pam_close_session(pamh,0); \
76a39a
-		pam_end(pamh,retcode); }
76a39a
-
76a39a
-#endif /* HAVE_PAM */
76a39a
+#endif /* WITH_PAM */
76a39a
 
76a39a
 /* Signal handlers */
76a39a
 RETSIGTYPE 
76a39a
@@ -292,7 +288,7 @@ run_file(const char *filename, uid_t uid
76a39a
     char fmt[64];
76a39a
     unsigned long jobno;
76a39a
     int rc;
76a39a
-#ifdef HAVE_PAM
76a39a
+#ifdef WITH_PAM
76a39a
     int retcode;
76a39a
 #endif
76a39a
 
76a39a
@@ -449,17 +445,11 @@ run_file(const char *filename, uid_t uid
76a39a
     fstat(fd_out, &buf;;
76a39a
     size = buf.st_size;
76a39a
 
76a39a
-#ifdef HAVE_PAM
76a39a
-    PRIV_START
76a39a
-    retcode = pam_start("atd", pentry->pw_name, &conv, &pamh);
76a39a
-    PAM_FAIL_CHECK;
76a39a
-    retcode = pam_acct_mgmt(pamh, PAM_SILENT);
76a39a
-    PAM_FAIL_CHECK;
76a39a
-    retcode = pam_open_session(pamh, PAM_SILENT);
76a39a
-    PAM_FAIL_CHECK;
76a39a
-    retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED | PAM_SILENT);
76a39a
-    PAM_FAIL_CHECK;
76a39a
-    PRIV_END
76a39a
+#ifdef WITH_PAM
76a39a
+    AT_START_PAM;
76a39a
+    AT_OPEN_PAM_SESSION;
76a39a
+    closelog(); 
76a39a
+    openlog("atd", LOG_PID, LOG_ATD);
76a39a
 #endif
76a39a
 
76a39a
     close(STDIN_FILENO);
76a39a
@@ -473,7 +463,14 @@ run_file(const char *filename, uid_t uid
76a39a
     else if (pid == 0) {
76a39a
 	char *nul = NULL;
76a39a
 	char **nenvp = &nul;
76a39a
+	char **pam_envp=0L;
76a39a
 
76a39a
+	PRIV_START
76a39a
+#ifdef WITH_PAM
76a39a
+	pam_envp = pam_getenvlist(pamh);
76a39a
+	if ( ( pam_envp != 0L ) && (pam_envp[0] != 0L) )
76a39a
+		nenvp = pam_envp;
76a39a
+#endif
76a39a
 	/* Set up things for the child; we want standard input from the
76a39a
 	 * input file, and standard output and error sent to our output file.
76a39a
 	 */
76a39a
@@ -492,8 +489,6 @@ run_file(const char *filename, uid_t uid
76a39a
 	close(fd_in);
76a39a
 	close(fd_out);
76a39a
 
76a39a
-	PRIV_START
76a39a
-
76a39a
 	    nice((tolower((int) queue) - 'a' + 1) * 2);
76a39a
 
76a39a
 #ifdef WITH_SELINUX
76a39a
@@ -514,9 +509,9 @@ run_file(const char *filename, uid_t uid
76a39a
 
76a39a
 	    chdir("/");
76a39a
 
76a39a
-	    if (execle("/bin/sh", "sh", (char *) NULL, nenvp) != 0)
76a39a
-		perr("Exec failed for /bin/sh");
76a39a
-
76a39a
+	    execle("/bin/sh", "sh", (char *) NULL, nenvp);
76a39a
+	    perr("Exec failed for /bin/sh");
76a39a
+            /* perr exits, the PRIV_END is just for nice form */
76a39a
 	PRIV_END
76a39a
     }
76a39a
     /* We're the parent.  Let's wait.
76a39a
@@ -529,14 +524,6 @@ run_file(const char *filename, uid_t uid
76a39a
      */
76a39a
     waitpid(pid, (int *) NULL, 0);
76a39a
 
76a39a
-#ifdef HAVE_PAM
76a39a
-    PRIV_START
76a39a
-	pam_setcred(pamh, PAM_DELETE_CRED | PAM_SILENT);
76a39a
-	retcode = pam_close_session(pamh, PAM_SILENT);
76a39a
-	pam_end(pamh, retcode);
76a39a
-    PRIV_END
76a39a
-#endif
76a39a
-
76a39a
     /* Send mail.  Unlink the output file after opening it, so it
76a39a
      * doesn't hang around after the run.
76a39a
      */
76a39a
@@ -567,8 +554,13 @@ run_file(const char *filename, uid_t uid
76a39a
     unlink(newname);
76a39a
     free(newname);
76a39a
 
76a39a
+#ifdef ATD_MAIL_PROGRAM
76a39a
     if (((send_mail != -1) && (buf.st_size != size)) || (send_mail == 1)) {
76a39a
+       int mail_pid = -1;
76a39a
 
76a39a
+     mail_pid = fork();
76a39a
+
76a39a
+     if ( mail_pid == 0 ) {
76a39a
 	PRIV_START
76a39a
 
76a39a
 	    if (initgroups(pentry->pw_name, pentry->pw_gid))
76a39a
@@ -590,7 +582,20 @@ run_file(const char *filename, uid_t uid
76a39a
 	    perr("Exec failed for mail command");
76a39a
 
76a39a
 	PRIV_END
76a39a
+     }
76a39a
+     else if ( mail_pid == -1 ) {
76a39a
+           syslog(LOG_ERR, "fork of mailer failed: %m");
76a39a
+     }
76a39a
+     /* Parent */
76a39a
+     waitpid(mail_pid, (int *) NULL, 0);
76a39a
     }
76a39a
+
76a39a
+#ifdef WITH_PAM
76a39a
+    AT_CLOSE_PAM;
76a39a
+    closelog();
76a39a
+    openlog("atd", LOG_PID, LOG_ATD);
76a39a
+#endif
76a39a
+#endif
76a39a
     exit(EXIT_SUCCESS);
76a39a
 }
76a39a
 
76a39a
diff -up at-3.1.20/config.h.in.pam at-3.1.20/config.h.in
76a39a
--- at-3.1.20/config.h.in.pam	2015-12-18 21:29:24.000000000 +0100
76a39a
+++ at-3.1.20/config.h.in	2016-07-01 09:44:22.251683924 +0200
76a39a
@@ -68,8 +68,8 @@
76a39a
 /* Define to 1 if you have the <nlist.h> header file. */
76a39a
 #undef HAVE_NLIST_H
76a39a
 
76a39a
-/* Define to 1 for PAM support */
76a39a
-#undef HAVE_PAM
76a39a
+/* Define if you are building with_pam */
76a39a
+#undef WITH_PAM
76a39a
 
76a39a
 /* Define to 1 if you have the `pstat_getdynamic' function. */
76a39a
 #undef HAVE_PSTAT_GETDYNAMIC
76a39a
diff -up at-3.1.20/configure.ac.pam at-3.1.20/configure.ac
76a39a
--- at-3.1.20/configure.ac.pam	2016-06-28 22:55:52.000000000 +0200
76a39a
+++ at-3.1.20/configure.ac	2016-07-01 09:45:23.268092527 +0200
76a39a
@@ -78,7 +78,7 @@ AC_FUNC_GETLOADAVG
76a39a
 AC_CHECK_FUNCS(getcwd mktime strftime setreuid setresuid sigaction waitpid)
76a39a
 AC_CHECK_HEADERS(security/pam_appl.h, [
76a39a
   PAMLIB="-lpam"
76a39a
-  AC_DEFINE(HAVE_PAM, 1, [Define to 1 for PAM support])
76a39a
+  AC_DEFINE(WITH_PAM, 1, [Define to 1 for PAM support])
76a39a
 ])
76a39a
 
76a39a
 dnl Checking for programs
76a39a
@@ -239,6 +239,13 @@ AC_ARG_WITH(daemon_username,
76a39a
 )
76a39a
 AC_SUBST(DAEMON_USERNAME)
76a39a
 
76a39a
+AC_ARG_WITH(pam,
76a39a
+[ --with-pam            Define to enable pam support ],
76a39a
+AC_DEFINE(WITH_PAM),
76a39a
+)
76a39a
+AC_CHECK_LIB(pam, pam_start, PAMLIB='-lpam -lpam_misc')
76a39a
+AC_SUBST(PAMLIB)
76a39a
+
76a39a
 AC_ARG_WITH(selinux,
76a39a
 [ --with-selinux       Define to run with selinux (default=check)],
76a39a
 [],
76a39a
diff -up at-3.1.20/Makefile.in.pam at-3.1.20/Makefile.in
76a39a
--- at-3.1.20/Makefile.in.pam	2016-07-01 09:44:22.250683901 +0200
76a39a
+++ at-3.1.20/Makefile.in	2016-07-01 09:44:22.252683947 +0200
76a39a
@@ -68,7 +68,7 @@ LIST = Filelist Filelist.asc
76a39a
 all: at atd atd.service atrun
76a39a
 
76a39a
 at: $(ATOBJECTS)
76a39a
-	$(CC) $(LDFLAGS) -pie -o at $(ATOBJECTS) $(LIBS) $(LEXLIB)
76a39a
+	$(CC) $(LDFLAGS) -pie -o at $(ATOBJECTS) $(LIBS) $(LEXLIB) $(PAMLIB)
76a39a
 	rm -f $(CLONES)
76a39a
 	$(LN_S) -f at atq
76a39a
 	$(LN_S) -f at atrm
76a39a
diff -up at-3.1.20/perm.c.pam at-3.1.20/perm.c
76a39a
--- at-3.1.20/perm.c.pam	2015-08-22 00:09:22.000000000 +0200
76a39a
+++ at-3.1.20/perm.c	2016-07-01 09:44:22.252683947 +0200
76a39a
@@ -51,6 +51,14 @@
76a39a
 #define PRIV_END while(0)
76a39a
 #endif
76a39a
 
76a39a
+#ifdef WITH_PAM
76a39a
+#include <security/pam_appl.h>
76a39a
+static pam_handle_t *pamh = NULL;
76a39a
+static const struct pam_conv conv = {
76a39a
+       NULL
76a39a
+};
76a39a
+#endif
76a39a
+
76a39a
 /* Structures and unions */
76a39a
 
76a39a
 
76a39a
@@ -108,18 +116,45 @@ user_in_file(const char *path, const cha
76a39a
 int
76a39a
 check_permission()
76a39a
 {
76a39a
-  uid_t uid = geteuid();
76a39a
+  uid_t euid = geteuid(), uid=getuid(), egid=getegid(), gid=getgid();
76a39a
   struct passwd *pentry;
76a39a
   int    allow = 0, deny = 1;
76a39a
 
76a39a
-  if (uid == 0)
76a39a
+  int    retcode = 0;
76a39a
+  if (euid == 0)
76a39a
     return 1;
76a39a
 
76a39a
-  if ((pentry = getpwuid(uid)) == NULL) {
76a39a
+  if ((pentry = getpwuid(euid)) == NULL) {
76a39a
     perror("Cannot access user database");
76a39a
     exit(EXIT_FAILURE);
76a39a
   }
76a39a
 
76a39a
+#ifdef  WITH_PAM
76a39a
+/*
76a39a
+ *  We must check if the atd daemon userid will be allowed to gain the job owner user's
76a39a
+ *  credentials with PAM . If not, the user has been denied at(1) usage, eg. with pam_access.
76a39a
+ */
76a39a
+  if (setreuid(daemon_uid, daemon_uid) != 0) {
76a39a
+      fprintf(stderr, "cannot set egid: %s", strerror(errno));
76a39a
+      exit(1);
76a39a
+  }
76a39a
+  if (setregid(daemon_gid, daemon_gid) != 0) {
76a39a
+      fprintf(stderr, "cannot set euid: %s", strerror(errno));
76a39a
+      exit(1);
76a39a
+  }
76a39a
+
76a39a
+    AT_START_PAM;
76a39a
+    AT_CLOSE_PAM;
76a39a
+    if (setregid(gid,egid) != 0) {
76a39a
+        fprintf(stderr, "cannot set egid: %s", strerror(errno));
76a39a
+        exit(1);
76a39a
+    }
76a39a
+    if (setreuid(uid,euid) != 0) {
76a39a
+        fprintf(stderr, "cannot set euid: %s", strerror(errno));
76a39a
+        exit(1);
76a39a
+    }
76a39a
+#endif
76a39a
+
76a39a
   allow = user_in_file(ETCDIR "/at.allow", pentry->pw_name);
76a39a
   if (allow==0 || allow==1)
76a39a
     return allow;
76a39a
diff -up at-3.1.20/privs.h.pam at-3.1.20/privs.h
76a39a
--- at-3.1.20/privs.h.pam	2015-08-22 00:09:22.000000000 +0200
76a39a
+++ at-3.1.20/privs.h	2016-07-01 09:44:22.252683947 +0200
76a39a
@@ -144,3 +144,63 @@ extern gid_t real_gid, effective_gid, da
76a39a
 #error "Cannot implement user ID swapping without setreuid or setresuid"
76a39a
 #endif
76a39a
 #endif
76a39a
+
76a39a
+#ifdef WITH_PAM
76a39a
+/* PAM failed after session was open.  */
76a39a
+#define PAM_SESSION_FAIL if (retcode != PAM_SUCCESS) \
76a39a
+       pam_close_session(pamh,PAM_SILENT);
76a39a
+
76a39a
+/* syslog will be logging error messages */
76a39a
+#ifdef HAVE_UNISTD_H
76a39a
+#include <syslog.h>
76a39a
+#endif
76a39a
+
76a39a
+/* PAM fail even before opening the session */
76a39a
+#define PAM_FAIL_CHECK \
76a39a
+       do { if (retcode != PAM_SUCCESS) { \
76a39a
+               fprintf(stderr,"PAM failure: %s\n",pam_strerror(pamh, retcode)); \
76a39a
+               syslog(LOG_ERR,"%s",pam_strerror(pamh, retcode)); \
76a39a
+               if (pamh) \
76a39a
+                   pam_end(pamh, retcode); \
76a39a
+               if (setregid(getgid(),getegid()) != 0) { \
76a39a
+                   fprintf(stderr, "cannot set egid: %s", strerror(errno)); \
76a39a
+                   exit(1); \
76a39a
+               } \
76a39a
+               if (setreuid(getuid(),geteuid()) != 0) { \
76a39a
+                   fprintf(stderr, "cannot set euid: %s", strerror(errno)); \
76a39a
+                   exit(1); \
76a39a
+               } \
76a39a
+               exit(1); \
76a39a
+           } \
76a39a
+       } while (0) \
76a39a
+
76a39a
+static int pam_session_opened = 0;      //global for open session
76a39a
+
76a39a
+#define AT_START_PAM { \
76a39a
+        retcode = pam_start("atd", pentry->pw_name, &conv, &pamh); \
76a39a
+        PAM_FAIL_CHECK; \
76a39a
+        retcode = pam_set_item(pamh, PAM_TTY, "atd"); \
76a39a
+        PAM_FAIL_CHECK; \
76a39a
+        retcode = pam_acct_mgmt(pamh, PAM_SILENT); \
76a39a
+        PAM_FAIL_CHECK; \
76a39a
+} 
76a39a
+
76a39a
+#define AT_OPEN_PAM_SESSION { \
76a39a
+        retcode = pam_open_session(pamh, PAM_SILENT); \
76a39a
+        PAM_FAIL_CHECK; \
76a39a
+        retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED | PAM_SILENT); \
76a39a
+        PAM_FAIL_CHECK; \
76a39a
+        if (retcode == PAM_SUCCESS) \
76a39a
+                pam_session_opened = 1; \
76a39a
+}
76a39a
+
76a39a
+#define AT_CLOSE_PAM { \
76a39a
+        if (pam_session_opened != 0) { \
76a39a
+                pam_setcred(pamh, PAM_DELETE_CRED | PAM_SILENT); \
76a39a
+                pam_close_session(pamh, PAM_SILENT); \
76a39a
+        } \
76a39a
+        pam_end(pamh, PAM_SUCCESS); \
76a39a
+}
76a39a
+
76a39a
+#endif
76a39a
+