blob: f60cc407e5fc96be8386fd5a7a4106e76b334e15 [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001// MorgenGrauen MUDlib
2//
3// master/network.c - UDP-Handling
4//
5// $Id: network.c 8934 2014-09-10 21:57:12Z Zesstra $
6
7#pragma strict_types
8
9#include "/secure/master.h"
10#define BBMASTER "/secure/bbmaster"
11
12/*
13#undef DEBUG
14#define DEBUG(x) if (call_sefun("find_player","zesstra")) \
15 tell_object(call_sefun("find_player","zesstra"),x);
16*/
17
18//ich will hieraus momentan kein Debug, ist zuviel. Zesstra
19
20#ifdef DEBUG
21#undef DEBUG
22#endif
23#define DEBUG(x)
24
25nosave int mails_last_hour;
26
27static string mail_normalize( string str )
28{
29 str = regreplace( str, "[^<]*<(.*)>[^>]*", "\\1", 0);
30 return regreplace( str, " *([^ ][^ ]*).*", "\\1", 0);
31}
32
33
34static string *mk_rec_list( string str )
35{
36 return map( explode( lower_case(str), "," ) - ({""}),
37 "mail_normalize", this_object() );
38}
39
40
41static int CheckPasswd( string name, string passwd ) {
42 mixed *uinf;
43
44 if (!stringp(passwd) || !sizeof(passwd))
45 return 0;
46 if ( sizeof(uinf = get_full_userinfo(name)) < 2 )
47 return 0;
48
49 string pwhash = uinf[USER_PASSWORD+1];
50 if (sizeof(pwhash) > 13) {
51 // MD5-Hash
52 passwd = md5_crypt(passwd, pwhash);
53 }
54 else if (sizeof(pwhash) > 2) {
55 // Crypt-Hash
56 passwd = crypt(passwd, pwhash[0..1]);
57 }
58 else return 0;
59
60 return (passwd == pwhash);
61}
62
MG Mud User88f12472016-06-24 23:31:02 +020063static int doReadMail( string file )
64{
65 string str, *lines, *parts, *tmp;
66 mixed *message;
67 int i, j;
68
69 if ( (i = file_size(file)) > 50000 || i < 5 ){
70 rm(file);
71 DEBUG( "Mail size invalid\n" );
72 return -1;
73 }
74
75 if ( !(str = read_bytes( file, 0, 50000 )) )
76 return -1;
77
78 if ( !(j = sizeof(lines = explode( str, "\n" ))) )
79 return -2;
80
81 i = 0;
82
83 while ( i < j && lines[i] != "" )
84 i++;
85
86 if ( i == j )
87 return -2;
88
89 DEBUG( sprintf( "Have %d headerlines:\n", i ) );
90
91 message= allocate(9);
92 message[MSG_CC] = ({});
93 message[MSG_BCC] = ({});
94 message[MSG_BODY] = implode( lines[i..], "\n" );
95
96 for ( j = 0; j < i; j++ ){
97 parts = explode( lines[j], ":" );
98
99 if ( sizeof(parts) > 1 ){
100 str = lower_case(parts[0]);
101 parts[0] = implode( parts[1..], ":" );
102
103 switch (str){
104 case "subject":
105 message[MSG_SUBJECT] = parts[0];
106 break;
107
108 case "from":
109 DEBUG( "Found from\n" );
110 DEBUG( sprintf( "PARTS[0]=%s\n", parts[0] ) );
111 message[MSG_FROM] = mail_normalize(parts[0]);
112 message[MSG_SENDER] = parts[0];
113 DEBUG( sprintf( "FROM: %s\nSENDER: %s\n",
114 message[MSG_FROM],
115 message[MSG_SENDER] ) );
116 break;
117
118 case "cc":
119 DEBUG( sprintf("FOUND CC: %O\n", parts[0]) );
120 message[MSG_CC] += mk_rec_list(parts[0]);
121 break;
122
123 case "bcc":
124 DEBUG( sprintf("FOUND BCC: %O\n", parts[0]) );
125 message[MSG_BCC] += mk_rec_list(parts[0]);
126 break;
127
128 case "to":
129 DEBUG( sprintf("FOUND TO: %O\n", parts[0]) );
130 tmp = mk_rec_list(parts[0]);
131
132 if ( !message[MSG_RECIPIENT] )
133 message[MSG_RECIPIENT] = tmp[0];
134
135 message[MSG_CC] += tmp;
136 break;
137
138 // Das MUD-TO: wird vom Perlskript als erste Headerzeile eingefuegt
139 case "mud-to":
140 DEBUG( sprintf("FOUND MUD-TO: %O\n", parts[0]) );
141 message[MSG_RECIPIENT] = mail_normalize(lower_case(parts[0]));
142 break;
143 }
144 }
145 }
146
147 // Eigentlichen Empfaenger aus CC: loeschen
148 message[MSG_CC] -= ({ message[MSG_RECIPIENT],
149 message[MSG_RECIPIENT]+"@mg.mud.de",
150 message[MSG_RECIPIENT]+"@morgengrauen.mud.de" });
151
152
153 DEBUG( sprintf( "TO: %O\n", message[MSG_RECIPIENT] ) );
154 DEBUG( sprintf( "CC: %O\n", message[MSG_CC] ) );
155 DEBUG( sprintf( "BCC: %O\n", message[MSG_BCC] ) );
156
157 if ( !stringp(message[MSG_FROM]) )
158 return -2;
159
160 if ( !stringp(message[MSG_RECIPIENT]) ){
161 str = explode( file, "/" )[<1];
162 i = 0;
163 j = sizeof(str);
164
165 while ( i < j && str[i] <= '9' && str[i] >= '0' )
166 i++;
167
168 if ( i >= j )
169 return -2;
170
171 message[MSG_RECIPIENT] = str[i..];
172 DEBUG( sprintf( "EMERGENCY RECIPIENT=%s\n", str[i..] ) );
173 }
174
175 rm(file);
176
177 // Da vom Master besser nichts von ausserhalb /secure #include't wird,
178 // direkt die '5'. Normalerweise hiesse der Aufruf:
179 // DeliverMail( message, NO_USER_ALIASES|NO_CARBON_COPIES );
180 "/secure/mailer"->DeliverMail( message, 5 );
181 return 1;
182}
183
184
185public void mailread()
186{
187 string *files;
188 int i;
189
190 DEBUG( "mailread called\n" );
191
192 if ( mails_last_hour >= MAX_MAILS_PER_HOUR )
193 return;
194
195 files = (get_dir( "/mail/inbound/*" )||({})) - ({ "..", "." });
196 i = sizeof(files);
197
198 while ( i-- && mails_last_hour < MAX_MAILS_PER_HOUR ){
199 DEBUG( "FOUND FILE \"" + files[i] + "\" ...\n" );
200 mails_last_hour++;
201
202 if ( doReadMail( "/mail/inbound/" + files[i]) < -1 ){
203 mixed message;
204
205 message = allocate(9);
206 mails_last_hour++;
207 rename( "/mail/inbound/" + files[i],
208 "/secure/ARCH/MAIL/" + files[i] );
209 message[MSG_SENDER] = geteuid();
210 message[MSG_FROM] = getuid();
211 message[MSG_SUBJECT] = "Fehlerhafte Mail: /secure/ARCH/MAIL/" +
212 files[i];
213 message[MSG_RECIPIENT] = "mud@mg.mud.de";
214 message[MSG_BODY] = "Bitte Ueberpruefen!\n";
215 // NO_SYSTEM_ALIASES|NO_USER_ALIASES == 3
216 "/secure/mailer"->DeliverMail( message, 3 );
217 }
218 }
219}
220
221
222static void udp_query( string query, string host, int port )
223{
224#if __EFUN_DEFINED__(send_udp)
225 string *mess;
226 mixed *data;
227 int i, j;
228
229 mess = explode( query, " " );
230
231 switch ( mess[1] ){
232 case "wholist":
233 case "who":
234 data = (string *)"/obj/werliste"->QueryWhoListe();
235 break;
236
237 case "uptime":
238 data = ({ call_sefun("uptime") });
239 break;
240
241 case "finger":
242 if ( sizeof(mess) < 3 )
243 data = ({ "Error: Wen soll ich fingern ?" });
244 else
245 data = explode( (string)"p/daemon/finger"->
246 finger_single( lower_case(mess[2]), 0 ), "\n" );
247 break;
248
249 case "mailread":
250 data = ({ "Okay" });
251 mailread();
252 break;
253
254 default:
255 data = ({ "Error: unknown request " + mess[1] + "\n" });
256 }
257
258
259 send_udp( host, port, sprintf( "%s 0 %d", mess[0], sizeof(data) ) );
260
261 for ( i = 0, j = sizeof(data); i < j; i++ )
262 send_udp( host, port, sprintf( "%s %d %s", mess[0], i+1, data[i] ) );
263#endif
264}
265
266#define UDP_DEBUG(x)
267//#define UDP_DEBUG(x) (write_file("/log/ARCH/udp.log",(x)))
268
269void receive_udp(string host, string message, int port)
270{
271 mixed *tmp;
272 UDP_DEBUG(sprintf("%s %s:%d: %s\n",strftime(),host,port,message));
273
274 if (message[0..6]=="EXTREQ:"
275 || message[0..5]=="IPNAME"
276 || message[0..3]=="AUTH"
277 ) {
278 return;
279 }
280
281 if( message[0..8]=="IPLOOKUP\n" ) {
282 "/p/daemon/iplookup"->update( message );
283 return;
284 }
285
286 if( message[0..9]=="DNSLOOKUP\n" ) {
287 "/p/daemon/dnslookup"->update( message );
288 return;
289 }
290
MG Mud User88f12472016-06-24 23:31:02 +0200291 if (message[0..9]=="udp_query:") {
292 return udp_query(message[10..],host,port);
293 }
294
295 "secure/inetd"->_receive_udp(host, message);
296}
297
298