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