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