main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2010 Tomas Mraz <tmraz@redhat.com>
  3. * Copyright (c) 2010 Red Hat, Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, and the entire permission notice in its entirety,
  10. * including the disclaimer of warranties.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote
  15. * products derived from this software without specific prior
  16. * written permission.
  17. *
  18. * ALTERNATIVELY, this product may be distributed under the terms of
  19. * the GNU Public License, in which case the provisions of the GPL are
  20. * required INSTEAD OF the above restrictions. (This clause is
  21. * necessary due to a potential bad interaction between the GPL and
  22. * the restrictions contained in a BSD-style copyright.)
  23. *
  24. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  26. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  28. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  29. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  32. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  34. * OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "config.h"
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <dirent.h>
  41. #include <errno.h>
  42. #include <pwd.h>
  43. #include <time.h>
  44. #include <sys/types.h>
  45. #include <unistd.h>
  46. #ifdef HAVE_LIBAUDIT
  47. #include <libaudit.h>
  48. #define AUDIT_NO_ID ((unsigned int) -1)
  49. #endif
  50. #include "faillock.h"
  51. struct options {
  52. unsigned int reset;
  53. const char *dir;
  54. const char *user;
  55. const char *progname;
  56. };
  57. static int
  58. args_parse(int argc, char **argv, struct options *opts)
  59. {
  60. int i;
  61. memset(opts, 0, sizeof(*opts));
  62. opts->dir = FAILLOCK_DEFAULT_TALLYDIR;
  63. opts->progname = argv[0];
  64. for (i = 1; i < argc; ++i) {
  65. if (strcmp(argv[i], "--dir") == 0) {
  66. ++i;
  67. if (i >= argc || strlen(argv[i]) == 0) {
  68. fprintf(stderr, "%s: No directory supplied.\n", argv[0]);
  69. return -1;
  70. }
  71. opts->dir = argv[i];
  72. }
  73. else if (strcmp(argv[i], "--user") == 0) {
  74. ++i;
  75. if (i >= argc || strlen(argv[i]) == 0) {
  76. fprintf(stderr, "%s: No user name supplied.\n", argv[0]);
  77. return -1;
  78. }
  79. opts->user = argv[i];
  80. }
  81. else if (strcmp(argv[i], "--reset") == 0) {
  82. opts->reset = 1;
  83. }
  84. else {
  85. fprintf(stderr, "%s: Unknown option: %s\n", argv[0], argv[i]);
  86. return -1;
  87. }
  88. }
  89. return 0;
  90. }
  91. static void
  92. usage(const char *progname)
  93. {
  94. fprintf(stderr, _("Usage: %s [--dir /path/to/tally-directory] [--user username] [--reset]\n"),
  95. progname);
  96. }
  97. static int
  98. do_user(struct options *opts, const char *user)
  99. {
  100. int fd;
  101. int rv;
  102. struct tally_data tallies;
  103. struct passwd *pwd;
  104. pwd = getpwnam(user);
  105. fd = open_tally(opts->dir, user, pwd != NULL ? pwd->pw_uid : 0, 0);
  106. if (fd == -1) {
  107. if (errno == ENOENT) {
  108. return 0;
  109. }
  110. else {
  111. fprintf(stderr, "%s: Error opening the tally file for %s:",
  112. opts->progname, user);
  113. perror(NULL);
  114. return 3;
  115. }
  116. }
  117. if (opts->reset) {
  118. #ifdef HAVE_LIBAUDIT
  119. int audit_fd;
  120. #endif
  121. while ((rv=ftruncate(fd, 0)) == -1 && errno == EINTR);
  122. if (rv == -1) {
  123. fprintf(stderr, "%s: Error clearing the tally file for %s:",
  124. opts->progname, user);
  125. perror(NULL);
  126. #ifdef HAVE_LIBAUDIT
  127. }
  128. if ((audit_fd=audit_open()) >= 0) {
  129. audit_log_acct_message(audit_fd, AUDIT_USER_MGMT, NULL,
  130. "faillock-reset", user,
  131. pwd != NULL ? pwd->pw_uid : AUDIT_NO_ID,
  132. NULL, NULL, NULL, rv == 0);
  133. close(audit_fd);
  134. }
  135. if (rv == -1) {
  136. #endif
  137. close(fd);
  138. return 4;
  139. }
  140. }
  141. else {
  142. unsigned int i;
  143. memset(&tallies, 0, sizeof(tallies));
  144. if (read_tally(fd, &tallies) == -1) {
  145. fprintf(stderr, "%s: Error reading the tally file for %s:",
  146. opts->progname, user);
  147. perror(NULL);
  148. close(fd);
  149. return 5;
  150. }
  151. printf("%s:\n", user);
  152. printf("%-19s %-5s %-48s %-5s\n", "When", "Type", "Source", "Valid");
  153. for (i = 0; i < tallies.count; i++) {
  154. struct tm *tm;
  155. char timebuf[80];
  156. uint16_t status = tallies.records[i].status;
  157. time_t when = tallies.records[i].time;
  158. tm = localtime(&when);
  159. strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm);
  160. printf("%-19s %-5s %-52.52s %s\n", timebuf,
  161. status & TALLY_STATUS_RHOST ? "RHOST" : (status & TALLY_STATUS_TTY ? "TTY" : "SVC"),
  162. tallies.records[i].source, status & TALLY_STATUS_VALID ? "V":"I");
  163. }
  164. free(tallies.records);
  165. }
  166. close(fd);
  167. return 0;
  168. }
  169. static int
  170. do_allusers(struct options *opts)
  171. {
  172. struct dirent **userlist;
  173. int rv, i;
  174. rv = scandir(opts->dir, &userlist, NULL, alphasort);
  175. if (rv < 0) {
  176. fprintf(stderr, "%s: Error reading tally directory: %m\n", opts->progname);
  177. return 2;
  178. }
  179. for (i = 0; i < rv; i++) {
  180. if (userlist[i]->d_name[0] == '.') {
  181. if ((userlist[i]->d_name[1] == '.' && userlist[i]->d_name[2] == '\0') ||
  182. userlist[i]->d_name[1] == '\0')
  183. continue;
  184. }
  185. do_user(opts, userlist[i]->d_name);
  186. free(userlist[i]);
  187. }
  188. free(userlist);
  189. return 0;
  190. }
  191. /*-----------------------------------------------------------------------*/
  192. int
  193. main (int argc, char *argv[])
  194. {
  195. struct options opts;
  196. if (args_parse(argc, argv, &opts)) {
  197. usage(argv[0]);
  198. return 1;
  199. }
  200. if (opts.user == NULL) {
  201. return do_allusers(&opts);
  202. }
  203. return do_user(&opts, opts.user);
  204. }