1/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation; either version 2 of
5 * the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
15 * MA 02111-1307 USA
16 */
17/*
18 * Part of Very Secure FTPd
19 * Licence: GPL v2
20 * Author: Chris Evans
21 * sysstr.c
22 *
23 * This file basically wraps system functions so that we can deal in our
24 * nice abstracted string buffer objects.
25 */
26
27#include <stdio.h>
28#include "sysstr.h"
29#include "str.h"
30#include "secbuf.h"
31#include "sysutil.h"
32#include "defs.h"
33#include "utility.h"
34#include "tunables.h"
35
36void
37str_getcwd(struct mystr* p_str)
38{
39  static char* p_getcwd_buf;
40  char* p_ret;
41  //if (p_getcwd_buf == 0)
42  if(p_getcwd_buf == NULL)
43  {
44    vsf_secbuf_alloc(&p_getcwd_buf, VSFTP_PATH_MAX);
45  }
46  /* In case getcwd() fails */
47  str_empty(p_str);
48  p_ret = vsf_sysutil_getcwd(p_getcwd_buf, VSFTP_PATH_MAX);
49  if (p_ret != 0)
50  {
51    str_alloc_text(p_str, p_getcwd_buf);
52  }
53}
54
55int
56str_write_loop(const struct mystr* p_str, const int fd)
57{
58  return vsf_sysutil_write_loop(fd, str_getbuf(p_str), str_getlen(p_str));
59}
60
61int
62str_read_loop(struct mystr* p_str, const int fd)
63{
64  return vsf_sysutil_read_loop(
65    fd, (char*) str_getbuf(p_str), str_getlen(p_str));
66}
67
68int
69str_mkdir(const struct mystr* p_str, const unsigned int mode)
70{
71  return vsf_sysutil_mkdir(str_getbuf(p_str), mode);
72}
73
74int
75str_rmdir(const struct mystr* p_str)
76{
77  return vsf_sysutil_rmdir(str_getbuf(p_str));
78}
79
80int
81str_unlink(const struct mystr* p_str)
82{
83  return vsf_sysutil_unlink(str_getbuf(p_str));
84}
85
86int
87str_chdir(const struct mystr* p_str)
88{
89  return vsf_sysutil_chdir(str_getbuf(p_str));
90}
91
92int
93str_open(const struct mystr* p_str, const enum EVSFSysStrOpenMode mode)
94{
95  enum EVSFSysUtilOpenMode open_mode = kVSFSysStrOpenUnknown;
96  switch (mode)
97  {
98    case kVSFSysStrOpenReadOnly:
99      open_mode = kVSFSysUtilOpenReadOnly;
100      break;
101    default:
102      bug("unknown mode value in str_open");
103      break;
104  }
105  return vsf_sysutil_open_file(str_getbuf(p_str), open_mode);
106}
107
108int
109str_stat(const struct mystr* p_str, struct vsf_sysutil_statbuf** p_ptr)
110{
111  return vsf_sysutil_stat(str_getbuf(p_str), p_ptr);
112}
113
114int
115str_lstat(const struct mystr* p_str, struct vsf_sysutil_statbuf** p_ptr)
116{
117  return vsf_sysutil_lstat(str_getbuf(p_str), p_ptr);
118}
119
120int
121str_create(const struct mystr* p_str)
122{
123  return vsf_sysutil_create_file(str_getbuf(p_str));
124}
125
126int
127str_create_overwrite(const struct mystr* p_str)
128{
129  return vsf_sysutil_create_overwrite_file(str_getbuf(p_str));
130}
131
132int
133str_create_append(const struct mystr* p_str)
134{
135  return vsf_sysutil_create_or_open_file(
136      str_getbuf(p_str), tunable_file_open_mode);
137}
138
139int
140str_chmod(const struct mystr* p_str, unsigned int mode)
141{
142  return vsf_sysutil_chmod(str_getbuf(p_str), mode);
143}
144
145int
146str_rename(const struct mystr* p_from_str, const struct mystr* p_to_str)
147{
148  return vsf_sysutil_rename(str_getbuf(p_from_str), str_getbuf(p_to_str));
149}
150
151struct vsf_sysutil_dir*
152str_opendir(const struct mystr* p_str)
153{
154  return vsf_sysutil_opendir(str_getbuf(p_str));
155}
156
157// James
158void str_next_dirent(const char *session_user, const char *base_dir, struct mystr *p_filename_str, struct vsf_sysutil_dir *p_dir)
159{
160  const char* p_filename = vsf_sysutil_next_dirent(session_user, base_dir, p_dir);
161  str_empty(p_filename_str);
162  if (p_filename != 0)
163  {
164    str_alloc_text(p_filename_str, p_filename);
165  }
166}
167
168int
169str_readlink(struct mystr* p_str, const struct mystr* p_filename_str)
170{
171  static char* p_readlink_buf;
172  int retval;
173  if (p_readlink_buf == 0)
174  {
175    vsf_secbuf_alloc(&p_readlink_buf, VSFTP_PATH_MAX);
176  }
177  /* In case readlink() fails */
178  str_empty(p_str);
179  /* Note: readlink(2) does not NULL terminate, but our wrapper does */
180  retval = vsf_sysutil_readlink(str_getbuf(p_filename_str), p_readlink_buf,
181                                VSFTP_PATH_MAX);
182  if (vsf_sysutil_retval_is_error(retval))
183  {
184    return retval;
185  }
186  str_alloc_text(p_str, p_readlink_buf);
187  return 0;
188}
189
190struct vsf_sysutil_user*
191str_getpwnam(const struct mystr* p_user_str)
192{
193  return vsf_sysutil_getpwnam(str_getbuf(p_user_str));
194}
195
196void
197str_syslog(const struct mystr* p_str, int severe)
198{
199  vsf_sysutil_syslog(str_getbuf(p_str), severe);
200}
201
202