]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
Adding run-parts style for pre and post useradd/del 234/head
authored <ed@s5h.net>
Thu, 12 Mar 2020 21:14:57 +0000 (21:14 +0000)
committerSerge Hallyn <shallyn@cisco.com>
Wed, 10 Jun 2020 05:26:55 +0000 (00:26 -0500)
Signed-off-by: ed neville <ed@s5h.net>
lib/Makefile.am
lib/run_part.c [new file with mode: 0644]
lib/run_part.h [new file with mode: 0644]
man/useradd.8.xml
man/userdel.8.xml
src/useradd.c
src/userdel.c

index a40c08a12fb91855de93462ccfbdfc4013ae26ef..705ef44941622d71e504cfc814f9fbda5b12e11d 100644 (file)
@@ -45,6 +45,8 @@ libshadow_la_SOURCES = \
        pwio.c \
        pwio.h \
        pwmem.c \
+       run_part.h \
+       run_part.c \
        subordinateio.h \
        subordinateio.c \
        selinux.c \
diff --git a/lib/run_part.c b/lib/run_part.c
new file mode 100644 (file)
index 0000000..ab0bcc1
--- /dev/null
@@ -0,0 +1,101 @@
+#include <dirent.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+int run_part (char *script_path, char *name, char *action)
+{
+       int pid;
+       int wait_status;
+       int pid_status;
+       char *args[] = { script_path, NULL };
+
+       pid=fork();
+       if (pid==-1){
+               perror ("Could not fork");
+               return 1;
+       }
+       if (pid==0) {
+               setenv ("ACTION",action,1);
+               setenv ("SUBJECT",name,1);
+               execv (script_path,args);
+               perror ("execv");
+               exit(1);
+       }
+
+       pid_status = wait (&wait_status);
+       if (pid_status == pid) {
+               return (wait_status);
+       }
+
+       perror ("waitpid");
+       return (1);
+}
+
+int run_parts (char *directory, char *name, char *action)
+{
+       struct dirent **namelist;
+       int scanlist;
+       int n;
+       int execute_result;
+
+       scanlist = scandir (directory, &namelist, 0, alphasort);
+       if (scanlist<0) {
+               return (0);
+       }
+
+       for (n=0; n<scanlist; n++) {
+               int path_length;
+               struct stat sb;
+
+               path_length=strlen(directory) + strlen(namelist[n]->d_name) + 2;
+               char *s = (char*)malloc(path_length);
+               if (!s) {
+                       printf ("could not allocate memory\n");
+                       for (; n<scanlist; n++) {
+                               free (namelist[n]);
+                       }
+                       free (namelist);
+                       return (1);
+               }
+               snprintf (s, path_length, "%s/%s", directory, namelist[n]->d_name);
+
+               execute_result = 0;
+               if (stat (s, &sb) == -1) {
+                       perror ("stat");
+                       free (s);
+                       for (; n<scanlist; n++) {
+                               free (namelist[n]);
+                       }
+                       free (namelist);
+                       return (1);
+               }
+
+               if (S_ISREG (sb.st_mode) || S_ISLNK (sb.st_mode)) {
+                       execute_result = run_part (s, name, action);
+               }
+
+               free (s);
+
+               if (execute_result!=0) {
+                       fprintf (stderr,
+                               "%s: did not exit cleanly.\n",
+                           namelist[n]->d_name);
+                       for (; n<scanlist; n++) {
+                               free (namelist[n]);
+                       }
+                       break;
+               }
+
+               free (namelist[n]);
+       }
+       free (namelist);
+
+       return (execute_result);
+}
+
diff --git a/lib/run_part.h b/lib/run_part.h
new file mode 100644 (file)
index 0000000..d3d8066
--- /dev/null
@@ -0,0 +1,2 @@
+int run_part (char *script_path, char *name, char *action);
+int run_parts (char *directory, char *name, char *action);
index 03612ce8aefc50323b18f32d271e89e740143738..2aacab7c5eb90826660bde7e701fe0a01417fa4b 100644 (file)
        </listitem>
       </varlistentry>
       <varlistentry>
+       <term><filename>/etc/shadow-maint/useradd-pre.d/*</filename>, <filename>/etc/shadow-maint/useradd-post.d/*</filename></term>
+       <listitem>
+         <para>Run-part files to execute during user addition. The environment variable <command>ACTION</command> will be populated with useradd and <command>SUBJECT</command> with the <command>username</command>. <filename>useradd-pre.d</filename> will be executed prior to any user addition. <filename>useradd-post.d</filename> will execute after user addition. If a script exits non-zero then execution will terminate.</para>
+       </listitem>
+      </varlistentry>
+  <varlistentry>
        <term><filename>/etc/skel/</filename></term>
        <listitem>
          <para>Directory containing default files.</para>
index b086383b748363a0e5b02113037b0eb43f7935bd..520a60b4198bd9258af869f2fedeaa248f2a23df 100644 (file)
          <para>Secure user account information.</para>
        </listitem>
       </varlistentry>
+    <varlistentry>
+       <term><filename>/etc/shadow-maint/userdel-pre.d/*</filename>, <filename>/etc/shadow-maint/userdel-post.d/*</filename></term>
+       <listitem>
+         <para>Run-part files to execute during user deletion. The environment variable <command>ACTION</command> will be populated with <command>userdel</command> and <command>SUBJECT</command> with the username. <filename>userdel-pre.d</filename> will be executed prior to any user deletion. <filename>userdel-post.d</filename> will execute after user deletion. If a script exits non-zero then execution will terminate.</para>
+       </listitem>
+      </varlistentry>
       <varlistentry condition="subids">
        <term><filename>/etc/subgid</filename></term>
        <listitem>
index 645d4a40f204a9142396bfaafb875a367a9e1e88..e84eeefbd40a6640a418015b25de15f6878b9c30 100644 (file)
@@ -64,6 +64,7 @@
 #include "prototypes.h"
 #include "pwauth.h"
 #include "pwio.h"
+#include "run_part.h"
 #ifdef SHADOWGRP
 #include "sgroupio.h"
 #endif
@@ -2318,6 +2319,11 @@ int main (int argc, char **argv)
            (!user_id || (user_id <= uid_max && user_id >= uid_min));
 #endif                         /* ENABLE_SUBIDS */
 
+       if (run_parts ("/etc/shadow-maint/useradd-pre.d", (char*)user_name,
+                       "useradd")) {
+               exit(1);
+       }
+
 #ifdef ACCT_TOOLS_SETUID
 #ifdef USE_PAM
        {
@@ -2530,6 +2536,11 @@ int main (int argc, char **argv)
        }
 #endif                         /* WITH_SELINUX */
 
+       if (run_parts ("/etc/shadow-maint/useradd-post.d", (char*)user_name,
+                       "useradd")) {
+               exit(1);
+       }
+
        nscd_flush_cache ("passwd");
        nscd_flush_cache ("group");
        sssd_flush_cache (SSSD_DB_PASSWD | SSSD_DB_GROUP);
index cc951e580c766d226752fe600e262e958b227751..2a09f4598bfe8c265251d362e9635dc272c75afa 100644 (file)
  */
 
 #include <config.h>
-
-#ident "$Id$"
-
 #include <assert.h>
+#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
 #include <grp.h>
 #include <pwd.h>
 #include <stdio.h>
-#include <stdio.h>
-#include <sys/stat.h>
 #include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
 #ifdef ACCT_TOOLS_SETUID
 #ifdef USE_PAM
 #include "pam_defs.h"
@@ -65,6 +63,7 @@
 #include <tcb.h>
 #include "tcbfuncs.h"
 #endif                         /* WITH_TCB */
+#include "run_part.h"
 /*@-exitarg@*/
 #include "exitcodes.h"
 #ifdef ENABLE_SUBIDS
@@ -1143,6 +1142,10 @@ int main (int argc, char **argv)
        {
                const struct passwd *pwd;
 
+               if (run_parts ("/etc/shadow-maint/userdel-pre.d", user_name,
+                               "userdel")) {
+                       exit(1);
+               }
                pw_open(O_RDONLY);
                pwd = pw_locate (user_name); /* we care only about local users */
                if (NULL == pwd) {
@@ -1342,6 +1345,10 @@ int main (int argc, char **argv)
                user_cancel (user_name);
        close_files ();
 
+       if (run_parts ("/etc/shadow-maint/userdel-post.d", user_name, "userdel")) {
+               exit(1);
+       }
+
 #ifdef WITH_TCB
        errors += remove_tcbdir (user_name, user_id);
 #endif                         /* WITH_TCB */