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
27 struct _SessionScope SS
; /* SS --> Session State*/
33 * Reaper function. For "zombies".
35 static void child_finish (int sig
)
37 while (waitpid(-1, NULL
, WNOHANG
) > 0)
38 debug("Collecting children");
45 * End. What where you expecting?
47 static void end (int sig
)
49 notice("Signal caught, exiting");
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
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
);
71 port
= atoi(argv
[1]) & 0x00FFFF;
73 fatal("This port number is restricted");
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
);
93 fatal("Creating main server socket");
96 setsockopt(bind_sk
, SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof(yes
));
97 e
= bind(bind_sk
, (struct sockaddr
*) &sai
, sai_len
);
99 fatal("Binding main server socket");
101 e
= listen(bind_sk
, 5);
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) */
111 sai_len
= sizeof(struct sockaddr_in
);
112 cmd_sk
= accept(bind_sk
, (struct sockaddr
*) &sai
, &sai_len
);
115 error("Accepting incoming connection");
124 init_session(cmd_sk
);
131 error("Could not create a child process");