extern int cut_main(int argc, char **argv) { unsigned long opt; char *sopt, *sdopt; bb_opt_complementally = "b--bcf:c--bcf:f--bcf"; opt = bb_getopt_ulflags(argc, argv, optstring, &sopt, &sopt, &sopt, &sdopt); part = opt & (OPT_BYTE_FLGS|OPT_CHAR_FLGS|OPT_FIELDS_FLGS); if(part == 0) bb_error_msg_and_die("you must specify a list of bytes, characters, or fields"); if(opt & BB_GETOPT_ERROR) bb_error_msg_and_die("only one type of list may be specified"); parse_lists(sopt); if((opt & (OPT_DELIM_FLGS))) { if (strlen(sdopt) > 1) { bb_error_msg_and_die("the delimiter must be a single character"); } delim = sdopt[0]; } supress_non_delimited_lines = opt & OPT_SUPRESS_FLGS; /* non-field (char or byte) cutting has some special handling */ if (part != OPT_FIELDS_FLGS) { if (supress_non_delimited_lines) { bb_error_msg_and_die("suppressing non-delimited lines makes sense" " only when operating on fields"); } if (delim != '\t') { bb_error_msg_and_die("a delimiter may be specified only when operating on fields"); } } /* argv[(optind)..(argc-1)] should be names of file to process. If no * files were specified or '-' was specified, take input from stdin. * Otherwise, we process all the files specified. */ if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) { cut_file(stdin); } else { int i; FILE *file; for (i = optind; i < argc; i++) { file = bb_wfopen(argv[i], "r"); if(file) { cut_file(file); fclose(file); } } } return EXIT_SUCCESS; }
/* EDR recoded such that the uid may be passed in *p */ static int passwd_study(const char *filename, struct passwd *p) { struct passwd *pw; FILE *passwd; const int min = 500; const int max = 65000; passwd = bb_wfopen(filename, "r"); if (!passwd) return 4; /* EDR if uid is out of bounds, set to min */ if ((p->pw_uid > max) || (p->pw_uid < min)) p->pw_uid = min; /* stuff to do: * make sure login isn't taken; * find free uid and gid; */ while ((pw = fgetpwent(passwd))) { if (strcmp(pw->pw_name, p->pw_name) == 0) { /* return 0; */ return 1; } if ((pw->pw_uid >= p->pw_uid) && (pw->pw_uid < max) && (pw->pw_uid >= min)) { p->pw_uid = pw->pw_uid + 1; } } if (p->pw_gid == 0) { /* EDR check for an already existing gid */ while (getgrgid(p->pw_uid) != NULL) p->pw_uid++; /* EDR also check for an existing group definition */ if (getgrnam(p->pw_name) != NULL) return 3; /* EDR create new gid always = uid */ p->pw_gid = p->pw_uid; } /* EDR bounds check */ if ((p->pw_uid > max) || (p->pw_uid < min)) return 2; /* return 1; */ return 0; }
extern int bb_xprint_file_by_name(const char *filename) { FILE *f; #if 0 /* This check shouldn't be necessary for linux, but is left * here disabled just in case. */ struct stat statBuf; if(is_directory(filename, TRUE, &statBuf)) { bb_error_msg("%s: Is directory", filename); } else #endif if ((f = bb_wfopen(filename, "r")) != NULL) { bb_xprint_and_close_file(f); return 0; } return -1; }
/* putpwent(3) remix */ static int adduser(const char *filename, struct passwd *p, int makehome, int setpass) { FILE *passwd; int r; #ifdef CONFIG_FEATURE_SHADOWPASSWDS FILE *shadow; struct spwd *sp; #endif int new_group = 1; /* if using a pre-existing group, don't create one */ if (p->pw_gid != 0) new_group = 0; /* make sure everything is kosher and setup uid && gid */ passwd = bb_wfopen(filename, "a"); if (passwd == NULL) { return 1; } fseek(passwd, 0, SEEK_END); /* if (passwd_study(filename, p) == 0) { */ r = passwd_study(filename, p); if (r) { if (r == 1) bb_error_msg("%s: login already in use", p->pw_name); else if (r == 2) bb_error_msg("illegal uid or no uids left"); else if (r == 3) bb_error_msg("group name %s already in use", p->pw_name); else bb_error_msg("generic error."); return 1; } /* add to passwd */ if (putpwent(p, passwd) == -1) { return 1; } fclose(passwd); #ifdef CONFIG_FEATURE_SHADOWPASSWDS /* add to shadow if necessary */ if (shadow_enabled) { shadow = bb_wfopen(bb_path_shadow_file, "a"); if (shadow == NULL) { return 1; } fseek(shadow, 0, SEEK_END); sp = pwd_to_spwd(p); sp->sp_max = 99999; /* debianish */ sp->sp_warn = 7; fprintf(shadow, "%s:!:%ld:%ld:%ld:%ld:::\n", sp->sp_namp, sp->sp_lstchg, sp->sp_min, sp->sp_max, sp->sp_warn); fclose(shadow); } #endif if (new_group) { /* add to group */ /* addgroup should be responsible for dealing w/ gshadow */ addgroup_wrapper(p->pw_name, p->pw_gid); } /* Clear the umask for this process so it doesn't * * screw up the permissions on the mkdir and chown. */ umask(0); if (makehome) { /* mkdir */ if (mkdir(p->pw_dir, 0755)) { bb_perror_msg("%s", p->pw_dir); } /* Set the owner and group so it is owned by the new user. */ if (chown(p->pw_dir, p->pw_uid, p->pw_gid)) { bb_perror_msg("%s", p->pw_dir); } /* Now fix up the permissions to 2755. Can't do it before now * since chown will clear the setgid bit */ if (chmod(p->pw_dir, 02755)) { bb_perror_msg("%s", p->pw_dir); } } if (setpass) { /* interactively set passwd */ passwd_wrapper(p->pw_name); } return 0; }
extern int sed_main(int argc, char **argv) { int opt, status = EXIT_SUCCESS; #ifdef CONFIG_FEATURE_CLEAN_UP /* destroy command strings on exit */ if (atexit(free_and_close_stuff) == -1) bb_perror_msg_and_die("atexit"); #endif #define LIE_TO_AUTOCONF #ifdef LIE_TO_AUTOCONF if(argc==2 && !strcmp(argv[1],"--version")) { printf("This is not GNU sed version 4.0\n"); exit(0); } #endif /* do normal option parsing */ while ((opt = getopt(argc, argv, "ne:f:")) > 0) { switch (opt) { case 'n': be_quiet++; break; case 'e': add_cmd_block(optarg); break; case 'f': { FILE *cmdfile; char *line; cmdfile = bb_xfopen(optarg, "r"); while ((line = bb_get_chomped_line_from_file(cmdfile)) != NULL) { add_cmd(line); free(line); } bb_xprint_and_close_file(cmdfile); break; } default: bb_show_usage(); } } /* if we didn't get a pattern from a -e and no command file was specified, * argv[optind] should be the pattern. no pattern, no worky */ if (sed_cmd_head.next == NULL) { if (argv[optind] == NULL) bb_show_usage(); else add_cmd_block(argv[optind++]); } /* Flush any unfinished commands. */ add_cmd(""); /* argv[(optind)..(argc-1)] should be names of file to process. If no * files were specified or '-' was specified, take input from stdin. * Otherwise, we process all the files specified. */ if (argv[optind] == NULL) { process_file(stdin); } else { int i; FILE *file; for (i = optind; i < argc; i++) { if(!strcmp(argv[i], "-")) { process_file(stdin); } else { file = bb_wfopen(argv[i], "r"); if (file) { process_file(file); fclose(file); } else { status = EXIT_FAILURE; } } } } return status; }