Annotation of src/usr.bin/skeyaudit/skeyaudit.c, Revision 1.1
1.1 ! millert 1: #include <err.h>
! 2: #include <errno.h>
! 3: #include <limits.h>
! 4: #include <paths.h>
! 5: #include <pwd.h>
! 6: #include <stdio.h>
! 7: #include <stdlib.h>
! 8: #include <string.h>
! 9: #include <unistd.h>
! 10: #include <skey.h>
! 11:
! 12: #include <sys/types.h>
! 13: #include <sys/param.h>
! 14:
! 15: extern char *__progname;
! 16:
! 17: void usage __P((void));
! 18:
! 19: int
! 20: main(argc, argv)
! 21: int argc;
! 22: char **argv;
! 23: {
! 24: struct passwd *pw;
! 25: struct skey key;
! 26: int ch, errs, left = 0, iflag = 0, limit = 12;
! 27: char *name, hostname[MAXHOSTNAMELEN];
! 28: FILE *out;
! 29:
! 30: while ((ch = getopt(argc, argv, "il:")) != -1)
! 31: switch(ch) {
! 32: case 'i':
! 33: iflag = 1;
! 34: break;
! 35: case 'l':
! 36: errno = 0;
! 37: if ((limit = (int)strtol(optarg, NULL, 10)) == 0)
! 38: errno = ERANGE;
! 39: if (errno) {
! 40: warn("key limit");
! 41: usage();
! 42: }
! 43: break;
! 44: default:
! 45: usage();
! 46: }
! 47:
! 48: if (argc - optind > 0)
! 49: usage();
! 50:
! 51: if ((pw = getpwuid(getuid())) == NULL)
! 52: errx(1, "no passwd entry for uid %u", getuid());
! 53: if ((name = strdup(pw->pw_name)) == NULL)
! 54: err(1, "cannot allocate memory");
! 55: sevenbit(name);
! 56:
! 57: errs = skeylookup(&key, name);
! 58: switch (errs) {
! 59: case 0: /* Success! */
! 60: left = key.n - 1;
! 61: break;
! 62: case -1: /* File error */
! 63: /* XXX - _PATH_SKEYFILE should be in paths.h? */
! 64: warnx("cannot open /etc/skeykeys");
! 65: break;
! 66: case 1: /* Unknown user */
! 67: warnx("%s is not listed in /etc/skeykeys", name);
! 68: }
! 69:
! 70: setuid(getuid()); /* Run sendmail as user not root */
! 71:
! 72: if (errs || left >= limit)
! 73: exit(errs);
! 74:
! 75: if (gethostname(hostname, sizeof(hostname)) == -1)
! 76: strcpy(hostname, "unknown");
! 77:
! 78: if (iflag) {
! 79: out = stdout;
! 80: } else {
! 81: char cmd[sizeof(_PATH_SENDMAIL) + 3];
! 82:
! 83: sprintf(cmd, "%s -t", _PATH_SENDMAIL);
! 84: out = popen(cmd, "w");
! 85: }
! 86:
! 87: if (!iflag)
! 88: (void)fprintf(out,
! 89: "To: %s\nSubject: IMPORTANT action required\n", name);
! 90:
! 91: (void)fprintf(out,
! 92: "\nYou are nearing the end of your current S/Key sequence for account\n\
! 93: %s on system %s.\n\n\
! 94: Your S/key sequence number is now %d. When it reaches zero\n\
! 95: you will no longer be able to use S/Key to login into the system.\n\n\
! 96: Type \"skeyinit -s\" to reinitialize your sequence number.\n\n",
! 97: name, hostname, left - 1);
! 98:
! 99: if (iflag)
! 100: (void)fclose(out);
! 101: else
! 102: (void)pclose(out);
! 103:
! 104: exit(0);
! 105: }
! 106:
! 107: void
! 108: usage()
! 109: {
! 110: (void)fprintf(stderr, "Usage: %s [-i] [-l limit]\n",
! 111: __progname);
! 112: exit(1);
! 113: }