diff options
Diffstat (limited to 'USER/USER.xs')
-rw-r--r-- | USER/USER.xs | 253 |
1 files changed, 0 insertions, 253 deletions
diff --git a/USER/USER.xs b/USER/USER.xs index 4cfb749..05e011e 100644 --- a/USER/USER.xs +++ b/USER/USER.xs @@ -63,259 +63,6 @@ static SV ** convert_value_array_list(register SV **sp, GValueArray *array) { return sp; } -/* Populate a user's home directory, copying data from a named skeleton - * directory, setting all ownerships as given, and setting the mode of - * the top-level directory as given. */ -int -my_homedir_populate(const char *skeleton, const char *directory, - uid_t owner, gid_t group, mode_t mode, - USER__ERR **error) -{ - struct dirent *ent; - DIR *dir; - struct stat st; - char skelpath[PATH_MAX], path[PATH_MAX], buf[PATH_MAX]; - struct utimbuf timebuf; - int ifd = -1, ofd = -1, i; - off_t offset; - LU_ERROR_CHECK(error); - /* If the destination directory exists, return. */ - dir = opendir(skeleton); - if (dir == NULL) { - lu_error_new(error, lu_error_generic, - _("Error reading `%s': %s"), skeleton, - strerror(errno)); - return 0; - } - /* Create the top-level directory. */ - if ((mkdir(directory, mode) == -1) && (errno != EEXIST)) { - lu_error_new(error, lu_error_generic, - _("Error creating `%s': %s"), directory, - strerror(errno)); - closedir(dir); - return 0; - } - /* Set the ownership on the top-level directory. */ - chown(directory, owner, group); - while ((ent = readdir(dir)) != NULL) { - /* Iterate through each item in the directory. */ - /* Skip over self and parent hard links. */ - if (strcmp(ent->d_name, ".") == 0) { - continue; - } - if (strcmp(ent->d_name, "..") == 0) { - continue; - } - /* Build the path of the skeleton file or directory and - * its corresponding member in the new tree. */ - snprintf(skelpath, sizeof(skelpath), "%s/%s", - skeleton, ent->d_name); - snprintf(path, sizeof(path), "%s/%s", directory, - ent->d_name); - /* What we do next depends on the type of entry we're - * looking at. */ - if (lstat(skelpath, &st) != -1) { - /* We always want to preserve atime/mtime. */ - timebuf.actime = st.st_atime; - timebuf.modtime = st.st_mtime; - /* If it's a directory, descend into it. */ - if (S_ISDIR(st.st_mode)) { - if (!my_homedir_populate(skelpath, - path, - owner, - st.st_gid ?: group, - st.st_mode, - error)) { - /* Aargh! Fail up. */ - closedir(dir); - return 0; - } - /* Set the date on the directory. */ - utime(path, &timebuf); - continue; - } - /* If it's a symlink, duplicate it. */ - if (S_ISLNK(st.st_mode)) { - if (readlink(skelpath, buf, - sizeof(buf) - 1) != -1) { - buf[sizeof(buf) - 1] = '\0'; - symlink(buf, path); - lchown(path, owner, st.st_gid ?: group); - utime(path, &timebuf); - } - continue; - } - /* If it's a regular file, copy it. */ - if (S_ISREG(st.st_mode)) { - /* Open both the input and output - * files. If we fail to do either, - * we have to give up. */ - ifd = open(skelpath, O_RDONLY); - if (ifd != -1) { - ofd = open(path, - O_EXCL | O_CREAT | O_WRONLY, - st.st_mode); - } - if ((ifd == -1) || (ofd == -1)) { - /* Sorry, no can do. */ - close (ifd); - close (ofd); - continue; - } - /* Now just copy the data. */ - do { - i = read(ifd, &buf, sizeof(buf)); - if (i > 0) { - write(ofd, buf, i); - } - } while (i > 0); - /* Close the files. */ - offset = lseek(ofd, 0, SEEK_CUR); - if (offset != ((off_t) -1)) { - ftruncate(ofd, offset); - } - close (ifd); - close (ofd); - /* Set the ownership and timestamp on - * the new file. */ - chown(path, owner, st.st_gid ?: group); - utime(path, &timebuf); - continue; - } - /* Note that we don't copy device specials. */ - } - } - closedir(dir); - return 1; -} - -/* Recursively remove a user's home (or really, any) directory. */ -int -lu_homedir_remove(const char *directory, struct lu_error ** error) -{ - struct dirent *ent; - DIR *dir; - struct stat st; - char path[PATH_MAX]; - LU_ERROR_CHECK(error); - /* Open the directory. This catches the case that it's already gone. */ - dir = opendir(directory); - if (dir == NULL) { - lu_error_new(error, lu_error_stat, - _("Error removing `%s': %s"), directory, - strerror(errno)); - return 0; - } - /* Iterate over all of its contents. */ - while ((ent = readdir(dir)) != NULL) { - /* Skip over the self and parent hard links. */ - if (strcmp(ent->d_name, ".") == 0) { - continue; - } - if (strcmp(ent->d_name, "..") == 0) { - continue; - } - /* Generate the full path of the next victim. */ - snprintf(path, sizeof(path), "%s/%s", directory, ent->d_name); - /* What we do next depends on whether or not the next item to - * remove is a directory. */ - if (lstat(path, &st) != -1) { - if (S_ISDIR(st.st_mode)) { - /* We decend into subdirectories... */ - if (lu_homedir_remove(path, error) == FALSE) { - closedir(dir); - return 0; - } - } else { - /* ... and unlink everything else. */ - if (unlink(path) == -1) { - lu_error_new(error, - lu_error_generic, - _("Error removing " - "`%s': %s"), - path, - strerror - (errno)); - closedir(dir); - return 0; - } - } - } - } - closedir(dir); - - /* As a final step, remove the directory itself. */ - if (rmdir(directory) == -1) { - lu_error_new(error, lu_error_generic, - _("Error removing `%s': %s"), directory, - strerror(errno)); - return 0; - } - - return 1; -} -/* Move a directory from one place to another. */ -int -lu_homedir_move(const char *oldhome, const char *newhome, - USER__ERR ** error) -{ - struct stat st; - LU_ERROR_CHECK(error); - /* If the directory exists... */ - if (stat(oldhome, &st) != -1) { - /* ... and we can copy it ... */ - if (my_homedir_populate(oldhome, newhome, - st.st_uid, st.st_gid, st.st_mode, - error)) { - /* ... remove the old one. */ - return lu_homedir_remove(oldhome, error); - } - } - return 0; -} -/* Concatenate a string onto another string on the heap. */ -char * -lu_strconcat(char *existing, const char *appendee) -{ - char *tmp; - if (existing == NULL) { - existing = g_strdup(appendee); - } else { - tmp = g_strconcat(existing, appendee, NULL); - g_free(existing); - existing = tmp; - } - return existing; -} -/* Send nscd an arbitrary signal. */ -void -lu_signal_nscd(int signum) -{ - FILE *fp; - char buf[LINE_MAX]; - /* If it's running, then its PID is in this file. Open it. */ - if ((fp = fopen("/var/run/nscd.pid", "r")) != NULL) { - /* Read the PID. */ - memset(buf, 0, sizeof(buf)); - fgets(buf, sizeof(buf), fp); - /* If the PID is sane, send it a signal. */ - if (strlen(buf) > 0) { - pid_t pid = atol(buf); - if (pid != 0) { - kill(pid, signum); - } - } - fclose(fp); - } -} - -/* Send nscd a SIGHUP. */ -void -lu_hup_nscd() -{ - lu_signal_nscd(SIGHUP); -} - /* Create a mail spool for the user. */ int lu_mailspool_create_remove(USER__ADMIN *ctx, USER__ENT *ent, |