Added a specific reply function for the data channel.
[uftps.git] / uftps.c
blobcac7a7cbeef63eed9a6584167c80a8a2ccb50bed
1 /*
2 * User FTP Server, Share folders over FTP without being root.
3 * Copyright (C) 2008 Isaac Jurado
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
8 * version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "uftps.h"
21 #include <sys/wait.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <stdio.h>
27 struct _SessionScope SS; /* SS --> Session State*/
31 * clid_finish
33 * Reaper function. For "zombies".
35 static void child_finish (int sig)
37 while (waitpid(-1, NULL, WNOHANG) > 0)
38 debug("Collecting children");
43 * end
45 * End. What where you expecting?
47 static void end (int sig)
49 notice("Signal caught, exiting");
50 exit(EXIT_SUCCESS);
55 * Main program. Opens the command port until a client requests a connection.
56 * Then the server is forked and the child will manage all that client's
57 * requests.
59 int main (int argc, char **argv)
61 int bind_sk, cmd_sk, e, yes;
62 int port = DEFAULT_PORT;
63 struct sigaction my_sa;
64 struct sockaddr_in sai;
65 socklen_t sai_len = sizeof(struct sockaddr_in);
67 setlinebuf(stdout);
69 if (argc > 1)
71 port = atoi(argv[1]) & 0x00FFFF;
72 if (port <= 1024)
73 fatal("This port number is restricted");
76 /* Signal handling */
77 sigfillset(&my_sa.sa_mask);
78 my_sa.sa_flags = SA_RESTART;
79 my_sa.sa_handler = child_finish;
80 sigaction(SIGCHLD, &my_sa, NULL);
82 my_sa.sa_flags = SA_NOMASK;
83 my_sa.sa_handler = end;
84 sigaction(SIGINT, &my_sa, NULL);
86 /* Connection handling */
87 sai.sin_family = AF_INET;
88 sai.sin_port = htons(port);
89 sai.sin_addr.s_addr = INADDR_ANY;
91 bind_sk = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
92 if (bind_sk == -1)
93 fatal("Creating main server socket");
95 yes = 1;
96 setsockopt(bind_sk, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
97 e = bind(bind_sk, (struct sockaddr *) &sai, sai_len);
98 if (e == -1)
99 fatal("Binding main server socket");
101 e = listen(bind_sk, 5);
102 if (e == -1)
103 fatal("Listening at main server socket");
105 notice("UFTPS listening on port %d (TCP)", port);
106 notice("Use CTRL + C to finish");
107 notice("If you want to use a different port, specify it as the only argument in the command line");
109 /* Main server loop (accepting connections) */
110 do {
111 sai_len = sizeof(struct sockaddr_in);
112 cmd_sk = accept(bind_sk, (struct sockaddr *) &sai, &sai_len);
113 if (cmd_sk == -1)
115 error("Accepting incoming connection");
116 continue;
119 e = fork();
120 if (e == 0)
122 /*** CHILD ***/
123 close(bind_sk);
124 init_session(cmd_sk);
125 command_loop();
127 else
129 /*** PARENT ***/
130 if (e == -1)
131 error("Could not create a child process");
132 close(cmd_sk);
134 } while (1);
136 return EXIT_SUCCESS;