/** * @brief checks the entry using subfunctions based on params, if passed, prints it * * @param path the path to be processed * @param params the parsed parameters * @param attr the entry attributes from lstat * * @returns EXIT_SUCCESS, EXIT_FAILURE */ int do_file(char *path, params_t *params, struct stat attr) { int printed = 0; do { /* ### FB: Mit ENUM Feld könnte hier ein switch genutzt werden und für mehr Übersicht sorgen. */ /* filtering */ if (params->type) { if (do_type(params->type, attr) != EXIT_SUCCESS) { return EXIT_SUCCESS; /* the entry didn't pass the check, do not print it */ } } if (params->nouser) { if (do_nouser(attr) != EXIT_SUCCESS) { return EXIT_SUCCESS; } } if (params->user) { if (do_user(params->userid, attr) != EXIT_SUCCESS) { return EXIT_SUCCESS; } } if (params->name) { if (do_name(path, params->name) != EXIT_SUCCESS) { return EXIT_SUCCESS; } } if (params->path) { if (do_path(path, params->path) != EXIT_SUCCESS) { return EXIT_SUCCESS; } } /* printing */ if (params->print) { if (do_print(path) != EXIT_SUCCESS) { return EXIT_FAILURE; /* a fatal error occurred */ } printed = 1; } if (params->ls) { if (do_ls(path, attr) != EXIT_SUCCESS) { return EXIT_FAILURE; } printed = 1; } params = params->next; } while (params); if (printed == 0) { if (do_print(path) != EXIT_SUCCESS) { return EXIT_FAILURE; } } return EXIT_SUCCESS; }
int main(int argc, char **argv) { int opt, r; char *alt_config = NULL; int allusers = 0; const char *audit_tool = NULL; while ((opt = getopt(argc, argv, "C:A:a")) != EOF) { switch (opt) { case 'C': /* alt config file */ alt_config = optarg; break; case 'a': allusers = 1; break; case 'A': audit_tool = optarg; break; default: usage(); } } cyrus_init(alt_config, "dav_reconstruct", 0, 0); /* Set namespace -- force standard (internal) */ if ((r = mboxname_init_namespace(&recon_namespace, 1)) != 0) { syslog(LOG_ERR, "%s", error_message(r)); fatal(error_message(r), EX_CONFIG); } signals_set_shutdown(&shut_down); signals_add_handlers(0); sqldb_init(); /* Initialize libical */ ical_support_init(); if (allusers) { mboxlist_alluser(do_user, (void *)audit_tool); } else if (optind == argc) { usage(); } else { int i; for (i = optind; i < argc; i++) do_user(argv[i], (void *)audit_tool); } exit(code); }
/** * @brief checks the entry using subfunctions based on params, if passed, prints it * * @param path the path to be processed * @param params the parsed parameters * @param attr the entry attributes from lstat * * @returns EXIT_SUCCESS, EXIT_FAILURE */ int do_file(char *path, params_t *params, struct stat attr) { int printed = 0; do { /* filtering */ if (params->type) { if (do_type(params->type, attr) != EXIT_SUCCESS) { return EXIT_SUCCESS; /* the entry didn't pass the check, do not print it */ } } if (params->nouser) { if (do_nouser(attr) != EXIT_SUCCESS) { return EXIT_SUCCESS; } } if (params->user) { if (do_user(params->userid, attr) != EXIT_SUCCESS) { return EXIT_SUCCESS; } } if (params->name) { if (do_name(path, params->name) != EXIT_SUCCESS) { return EXIT_SUCCESS; } } if (params->path) { if (do_path(path, params->path) != EXIT_SUCCESS) { return EXIT_SUCCESS; } } /* printing */ if (params->print) { if (do_print(path) != EXIT_SUCCESS) { return EXIT_FAILURE; /* a fatal error occurred */ } printed = 1; } if (params->ls) { if (do_ls(path, attr) != EXIT_SUCCESS) { return EXIT_FAILURE; } printed = 1; } params = params->next; } while (params); if (printed == 0) { if (do_print(path) != EXIT_SUCCESS) { return EXIT_FAILURE; } } return EXIT_SUCCESS; }
int main(int argc, char **argv) { int beforec, afterc, i; char *table, **before, **after; struct rlimit rl; getrlimit(RLIMIT_NOFILE, &rl); for (i = rl.rlim_cur; i > 2; i--) close(i); whoami = ((whoami = strrchr(argv[0], '/')) ? whoami+1 : argv[0]); table = argv[1]; beforec = atoi(argv[2]); before = &argv[4]; afterc = atoi(argv[3]); after = &argv[4 + beforec]; setvbuf(stdout, NULL, _IOLBF, BUFSIZ); strcpy(tbl_buf, table); strcat(tbl_buf, " ("); for (i = 0; i < beforec; i++) { if (i > 0) strcat(tbl_buf, ","); strcat(tbl_buf, before[i]); } strcat(tbl_buf, ")->("); for (i = 0; i < afterc; i++) { if (i > 0) strcat(tbl_buf, ","); strcat(tbl_buf, after[i]); } strcat(tbl_buf, ")"); initialize_sms_error_table(); initialize_krb_error_table(); if (!strcmp(table, "users")) do_user(before, beforec, after, afterc); else if (!strcmp(table, "list")) do_list(before, beforec, after, afterc); else if (!strcmp(table, "imembers")) do_member(before, beforec, after, afterc); else if (!strcmp(table, "filesys")) do_filesys(before, beforec, after, afterc); else if (!strcmp(table, "quota")) do_quota(before, beforec, after, afterc); exit(0); }
/*! \brief USER command handler * * \param source_p Pointer to allocated Client struct from which the message * originally comes from. This can be a local or remote client. * \param parc Integer holding the number of supplied arguments. * \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL * pointers. * \note Valid arguments for this command are: * - parv[0] = command * - parv[1] = username (login name, account) * - parv[2] = client host name (ignored) * - parv[3] = server host name (ignored) * - parv[4] = user's real name info */ static int mr_user(struct Client *source_p, int parc, char *parv[]) { char *p = NULL; if (source_p->connection->listener->flags & LISTENER_SERVER) { exit_client(source_p, "Use a different port"); return 0; } if (EmptyString(parv[4])) { sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, "USER"); return 0; } if ((p = strchr(parv[1], '@'))) *p = '\0'; do_user(source_p, parv[1], parv[4]); return 0; }
/* * m_nick * parv[0] = sender prefix * parv[1] = nickname * parv[2] = hopcount when new user; TS when nick change * parv[3] = TS * ---- new user only below ---- * parv[4] = umode * parv[5] = username * parv[6] = hostname * parv[7] = server * parv[8] = serviceid * parv[9] = IP * parv[10] = ircname * -- endif */ int m_nick(aClient *cptr, aClient *sptr, int parc, char *parv[]) { struct simBan *ban; aClient *acptr, *uplink; Link *lp, *lp2; char nick[NICKLEN + 2]; ts_val newts = 0; int sameuser = 0, samenick = 0; if (parc < 2) { sendto_one(sptr, err_str(ERR_NONICKNAMEGIVEN), me.name, parv[0]); return 0; } if (!IsServer(sptr) && IsServer(cptr) && parc > 2) newts = atol(parv[2]); else if (IsServer(sptr) && parc > 3) newts = atol(parv[3]); else parc = 2; /* * parc == 2 on a normal client sign on (local) and a normal client * nick change * parc == 4 on a normal server-to-server client nick change * parc == 11 on a normal TS style server-to-server NICK introduction */ if ((IsServer(sptr) || (parc > 4)) && (parc < 11)) { /* * We got the wrong number of params. Someone is trying to trick * us. Kill it. -ThemBones As discussed with ThemBones, not much * point to this code now sending a whack of global kills would * also be more annoying then its worth, just note the problem, * and continue -Dianora */ sendto_realops("IGNORING BAD NICK: %s[%s@%s] on %s (from %s)", parv[1], (parc >= 6) ? parv[5] : "-", (parc >= 7) ? parv[6] : "-", (parc >= 8) ? parv[7] : "-", parv[0]); return 0; } strncpyzt(nick, parv[1], NICKLEN + 1); /* * if do_nick_name() returns a null name OR if the server sent a * nick name and do_nick_name() changed it in some way (due to rules * of nick creation) then reject it. If from a server and we reject * it, and KILL it. -avalon 4/4/92 */ if (do_nick_name(nick) == 0 || (IsServer(cptr) && strcmp(nick, parv[1]))) { sendto_one(sptr, err_str(ERR_ERRONEUSNICKNAME), me.name, parv[0], parv[1], "Erroneous Nickname"); if (IsServer(cptr)) { ircstp->is_kill++; sendto_realops_lev(DEBUG_LEV, "Bad Nick: %s From: %s Via: %s", parv[1], parv[0], get_client_name(cptr, HIDEME)); sendto_one(cptr, ":%s KILL %s :%s (Bad Nick)", me.name, parv[1], me.name); if (sptr != cptr) { /* bad nick change */ sendto_serv_butone(cptr, ":%s KILL %s :%s (Bad Nick)", me.name, parv[0], me.name); sptr->flags |= FLAGS_KILLED; return exit_client(cptr, sptr, &me, "BadNick"); } } return 0; } /* * Check against nick name collisions. * * Put this 'if' here so that the nesting goes nicely on the screen * :) We check against server name list before determining if the * nickname is present in the nicklist (due to the way the below * for loop is constructed). -avalon */ do { if ((acptr = find_server(nick, NULL))) if (MyConnect(sptr)) { sendto_one(sptr, err_str(ERR_NICKNAMEINUSE), me.name, BadPtr(parv[0]) ? "*" : parv[0], nick); return 0; } /* * acptr already has result from find_server * Well. unless we have a capricious server on the net, a nick can * never be the same as a server name - Dianora * That's not the only case; maybe someone broke do_nick_name * or changed it so they could use "." in nicks on their network * - sedition */ if (acptr) { /* * We have a nickname trying to use the same name as a * server. Send out a nick collision KILL to remove the * nickname. As long as only a KILL is sent out, there is no * danger of the server being disconnected. Ultimate way to * jupiter a nick ? >;-). -avalon */ sendto_realops_lev(SKILL_LEV, "Nick collision on %s", sptr->name); ircstp->is_kill++; sendto_one(cptr, ":%s KILL %s :%s (Nick Collision)", me.name, sptr->name, me.name); sptr->flags |= FLAGS_KILLED; return exit_client(cptr, sptr, &me, "Nick/Server collision"); } if (!(acptr = find_client(nick, NULL))) break; /* * If acptr == sptr, then we have a client doing a nick change * between *equivalent* nicknames as far as server is concerned * (user is changing the case of his/her nickname or somesuch) */ if (acptr == sptr) { if (strcmp(acptr->name, nick) == 0) return 0; else break; } /* If user is changing nick to itself no point in propogating */ /* * Note: From this point forward it can be assumed that acptr != * sptr (point to different client structures). * * If the older one is "non-person", the new entry is just * allowed to overwrite it. Just silently drop non-person, and * proceed with the nick. This should take care of the "dormant * nick" way of generating collisions... */ if (IsUnknown(acptr)) { if (MyConnect(acptr)) { exit_client(NULL, acptr, &me, "Overridden"); break; } else if (!(acptr->user)) { sendto_realops_lev(SKILL_LEV, "Nick Collision on %s", parv[1]); sendto_serv_butone(NULL, ":%s KILL %s :%s (Nick Collision)", me.name, acptr->name, me.name); acptr->flags |= FLAGS_KILLED; /* Having no USER struct should be ok... */ return exit_client(cptr, acptr, &me, "Got TS NICK before Non-TS USER"); } } if (!IsServer(cptr)) { /* * NICK is coming from local client connection. Just send * error reply and ignore the command. * parv[0] is empty on connecting clients */ sendto_one(sptr, err_str(ERR_NICKNAMEINUSE), me.name, BadPtr(parv[0]) ? "*" : parv[0], nick); return 0; } /* * NICK was coming from a server connection. Means that the same * nick is registered for different users by different server. * This is either a race condition (two users coming online about * same time, or net reconnecting) or just two net fragments * becoming joined and having same nicks in use. We cannot have * TWO users with same nick--purge this NICK from the system with * a KILL... >;) * * Changed to something reasonable like IsServer(sptr) (true if * "NICK new", false if ":old NICK new") -orabidoo */ if (IsServer(sptr)) { /* * A new NICK being introduced by a neighbouring server (e.g. * message type "NICK new" received) */ if (!newts || !acptr->tsinfo || (newts == acptr->tsinfo)) { sendto_realops_lev(SKILL_LEV, "Nick collision on %s", parv[1]); ircstp->is_kill++; sendto_one(acptr, err_str(ERR_NICKCOLLISION), me.name, acptr->name, acptr->name); sendto_serv_butone(NULL, ":%s KILL %s :%s (Nick Collision)", me.name, acptr->name, me.name); acptr->flags |= FLAGS_KILLED; return exit_client(cptr, acptr, &me, "Nick collision"); } else { /* XXX This looks messed up to me XXX - Raist */ sameuser = (acptr->user) && mycmp(acptr->user->username, parv[5]) == 0 && mycmp(acptr->user->host, parv[6]) == 0; if ((sameuser && newts < acptr->tsinfo) || (!sameuser && newts > acptr->tsinfo)) { return 0; } else { sendto_realops_lev(SKILL_LEV, "Nick collision on %s",parv[1]); ircstp->is_kill++; sendto_one(acptr, err_str(ERR_NICKCOLLISION), me.name, acptr->name, acptr->name); sendto_serv_butone(sptr, ":%s KILL %s :%s (Nick Collision)", me.name, acptr->name, me.name); acptr->flags |= FLAGS_KILLED; (void) exit_client(cptr, acptr, &me, "Nick collision"); break; } } } /* * * A NICK change has collided (e.g. message type * ":old NICK * new". This requires more complex cleanout. * Both clients must be * purged from this server, the "new" * must be killed from the * incoming connection, and "old" must * be purged from all outgoing * connections. */ if (!newts || !acptr->tsinfo || (newts == acptr->tsinfo) || !sptr->user) { sendto_realops_lev(SKILL_LEV, "Nick change collision: %s", parv[1]); ircstp->is_kill++; sendto_one(acptr, err_str(ERR_NICKCOLLISION), me.name, acptr->name, acptr->name); sendto_serv_butone(NULL, ":%s KILL %s :%s (Nick Collision)",me.name, sptr->name, me.name); ircstp->is_kill++; sendto_serv_butone(NULL, ":%s KILL %s :%s (Nick Collision)",me.name, acptr->name, me.name); acptr->flags |= FLAGS_KILLED; (void) exit_client(NULL, acptr, &me, "Nick collision(new)"); sptr->flags |= FLAGS_KILLED; return exit_client(cptr, sptr, &me, "Nick collision(old)"); } else { /* XXX This looks messed up XXX */ sameuser = mycmp(acptr->user->username, sptr->user->username) == 0 && mycmp(acptr->user->host, sptr->user->host) == 0; if ((sameuser && newts < acptr->tsinfo) || (!sameuser && newts > acptr->tsinfo)) { if (sameuser) sendto_realops_lev(SKILL_LEV, "Nick change collision from %s to %s", sptr->name, acptr->name); ircstp->is_kill++; sendto_serv_butone(cptr, ":%s KILL %s :%s (Nick Collision)", me.name, sptr->name, me.name); sptr->flags |= FLAGS_KILLED; if (sameuser) return exit_client(cptr, sptr, &me, "Nick collision(old)"); else return exit_client(cptr, sptr, &me, "Nick collision(new)"); } else { sendto_realops_lev(SKILL_LEV, "Nick collision on %s", acptr->name); ircstp->is_kill++; sendto_one(acptr, err_str(ERR_NICKCOLLISION), me.name, acptr->name, acptr->name); sendto_serv_butone(sptr, ":%s KILL %s :%s (Nick Collision)",me.name, acptr->name, me.name); acptr->flags |= FLAGS_KILLED; (void) exit_client(cptr, acptr, &me, "Nick collision"); } } } while (0); if (IsServer(sptr)) { uplink = find_server(parv[7], NULL); if(!uplink) { /* if we can't find the server this nick is on, * complain loudly and ignore it. - lucas */ sendto_realops("Remote nick %s on UNKNOWN server %s", nick, parv[7]); return 0; } sptr = make_client(cptr, uplink); /* If this is on a U: lined server, it's a U: lined client. */ if(IsULine(uplink)) sptr->flags|=FLAGS_ULINE; add_client_to_list(sptr); if (parc > 2) sptr->hopcount = atoi(parv[2]); if (newts) { sptr->tsinfo = newts; } else { newts = sptr->tsinfo = (ts_val) timeofday; ts_warn("Remote nick %s introduced without a TS", nick); } /* copy the nick in place */ (void) strcpy(sptr->name, nick); (void) add_to_client_hash_table(nick, sptr); if (parc >= 10) { int *s, flag; char *m; /* parse the usermodes -orabidoo */ m = &parv[4][1]; while (*m) { for (s = user_modes; (flag = *s); s += 2) if (*m == *(s + 1)) { if ((flag == UMODE_o) || (flag == UMODE_O)) Count.oper++; sptr->umode |= flag & SEND_UMODES; break; } m++; } if (parc==10) { return do_user(nick, cptr, sptr, parv[5], parv[6], parv[7], strtoul(parv[8], NULL, 0), "0.0.0.0", parv[9]); } else if (parc==11) { return do_user(nick, cptr, sptr, parv[5], parv[6], parv[7], strtoul(parv[8], NULL, 0), parv[9], parv[10]); } } } else if (sptr->name[0]) { #ifdef DONT_CHECK_QLINE_REMOTE if (MyConnect(sptr)) { #endif if ((ban = check_mask_simbanned(nick, SBAN_NICK))) { #ifndef DONT_CHECK_QLINE_REMOTE if (!MyConnect(sptr)) sendto_realops("Restricted nick %s from %s on %s", nick, (*sptr->name != 0 && !IsServer(sptr)) ? sptr->name : "<unregistered>", (sptr->user == NULL) ? ((IsServer(sptr)) ? parv[6] : me.name) : sptr->user->server); #endif if (MyConnect(sptr) && (!IsServer(cptr)) && (!IsOper(cptr)) && (!IsULine(sptr))) { sendto_one(sptr, err_str(ERR_ERRONEUSNICKNAME), me.name, BadPtr(parv[0]) ? "*" : parv[0], nick, BadPtr(ban->reason) ? "Erroneous Nickname" : ban->reason); if (call_hooks(CHOOK_FORBID, cptr, nick, ban) != FLUSH_BUFFER) sendto_realops_lev(REJ_LEV, "Forbidding restricted nick %s from %s", nick, get_client_name(cptr, FALSE)); return 0; } } #ifdef DONT_CHECK_QLINE_REMOTE } #endif if (MyConnect(sptr)) { if (IsRegisteredUser(sptr)) { /* before we change their nick, make sure they're not banned * on any channels, and!! make sure they're not changing to * a banned nick -sed */ /* a little cleaner - lucas */ for (lp = sptr->user->channel; lp; lp = lp->next) { if (can_send(sptr, lp->value.chptr, NULL)) { sendto_one(sptr, err_str(ERR_BANNICKCHANGE), me.name, sptr->name, lp->value.chptr->chname); return 0; } if (nick_is_banned(lp->value.chptr, nick, sptr) != NULL) { sendto_one(sptr, err_str(ERR_BANONCHAN), me.name, sptr->name, nick, lp->value.chptr->chname); return 0; } } #ifdef ANTI_NICK_FLOOD if ((sptr->last_nick_change + MAX_NICK_TIME) < NOW) sptr->number_of_nick_changes = 0; sptr->last_nick_change = NOW; sptr->number_of_nick_changes++; if (sptr->number_of_nick_changes > MAX_NICK_CHANGES && !IsAnOper(sptr)) { sendto_one(sptr, ":%s NOTICE %s :*** Notice -- Too many nick " "changes. Wait %d seconds before trying again.", me.name, sptr->name, MAX_NICK_TIME); return 0; } #endif /* If it changed nicks, -r it */ if ((sptr->umode & UMODE_r) && (mycmp(parv[0], nick) != 0)) { unsigned int oldumode; char mbuf[BUFSIZE]; oldumode = sptr->umode; sptr->umode &= ~UMODE_r; send_umode(sptr, sptr, oldumode, ALL_UMODES, mbuf); } /* LOCAL NICKHANGE */ /* * Client just changing his/her nick. If he/she is on a * channel, send note of change to all clients on that channel. * Propagate notice to other servers. */ /* if the nickname is different, set the TS */ if (mycmp(parv[0], nick)) { sptr->tsinfo = newts ? newts : (ts_val) timeofday; } sendto_common_channels(sptr, ":%s NICK :%s", parv[0], nick); if (sptr->user) { add_history(sptr, 1); sendto_serv_butone(cptr, ":%s NICK %s :%ld", parv[0], nick, sptr->tsinfo); } } } else { /* REMOTE NICKCHANGE */ /* * Client just changing his/her nick. If he/she is on a * channel, send note of change to all clients on that channel. * Propagate notice to other servers. */ /* if the nickname is different, set the TS */ if (mycmp(parv[0], nick)) { sptr->tsinfo = newts ? newts : (ts_val) timeofday; } sendto_common_channels(sptr, ":%s NICK :%s", parv[0], nick); if (sptr->user) { add_history(sptr, 1); sendto_serv_butone(cptr, ":%s NICK %s :%ld", parv[0], nick, sptr->tsinfo); } /* If it changed nicks, -r it */ if (mycmp(parv[0], nick)) sptr->umode &= ~UMODE_r; /* * Flush the banserial for the channels the user is in, since this * could be a SVSNICK induced nick change, which overrides any ban * checking on the originating server. */ flush_user_banserial(sptr); } /* Remove dccallow entries for users who don't share common channel(s) unless they only change their nick capitalization -Kobi_S */ if(sptr->user && mycmp(parv[0], nick)) { for(lp = sptr->user->dccallow; lp; lp = lp2) { lp2 = lp->next; if(lp->flags == DCC_LINK_ME) continue; if(!find_shared_chan(sptr, lp->value.cptr)) { sendto_one(lp->value.cptr, ":%s %d %s :%s has been removed from " "your DCC allow list for signing off", me.name, RPL_DCCINFO, lp->value.cptr->name, parv[0]); del_dccallow(lp->value.cptr, sptr, 1); } } } } else { /* Client setting NICK the first time */ if (MyConnect(sptr)) { if ((ban = check_mask_simbanned(nick, SBAN_NICK))) { if (MyConnect(sptr) && (!IsServer(cptr)) && (!IsOper(cptr)) && (!IsULine(sptr))) { sendto_one(sptr, err_str(ERR_ERRONEUSNICKNAME), me.name, BadPtr(parv[0]) ? "*" : parv[0], nick, BadPtr(ban->reason) ? "Erroneous Nickname" : ban->reason); if (call_hooks(CHOOK_FORBID, cptr, nick, ban) != FLUSH_BUFFER) sendto_realops_lev(REJ_LEV, "Forbidding restricted nick %s from %s", nick, get_client_name(cptr, FALSE)); return 0; } } } strcpy(sptr->name, nick); sptr->tsinfo = timeofday; if (sptr->user) { /* USER already received, now we have NICK */ if (register_user(cptr, sptr, nick, sptr->user->username, NULL) == FLUSH_BUFFER) return FLUSH_BUFFER; } } /* Finally set new nick name. */ if (sptr->name[0]) { del_from_client_hash_table(sptr->name, sptr); samenick = mycmp(sptr->name, nick) ? 0 : 1; if (IsPerson(sptr)) { if (!samenick) hash_check_watch(sptr, RPL_LOGOFF); #ifdef RWHO_PROBABILITY probability_change(sptr->name, nick); #endif } } strcpy(sptr->name, nick); add_to_client_hash_table(nick, sptr); if (IsPerson(sptr) && !samenick) hash_check_watch(sptr, RPL_LOGON); return 0; }
int main(int argc, char **argv) { int opt, all_users = 0; int ret = 1; extern int optind; /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ sanitise_stdfd(); __progname = ssh_get_progname(argv[0]); SSLeay_add_all_algorithms(); log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1); /* We don't need the RNG ourselves, but symbol references here allow * ld to link us properly. */ seed_rng(); while ((opt = getopt(argc, argv, "ahq")) != -1) { switch (opt) { case 'a': all_users = 1; break; case 'q': quiet = 1; break; case 'h': default: usage(); } } if (all_users) { struct passwd *pw; if (!do_host()) ret = 0; while ((pw = getpwent()) != NULL) { if (pw->pw_dir) { if (!do_user(pw->pw_dir)) ret = 0; } } } else if (optind == argc) { struct passwd *pw; if (!do_host()) ret = 0; if ((pw = getpwuid(getuid())) == NULL) fprintf(stderr, "No user found with uid %u\n", (u_int)getuid()); else { if (!do_user(pw->pw_dir)) ret = 0; } } else { while (optind < argc) if (!do_filename(argv[optind++], 0)) ret = 0; } return ret; }
int main(int argc, char *argv[]) { int rc, i; pid_t pid = -1; pid_t ppid = getpid(); uid_t uid = -1; gid_t gid = -1; _free_ char *user = NULL; _free_ char *dest = NULL; _free_ char *change = NULL; _free_ char *env = NULL; _free_ char *cgroup = NULL; _close_ int master_fd = -1; char *master_name; int detach = 0; int keepenv = 0; siginfo_t status; while ((rc = getopt_long(argc, argv, short_opts, long_opts, &i)) !=-1) { switch (rc) { case 'm': validate_optlist("--mount", optarg); add_mount_from_spec(optarg); break; case 'n': clone_flags |= CLONE_NEWNET; if (optarg != NULL) { validate_optlist("--netif", optarg); add_netif_from_spec(optarg); } break; case 'u': clone_flags |= CLONE_NEWUSER; freep(&user); user = strdup(optarg); break; case 'r': freep(&dest); dest = realpath(optarg, NULL); if (dest == NULL) sysf_printf("realpath()"); break; case 'c': freep(&change); change = strdup(optarg); break; case 'g': validate_optlist("--cgroup", optarg); validate_cgroup_spec(optarg); freep(&change); cgroup = strdup(optarg); break; case 'd': detach = 1; break; case 'a': { char *end = NULL; pid = strtol(optarg, &end, 10); if (*end != '\0') fail_printf("Invalid value '%s' for --attach", optarg); break; } case 's': { validate_optlist("--setenv", optarg); if (env != NULL) { char *tmp = env; rc = asprintf(&env, "%s,%s", env, optarg); if (rc < 0) fail_printf("OOM"); freep(&tmp); } else { env = strdup(optarg); } break; } case 'k': keepenv = 1; break; case 'U': clone_flags &= ~(CLONE_NEWUSER); break; case 'M': clone_flags &= ~(CLONE_NEWNS); break; case 'N': clone_flags &= ~(CLONE_NEWNET); break; case 'I': clone_flags &= ~(CLONE_NEWIPC); break; case 'H': clone_flags &= ~(CLONE_NEWUTS); break; case 'P': clone_flags &= ~(CLONE_NEWPID); break; case '?': case 'h': help(); return 0; } } if (pid != -1) { master_fd = recv_pty(pid); if (master_fd < 0) fail_printf("Invalid PID '%u'", pid); pid = -1; goto process_fd; } if (user == NULL) { user = strdup("root"); if (user == NULL) fail_printf("OOM"); } open_master_pty(&master_fd, &master_name); uid = getuid(); gid = getgid(); if (detach == 1) do_daemonize(); pid = do_clone(); if (pid == 0) { closep(&master_fd); open_slave_pty(master_name); rc = setsid(); if (rc < 0) sysf_printf("setsid()"); rc = prctl(PR_SET_PDEATHSIG, SIGKILL); if (rc < 0) sysf_printf("prctl(PR_SET_PDEATHSIG)"); if (clone_flags & CLONE_NEWUSER) map_user_to_user(uid, gid, user); do_cgroup(cgroup, ppid); do_mount(dest); if (dest != NULL) { copy_nodes(dest); make_ptmx(dest); make_symlinks(dest); make_console(dest, master_name); do_chroot(dest); } if (clone_flags & CLONE_NEWNET) setup_loopback(); umask(0022); /* TODO: drop capabilities */ do_user(user); if (change != NULL) { rc = chdir(change); if (rc < 0) sysf_printf("chdir()"); } if (dest != NULL) { char *term = getenv("TERM"); if (keepenv == 0) clearenv(); setenv("PATH", "/usr/sbin:/usr/bin:/sbin:/bin", 1); setenv("USER", user, 1); setenv("LOGNAME", user, 1); setenv("TERM", term, 1); } if (env != NULL) { size_t i, c; _free_ char **vars = NULL; _free_ char *tmp = strdup(env); if (tmp == NULL) fail_printf("OOM"); c = split_str(tmp, &vars, ","); for (i = 0; i < c; i++) { rc = putenv(strdup(vars[i])); if (rc != 0) sysf_printf("putenv()"); } } setenv("container", "pflask", 1); if (argc > optind) rc = execvpe(argv[optind], argv + optind, environ); else rc = execle("/bin/bash", "-bash", NULL, environ); if (rc < 0) sysf_printf("exec()"); } do_netif(pid); process_fd: if (detach == 1) serve_pty(master_fd); else process_pty(master_fd); if (pid == -1) return 0; kill(pid, SIGKILL); rc = waitid(P_PID, pid, &status, WEXITED); if (rc < 0) sysf_printf("waitid()"); switch (status.si_code) { case CLD_EXITED: if (status.si_status != 0) err_printf("Child failed with code '%d'", status.si_status); else ok_printf("Child exited"); break; case CLD_KILLED: err_printf("Child was terminated"); break; default: err_printf("Child failed"); break; } undo_cgroup(cgroup, ppid); return status.si_status; }