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
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
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
21 #include <sys/socket.h>
29 struct _SessionScope SS
; /* SS --> Session State*/
35 * Reaper function. For "zombies".
37 static void child_finish (int sig
)
39 while (waitpid(-1, NULL
, WNOHANG
) > 0)
40 debug("Collecting children");
47 * End. What where you expecting?
49 static void end (int sig
)
51 notice("Signal caught, exiting");
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
);
69 port
= atoi(argv
[1]) & 0x00FFFF;
73 fatal("Invalid port number");
78 sigfillset(&my_sa
.sa_mask
);
79 my_sa
.sa_flags
= SA_RESTART
;
80 my_sa
.sa_handler
= child_finish
;
81 sigaction(SIGCHLD
, &my_sa
, NULL
);
83 sigemptyset(&my_sa
.sa_mask
);
85 my_sa
.sa_handler
= end
;
86 sigaction(SIGINT
, &my_sa
, NULL
);
87 sigaction(SIGTERM
, &my_sa
, NULL
);
89 /* Connection handling, preparing to serve */
90 sai
.sin_family
= AF_INET
;
91 sai
.sin_port
= htons(port
);
92 sai
.sin_addr
.s_addr
= INADDR_ANY
;
94 bind_sk
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
96 fatal("Creating main server socket");
99 setsockopt(bind_sk
, SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof(yes
));
100 e
= bind(bind_sk
, (struct sockaddr
*) &sai
, sai_len
);
102 fatal("Binding main server socket");
104 e
= listen(bind_sk
, 5);
106 fatal("Listening at main server socket");
108 notice("UFTPS listening on port %d (TCP)", port
);
109 notice("Use CTRL + C to finish");
110 notice("If you want to use a different port, specify it as the only argument in the command line");
112 /* Main server loop (accepting connections) */
114 sai_len
= sizeof(struct sockaddr_in
);
115 cmd_sk
= accept(bind_sk
, (struct sockaddr
*) &sai
, &sai_len
);
119 error("Accepting incoming connection");
128 init_session(cmd_sk
);
135 error("Could not create a child process");