summaryrefslogtreecommitdiff
path: root/store.c
blob: b1cb275d00c65bfce05f0185815225668be160d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* store.c - twoftpd routines for handling storage-modifying commands
 * Copyright (C) 2008  Bruce Guenter <bruce@untroubled.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "twoftpd.h"
#include "backend.h"

static str rnfr_filename;

static unsigned long xlate_ascii(char* out,
				 const char* in,
				 unsigned long inlen)
{
  unsigned long outlen;
  for (outlen = 0; inlen > 0; --inlen, ++in) {
    if (*in != CR) {
      *out = *in;
      ++outlen;
      ++out;
    }
  }
  return outlen;
}

static int open_copy_close(int append)
{
  int in;
  int out;
  unsigned long ss;
  unsigned long bytes_in;
  unsigned long bytes_out;
  int result;
  
  ss = startpos;
  startpos = 0;
  if ((out = open_fd(req_param,
		    append ? O_WRONLY | O_APPEND
		    : store_exclusive ? O_WRONLY | O_CREAT | O_EXCL
		    : ss ? O_WRONLY
		    : O_WRONLY | O_CREAT | O_TRUNC,
		    0666)) == -1)
    return respond_syserr(550, "Could not open output file");
  if (ss && lseek(out, ss, SEEK_SET) != ss) {
    close(out);
    return respond(550, 1, "Could not seek to start position in output file.");
  }
  if ((in = make_in_connection()) == -1) {
    close(out);
    if (store_exclusive)
      unlink(req_param);
    return 1;
  }
  result = copy_xlate_close(in, out, timeout * 1000,
			    binary_flag ? 0 : xlate_ascii,
			    &bytes_in, &bytes_out);
  return respond_xferresult(result, bytes_in, 0);
}

int handle_stor(void)
{
  return open_copy_close(0);
}

int handle_appe(void)
{
  return open_copy_close(1);
}

int handle_mkd(void)
{
  if (!qualify_validate(req_param)) return 1;
  if (mkdir(fullpath.s+1, 0777))
    return respond_syserr(550, "Could not create directory");
  return respond(257, 1, "Directory created successfully.");
}

int handle_rmd(void)
{
  if (!qualify_validate(req_param)) return 1;
  if (rmdir(fullpath.s+1))
    return respond_syserr(550, "Could not remove directory");
  return respond(250, 1, "Directory removed successfully.");
}

int handle_dele(void)
{
  if (!qualify_validate(req_param)) return 1;
  if (unlink(fullpath.s+1))
    return respond_syserr(550, "Could not remove file");
  return respond(250, 1, "File removed successfully.");
}

int handle_rnfr(void)
{
  struct stat st;
  if (!qualify_validate(req_param)) return 1;
  if (stat(fullpath.s+1, &st) == -1) {
    if (errno == EEXIST)
      return respond(550, 1, "File does not exist.");
    else
      return respond(450, 1, "Could not locate file.");
  }
  if (!str_copy(&rnfr_filename, &fullpath)) return respond_internal_error();
  return respond(350, 1, "OK, file exists.");
}

int handle_rnto(void)
{
  int r;
  if (!qualify_validate(req_param)) return 1;
  if (!rnfr_filename.len) return respond(425, 1, "Send RNFR first.");
  r = rename(rnfr_filename.s+1, fullpath.s+1);
  str_truncate(&rnfr_filename, 0);
  if (r == -1) return respond_syserr(550, "Could not rename file");
  return respond(250, 1, "File renamed.");
}

int handle_site_chmod(void)
{
  unsigned mode;
  const char* ptr;
  for (ptr = req_param, mode = 0; *ptr >= '0' && *ptr <= '7'; ++ptr)
    mode = (mode * 8) | (*ptr - '0');
  if (ptr == req_param || *ptr != SPACE || mode > 0777)
    return respond(501, 1, "Invalid mode specification.");
  while (*ptr == SPACE) ++ptr;
  if (!qualify_validate(ptr)) return 1;
  if (chmod(fullpath.s+1, mode) != 0)
    return respond_syserr(550, "Could not change modes on file");
  return respond(250, 1, "File modes changed.");
}