- enum { NONE, EDIT, LIST, REPLACE, DELETE } option = NONE;
- const struct passwd *pas;
- const char *repFile = NULL;
- int repFd = 0;
- int i;
- char caller[256]; /* user that ran program */
- int UserId;
-
- UserId = getuid();
- if ((pas = getpwuid(UserId)) == NULL)
- bb_perror_msg_and_die("getpwuid");
-
- strncpy(caller, pas->pw_name, sizeof(caller));
-
- i = 1;
- if (ac > 1) {
- if (av[1][0] == '-' && av[1][1] == 0) {
- option = REPLACE;
- ++i;
- } else if (av[1][0] != '-') {
- option = REPLACE;
- ++i;
- repFile = av[1];
- }
- }
-
- for (; i < ac; ++i) {
- char *ptr = av[i];
-
- if (*ptr != '-')
- break;
- ptr += 2;
-
- switch(ptr[-1]) {
- case 'l':
- if (ptr[-1] == 'l')
- option = LIST;
- /* fall through */
- case 'e':
- if (ptr[-1] == 'e')
- option = EDIT;
- /* fall through */
- case 'd':
- if (ptr[-1] == 'd')
- option = DELETE;
- /* fall through */
- case 'u':
- if (i + 1 < ac && av[i+1][0] != '-') {
- ++i;
- if (getuid() == geteuid()) {
- pas = getpwnam(av[i]);
- if (pas) {
- UserId = pas->pw_uid;
- } else {
- bb_error_msg_and_die("user %s unknown", av[i]);
- }
- } else {
- bb_error_msg_and_die("only the superuser may specify a user");
- }
- }
- break;
- case 'c':
- if (getuid() == geteuid()) {
- CDir = (*ptr) ? ptr : av[++i];
- } else {
- bb_error_msg_and_die("-c option: superuser only");
- }
- break;
- default:
- i = ac;
- break;
- }
- }
- if (i != ac || option == NONE)
- bb_show_usage();
-
- /*
- * Get password entry
- */
-
- if ((pas = getpwuid(UserId)) == NULL)
- bb_perror_msg_and_die("getpwuid");
-
- /*
- * If there is a replacement file, obtain a secure descriptor to it.
- */
-
- if (repFile) {
- repFd = GetReplaceStream(caller, repFile);
- if (repFd < 0)
- bb_error_msg_and_die("unable to read replacement file");
- }
-
- /*
- * Change directory to our crontab directory
- */
-
- bb_xchdir(CDir);
-
- /*
- * Handle options as appropriate
- */
-
- switch(option) {
- case LIST:
- {
- FILE *fi;
- char buf[1024];
-
- if ((fi = fopen(pas->pw_name, "r"))) {
- while (fgets(buf, sizeof(buf), fi) != NULL)
- fputs(buf, stdout);
- fclose(fi);
- } else {
- bb_error_msg("no crontab for %s", pas->pw_name);
- }
- }
- break;
- case EDIT:
- {
- FILE *fi;
- int fd;
- int n;
- char tmp[128];
- char buf[1024];