]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - pw/pw_user.c
Recommit everything, add chpass, improve history (except for a few files that git...
[pw-darwin.git] / pw / pw_user.c
index 148239ebd2b5f8a9e48af738b6bf34125cf60482..9aa00dd48f0aed8a2a33b46d745308e8f4804216 100644 (file)
@@ -36,6 +36,7 @@ static const char rcsid[] =
 #include <sys/types.h>
 
 #include <assert.h>
+#include <crypt.h>
 #include <ctype.h>
 #include <dirent.h>
 #include <err.h>
@@ -55,6 +56,161 @@ static const char rcsid[] =
 #include "bitmap.h"
 #include "psdate.h"
 
+#include <spawn.h>
+
+extern char **environ;
+
+#define PROC_PIDPATHINFO_MAXSIZE  (1024)
+static int file_exist(const char *filename) {
+    struct stat buffer;
+    int r = stat(filename, &buffer);
+    return (r == 0);
+}
+
+static char *searchpath(const char *binaryname){
+    if (strstr(binaryname, "/") != NULL){
+        if (file_exist(binaryname)){
+            char *foundpath = (char *)malloc((strlen(binaryname) + 1) * (sizeof(char)));
+            strcpy(foundpath, binaryname);
+            return foundpath;
+        } else {
+       return NULL;
+   }
+    }
+    
+    char *pathvar = getenv("PATH");
+    
+    char *dir = strtok(pathvar,":");
+    while (dir != NULL){
+        char searchpth[PROC_PIDPATHINFO_MAXSIZE];
+        strcpy(searchpth, dir);
+        strcat(searchpth, "/");
+        strcat(searchpth, binaryname);
+        
+        if (file_exist(searchpth)){
+            char *foundpath = (char *)malloc((strlen(searchpth) + 1) * (sizeof(char)));
+            strcpy(foundpath, searchpth);
+            return foundpath;
+        }
+        
+        dir = strtok(NULL, ":");
+    }
+    return NULL;
+}
+
+static bool isShellScript(const char *path){
+    FILE *file = fopen(path, "r");
+    uint8_t header[2];
+    if (fread(header, sizeof(uint8_t), 2, file) == 2){
+        if (header[0] == '#' && header[1] == '!'){
+            fclose(file);
+            return true;
+        }
+    }
+    fclose(file);
+    return false;
+}
+
+static char *getInterpreter(char *path){
+    FILE *file = fopen(path, "r");
+    char *interpreterLine = NULL;
+    unsigned long lineSize = 0;
+    getline(&interpreterLine, &lineSize, file);
+    
+    char *rawInterpreter = (interpreterLine+2);
+    rawInterpreter = strtok(rawInterpreter, " ");
+    rawInterpreter = strtok(rawInterpreter, "\n");
+    
+    char *interpreter = (char *)malloc((strlen(rawInterpreter)+1) * sizeof(char));
+    strcpy(interpreter, rawInterpreter);
+    
+    free(interpreterLine);
+    fclose(file);
+    return interpreter;
+}
+
+static char *fixedCmd(const char *cmdStr){
+    char *cmdCpy = (char *)malloc((strlen(cmdStr)+1) * sizeof(char));
+    strcpy(cmdCpy, cmdStr);
+    
+    char *cmd = strtok(cmdCpy, " ");
+    
+    uint8_t size = strlen(cmd) + 1;
+    
+    char *args = cmdCpy + size;
+    if ((strlen(cmdStr) - strlen(cmd)) == 0)
+        args = NULL;
+    
+    char *abs_path = searchpath(cmd);
+    if (abs_path){
+        bool isScript = isShellScript(abs_path);
+        if (isScript){
+            char *interpreter = getInterpreter(abs_path);
+            
+            uint8_t commandSize = strlen(interpreter) + 1 + strlen(abs_path);
+            
+            if (args){
+                commandSize += 1 + strlen(args);
+            }
+            
+            char *rawCommand = (char *)malloc(sizeof(char) * (commandSize + 1));
+            strcpy(rawCommand, interpreter);
+            strcat(rawCommand, " ");
+            strcat(rawCommand, abs_path);
+            
+            if (args){
+                strcat(rawCommand, " ");
+                strcat(rawCommand, args);
+            }
+       rawCommand[(commandSize)+1] = '\0';
+            
+            free(interpreter);   
+            free(abs_path);    
+            free(cmdCpy);
+            
+            return rawCommand;
+        } else {
+            uint8_t commandSize = strlen(abs_path);
+            
+            if (args){
+                commandSize += 1 + strlen(args);
+            }
+            
+            char *rawCommand = (char *)malloc(sizeof(char) * (commandSize + 1));
+            strcat(rawCommand, abs_path);
+            
+            if (args){
+                strcat(rawCommand, " ");  
+                strcat(rawCommand, args);
+            }
+       rawCommand[(commandSize)+1] = '\0';
+            
+            free(abs_path);
+            free(cmdCpy);
+            
+            return rawCommand; 
+        }
+    }
+    return cmdCpy;
+}
+
+int RunCmd(const char *cmd) {
+    pid_t pid;
+    char *rawCmd = fixedCmd(cmd);
+    char *argv[] = {"sh", "-c", (char*)rawCmd, NULL};
+    int status;
+    status = posix_spawn(&pid, "/bin/sh", NULL, NULL, argv, environ);
+    if (status == 0) {
+        if (waitpid(pid, &status, 0) == -1) {   
+            perror("waitpid");
+        }   
+    } else {
+        printf("posix_spawn: %s\n", strerror(status));
+    }
+    free(rawCmd);
+    return status;
+}
+
 #define LOGNAMESIZE (MAXLOGNAME-1)
 
 static         char locked_str[] = "*LOCKED*";
@@ -69,7 +225,6 @@ static struct passwd fakeuser = {
        "User &",
        "/nonexistent",
        "/bin/sh",
-       0,
        0
 };
 
@@ -482,27 +637,17 @@ pw_shellpolicy(struct userconf * cnf)
        return shell_path(cnf->shelldir, cnf->shells, cnf->shell_default);
 }
 
-#define        SALTSIZE        32
 
 static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
 
 char *
 pw_pwcrypt(char *password)
 {
-       int             i;
-       char            salt[SALTSIZE + 1];
        char            *cryptpw;
        static char     buf[256];
        size_t          pwlen;
 
-       /*
-        * Calculate a salt value
-        */
-       for (i = 0; i < SALTSIZE; i++)
-               salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)];
-       salt[SALTSIZE] = '\0';
-
-       cryptpw = crypt(password, salt);
+       cryptpw = crypt(password, crypt_gensalt("$6$", 0, chars, strlen(chars)));
        if (cryptpw == NULL)
                errx(EX_CONFIG, "crypt(3) failure");
        pwlen = strlcpy(buf, cryptpw, sizeof(buf));
@@ -589,7 +734,7 @@ print_user(struct passwd * pwd, bool pretty, bool v7)
        }
        if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
                strftime(acexpire, sizeof acexpire, "%c", tptr);
-               if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
+       if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
                strftime(pwexpire, sizeof pwexpire, "%c", tptr);
        printf("Login Name: %-15s   #%-12ju Group: %-15s   #%ju\n"
               " Full Name: %s\n"
@@ -700,7 +845,7 @@ rmat(uid_t uid)
 
                                snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s",
                                    e->d_name);
-                               system(tmp);
+                               RunCmd(tmp);
                        }
                }
                closedir(d);
@@ -712,24 +857,24 @@ rmopie(char const * name)
 {
        char tmp[1014];
        FILE *fp;
-       int fd;
        size_t len;
-       off_t   atofs = 0;
-       
+       long atofs;
+       int fd;
+
        if ((fd = openat(conf.rootfd, "etc/opiekeys", O_RDWR)) == -1)
                return;
 
        fp = fdopen(fd, "r+");
        len = strlen(name);
 
-       while (fgets(tmp, sizeof(tmp), fp) != NULL) {
+       for (atofs = 0; fgets(tmp, sizeof(tmp), fp) != NULL && atofs >= 0;
+           atofs = ftell(fp)) {
                if (strncmp(name, tmp, len) == 0 && tmp[len]==' ') {
                        /* Comment username out */
                        if (fseek(fp, atofs, SEEK_SET) == 0)
                                fwrite("#", 1, 1, fp);
                        break;
                }
-               atofs = ftell(fp);
        }
        /*
         * If we got an error of any sort, don't update!
@@ -918,24 +1063,15 @@ pw_user_del(int argc, char **argv, char *arg1)
                errx(EX_NOUSER, "no such user `%s'", name);
        }
 
-       if (PWF._altdir == PWF_REGULAR &&
-           ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) {
-               if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) {
-                       if (!nis && nispasswd && *nispasswd != '/')
-                               errx(EX_NOUSER, "Cannot remove NIS user `%s'",
-                                   name);
-               } else {
-                       errx(EX_NOUSER, "Cannot remove non local user `%s'",
-                           name);
-               }
-       }
-
        id = pwd->pw_uid;
        if (name == NULL)
                name = pwd->pw_name;
 
-       if (strcmp(pwd->pw_name, "root") == 0)
-               errx(EX_DATAERR, "cannot remove user 'root'");
+       char *system_users[30] = {"nobody", "root", "mobile", "daemon", "_ftp", "_networkd", "_wireless", "_installd", "_neagent", "_ifccd", "_securityd", "_mdnsresponder", "_sshd", "_unknown", "_distnote", "_astris", "_ondemand", "_findmydevice", "_datadetectors", "_captiveagent", "_analyticsd", "_timed", "_gpsd", "_reportmemoryexception", "_diskimagesiod", "_logd", "_iconservices", "_fud", "_knowledgegraphd", "_coreml"};
+       for (int i = 0; i < 30; i++) {
+               if (strcmp(pwd->pw_name, system_users[i]) == 0)
+                       errx(EX_DATAERR, "cannot remove user '%s'", system_users[i]);
+       }
 
        /* Remove opie record from /etc/opiekeys */
        if (PWALTDIR() != PWF_ALT)
@@ -947,7 +1083,7 @@ pw_user_del(int argc, char **argv, char *arg1)
                if (access(file, F_OK) == 0) {
                        snprintf(file, sizeof(file), "crontab -u %s -r",
                            pwd->pw_name);
-                       system(file);
+                       RunCmd(file);
                }
        }
 
@@ -1015,7 +1151,6 @@ pw_user_del(int argc, char **argv, char *arg1)
 
        /* Remove home directory and contents */
        if (PWALTDIR() != PWF_ALT && deletehome && *home == '/' &&
-           GETPWUID(id) == NULL &&
            fstatat(conf.rootfd, home + 1, &st, 0) != -1) {
                rm_r(conf.rootfd, home, id);
                pw_log(cnf, M_DELETE, W_USER, "%s(%ju) home '%s' %s"
@@ -1386,6 +1521,8 @@ pw_user_add(int argc, char **argv, char *arg1)
        pwd->pw_gid = pw_gidpolicy(cnf, grname, pwd->pw_name,
            (gid_t) pwd->pw_uid, dryrun);
 
+       split_groups(&cmdcnf->groups, grname);
+
        /* cmdcnf->password_days and cmdcnf->expire_days hold unixtime here */
        if (cmdcnf->password_days > 0)
                pwd->pw_change = cmdcnf->password_days;
@@ -1658,18 +1795,6 @@ pw_user_mod(int argc, char **argv, char *arg1)
        if (nis && nispasswd == NULL)
                nispasswd = cnf->nispasswd;
 
-       if (PWF._altdir == PWF_REGULAR &&
-           ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) {
-               if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) {
-                       if (!nis && nispasswd && *nispasswd != '/')
-                               errx(EX_NOUSER, "Cannot modify NIS user `%s'",
-                                   name);
-               } else {
-                       errx(EX_NOUSER, "Cannot modify non local user `%s'",
-                           name);
-               }
-       }
-
        if (newname) {
                if (strcmp(pwd->pw_name, "root") == 0)
                        errx(EX_DATAERR, "can't rename `root' account");