File: Main.c

package info (click to toggle)
mysecureshell 2.00%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 888 kB
  • sloc: ansic: 7,421; sh: 700; perl: 264; makefile: 116
file content (106 lines) | stat: -rw-r--r-- 3,399 bytes parent folder | download | duplicates (4)
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
/*
MySecureShell permit to add restriction to modified sftp-server
when using MySecureShell as shell.
Copyright (C) 2007-2014 MySecureShell Team

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 (version 2)

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 "../config.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "SftpWho.h"
#include "../Core/security.h"

int	main(int ac, char **av)
{
  int	i, fd, assume_yes_to_all, do_clean;

  assume_yes_to_all = 0;
  do_clean = 0;
  if (ac > 1)
    for (i = 1; i < ac; i++)
      if (strcmp(av[i], "fullstop") == 0)
	{
	  assume_yes_to_all = 1;
	  do_clean = 1;
	  goto doShutdown;
	}
      else if (strcmp(av[i], "shutdown") == 0 || strcmp(av[i], "stop") == 0)
	{
	doShutdown:
	  if ((fd = open(SHUTDOWN_FILE, O_CREAT | O_TRUNC | O_RDWR, 0644)) >= 0)
	    {
	      char	buf[4];

	      (void )printf("Shutdown server for new connection (active connection are keeped)\n");
	      (void )printf("Do you want to kill all users ? [YES/no] ");
	      (void )fflush(stdout);
	      if (assume_yes_to_all == 0)
		i = read(0, buf, sizeof(buf));
	      else
		(void )printf("yes\n");
	      buf[i >= 1 ? i - 1 : 0] = '\0';
	      if (assume_yes_to_all == 1 || strcasecmp(buf, "yes") == 0 || strcasecmp(buf, "y") == 0)
		{
		  if (system("sftp-kill all > /dev/null") == -1)
		    (void )printf("Error while deconnection users: %s\n", strerror(errno));
		  if (do_clean == 1)
		    {
		      if (SftpWhoDeleteStructs() == 0)
			(void )printf("Can't clean server: %s\n", strerror(errno));
		    }
		}
	      else
		(void )printf("Clients aren't disconnected\n");
	      xclose(fd);
	    }
	  else
	    (void )printf("Can't shutdown server: %s\n", strerror(errno));
	}
      else if (strcmp(av[i], "active") == 0 || strcmp(av[i], "start") == 0)
	{
	  if (unlink(SHUTDOWN_FILE) == 0 || errno == ENOENT)
	    (void )printf("Server is now online.\n");
	  else
	    (void )printf("Can't wake up server: %s\n", strerror(errno));
	}
      else if (strcmp(av[i], "-yes") == 0)
	assume_yes_to_all = 1;
      else
	{
	  (void )printf("Usage:\n------\n\n");
	  (void )printf("%s {options} {states}\n\n", av[0]);
	  (void )printf("\nOptions:\n");
	  (void )printf("\t-yes : assume yes to all questions\n");
	  (void )printf("\nStates:\n");
	  (void )printf("\t- active : wake up server\n");
	  (void )printf("\t- start : same as 'active'\n");
	  (void )printf("\t- shutdown : shutdown the server (but don't kill current connections)\n");
	  (void )printf("\t- stop : same as 'shutdown'\n");
	  (void )printf("\t- fullstop : shutdown the server (kill all connections and clean memory)\n");
	}
  else
    {
      if ((fd = open(SHUTDOWN_FILE, O_RDONLY)) >= 0)
	xclose(fd);
      (void )printf("Server is %s\n", fd == -1 ? "up" : "down");
    }
  return (0);
}