1 /*
2  * ProFTPD - FTP server daemon
3  * Copyright (c) 1997, 1998 Public Flood Software
4  * Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <macgyver@tos.net>
5  * Copyright (c) 2001-2015 The ProFTPD Project team
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
20  *
21  * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
22  * and other respective copyright holders give permission to link this program
23  * with OpenSSL, and distribute the resulting executable, without including
24  * the source code for OpenSSL in the source distribution.
25  */
26 
27 /* Utility scoreboard routines. */
28 
29 #ifndef UTILS_UTILS_H
30 #define UTILS_UTILS_H
31 
32 #include "config.h"
33 #include "version.h"
34 #include "options.h"
35 
36 #include <ctype.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <time.h>
43 
44 #ifdef HAVE_GETOPT_H
45 #  include <getopt.h>
46 #else
47 #  include "../lib/getopt.h"
48 #endif /* !HAVE_GETOPT_H */
49 
50 #ifdef HAVE_UNISTD_H
51 # include <unistd.h>
52 #endif
53 
54 #ifdef HAVE_NETINET_IN_H
55 # include <netinet/in.h>
56 #endif
57 
58 #ifdef HAVE_SIGNAL_H
59 # include <signal.h>
60 #endif
61 
62 #ifdef HAVE_SYS_FILE_H
63 # include <sys/file.h>
64 #endif
65 
66 #ifdef HAVE_SYS_SOCKET_H
67 # include <sys/socket.h>
68 #endif
69 
70 #ifdef HAVE_SYS_STAT_H
71 # include <sys/stat.h>
72 #endif
73 
74 #include "pool.h"
75 #include "ascii.h"
76 #include "default_paths.h"
77 
78 #define	FALSE	0
79 #define TRUE	1
80 
81 #ifndef INET_ADDRSTRLEN
82 # define INET_ADDRSTRLEN        16
83 #endif /* INET_ADDRSTRLEN */
84 
85 #ifndef INET6_ADDRSTRLEN
86 # define INET6_ADDRSTRLEN       46
87 #endif /* INET6_ADDRSTRLEN */
88 
89 /* UTIL_SCOREBOARD_VERSION is used for checking for scoreboard compatibility
90  */
91 #define UTIL_SCOREBOARD_VERSION        0x01040003
92 
93 /* Structure used as a header for scoreboard files.
94  */
95 #define UTIL_SCOREBOARD_MAGIC			0xdeadbeef
96 
97 typedef struct {
98 
99   /* Always 0xDEADBEEF */
100   unsigned long sch_magic;
101 
102   /* Version of proftpd that created the scoreboard file */
103   unsigned long sch_version;
104 
105   /* PID of the process to which this scoreboard belongs, or zero if inetd */
106   pid_t sch_pid;
107 
108   /* Time when the daemon wrote this header */
109   time_t sch_uptime;
110 
111 } pr_scoreboard_header_t;
112 
113 /* Structure used for writing scoreboard file entries.
114  */
115 
116 typedef struct {
117   pid_t	sce_pid;
118   uid_t sce_uid;
119   gid_t sce_gid;
120   char sce_user[32];
121 
122   int sce_server_port;
123   char sce_server_addr[80], sce_server_label[32];
124 
125 #ifdef PR_USE_IPV6
126   char sce_client_addr[INET6_ADDRSTRLEN];
127 #else
128   char sce_client_addr[INET_ADDRSTRLEN];
129 #endif /* PR_USE_IPV6 */
130   char sce_client_name[PR_TUNABLE_SCOREBOARD_BUFFER_SIZE];
131 
132   char sce_class[32];
133   char sce_protocol[32];
134   char sce_cwd[PR_TUNABLE_SCOREBOARD_BUFFER_SIZE];
135 
136   char sce_cmd[65];
137   char sce_cmd_arg[PR_TUNABLE_SCOREBOARD_BUFFER_SIZE];
138 
139   time_t sce_begin_idle, sce_begin_session;
140 
141   off_t sce_xfer_size, sce_xfer_done, sce_xfer_len;
142   unsigned long sce_xfer_elapsed;
143 
144 } pr_scoreboard_entry_t;
145 
146 /* Scoreboard error values */
147 #define UTIL_SCORE_ERR_BAD_MAGIC	-2
148 #define UTIL_SCORE_ERR_OLDER_VERSION	-3
149 #define UTIL_SCORE_ERR_NEWER_VERSION	-4
150 
151 char *util_sstrncpy(char *, const char *, size_t);
152 
153 const char *util_get_scoreboard(void);
154 int util_set_scoreboard(const char *);
155 
156 char *util_scan_config(const char *, const char *);
157 
158 int util_close_scoreboard(void);
159 int util_open_scoreboard(int);
160 pid_t util_scoreboard_get_daemon_pid(void);
161 time_t util_scoreboard_get_daemon_uptime(void);
162 pr_scoreboard_entry_t *util_scoreboard_entry_read(void);
163 int util_scoreboard_scrub(int);
164 
165 #endif /* UTILS_UTILS_H */
166