sysstr.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Part of Very Secure FTPd
  3. * Licence: GPL v2
  4. * Author: Chris Evans
  5. * sysstr.c
  6. *
  7. * This file basically wraps system functions so that we can deal in our
  8. * nice abstracted string buffer objects.
  9. */
  10. #include "sysstr.h"
  11. #include "str.h"
  12. #include "secbuf.h"
  13. #include "sysutil.h"
  14. #include "defs.h"
  15. #include "utility.h"
  16. #include "tunables.h"
  17. void
  18. str_getcwd(struct mystr* p_str)
  19. {
  20. static char* p_getcwd_buf;
  21. char* p_ret;
  22. if (p_getcwd_buf == 0)
  23. {
  24. vsf_secbuf_alloc(&p_getcwd_buf, VSFTP_PATH_MAX);
  25. }
  26. /* In case getcwd() fails */
  27. str_empty(p_str);
  28. p_ret = vsf_sysutil_getcwd(p_getcwd_buf, VSFTP_PATH_MAX);
  29. if (p_ret != 0)
  30. {
  31. str_alloc_text(p_str, p_getcwd_buf);
  32. }
  33. }
  34. int
  35. str_write_loop(const struct mystr* p_str, const int fd)
  36. {
  37. return vsf_sysutil_write_loop(fd, str_getbuf(p_str), str_getlen(p_str));
  38. }
  39. int
  40. str_read_loop(struct mystr* p_str, const int fd)
  41. {
  42. return vsf_sysutil_read_loop(
  43. fd, (char*) str_getbuf(p_str), str_getlen(p_str));
  44. }
  45. int
  46. str_mkdir(const struct mystr* p_str, const unsigned int mode)
  47. {
  48. return vsf_sysutil_mkdir(str_getbuf(p_str), mode);
  49. }
  50. int
  51. str_rmdir(const struct mystr* p_str)
  52. {
  53. return vsf_sysutil_rmdir(str_getbuf(p_str));
  54. }
  55. int
  56. str_unlink(const struct mystr* p_str)
  57. {
  58. return vsf_sysutil_unlink(str_getbuf(p_str));
  59. }
  60. int
  61. str_chdir(const struct mystr* p_str)
  62. {
  63. return vsf_sysutil_chdir(str_getbuf(p_str));
  64. }
  65. int
  66. str_open(const struct mystr* p_str, const enum EVSFSysStrOpenMode mode)
  67. {
  68. enum EVSFSysUtilOpenMode open_mode = kVSFSysStrOpenUnknown;
  69. switch (mode)
  70. {
  71. case kVSFSysStrOpenReadOnly:
  72. open_mode = kVSFSysUtilOpenReadOnly;
  73. break;
  74. case kVSFSysStrOpenUnknown:
  75. /* Fall through */
  76. default:
  77. bug("unknown mode value in str_open");
  78. break;
  79. }
  80. return vsf_sysutil_open_file(str_getbuf(p_str), open_mode);
  81. }
  82. int
  83. str_stat(const struct mystr* p_str, struct vsf_sysutil_statbuf** p_ptr)
  84. {
  85. return vsf_sysutil_stat(str_getbuf(p_str), p_ptr);
  86. }
  87. int
  88. str_lstat(const struct mystr* p_str, struct vsf_sysutil_statbuf** p_ptr)
  89. {
  90. return vsf_sysutil_lstat(str_getbuf(p_str), p_ptr);
  91. }
  92. int
  93. str_create_exclusive(const struct mystr* p_str)
  94. {
  95. return vsf_sysutil_create_file_exclusive(str_getbuf(p_str));
  96. }
  97. int
  98. str_create(const struct mystr* p_str)
  99. {
  100. return vsf_sysutil_create_or_open_file(
  101. str_getbuf(p_str), tunable_file_open_mode);
  102. }
  103. int
  104. str_chmod(const struct mystr* p_str, unsigned int mode)
  105. {
  106. return vsf_sysutil_chmod(str_getbuf(p_str), mode);
  107. }
  108. int
  109. str_rename(const struct mystr* p_from_str, const struct mystr* p_to_str)
  110. {
  111. return vsf_sysutil_rename(str_getbuf(p_from_str), str_getbuf(p_to_str));
  112. }
  113. struct vsf_sysutil_dir*
  114. str_opendir(const struct mystr* p_str)
  115. {
  116. return vsf_sysutil_opendir(str_getbuf(p_str));
  117. }
  118. void
  119. str_next_dirent(struct mystr* p_filename_str, struct vsf_sysutil_dir* p_dir)
  120. {
  121. const char* p_filename = vsf_sysutil_next_dirent(p_dir);
  122. str_empty(p_filename_str);
  123. if (p_filename != 0)
  124. {
  125. str_alloc_text(p_filename_str, p_filename);
  126. }
  127. }
  128. int
  129. str_readlink(struct mystr* p_str, const struct mystr* p_filename_str)
  130. {
  131. static char* p_readlink_buf;
  132. int retval;
  133. if (p_readlink_buf == 0)
  134. {
  135. vsf_secbuf_alloc(&p_readlink_buf, VSFTP_PATH_MAX);
  136. }
  137. /* In case readlink() fails */
  138. str_empty(p_str);
  139. /* Note: readlink(2) does not NULL terminate, but our wrapper does */
  140. retval = vsf_sysutil_readlink(str_getbuf(p_filename_str), p_readlink_buf,
  141. VSFTP_PATH_MAX);
  142. if (vsf_sysutil_retval_is_error(retval))
  143. {
  144. return retval;
  145. }
  146. str_alloc_text(p_str, p_readlink_buf);
  147. return 0;
  148. }
  149. struct vsf_sysutil_user*
  150. str_getpwnam(const struct mystr* p_user_str)
  151. {
  152. return vsf_sysutil_getpwnam(str_getbuf(p_user_str));
  153. }
  154. void
  155. str_syslog(const struct mystr* p_str, int severe)
  156. {
  157. vsf_sysutil_syslog(str_getbuf(p_str), severe);
  158. }