1 /*  Open DC Hub - A Linux/Unix version of the Direct Connect hub.
2  *  Copyright (C) 2002,2003  Jonatan Nilsson
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 
20 
21 #include <sys/types.h>
22 
23 /* Using the 32 bit int (sometimes even 64 bits) for boolean variables and
24  * other variables that always will be between -128 and 127 would be a waste
25  * of space, especially those in the user_t struct since it's used to
26  * frequently.  */
27 #define BYTE char
28 
29 #define ALARM_TIME         900             /* Seconds between alarm calls */
30 #define MAX_NICK_LEN       50              /* Maximum length of nickname, 20 is max in win client */
31 #define MAX_HOST_LEN       121             /* Maximum length of hostname */
32 #define MAX_VERSION_LEN    30              /* Maximum length of version name */
33 #define MAX_MESS_SIZE      0xFFFF          /* Maximum size of a received message */
34 #define MAX_HUB_NAME       25              /* Maximum length of hub name, 25 from win version */
35 #define MAX_HUB_DESC       100             /* Maximum length of hub description */
36 #define MAX_ADMIN_PASS_LEN 50              /* Maximum length of admin pass */
37 #define MAX_BUF_SIZE       1000000         /* Maximum length of users buf */
38 #define MAX_FDP_LEN	   100		   /* Maximum length of file/dir/path variables */
39 #define USER_LIST_ENT_SIZE 173             /* Size of an entry in the user list,
40 					    * nick length + host length.  */
41 
42 #define CONFIG_FILE        "config"        /* Name of config file */
43 #define MOTD_FILE          "motd"          /* Name of file containing the motd */
44 #define BAN_FILE           "banlist"       /* Name of file with banlist */
45 #define NICKBAN_FILE       "nickbanlist"   /* Name of file with nick banlist */
46 #define ALLOW_FILE         "allowlist"     /* Name of file with allowlist */
47 #define REG_FILE           "reglist"       /* Name of file with list of registered users */
48 #define LINK_FILE          "linklist"      /* Name of file with list of linked hubs */
49 #define OP_PERM_FILE       "op_permlist"   /* Name of file with op permissions */
50 #define LOG_FILE           "log"           /* Name of log file */
51 #define UN_SOCK_NAME       "odch"          /* Name of unix socket file */
52 #define USER_LIST          "odchlist"      /* Name of temporary user list file */
53 #define SCRIPT_DIR         "scripts"       /* Name of script directory.  */
54 #define SYSLOG_IDENT       "odch"          /* Identity for system log */
55 
56 #define INIT_MESS          1
57 #define HELLO_MESS         2
58 #define HUB_FULL_MESS      3
59 #define BAN_MESS           4
60 #define GET_PASS_MESS      5
61 #define LOGGED_IN_MESS     6
62 #define OP_LOGGED_IN_MESS  7
63 #define BAD_PASS_MESS      8
64 #define INIT_ADMIN_MESS    9
65 #define	GET_PASS_MESS2	  10
66 
67 /* The different user types */
68 #define UNKEYED            0x1
69 #define NON_LOGGED         0x2
70 #define REGULAR            0x4
71 #define REGISTERED         0x8
72 #define OP                 0x10
73 #define OP_ADMIN           0x20
74 #define ADMIN              0x40
75 #define FORKED             0x80
76 #define LINKED             0x100
77 #define SCRIPT             0x200
78 #define NON_LOGGED_ADM     0x400
79 
80 /* The different OP permissions */
81 #define BAN_ALLOW          0x1
82 #define USER_INFO          0x2
83 #define MASSMESSAGE        0x4
84 #define USER_ADMIN         0x8
85 
86 #define PRIV               0
87 #define TO_ALL             1
88 
89 #define ALLOW              0
90 #define BAN                1
91 #define REG                2
92 #define CONFIG             3
93 #define LINK               4
94 #define NICKBAN            5
95 
96 #define HOST               0
97 #define IP                 1
98 
99 #ifndef HAVE_STRTOLL
100 # ifdef HAVE_STRTOQ
101 #  define strtoll(X, Y, Z) (long long)strtoq(X, Y, Z)
102 # endif
103 #endif
104 
105 /* Possible values for user->rem  */
106 #define REMOVE_USER        0x1
107 #define SEND_QUIT          0x2
108 #define REMOVE_FROM_LIST   0x4
109 
110 
111 struct user_t
112 {
113    int sock;                          /* What socket the user is on */
114    long unsigned ip;                  /* Ip address of user */
115    char hostname[MAX_HOST_LEN+1];     /* Hostname of user */
116    int  type;                         /* Type of user, types defined above. */
117    char nick[MAX_NICK_LEN+1];         /* Nickname of user */
118    char version[MAX_VERSION_LEN+1];   /* Version of client */
119    char *email;                       /* Email of user, optional */
120    char *desc;                        /* Description of users files, optional */
121    BYTE con_type;                     /* Users connection type: 1: 28,8; 2: 33,6;
122 				       3: 56; 4: Satellite; 5: ISDN; 6: DSL;
123 				       7: Cable; 8: LAN(T1); 9: LAN(T3);
124 				       10: Wireless; 11: Modem; 12:Netlimiter;
125 				       255: Unknown */
126    BYTE flag;                         /* Users flag, represented by one byte */
127    long long share;                   /* Size of users share in bytes */
128    char *buf;                         /* If a command doesnt't fit in one packet,
129 				       * it's saved here for later */
130    char *outbuf;                      /* Buf of stuff that will be sent to a user */
131    BYTE timeout;                      /* Check user timeout */
132    struct user_t *next;               /* Next user in list*/
133    int key;                           /* Start value for the generated key */
134    BYTE rem;                          /* 1 if user is to be removed */
135    time_t last_search;                /* Time of the last search attempt */
136    int  permissions;                  /* Operator permissions (listed above) */
137 };
138 
139 /* This is used for a linked list of the humans. This is to get faster
140  * send_to_all:s. I'm pretending it contains the user's sockets, but it really
141  * only contains pointers to the users.  */
142 struct sock_t
143 {
144    struct user_t *user;
145    struct sock_t *next;
146 };
147 
148 /* This is system defined as "semun" on some systems, but not defined at all on
149  * other systems. I'm just defining it as my_semun for simplicity.  */
150 union my_semun
151 {
152       int val;                    /* value for SETVAL */
153       struct semid_ds *buf;       /* buffer for IPC_STAT, IPC_SET */
154       unsigned short int *array;  /* array for GETALL, SETALL */
155       struct seminfo *__buf;      /* buffer for IPC_INFO */
156 };
157 
158 /* Global variables */
159 pid_t  pid;                         /* Pid of process if parent, if it's a child, pid is 0, for scripts, it's -1 and for hublist upload processes it's -2  */
160 int    users_per_fork;              /* Users in hub when fork occurs */
161 struct user_t *non_human_user_list; /* List of non-human users */
162 struct user_t **human_hash_table;  /* Hashtable of human users */
163 struct sock_t *human_sock_list;
164 unsigned int listening_port;        /* Port on which we listen for connections */
165 unsigned int admin_port;            /* Administration port */
166 BYTE   admin_localhost;             /* 1 to bind administration port localhost only */
167 int    admin_listening_socket;      /* Socket for incoming connections from admins */
168 int    listening_socket;            /* Socket for incoming connections from clients */
169 int    listening_unx_socket;        /* Socket for forked processes to connect to */
170 int    listening_udp_socket;        /* Socket for incoming multi-hub messages */
171 char   hub_name[MAX_HUB_NAME+1];    /* Name of the hub. */
172 BYTE   debug;                       /* 1 for debug mode, else 0 */
173 BYTE   registered_only;             /* 1 for registered only mode, else 0 */
174 BYTE   hublist_upload;              /* User set variable, if 1, upload */
175 BYTE   ban_overrides_allow;         /* 1 for banlist to override allowlist */
176 BYTE   redir_on_min_share;          /* 1 if user should be redirected if user shares less than the minimum share */
177 BYTE   check_key;                   /* Checks key from client if set to 1 */
178 BYTE   reverse_dns;                 /* If 1, reverse dns lookups are made on newly connected clients.  */
179 BYTE   verbosity;                   /* This sets the verbosity of the log file, may vary from 0 to 5 */
180 char   hub_description[MAX_HUB_DESC+1]; /* The description of hub that is uploaded to public hublist */
181 char   public_hub_host[MAX_HOST_LEN+1]; /* This is the hostname to upload hub description to */
182 char   min_version[MAX_VERSION_LEN+1];  /* Minimum client verison to allow users to the hub. */
183 char   hub_hostname[MAX_HOST_LEN+1];    /* This is the hostname that is uploaded to the public hublist, so don't try setting this to "127.0.0.1" or "localhost" */
184 char   redirect_host[MAX_HOST_LEN+1]; /* Host to redirect users to if hub is full */
185 char   *hub_full_mess;
186 int    max_users;
187 int    max_sockets;
188 long long min_share;       /* Minimum share for clients */
189 int    min_upload_slots;    /* Minimum upload slots for clients */
190 int    total_share_shm;    /* Identifier for the shared memory segment that contains the total share on hub, uploaded to public hub list.  */
191 int    total_share_sem;    /* Semaphore Id for the shared momry segment above.  */
192 int    user_list_shm_shm;  /* Identifier for shared memory segment containing the shared memory segment for the user list :)  */
193 int    user_list_sem;      /* And a semaphore to control access to it.  */
194 char   admin_pass[MAX_ADMIN_PASS_LEN+1];
195 char   link_pass[MAX_ADMIN_PASS_LEN+1]; /* Password for hub linking */
196 char   default_pass[MAX_ADMIN_PASS_LEN+1];
197 BYTE   upload;                      /* keeps track on when it's time to upload to public hub list */
198 BYTE   quit;
199 BYTE   do_write;
200 BYTE   do_send_linked_hubs;
201 BYTE   do_purge_user_list;
202 BYTE   do_fork;
203 BYTE   script_reload;
204 char   config_dir[MAX_FDP_LEN+1];
205 char   un_sock_path[MAX_FDP_LEN+1];
206 char   logfile[MAX_FDP_LEN+1];	/* Logfile if specifically set */
207 BYTE   syslog_enable;
208 BYTE   syslog_switch;
209 BYTE   searchcheck_exclude_internal;
210 BYTE   searchcheck_exclude_all;
211 int    kick_bantime;
212 int    searchspam_time;
213 uid_t  dchub_user;
214 gid_t  dchub_group;
215 char   working_dir[MAX_FDP_LEN+1];
216 time_t hub_start_time;
217 int    max_email_len;
218 int    max_desc_len;
219 BYTE   crypt_enable;
220 int    current_forked;   /* This is used to keep track on which
221 			  * process that holds the listening
222 			  * sockets.  */
223 
224 /* Functions */
225 void   hub_mess(struct user_t *user, int mess_type);
226 int    new_human_user(int sock);
227 int    socket_action(struct user_t *user);
228 int    udp_action(void);
229 void   remove_user(struct user_t *our_user, int send_quit, int remove_from_list);
230 void   send_init(int sock);
231 void   do_upload_to_hublist(void);
232 int    handle_command(char *buf, struct user_t *user);
233 void   send_user_info(struct user_t *from_user, char *to_user_nick, int all);
234 void   init_sig(void);
235 void   remove_all(int type, int send_quit, int remove_from_list);
236 void   new_forked_process(void);
237 void   kill_forked_process(void);
238 void   term_signal(int z);
239 void   alarm_signal(int z);
240 int    set_default_vars(void);
241 void   new_admin_connection();
242 void   add_non_human_to_list(struct user_t *user);
243 void   remove_non_human(struct user_t *our_user);
244 void   add_human_to_hash(struct user_t *user);
245 void   remove_human_from_hash(char *nick);
246 struct user_t* get_human_user(char *nick);
247 void   remove_human_user(struct user_t *user);
248 void   encrypt_pass(char* password);
249