void cp(char* src_path, char* dst_path) { if(path_exists(src_path) != PATH_DOES_NOT_EXIST && path_exists(src_path) != PATH_IS_DIRECTORY ) { //make necessary parent directories rm_r(dst_path); mkdir_p(dst_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); rm_r(dst_path); FILE* src = fopen(src_path, "rb"); FILE* dst= fopen(dst_path, "wb"); if(src != NULL && dst != NULL) { unsigned char buf[1024]; int num_read = 1; while(num_read > 0) { num_read = fread(buf, 1, 1024, src); if(num_read > 0) { fwrite(buf, 1, num_read, dst); } } } if(src != NULL){ fclose(src); } if(dst != NULL){ fclose(dst); } } }
void rm_r(int rootfd, const char *path, uid_t uid) { int dirfd; DIR *d; struct dirent *e; struct stat st; if (*path == '/') path++; dirfd = openat(rootfd, path, O_DIRECTORY); if (dirfd == -1) { return; } d = fdopendir(dirfd); while ((e = readdir(d)) != NULL) { if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) continue; if (fstatat(dirfd, e->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0) continue; if (S_ISDIR(st.st_mode)) rm_r(dirfd, e->d_name, uid); else if (S_ISLNK(st.st_mode) || st.st_uid == uid) unlinkat(dirfd, e->d_name, 0); } closedir(d); if (fstatat(rootfd, path, &st, AT_SYMLINK_NOFOLLOW) != 0) return; unlinkat(rootfd, path, S_ISDIR(st.st_mode) ? AT_REMOVEDIR : 0); }
void rm_r(const string &path, boost::function<bool (const string &, bool)> filter) { struct stat buffer; if (lstat(path.c_str(), &buffer)) { if (errno == ENOENT) { return; } else { SyncContext::throwError(path, errno); } } if (!S_ISDIR(buffer.st_mode)) { if (!filter(path, false) || !unlink(path.c_str())) { return; } else { SyncContext::throwError(path, errno); } } ReadDir dir(path); BOOST_FOREACH(const string &entry, dir) { rm_r(path + "/" + entry, filter); }
void rm_r(char const * dir, uid_t uid) { DIR *d = opendir(dir); if (d != NULL) { struct dirent *e; struct stat st; char file[MAXPATHLEN]; while ((e = readdir(d)) != NULL) { if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0) { snprintf(file, sizeof(file), "%s/%s", dir, e->d_name); if (lstat(file, &st) == 0) { /* Need symlinks, not * linked file */ if (S_ISDIR(st.st_mode)) /* Directory - recurse */ rm_r(file, uid); else { if (S_ISLNK(st.st_mode) || st.st_uid == uid) remove(file); } } } } closedir(d); if (lstat(dir, &st) == 0) { if (S_ISLNK(st.st_mode)) remove(dir); else if (st.st_uid == uid) rmdir(dir); } } }
static bool rm_r(const QString &rmPath) { QFileInfo fi(rmPath); if (!fi.exists()) { qDebug() << rmPath << "does not exist"; return false; } else if (fi.isFile()) { if (!QFile::remove(rmPath)) { qDebug() << "can't remove file" << rmPath; return false; } return true; } else if (fi.isDir()) { QFileInfoList fList = QDir(rmPath).entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot); foreach (QFileInfo sub, fList) { rm_r(sub.absoluteFilePath()); }
int create_dir_and_test_writable(char* dir) { int success; FILE* test_file = NULL; char* test_path = dynamic_strcat(2, dir, "/tmp.gpkg.write.test.tmp"); mkdir_p(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH ); test_file = fopen(test_path, "w"); success = test_file == NULL ? 0 : 1; if(test_file != NULL) { fclose(test_file); rm_r(test_path); } free(test_path); return success; }
static void DeregisterSession( SessionData *data) { SessionCtrl *ctrl; ENTER_FUNC; #if 0 if (!getenv("WFC_KEEP_TEMPDIR")) { if (!rm_r(data->hdr->tempdir)) { Error("cannot remove session tempdir %s",data->hdr->tempdir); } } #endif ctrl = NewSessionCtrl(SESSION_CONTROL_DELETE); ctrl->session = data; SessionEnqueue(ctrl); ctrl = (SessionCtrl*)DeQueue(ctrl->waitq); FreeSessionCtrl(ctrl); FreeSessionData(data); LEAVE_FUNC; }
int util::rm_r(int dfd, const char * path) { DIR * dir; struct stat64 st; struct dirent * ent; int fd, copy, r = fstatat64(dfd, path, &st, AT_SYMLINK_NOFOLLOW); if(r < 0) return r; if(!S_ISDIR(st.st_mode)) return unlinkat(dfd, path, 0); fd = openat(dfd, path, O_RDONLY); if(fd < 0) return fd; copy = dup(fd); if(copy < 0) { close(fd); return copy; } dir = fdopendir(copy); if(!dir) { close(copy); close(fd); return -1; } while((ent = readdir(dir))) { if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) continue; r = rm_r(fd, ent->d_name); if(r < 0) break; } closedir(dir); close(fd); if(r < 0) return r; return unlinkat(dfd, path, AT_REMOVEDIR); }
int pw_user(struct userconf * cnf, int mode, struct cargs * args) { int rc, edited = 0; char *p = NULL; char *passtmp; struct carg *a_name; struct carg *a_uid; struct carg *arg; struct passwd *pwd = NULL; struct group *grp; struct stat st; char line[_PASSWORD_LEN+1]; FILE *fp; char *dmode_c; void *set = NULL; static struct passwd fakeuser = { NULL, "*", -1, -1, 0, "", "User &", "/nonexistent", "/bin/sh", 0 #if defined(__FreeBSD__) ,0 #endif }; /* * With M_NEXT, we only need to return the * next uid to stdout */ if (mode == M_NEXT) { uid_t next = pw_uidpolicy(cnf, args); if (getarg(args, 'q')) return next; printf("%ld:", (long)next); pw_group(cnf, mode, args); return EXIT_SUCCESS; } /* * We can do all of the common legwork here */ if ((arg = getarg(args, 'b')) != NULL) { cnf->home = arg->val; } if ((arg = getarg(args, 'M')) != NULL) { dmode_c = arg->val; if ((set = setmode(dmode_c)) == NULL) errx(EX_DATAERR, "invalid directory creation mode '%s'", dmode_c); cnf->homemode = getmode(set, _DEF_DIRMODE); free(set); } /* * If we'll need to use it or we're updating it, * then create the base home directory if necessary */ if (arg != NULL || getarg(args, 'm') != NULL) { int l = strlen(cnf->home); if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ cnf->home[--l] = '\0'; if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); if (stat(cnf->home, &st) == -1) { char dbuf[MAXPATHLEN]; /* * This is a kludge especially for Joerg :) * If the home directory would be created in the root partition, then * we really create it under /usr which is likely to have more space. * But we create a symlink from cnf->home -> "/usr" -> cnf->home */ if (strchr(cnf->home+1, '/') == NULL) { strcpy(dbuf, "/usr"); strncat(dbuf, cnf->home, MAXPATHLEN-5); if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) { chown(dbuf, 0, 0); /* * Skip first "/" and create symlink: * /home -> usr/home */ symlink(dbuf+1, cnf->home); } /* If this falls, fall back to old method */ } strlcpy(dbuf, cnf->home, sizeof(dbuf)); p = dbuf; if (stat(dbuf, &st) == -1) { while ((p = strchr(p + 1, '/')) != NULL) { *p = '\0'; if (stat(dbuf, &st) == -1) { if (mkdir(dbuf, _DEF_DIRMODE) == -1) goto direrr; chown(dbuf, 0, 0); } else if (!S_ISDIR(st.st_mode)) errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); *p = '/'; } } if (stat(dbuf, &st) == -1) { if (mkdir(dbuf, _DEF_DIRMODE) == -1) { direrr: err(EX_OSFILE, "mkdir '%s'", dbuf); } chown(dbuf, 0, 0); } } else if (!S_ISDIR(st.st_mode)) errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); } if ((arg = getarg(args, 'e')) != NULL) cnf->expire_days = atoi(arg->val); if ((arg = getarg(args, 'y')) != NULL) cnf->nispasswd = arg->val; if ((arg = getarg(args, 'p')) != NULL && arg->val) cnf->password_days = atoi(arg->val); if ((arg = getarg(args, 'g')) != NULL) { if (!*(p = arg->val)) /* Handle empty group list specially */ cnf->default_group = ""; else { if ((grp = GETGRNAM(p)) == NULL) { if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) errx(EX_NOUSER, "group `%s' does not exist", p); } cnf->default_group = newstr(grp->gr_name); } } if ((arg = getarg(args, 'L')) != NULL) cnf->default_class = pw_checkname((u_char *)arg->val, 0); if ((arg = getarg(args, 'G')) != NULL && arg->val) { int i = 0; for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { if ((grp = GETGRNAM(p)) == NULL) { if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) errx(EX_NOUSER, "group `%s' does not exist", p); } if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) cnf->groups[i++] = newstr(grp->gr_name); } while (i < cnf->numgroups) cnf->groups[i++] = NULL; } if ((arg = getarg(args, 'k')) != NULL) { if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); } if ((arg = getarg(args, 's')) != NULL) cnf->shell_default = arg->val; if ((arg = getarg(args, 'w')) != NULL) cnf->default_password = boolean_val(arg->val, cnf->default_password); if (mode == M_ADD && getarg(args, 'D')) { if (getarg(args, 'n') != NULL) errx(EX_DATAERR, "can't combine `-D' with `-n name'"); if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { if ((cnf->min_uid = (uid_t) atoi(p)) == 0) cnf->min_uid = 1000; if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) cnf->max_uid = 32000; } if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { if ((cnf->min_gid = (gid_t) atoi(p)) == 0) cnf->min_gid = 1000; if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) cnf->max_gid = 32000; } arg = getarg(args, 'C'); if (write_userconfig(arg ? arg->val : NULL)) return EXIT_SUCCESS; warn("config update"); return EX_IOERR; } if (mode == M_PRINT && getarg(args, 'a')) { int pretty = getarg(args, 'P') != NULL; int v7 = getarg(args, '7') != NULL; SETPWENT(); while ((pwd = GETPWENT()) != NULL) print_user(pwd, pretty, v7); ENDPWENT(); return EXIT_SUCCESS; } if ((a_name = getarg(args, 'n')) != NULL) pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0)); a_uid = getarg(args, 'u'); if (a_uid == NULL) { if (a_name == NULL) errx(EX_DATAERR, "user name or id required"); /* * Determine whether 'n' switch is name or uid - we don't * really don't really care which we have, but we need to * know. */ if (mode != M_ADD && pwd == NULL && strspn(a_name->val, "0123456789") == strlen(a_name->val) && *a_name->val) { (a_uid = a_name)->ch = 'u'; a_name = NULL; } } /* * Update, delete & print require that the user exists */ if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) { if (a_name == NULL && pwd == NULL) /* Try harder */ pwd = GETPWUID(atoi(a_uid->val)); if (pwd == NULL) { if (mode == M_PRINT && getarg(args, 'F')) { fakeuser.pw_name = a_name ? a_name->val : "nouser"; fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; return print_user(&fakeuser, getarg(args, 'P') != NULL, getarg(args, '7') != NULL); } if (a_name == NULL) errx(EX_NOUSER, "no such uid `%s'", a_uid->val); errx(EX_NOUSER, "no such user `%s'", a_name->val); } if (a_name == NULL) /* May be needed later */ a_name = addarg(args, 'n', newstr(pwd->pw_name)); /* * The M_LOCK and M_UNLOCK functions simply add or remove * a "*LOCKED*" prefix from in front of the password to * prevent it decoding correctly, and therefore prevents * access. Of course, this only prevents access via * password authentication (not ssh, kerberos or any * other method that does not use the UNIX password) but * that is a known limitation. */ if (mode == M_LOCK) { if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0) errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str)); if (passtmp == NULL) /* disaster */ errx(EX_UNAVAILABLE, "out of memory"); strcpy(passtmp, locked_str); strcat(passtmp, pwd->pw_passwd); pwd->pw_passwd = passtmp; edited = 1; } else if (mode == M_UNLOCK) { if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0) errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); pwd->pw_passwd += sizeof(locked_str)-1; edited = 1; } else if (mode == M_DELETE) { /* * Handle deletions now */ char file[MAXPATHLEN]; char home[MAXPATHLEN]; uid_t uid = pwd->pw_uid; struct group *gr; char grname[LOGNAMESIZE]; if (strcmp(pwd->pw_name, "root") == 0) errx(EX_DATAERR, "cannot remove user 'root'"); if (!PWALTDIR()) { /* * Remove opie record from /etc/opiekeys */ rmopie(pwd->pw_name); /* * Remove crontabs */ snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name); if (access(file, F_OK) == 0) { sprintf(file, "crontab -u %s -r", pwd->pw_name); system(file); } } /* * Save these for later, since contents of pwd may be * invalidated by deletion */ sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); strlcpy(home, pwd->pw_dir, sizeof(home)); gr = GETGRGID(pwd->pw_gid); if (gr != NULL) strlcpy(grname, gr->gr_name, LOGNAMESIZE); else grname[0] = '\0'; rc = delpwent(pwd); if (rc == -1) err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); else if (rc != 0) { warn("passwd update"); return EX_IOERR; } if (cnf->nispasswd && *cnf->nispasswd=='/') { rc = delnispwent(cnf->nispasswd, a_name->val); if (rc == -1) warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name); else if (rc != 0) warn("WARNING: NIS passwd update"); /* non-fatal */ } grp = GETGRNAM(a_name->val); if (grp != NULL && (grp->gr_mem == NULL || *grp->gr_mem == NULL) && strcmp(a_name->val, grname) == 0) delgrent(GETGRNAM(a_name->val)); SETGRENT(); while ((grp = GETGRENT()) != NULL) { int i, j; char group[MAXLOGNAME]; if (grp->gr_mem != NULL) { for (i = 0; grp->gr_mem[i] != NULL; i++) { if (!strcmp(grp->gr_mem[i], a_name->val)) { for (j = i; grp->gr_mem[j] != NULL; j++) grp->gr_mem[j] = grp->gr_mem[j+1]; strlcpy(group, grp->gr_name, MAXLOGNAME); chggrent(group, grp); } } } } ENDGRENT(); pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); if (!PWALTDIR()) { /* * Remove mail file */ remove(file); /* * Remove at jobs */ if (getpwuid(uid) == NULL) rmat(uid); /* * Remove home directory and contents */ if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { if (stat(home, &st) != -1) { rm_r(home, uid); pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", a_name->val, (long) uid, home, stat(home, &st) == -1 ? "" : "not completely "); } } } return EXIT_SUCCESS; } else if (mode == M_PRINT) return print_user(pwd, getarg(args, 'P') != NULL, getarg(args, '7') != NULL); /* * The rest is edit code */ if ((arg = getarg(args, 'l')) != NULL) { if (strcmp(pwd->pw_name, "root") == 0) errx(EX_DATAERR, "can't rename `root' account"); pwd->pw_name = pw_checkname((u_char *)arg->val, 0); edited = 1; } if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) { pwd->pw_uid = (uid_t) atol(arg->val); edited = 1; if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) errx(EX_DATAERR, "can't change uid of `root' account"); if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); } if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; if (newgid != pwd->pw_gid) { edited = 1; pwd->pw_gid = newgid; } } if ((arg = getarg(args, 'p')) != NULL) { if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { if (pwd->pw_change != 0) { pwd->pw_change = 0; edited = 1; } } else { time_t now = time(NULL); time_t expire = parse_date(now, arg->val); if (pwd->pw_change != expire) { pwd->pw_change = expire; edited = 1; } } } if ((arg = getarg(args, 'e')) != NULL) { if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { if (pwd->pw_expire != 0) { pwd->pw_expire = 0; edited = 1; } } else { time_t now = time(NULL); time_t expire = parse_date(now, arg->val); if (pwd->pw_expire != expire) { pwd->pw_expire = expire; edited = 1; } } } if ((arg = getarg(args, 's')) != NULL) { char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); if (shell == NULL) shell = ""; if (strcmp(shell, pwd->pw_shell) != 0) { pwd->pw_shell = shell; edited = 1; } } if (getarg(args, 'L')) { if (cnf->default_class == NULL) cnf->default_class = ""; if (strcmp(pwd->pw_class, cnf->default_class) != 0) { pwd->pw_class = cnf->default_class; edited = 1; } } if ((arg = getarg(args, 'd')) != NULL) { if (strcmp(pwd->pw_dir, arg->val)) edited = 1; if (stat(pwd->pw_dir = arg->val, &st) == -1) { if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) warnx("WARNING: home `%s' does not exist", pwd->pw_dir); } else if (!S_ISDIR(st.st_mode)) warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); } if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) { login_cap_t *lc; lc = login_getpwclass(pwd); if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) warn("setting crypt(3) format"); login_close(lc); pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); edited = 1; } } else { login_cap_t *lc; /* * Add code */ if (a_name == NULL) /* Required */ errx(EX_DATAERR, "login name required"); else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */ errx(EX_DATAERR, "login name `%s' already exists", a_name->val); /* * Now, set up defaults for a new user */ pwd = &fakeuser; pwd->pw_name = a_name->val; pwd->pw_class = cnf->default_class ? cnf->default_class : ""; pwd->pw_uid = pw_uidpolicy(cnf, args); pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); pwd->pw_change = pw_pwdpolicy(cnf, args); pwd->pw_expire = pw_exppolicy(cnf, args); pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); lc = login_getpwclass(pwd); if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) warn("setting crypt(3) format"); login_close(lc); pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); edited = 1; if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); } /* * Shared add/edit code */ if ((arg = getarg(args, 'c')) != NULL) { char *gecos = pw_checkname((u_char *)arg->val, 1); if (strcmp(pwd->pw_gecos, gecos) != 0) { pwd->pw_gecos = gecos; edited = 1; } } if ((arg = getarg(args, 'h')) != NULL || (arg = getarg(args, 'H')) != NULL) { if (strcmp(arg->val, "-") == 0) { if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { pwd->pw_passwd = "*"; /* No access */ edited = 1; } } else { int fd = atoi(arg->val); int precrypt = (arg->ch == 'H'); int b; int istty = isatty(fd); struct termios t; login_cap_t *lc; if (istty) { if (tcgetattr(fd, &t) == -1) istty = 0; else { struct termios n = t; /* Disable echo */ n.c_lflag &= ~(ECHO); tcsetattr(fd, TCSANOW, &n); printf("%s%spassword for user %s:", (mode == M_UPDATE) ? "new " : "", precrypt ? "encrypted " : "", pwd->pw_name); fflush(stdout); } } b = read(fd, line, sizeof(line) - 1); if (istty) { /* Restore state */ tcsetattr(fd, TCSANOW, &t); fputc('\n', stdout); fflush(stdout); } if (b < 0) { warn("-%c file descriptor", precrypt ? 'H' : 'h'); return EX_IOERR; } line[b] = '\0'; if ((p = strpbrk(line, "\r\n")) != NULL) *p = '\0'; if (!*line) errx(EX_DATAERR, "empty password read on file descriptor %d", fd); if (precrypt) { if (strchr(line, ':') != NULL) return EX_DATAERR; pwd->pw_passwd = line; } else { lc = login_getpwclass(pwd); if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) warn("setting crypt(3) format"); login_close(lc); pwd->pw_passwd = pw_pwcrypt(line); } edited = 1; } } /* * Special case: -N only displays & exits */ if (getarg(args, 'N') != NULL) return print_user(pwd, getarg(args, 'P') != NULL, getarg(args, '7') != NULL); if (mode == M_ADD) { edited = 1; /* Always */ rc = addpwent(pwd); if (rc == -1) { warnx("user '%s' already exists", pwd->pw_name); return EX_IOERR; } else if (rc != 0) { warn("passwd file update"); return EX_IOERR; } if (cnf->nispasswd && *cnf->nispasswd=='/') { rc = addnispwent(cnf->nispasswd, pwd); if (rc == -1) warnx("User '%s' already exists in NIS passwd", pwd->pw_name); else warn("NIS passwd update"); /* NOTE: we treat NIS-only update errors as non-fatal */ } } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) { if (edited) { /* Only updated this if required */ rc = chgpwent(a_name->val, pwd); if (rc == -1) { warnx("user '%s' does not exist (NIS?)", pwd->pw_name); return EX_IOERR; } else if (rc != 0) { warn("passwd file update"); return EX_IOERR; } if ( cnf->nispasswd && *cnf->nispasswd=='/') { rc = chgnispwent(cnf->nispasswd, a_name->val, pwd); if (rc == -1) warn("User '%s' not found in NIS passwd", pwd->pw_name); else warn("NIS passwd update"); /* NOTE: NIS-only update errors are not fatal */ } } } /* * Ok, user is created or changed - now edit group file */ if (mode == M_ADD || getarg(args, 'G') != NULL) { int i; for (i = 0; cnf->groups[i] != NULL; i++) { grp = GETGRNAM(cnf->groups[i]); grp = gr_add(grp, pwd->pw_name); /* * grp can only be NULL in 2 cases: * - the new member is already a member * - a problem with memory occurs * in both cases we want to skip now. */ if (grp == NULL) continue; chggrent(cnf->groups[i], grp); free(grp); } } /* go get a current version of pwd */ pwd = GETPWNAM(a_name->val); if (pwd == NULL) { /* This will fail when we rename, so special case that */ if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) { a_name->val = arg->val; /* update new name */ pwd = GETPWNAM(a_name->val); /* refetch renamed rec */ } } if (pwd == NULL) /* can't go on without this */ errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val); grp = GETGRGID(pwd->pw_gid); pw_log(cnf, mode, W_USER, "%s(%ld):%s(%ld):%s:%s:%s", pwd->pw_name, (long) pwd->pw_uid, grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); /* * If adding, let's touch and chown the user's mail file. This is not * strictly necessary under BSD with a 0755 maildir but it also * doesn't hurt anything to create the empty mailfile */ if (mode == M_ADD) { if (!PWALTDIR()) { sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & * mtime */ chown(line, pwd->pw_uid, pwd->pw_gid); } } /* * Let's create and populate the user's home directory. Note * that this also `works' for editing users if -m is used, but * existing files will *not* be overwritten. */ if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { copymkdir(pwd->pw_dir, cnf->dotdir, cnf->homemode, pwd->pw_uid, pwd->pw_gid); pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); } /* * Finally, send mail to the new user as well, if we are asked to */ if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); if (pfp == NULL) warn("sendmail"); else { fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); while (fgets(line, sizeof(line), fp) != NULL) { /* Do substitutions? */ fputs(line, pfp); } pclose(pfp); pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", pwd->pw_name, (long) pwd->pw_uid); } fclose(fp); } return EXIT_SUCCESS; }
int rm_r(const char *path) { int ret = 0; DIR *dir; struct dirent *dent; int r; if (path == NULL) { opkg_perror(ERROR, "Missing directory parameter"); return -1; } dir = opendir(path); if (dir == NULL) { opkg_perror(ERROR, "Failed to open dir %s", path); return -1; } r = fchdir(dirfd(dir)); if (r == -1) { opkg_perror(ERROR, "Failed to change to dir %s", path); closedir(dir); return -1; } while (1) { errno = 0; dent = readdir(dir); if (dent == NULL) { if (errno) { opkg_perror(ERROR, "Failed to read dir %s", path); ret = -1; } break; } if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue; #ifdef _BSD_SOURCE if (dent->d_type == DT_DIR) { ret = rm_r(dent->d_name); if (ret == -1) break; continue; } else if (dent->d_type == DT_UNKNOWN) #endif { struct stat st; ret = lstat(dent->d_name, &st); if (ret == -1) { opkg_perror(ERROR, "Failed to lstat %s", dent->d_name); break; } if (S_ISDIR(st.st_mode)) { ret = rm_r(dent->d_name); if (ret == -1) break; continue; } } ret = unlink(dent->d_name); if (ret == -1) { opkg_perror(ERROR, "Failed to unlink %s", dent->d_name); break; } } r = chdir(".."); if (r == -1) { ret = -1; opkg_perror(ERROR, "Failed to change to dir %s/..", path); } r = rmdir(path); if (r == -1) { ret = -1; opkg_perror(ERROR, "Failed to remove dir %s", path); } r = closedir(dir); if (r == -1) { ret = -1; opkg_perror(ERROR, "Failed to close dir %s", path); } return ret; }
int recursively_install(char* pkg_name, char* pkg_version, char* install_root_name, char* link_to_root, char* overlay_path, int is_upgrade, int overwrite_config, int overwrite_other_package_files, char* tmp_dir, opkg_conf* conf, string_map* package_data, string_map* install_called_pkgs) { int err=0; /* variables not allocated in this function, do not need to be freed */ string_map* install_pkg_data = get_package_with_version(package_data, pkg_name, pkg_version); char* src_file_path = get_string_map_element(install_pkg_data, "Install-File-Location"); char* src_id = get_string_map_element(install_pkg_data, "Source-ID"); char* pkg_filename = get_string_map_element(install_pkg_data, "Filename"); string_map* pkg_dependencies = get_string_map_element(install_pkg_data, "Required-Depends"); char* install_root_path = get_string_map_element(conf->dest_names, install_root_name); char* link_root_path = link_to_root != NULL && safe_strcmp(link_to_root, install_root_name) != 0 ? get_string_map_element(conf->dest_names, link_to_root) : NULL; char* base_url = NULL; /* variables that may need to be freed */ char* pkg_dest = NULL; string_map* files_to_link = NULL; char* info_dir = NULL; char* control_name_prefix = NULL; char* list_file_name = NULL; string_map* conf_files = NULL; string_map* copied_conf_files = NULL; int install_root_len = strlen(install_root_path); char* fs_terminated_install_root = install_root_path[install_root_len-1] == '/' ? strdup(install_root_path) : dynamic_strcat(2, install_root_path, "/"); int overlay_root_len; char* fs_terminated_overlay_root = NULL; if(overlay_path != NULL) { overlay_root_len = strlen(overlay_path); fs_terminated_overlay_root = overlay_path[overlay_root_len-1] == '/' ? strdup(overlay_path) : dynamic_strcat(2, overlay_path, "/"); } else { fs_terminated_overlay_root = strdup(fs_terminated_install_root); } int link_root_len; char* fs_terminated_link_root = NULL; if(link_root_path != NULL) { link_root_len = strlen(link_root_path); fs_terminated_link_root = link_root_path[link_root_len-1] == '/' ? strdup(link_root_path) : dynamic_strcat(2, link_root_path, "/"); } set_string_map_element(install_called_pkgs, pkg_name, strdup("D")); if(pkg_dependencies != NULL) { //recurse unsigned long num_deps; char** deps = get_string_map_keys(pkg_dependencies, &num_deps); int dep_index; for(dep_index=0; err == 0 && dep_index < num_deps ; dep_index++) { if(get_string_map_element(install_called_pkgs, deps[dep_index]) == NULL ) { char** dep_def = get_string_map_element(pkg_dependencies, deps[dep_index]); int is_current; char* matching_version; string_map* dep_pkg = get_package_current_or_latest_matching(package_data, deps[dep_index], dep_def, &is_current, &matching_version); if(dep_pkg != NULL) { char* dep_status = get_string_map_element(dep_pkg, "Status"); if(strstr(dep_status, " half-installed") != NULL) { err = recursively_install(deps[dep_index], matching_version, install_root_name, link_to_root, overlay_path, is_upgrade, overwrite_config, overwrite_other_package_files, tmp_dir, conf, package_data, install_called_pkgs); } } else { err = 1; } } } } if(install_root_path == NULL || ( src_id == NULL && install_pkg_data == NULL) || (pkg_filename == NULL && install_pkg_data == NULL) ) { //sanity check err = 1; } if(err == 0) { printf("Preparing to install package %s...\n", pkg_name); } if(err == 0 && src_file_path == NULL) { //determine source url string_map* src_lists[2] = { conf->gzip_sources, conf->plain_sources }; int src_list_index; for(src_list_index=0; src_list_index < 2 && base_url == NULL; src_list_index++) { base_url = (char*)get_string_map_element(src_lists[src_list_index], src_id); } err = base_url == NULL ? 1 : err; if(err == 1) { fprintf(stderr, "ERROR: Could determine download URL for package %s\n", pkg_name); } } if(err == 0 && src_file_path == NULL) { //download package printf("\tDownloading...\n"); char* src_url = dynamic_strcat(3, base_url, "/", pkg_filename); pkg_dest = dynamic_strcat(3, tmp_dir, "/", pkg_name); FILE* package_file = fopen(pkg_dest, "w"); if(package_file != NULL) { err = write_url_to_stream(src_url, "gpkg", NULL, package_file, NULL); fclose(package_file); } else { err = 1; } if(err == 1) { fprintf(stderr, "ERROR: Could not download package %s\n", pkg_name); } free(src_url); } if(err == 0 && src_file_path != NULL) { pkg_dest = strdup(src_file_path); } if(err == 0 && src_file_path == NULL) { //check md5sum char* md5sum = file_md5sum_alloc(pkg_dest); char* expected_md5sum = (char*)get_string_map_element(install_pkg_data, "MD5Sum"); //printf("md5sum = %s\n", md5sum); //printf("package md5sum = %s\n", (char*)get_string_map_element(install_pkg_data, "MD5Sum")); if(md5sum == NULL || expected_md5sum == NULL) { fprintf(stderr, "ERROR: Expected MD5Sum for %s not specified, cannot verify package\n", pkg_name); err = 1; } else if (safe_strcmp(md5sum, expected_md5sum) != 0) { fprintf(stderr, "ERROR: MD5Sum mismatch for %s package\n", pkg_name); fprintf(stderr, " Expected: %s\n", expected_md5sum); fprintf(stderr, " Downloaded: %s\n\n", md5sum); err = 1; } else { printf("\tDownloaded %s successfully.\n\tInstalling %s...\n", pkg_name, pkg_name); } if(md5sum != NULL) { free(md5sum); } } if(err == 0) { // Extract list file contaiing list of files to install info_dir = dynamic_strcat(2, fs_terminated_overlay_root, "usr/lib/opkg/info"); control_name_prefix = dynamic_strcat(4, info_dir, "/", pkg_name, "."); list_file_name = dynamic_strcat(4, info_dir, "/", pkg_name, ".list"); mkdir_p(info_dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH ); FILE* list_file = fopen(list_file_name, "w"); deb_extract( pkg_dest, list_file, extract_quiet | extract_data_tar_gz | extract_list, NULL, NULL, &err); fclose(list_file); if(err) { rm_r(list_file_name); fprintf(stderr, "ERROR: could not extract file list from packge %s.\n", pkg_name); fprintf(stderr, " package file may be corrupt\n\n"); } } if(err == 0) { //extract control files deb_extract( pkg_dest, stderr, extract_control_tar_gz | extract_all_to_fs| extract_preserve_date | extract_unconditional, control_name_prefix, NULL, &err); if(err) { fprintf(stderr, "ERROR: could not extract control files from packge %s.\n", pkg_name); fprintf(stderr, " package file may be corrupt\n\n"); } } if(err == 0) { //check for file conflicts & correct list file to contain real root name in file paths unsigned long num_list_lines; char** list_file_lines = get_file_lines(list_file_name, &num_list_lines); char* conf_file_path = dynamic_strcat(4, info_dir, "/", pkg_name, ".conffiles"); if(path_exists(conf_file_path)) { unsigned long num_conf_lines; char** conf_file_lines = get_file_lines(conf_file_path, &num_conf_lines); int conf_line_index; conf_files = initialize_string_map(1); for(conf_line_index=0; conf_line_index < num_conf_lines; conf_line_index++) { char* adjusted_conf_path = dynamic_strcat(2, fs_terminated_install_root, conf_file_lines[conf_line_index] + 1); set_string_map_element(conf_files, adjusted_conf_path, strdup("D")); free(adjusted_conf_path); } free_null_terminated_string_array(conf_file_lines); } free(conf_file_path); FILE* list_file = fopen(list_file_name, "w"); int line_index; for(line_index=0; line_index < num_list_lines && (!err) ; line_index++) { int line_len = strlen( list_file_lines[line_index] ); if(line_len > 2) { if(list_file_lines[line_index][0] == '.' && list_file_lines[line_index][1] == '/' && list_file_lines[line_index][line_len-1] != '/') { char* adjusted_file_path = dynamic_strcat(2, fs_terminated_install_root, list_file_lines[line_index] + 2); int is_conf_file = conf_files != NULL ? (get_string_map_element(conf_files, adjusted_file_path) != NULL ? 1 : 0) : 0; if(strcmp(pkg_name, "opkg") == 0 && strcmp(list_file_lines[line_index], "./bin/opkg") == 0 && path_exists("/bin/opkg") == PATH_IS_SYMLINK) { //very special case: we're installing opkg, and here all the preliminary checks have already been passed //remove symlink placeholder to gpkg from /bin/opkg if it exists rm_r("/bin/opkg"); } err = path_exists(adjusted_file_path) && is_conf_file == 0 && overwrite_other_package_files == 0 ? 1 : 0; if(err) { fprintf(stderr, "ERROR: file '%s'\n", adjusted_file_path); fprintf(stderr, " from package %s already exists.\n\n", pkg_name); } else { fprintf(list_file, "%s\n", adjusted_file_path); if(link_root_path != NULL) { char* link_to_path = dynamic_strcat(2, fs_terminated_link_root, list_file_lines[line_index] + 2); files_to_link = files_to_link == NULL ? initialize_string_map(1) : files_to_link; set_string_map_element(files_to_link, adjusted_file_path, link_to_path); //don't free link_to_path, should be freed with files_to_link map } if(is_conf_file && path_exists(adjusted_file_path) && overwrite_config == 0) { char* tmp_conf_path = dynamic_strcat(2, tmp_dir, adjusted_file_path); cp(adjusted_file_path, tmp_conf_path); copied_conf_files = copied_conf_files == NULL ? initialize_string_map(1) : copied_conf_files; set_string_map_element(copied_conf_files, adjusted_file_path, tmp_conf_path); //don't free tmp_conf_path, should be freed with copied_conf_files map } } free(adjusted_file_path); } } } fclose(list_file); if(list_file_lines != NULL) { free_null_terminated_string_array(list_file_lines); } } if(err == 0) { //run preinst err = run_script_if_exists(install_root_path, link_root_path, pkg_name, "preinst", (is_upgrade ? "upgrade" : "install") ); } if(err == 0) { //extract package files deb_extract( pkg_dest, stderr, extract_data_tar_gz | extract_all_to_fs| extract_preserve_date| extract_unconditional, fs_terminated_overlay_root, NULL, &err); if(err) { fprintf(stderr, "ERROR: could not extract application files from packge %s.\n", pkg_name); fprintf(stderr, " package file may be corrupt\n\n"); } //move any conf files back if(copied_conf_files != NULL) { unsigned long num_conf_paths; char** conf_paths = get_string_map_keys(copied_conf_files, &num_conf_paths); int conf_index; for(conf_index=0; conf_index < num_conf_paths; conf_index++) { char* tmp_conf_path = get_string_map_element(copied_conf_files, conf_paths[conf_index]); cp(tmp_conf_path, conf_paths[conf_index]); } destroy_string_map(copied_conf_files, DESTROY_MODE_FREE_VALUES, &num_conf_paths); if(conf_paths != NULL ) { free_null_terminated_string_array(conf_paths); } copied_conf_files = NULL; } } if(err == 0 && files_to_link != NULL) { unsigned long num_files; char** real_files = get_string_map_keys(files_to_link, &num_files); int file_index; if(num_files > 0) { char* link_file_name = dynamic_strcat(4, info_dir, "/", pkg_name, ".linked"); FILE* link_file = fopen(link_file_name, "w"); for(file_index=0; link_file != NULL && file_index < num_files; file_index++) { char* link_path = get_string_map_element(files_to_link, real_files[file_index]); if(!path_exists(link_path)) { int sym_success; mkdir_p(link_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH ); rm_r(link_path); sym_success = symlink(real_files[file_index], link_path); fprintf(link_file, "%s\n", link_path); } } if(link_file != NULL) { fclose(link_file); } free(link_file_name); } destroy_string_map(files_to_link, DESTROY_MODE_FREE_VALUES, &num_files); free_null_terminated_string_array(real_files); files_to_link = NULL; } if(err == 0) { //run postinst int warn = run_script_if_exists(install_root_path, link_root_path, pkg_name, "postinst", (is_upgrade ? "upgrade" : "install") ); if(warn != 0) { fprintf(stderr, "Warning: postinstall script failed for package %s.\n", pkg_name); } } if(err == 0) { // remove downloaded package file in tmp dir & print success if(src_file_path == NULL) { rm_r(pkg_dest); } printf("\tSuccessfully installed %s.\n", pkg_name); } //cleanup unsigned long num_destroyed; free_if_not_null(pkg_dest); free_if_not_null(info_dir); free_if_not_null(control_name_prefix); free_if_not_null(list_file_name); free_if_not_null(fs_terminated_install_root); free_if_not_null(fs_terminated_overlay_root); free_if_not_null(fs_terminated_link_root); if(files_to_link != NULL) { destroy_string_map(files_to_link, DESTROY_MODE_FREE_VALUES, &num_destroyed); } if(conf_files != NULL) { destroy_string_map(conf_files, DESTROY_MODE_FREE_VALUES, &num_destroyed); } if(copied_conf_files!= NULL){ destroy_string_map(copied_conf_files, DESTROY_MODE_FREE_VALUES, &num_destroyed); } return err; }
//void do_install(opkg_conf* conf, char* pkg_name, char* install_root_name, char* link_root_name, char** version_criteria) void do_install(opkg_conf* conf, string_map* pkgs, char* install_root_name, char* link_root_name, int is_upgrade, int overwrite_config, int overwrite_other_package_files, int force_reinstall, char* tmp_root) { string_map* package_data = initialize_string_map(1); string_map* matching_packages = initialize_string_map(1); string_map* pkgs_from_file = initialize_string_map(1); unsigned long num_destroyed; char* install_root_path = (char*)get_string_map_element(conf->dest_names, install_root_name); char* overlay_path = NULL; // no special treatment of overlay, can be reenabled by setting this variable here if we ever need it char* test_dir = dynamic_strcat(2, (overlay_path != NULL ? overlay_path : install_root_path), "/usr/lib/opkg/info"); if(!create_dir_and_test_writable(test_dir)) { fprintf(stderr, "ERROR: Specified install destination is not writable, exiting\n"); exit(1); } free(test_dir); if(install_root_path == NULL) { printf("ERROR: No destination %s found, cannot install\n\n", install_root_name); exit(1); } char* tmp_dir = (char*)malloc(1024); if(create_tmp_dir(tmp_root == NULL ? "/tmp" : tmp_root, &tmp_dir) != 0) { fprintf(stderr, "ERROR: Could not create tmp dir, exiting\n"); exit(1); } /* Determine all packages to install by first loading all package names, status & dependencies (and no other variables) */ load_all_package_data(conf, package_data, matching_packages, NULL, LOAD_MINIMAL_PKG_VARIABLES_FOR_ALL, install_root_name, 1, NULL ); destroy_string_map(matching_packages, DESTROY_MODE_FREE_VALUES, &num_destroyed); /* determine list of all packiages we are about to install, including dependencies */ string_map* install_pkgs_map = initialize_string_map(1); char** install_pkg_list = NULL; unsigned long install_pkg_list_len = 0; char* unsatisfied_dep_err = NULL; /* new string map var with all pkgs to install = pkgs, keys = version */ unsigned long num_pkg_names; char** pkg_names = get_string_map_keys(pkgs, &num_pkg_names); int pkg_name_index; /* * Load data for any packages being installed via ipk and * determine if any packages we are about to install * provide anything, and if so set package we are installing to preferred */ string_map* preferred_provides = initialize_string_map(1); for(pkg_name_index=0;pkg_name_index < num_pkg_names; pkg_name_index++) { char* pkg_name = pkg_names[pkg_name_index]; char** version_criteria = get_string_map_element(pkgs, pkg_name); char* install_pkg_version = NULL; int install_pkg_is_current; /* deal with case where we're installing from file */ if(path_exists(pkg_name)) { //installing from file char* pkg_file = pkg_name; //extract control files int err = 0; char* tmp_control = dynamic_strcat(2, tmp_dir, "/tmp_ctrl"); char* tmp_control_prefix = dynamic_strcat(2, tmp_control, "/tmp."); char* tmp_control_name = dynamic_strcat(2, tmp_control_prefix, "control"); mkdir_p(tmp_control, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH ); deb_extract( pkg_file, stderr, extract_control_tar_gz | extract_all_to_fs| extract_preserve_date | extract_unconditional, tmp_control_prefix, NULL, &err); if(err != 0) { fprintf(stderr, "ERROR: %s is not a valid package file, cannot install\n", pkg_file); rm_r(tmp_dir); exit(1); } string_map* tmp_control_pkg_data = initialize_string_map(1); matching_packages = initialize_string_map(1); load_package_data(tmp_control_name, 0, tmp_control_pkg_data, matching_packages, NULL, LOAD_ALL_PKG_VARIABLES, NULL, NULL); unsigned long num_ctrl_names; char** ctrl_name_list = get_string_map_keys(tmp_control_pkg_data, &num_ctrl_names); destroy_string_map(matching_packages, DESTROY_MODE_FREE_VALUES, &num_destroyed); err = 1; //set back to 0 when data successfully loaded if(num_ctrl_names > 0) { int ctrl_name_index; for(ctrl_name_index=0; ctrl_name_list[ctrl_name_index] != NULL; ctrl_name_index++) { if( strcmp(ctrl_name_list[ctrl_name_index], PROVIDES_STRING) != 0) { pkg_name = strdup(ctrl_name_list[ctrl_name_index]); } } char* version = NULL; int is_current; string_map* pkg_info = get_package_current_or_latest(tmp_control_pkg_data, pkg_name, &is_current, &version); if(pkg_info != NULL) { err = 0; set_string_map_element(pkg_info, "Install-File-Location", strdup(pkg_file)); set_string_map_element(pkg_info, "Version", version); //we need to save this, since we are going to set a special version to make sure data doesn't get over-written later, also no need to free version now char* special_version = dynamic_strcat(2, version, "@@_FILE_INSTALL_VERSION_@@"); char** new_version_criteria = malloc(3*sizeof(char*)); new_version_criteria[0] = strdup("="); new_version_criteria[1] = special_version; new_version_criteria[2] = NULL; version_criteria = new_version_criteria; string_map* all_current_versions = get_string_map_element(package_data, pkg_name); if(all_current_versions == NULL) { all_current_versions=initialize_string_map(1); set_string_map_element(package_data, pkg_name, all_current_versions); } set_string_map_element(all_current_versions, special_version, pkg_info); set_string_map_element(all_current_versions, LATEST_VERSION_STRING, special_version); free(pkg_names[pkg_name_index]); pkg_names[pkg_name_index] = strdup(pkg_name); set_string_map_element(pkgs, pkg_name, copy_null_terminated_string_array(new_version_criteria)); set_string_map_element(pkgs_from_file, pkg_name, strdup("D")); } } free_null_terminated_string_array(ctrl_name_list); if(err != 0) { fprintf(stderr, "ERROR: %s is not a valid package file, cannot install\n", pkg_file); rm_r(tmp_dir); exit(1); } free_if_not_null(tmp_control); free_if_not_null(tmp_control_prefix); free_if_not_null(tmp_control_name); rm_r(tmp_control); } /* determine if package provides anything, and set this package to preferred if so*/ string_map* install_pkg_data = get_package_current_or_latest_matching(package_data, pkg_name, version_criteria, &install_pkg_is_current, &install_pkg_version); if(install_pkg_data != NULL) { char* provides_str = get_string_map_element(install_pkg_data, "Provides"); if(provides_str != NULL) { if(strlen(provides_str) > 0) { unsigned long num_provides; char package_separators[] = {' ', ',', ':', ';', '\'', '\"', '\t', '\r', '\n'}; char** provides_list = split_on_separators(provides_str, package_separators, 9, -1, 0, &num_provides); int provides_index; char* provides_unique_key = dynamic_strcat(3, pkg_name, "@", install_pkg_version); for(provides_index=0; provides_index < num_provides; provides_index++) { char* provides_name = strdup(provides_list[provides_index]); char* eq = strchr(provides_name, '='); if(eq != NULL) { *eq = '\0' ; } if(strlen(provides_name) > 0) { set_string_map_element(preferred_provides, provides_name, strdup(provides_unique_key)); } } } } } } /* reload with new preferred_provides */ free_recursive_package_vars(package_data); matching_packages = initialize_string_map(1); load_all_package_data(conf, package_data, matching_packages, NULL, LOAD_MINIMAL_PKG_VARIABLES_FOR_ALL, install_root_name, 1, preferred_provides ); destroy_string_map(matching_packages, DESTROY_MODE_FREE_VALUES, &num_destroyed); /* load data and do sanity checks for packages we are about to install */ for(pkg_name_index=0;pkg_name_index < num_pkg_names; pkg_name_index++) { char* pkg_name = pkg_names[pkg_name_index]; char** version_criteria = get_string_map_element(pkgs, pkg_name); char* install_pkg_version = NULL; int install_pkg_is_current; load_recursive_package_data_variables(package_data, pkg_name, 1, 0, 0); // load required-depends for package of interest only string_map* install_pkg_data = get_package_current_or_latest_matching(package_data, pkg_name, version_criteria, &install_pkg_is_current, &install_pkg_version); char* install_status = install_pkg_data == NULL ? NULL : get_string_map_element(install_pkg_data, "Status"); if(install_status != NULL) { char** old_el = set_string_map_element(install_pkgs_map, pkg_name, copy_null_terminated_string_array(version_criteria) ); if(old_el != NULL){ free_null_terminated_string_array(old_el); } string_map* install_pkg_depend_map = get_string_map_element(install_pkg_data, "Required-Depends"); if(install_pkg_depend_map != NULL) { unsigned long num_keys; char** load_detail_pkgs = get_string_map_keys(install_pkg_depend_map, &num_keys); int ldp_index; for(ldp_index=0;ldp_index < num_keys && unsatisfied_dep_err == NULL; ldp_index++) { char* dep_name = load_detail_pkgs[ldp_index]; char** dep_def= get_string_map_element(install_pkg_depend_map, dep_name); if(get_string_map_element(install_pkgs_map, dep_name) != NULL) { /* * We really should check here whether old dependency def can be reconciled with the new one, and report an error if it can't * Right now we just use the heuristic that top-level (user specified, not dependency) package defs get preference, followed * by first dependency encountered. * * Since right now versioning features aren't really being used very much other than kernel dependencies in Gargoyle/OpenWrt * I'm just leaving this comment here as a reminder that this should be addressed at some point rather than messing with it now * */ dep_def = get_string_map_element(install_pkgs_map, dep_name); } else { set_string_map_element(install_pkgs_map, dep_name, copy_null_terminated_string_array(dep_def)); } //error checking, check that dependency definition exists char* latest_version = NULL; int latest_is_current = 0; string_map* dep_info = get_package_current_or_latest_matching(package_data, dep_name, dep_def, &latest_is_current, &latest_version); //check if we have a version installed different than what is required int have_current; char* current_version = NULL; string_map* cur_info = get_package_current_or_latest(package_data, dep_name, &have_current, ¤t_version); if(have_current && (latest_is_current == 0 || dep_info == NULL)) { //should only get here if dep_def[1] is not null (version mismatch doesn't make sense if no version is specified) char* cur_status = get_string_map_element(cur_info, "Status"); if(strstr(cur_status, " hold ") != NULL) { unsatisfied_dep_err = dynamic_strcat(11, "ERROR: Dependency ", dep_name, " (", dep_def[0], " ", dep_def[1], ") of package ", pkg_name, " is installed,\n\t\tbut has incompatible version ", current_version, " and is marked as 'hold'"); } else { unsatisfied_dep_err = dynamic_strcat(10, "ERROR: Dependency ", dep_name, " (", dep_def[0], " ", dep_def[1], ") of package ", pkg_name, " is installed,\n\t\tbut has incompatible version ", current_version); } } free_if_not_null(current_version); free_if_not_null(latest_version); // check that dependency definition exists if(unsatisfied_dep_err == NULL && dep_info == NULL) { if(dep_def[1] != NULL) { unsatisfied_dep_err = dynamic_strcat(9, "ERROR: Dependency ", dep_name, " (", dep_def[0], " ", dep_def[1], ") of package ", pkg_name, " cannot be found, try updating your package lists"); } else { unsatisfied_dep_err = dynamic_strcat(5, "ERROR: Dependency ", dep_name, " of package ", pkg_name, " cannot be found, try updating your package lists"); } } } free_null_terminated_string_array(load_detail_pkgs); } } install_status = install_pkg_data == NULL ? NULL : get_string_map_element(install_pkg_data, "Status"); /* error checking before we start install */ if(install_pkg_data == NULL || install_status == NULL) { fprintf(stderr, "ERROR: No package named %s found, try updating your package lists\n\n", pkg_name); rm_r(tmp_dir); exit(1); } if(strstr(install_status, " installed") != NULL) { if(force_reinstall) { fprintf(stderr, "WARNING: Package %s is already installed, forcing removal and reinstallation\n\n", pkg_name); free_package_data(package_data); string_map* rm_pkg = initialize_string_map(1); set_string_map_element(rm_pkg, pkg_name, alloc_depend_def(NULL)); do_remove(conf, rm_pkg, (overwrite_config ? 0 : 1), 0, 1, 0, tmp_root); //restart install return do_install(conf, pkgs, install_root_name, link_root_name, is_upgrade, overwrite_config, overwrite_other_package_files, force_reinstall, tmp_root); } else { fprintf(stderr, "WARNING: Package %s is already installed, ignoring\n", pkg_name); fprintf(stderr, " Use --force-reinstall to force reinstallation\n\n"); char** old_el = remove_string_map_element(install_pkgs_map, pkg_name); if(old_el != NULL){ free_null_terminated_string_array(old_el); }; } } if(unsatisfied_dep_err != NULL) { fprintf(stderr, "%s\n", unsatisfied_dep_err); rm_r(tmp_dir); exit(1); } } /* load more detailed data on packages we are about to install */ free_recursive_package_vars(package_data); /* note: whacks install_pkg_depend_map */ string_map* parameters = initialize_string_map(1); matching_packages = initialize_string_map(1); set_string_map_element(parameters, "package-list", install_pkgs_map); load_all_package_data(conf, package_data, matching_packages, parameters, LOAD_MINIMAL_FOR_ALL_PKGS_ALL_FOR_MATCHING, install_root_name, 0, preferred_provides); unsigned long from_file_pkg_list_len; char** from_file_pkg_list = get_string_map_keys(pkgs_from_file, &from_file_pkg_list_len); int from_file_index; for(from_file_index=0; from_file_index < from_file_pkg_list_len; from_file_index++) { char* old = set_string_map_element(matching_packages, from_file_pkg_list[from_file_index], strdup("D")); free_if_not_null(old); } free_null_terminated_string_array(from_file_pkg_list); install_pkg_list = get_string_map_keys(matching_packages, &install_pkg_list_len); destroy_string_map(matching_packages, DESTROY_MODE_FREE_VALUES, &num_destroyed); destroy_string_map(parameters, DESTROY_MODE_IGNORE_VALUES, &num_destroyed); char* all_pkg_list_str = join_strs(", ", install_pkg_list, install_pkg_list_len, 0, 0); uint64_t combined_size = 0; int pkg_index; for(pkg_index=0; pkg_index < install_pkg_list_len; pkg_index++) { char** match_criteria = get_string_map_element(install_pkgs_map, install_pkg_list[pkg_index]); string_map* pkg = get_package_current_or_latest_matching(package_data, install_pkg_list[pkg_index], match_criteria, NULL, NULL); char* next_size_str = get_string_map_element(pkg, "Installed-Size"); uint64_t next_size = 0; if(sscanf(next_size_str, SCANFU64, &next_size) > 0) { combined_size = combined_size + next_size; } } uint64_t root_size = destination_bytes_free(conf, install_root_name); if(combined_size >= root_size ) { fprintf(stderr, "ERROR: Not enough space in destination %s to install specified packages:\n\t%s\n\n", install_root_name, all_pkg_list_str); rm_r(tmp_dir); exit(1); } if(all_pkg_list_str != NULL) { printf("Preparing to install the following packages, which will require " SCANFU64 " bytes:\n\t%s\n\n", combined_size, all_pkg_list_str); } else { fprintf(stderr, "No packages to install.\n\n"); } /* Set status of new required packages to half-installed, set user-installed on requested package, installed time on all */ char* install_root_status_path = dynamic_strcat(2, install_root_path, "/usr/lib/opkg/status"); string_map* install_root_status = initialize_string_map(1); matching_packages = initialize_string_map(1); if(path_exists(install_root_status_path)) { load_package_data(install_root_status_path, 0, install_root_status, matching_packages, NULL, LOAD_ALL_PKG_VARIABLES, install_root_name, preferred_provides); } destroy_string_map(matching_packages, DESTROY_MODE_FREE_VALUES, &num_destroyed); time_t now = time(NULL); char install_time[20]; sprintf(install_time, "%lu", now); for(pkg_index=0; pkg_index < install_pkg_list_len; pkg_index++) { int is_installed; char* install_version = NULL; char** match_criteria = get_string_map_element(install_pkgs_map, install_pkg_list[pkg_index]); string_map* pkg = get_package_current_or_latest_matching(package_data, install_pkg_list[pkg_index], match_criteria, &is_installed, &install_version); if(is_installed == 0) /* should never be true, but check anyway */ { char* old_status = remove_string_map_element(pkg, "Status"); free(old_status); char* status_parts[3] = { "install", "ok", "half-installed" }; status_parts[1] = get_string_map_element(pkgs, install_pkg_list[pkg_index]) != NULL ? "user" : status_parts[1]; char* new_status = dynamic_strcat(5, status_parts[0], " ", status_parts[1], " ", status_parts[2]); set_string_map_element(pkg, "Status", new_status); set_string_map_element(pkg, "Installed-Time", strdup(install_time)); set_string_map_element(pkg, "Install-Destination", strdup(install_root_name)); if(link_root_name != NULL) { set_string_map_element(pkg, "Link-Destination", strdup(link_root_name)); } add_package_data(install_root_status, &pkg, install_pkg_list[pkg_index], install_version, NULL); /* Note: we just added pkg data structure from package_data to install_root_status, Be careful on cleanup! */ } } save_package_data_as_status_file(install_root_status, install_root_status_path); string_map* install_called_pkgs = initialize_string_map(1); int err = 0; for(pkg_name_index=0;pkg_name_index < num_pkg_names; pkg_name_index++) { char* pkg_name = pkg_names[pkg_name_index]; if(get_string_map_element(install_pkgs_map, pkg_name) != NULL && get_string_map_element(install_called_pkgs, pkg_name) == NULL) { int install_pkg_is_current; char* install_pkg_version = NULL; char** version_criteria = get_string_map_element(pkgs, pkg_name); get_package_current_or_latest_matching(package_data, pkg_name, version_criteria, &install_pkg_is_current, &install_pkg_version); err = recursively_install(pkg_name, install_pkg_version, install_root_name, link_root_name, overlay_path, is_upgrade, overwrite_config, overwrite_other_package_files, tmp_dir, conf, package_data, install_called_pkgs); free_if_not_null(install_pkg_version); } } if(err) { fprintf(stderr, "An error occurred during Installation, removing partially installed packages.\n"); unsigned long num_install_called_pkgs; char** install_called_pkg_list = get_string_map_keys(install_called_pkgs, &num_install_called_pkgs); int pkg_index; for(pkg_index=0; pkg_index < num_install_called_pkgs; pkg_index++) { remove_individual_package(install_called_pkg_list[pkg_index], conf, package_data, tmp_dir, 0, 0); } free_null_terminated_string_array(install_called_pkg_list); //call remove function to do cleanup of partial install //DO NOT EXIT HERE, fixup status file below } //remove tmp dir -- need to do this whether or not there is an error rm_r(tmp_dir); free(tmp_dir); //set status of new packages to installed on success, and remove on failure for(pkg_index=0; pkg_index < install_pkg_list_len; pkg_index++) { /* no need to check version, should only be one installed version at a time... */ string_map* pkg = get_package_current_or_latest(install_root_status, install_pkg_list[pkg_index], NULL, NULL); if(pkg != NULL) { if(!err) { char* status = get_string_map_element(pkg, "Status"); if(strstr(status, " half-installed") != NULL) { char* status_parts[3] = { "install", "ok", "installed" }; status_parts[1] = get_string_map_element(pkgs, install_pkg_list[pkg_index]) != NULL ? "user" : status_parts[1]; char* new_status = dynamic_strcat(5, status_parts[0], " ", status_parts[1], " ", status_parts[2]); char* old_status = set_string_map_element(pkg, "Status", new_status); free_if_not_null(old_status); } } else { string_map* all_pkg_versions = remove_string_map_element(install_root_status, install_pkg_list[pkg_index]); free_all_package_versions(all_pkg_versions); } } } save_package_data_as_status_file(install_root_status, install_root_status_path); if(!err) { if(all_pkg_list_str != NULL) { printf("Installation of packages successful.\n\n"); } } else { printf("Finished removing partially installed packages.\n\n"); } }
int main(int argc, char** argv) { if(argc < 2) show_usage(argc, argv); static struct option long_options[] = { { "force", no_argument, NULL, 'f'}, { "recursive", no_argument, NULL, 'r'}, { "dir", no_argument, NULL, 'd'}, { "verbose", no_argument, NULL, 'v'}, { "help", no_argument, NULL, 'h'}, { "version", no_argument, NULL, 'q'}, { NULL, 0, NULL, 0 } }; int force = 0; int rmmode = 0; /* 1: recursive, 2: empty dir */ int verbose = 0; int c, idx; while((c = getopt_long(argc, argv, "frRdv", long_options, &idx)) != -1) { switch(c) { case 'f': force = 1; break; case 'r': case 'R': rmmode = 1; break; case 'd': rmmode = 2; break; case 'v': verbose = 1; break; case 'q': show_version(argc, argv); break; case 'h': case '?': show_usage(argc, argv); break; default: abort(); } } if(optind >= argc) show_usage(argc, argv); void rm(char* file) { if(unlink(file) != 0) { if(force) return; fprintf(stderr, "%s: %s: can not remove: %s\n", argv[0], file, strerror(errno)); exit(-1); } if(verbose) fprintf(stdout, "%s: removed\n", file); } int rm_r(char* dir) { DIR* d = opendir(dir); if(!d) { if(force) return -1; fprintf(stderr, "%s: %s: %s\n", argv[0], dir, strerror(errno)); exit(-1); } struct dirent* ent; while((ent = readdir(d))) { char* buf = (char*) calloc(BUFSIZ, 1); sprintf(buf, "%s/%s", dir, ent->d_name); struct stat st; if(lstat(buf, &st) != 0) { free(buf); if(force) return -1; fprintf(stderr, "%s: %s: can not remove: %s\n", argv[0], buf, strerror(errno)); exit(-1); } if(S_ISDIR(st.st_mode)) { if(rm_r(buf) != 0) { free(buf); fprintf(stderr, "%s: %s: can not remove: %s\n", argv[0], buf, strerror(errno)); exit(-1); } } else rm(buf); free(buf); rewinddir(d); } closedir(d); rm(dir); return 0; }