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