faillock.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2010 Tomas Mraz <tmraz@redhat.com>
  3. * Copyright (c) 2010, 2016, 2017 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 <string.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <errno.h>
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #include <sys/file.h>
  44. #include <sys/stat.h>
  45. #include <fcntl.h>
  46. #include <security/pam_modutil.h>
  47. #include "faillock.h"
  48. #define ignore_return(x) if (1==((int)x)) {;}
  49. int
  50. open_tally (const char *dir, const char *user, uid_t uid, int create)
  51. {
  52. char *path;
  53. int flags = O_RDWR;
  54. int fd;
  55. if (dir == NULL || strstr(user, "../") != NULL)
  56. /* just a defensive programming as the user must be a
  57. * valid user on the system anyway
  58. */
  59. return -1;
  60. path = malloc(strlen(dir) + strlen(user) + 2);
  61. if (path == NULL)
  62. return -1;
  63. strcpy(path, dir);
  64. if (*dir && dir[strlen(dir) - 1] != '/') {
  65. strcat(path, "/");
  66. }
  67. strcat(path, user);
  68. if (create) {
  69. flags |= O_CREAT;
  70. if (access(dir, F_OK) != 0) {
  71. mkdir(dir, 0755);
  72. }
  73. }
  74. fd = open(path, flags, 0660);
  75. free(path);
  76. if (fd != -1) {
  77. struct stat st;
  78. while (flock(fd, LOCK_EX) == -1 && errno == EINTR);
  79. if (fstat(fd, &st) == 0) {
  80. if (st.st_uid != uid) {
  81. ignore_return(fchown(fd, uid, -1));
  82. }
  83. /*
  84. * If umask is set to 022, as will probably in most systems, then the
  85. * group will not be able to write to the file. So, change the file
  86. * permissions just in case.
  87. * Note: owners of this file are user:root, so if the permissions are
  88. * not changed the root process writing to this file will require
  89. * CAP_DAC_OVERRIDE.
  90. */
  91. if (!(st.st_mode & S_IWGRP)) {
  92. ignore_return(fchmod(fd, 0660));
  93. }
  94. }
  95. }
  96. return fd;
  97. }
  98. #define CHUNK_SIZE (64 * sizeof(struct tally))
  99. #define MAX_RECORDS 1024
  100. int
  101. read_tally(int fd, struct tally_data *tallies)
  102. {
  103. void *data = NULL, *newdata;
  104. unsigned int count = 0;
  105. ssize_t chunk = 0;
  106. do {
  107. newdata = realloc(data, count * sizeof(struct tally) + CHUNK_SIZE);
  108. if (newdata == NULL) {
  109. free(data);
  110. return -1;
  111. }
  112. data = newdata;
  113. chunk = pam_modutil_read(fd, (char *)data + count * sizeof(struct tally), CHUNK_SIZE);
  114. if (chunk < 0) {
  115. free(data);
  116. return -1;
  117. }
  118. count += chunk/sizeof(struct tally);
  119. if (count >= MAX_RECORDS)
  120. break;
  121. }
  122. while (chunk == CHUNK_SIZE);
  123. tallies->records = data;
  124. tallies->count = count;
  125. return 0;
  126. }
  127. int
  128. update_tally(int fd, struct tally_data *tallies)
  129. {
  130. void *data = tallies->records;
  131. unsigned int count = tallies->count;
  132. ssize_t chunk;
  133. if (tallies->count > MAX_RECORDS) {
  134. data = tallies->records + (count - MAX_RECORDS);
  135. count = MAX_RECORDS;
  136. }
  137. if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
  138. return -1;
  139. }
  140. chunk = pam_modutil_write(fd, data, count * sizeof(struct tally));
  141. if (chunk != (ssize_t)(count * sizeof(struct tally))) {
  142. return -1;
  143. }
  144. if (ftruncate(fd, count * sizeof(struct tally)) == -1)
  145. return -1;
  146. return 0;
  147. }