Blame SOURCES/at-3.1.13-pam.patch

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