… | |
… | |
2 | |
2 | |
3 | # login handling |
3 | # login handling |
4 | |
4 | |
5 | use Fcntl; |
5 | use Fcntl; |
6 | use Coro::AIO; |
6 | use Coro::AIO; |
|
|
7 | use Deliantra::Util (); |
7 | |
8 | |
8 | our $MAX_DISCONNECT_TIME = $cf::CFG{max_disconnect_time} || 3600; |
9 | CONF MAX_DISCONNECT_TIME = 3600; |
9 | |
|
|
10 | # paranoia function to overwrite a string-in-place |
|
|
11 | sub nuke_str { |
|
|
12 | substr $_[0], 0, (length $_[0]), "x" x length $_[0] |
|
|
13 | } |
|
|
14 | |
10 | |
15 | sub query { |
11 | sub query { |
16 | my ($ns, $flags, $text) = @_; |
12 | my ($ns, $flags, $text) = @_; |
17 | |
13 | |
18 | $ns->query ($flags, $text, Coro::rouse_cb); |
14 | $ns->query ($flags, $text, Coro::rouse_cb); |
… | |
… | |
62 | my $y = $ob->y; |
58 | my $y = $ob->y; |
63 | |
59 | |
64 | # never happens normally, but helps when shell users make mistakes |
60 | # never happens normally, but helps when shell users make mistakes |
65 | $m->linkable |
61 | $m->linkable |
66 | or return 1; |
62 | or return 1; |
67 | |
|
|
68 | # return 0;#d# |
|
|
69 | # warn join ":", $m->at ($x, $y);#d# |
|
|
70 | # warn "FOO$m { ".scalar ($m->at ($x, $y))." }\n"; |
|
|
71 | # return 0; |
|
|
72 | |
63 | |
73 | scalar grep $_->type == cf::SAVEBED, $m->at ($x, $y) |
64 | scalar grep $_->type == cf::SAVEBED, $m->at ($x, $y) |
74 | } |
65 | } |
75 | |
66 | |
76 | sub enter_map { |
67 | sub enter_map { |
… | |
… | |
131 | |
122 | |
132 | $ob->goto ($map, $x, $y); |
123 | $ob->goto ($map, $x, $y); |
133 | } |
124 | } |
134 | |
125 | |
135 | sub encode_password($) { |
126 | sub encode_password($) { |
136 | # crypt $_[0], |
|
|
137 | # join '', |
|
|
138 | # ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64), (cf::rndm 64)] |
|
|
139 | "!" . unpack "H*", $_[0] |
127 | "!" . unpack "H*", $_[0] |
140 | } |
128 | } |
141 | |
129 | |
142 | sub compare_password($$) { |
130 | sub compare_password($$) { |
143 | my ($pass, $token) = @_; |
131 | my ($pass, $token) = @_; |
144 | |
132 | |
145 | if ($token =~ /\!(.*)/) { |
133 | if ($token =~ /!!(.*)/) { |
|
|
134 | return +(substr $pass, 0, 8) eq pack "H*", $1; |
|
|
135 | } elsif ($token =~ /!(.*)/) { |
146 | return $pass eq pack "H*", $1; |
136 | return $pass eq pack "H*", $1; |
147 | } else { |
137 | } else { |
148 | return $token eq crypt $pass, $token; |
138 | return $token eq crypt $pass, $token; |
149 | } |
139 | } |
150 | } |
140 | } |
… | |
… | |
158 | my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; |
148 | my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; |
159 | aio_rename "$PLAYERDIR/$user", $temp; |
149 | aio_rename "$PLAYERDIR/$user", $temp; |
160 | IO::AIO::aio_rmtree $temp; |
150 | IO::AIO::aio_rmtree $temp; |
161 | } |
151 | } |
162 | |
152 | |
|
|
153 | sub login_done { |
|
|
154 | my ($pl) = @_; |
|
|
155 | |
|
|
156 | if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) { |
|
|
157 | $pl->ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); |
|
|
158 | } |
|
|
159 | } |
|
|
160 | |
|
|
161 | sub chargen { |
|
|
162 | my ($ns, $user, $pass) = @_; |
|
|
163 | |
|
|
164 | # the rest of this function is character creation |
|
|
165 | $Coro::current->{desc} = "addme($user) chargen"; |
|
|
166 | |
|
|
167 | # just to make sure nothing is left over |
|
|
168 | # normally, nothing is there. |
|
|
169 | nuke_playerdir $user; |
|
|
170 | |
|
|
171 | my $pl = cf::player::new $user; |
|
|
172 | $pl->password (encode_password $pass); |
|
|
173 | $pl->connect ($ns); |
|
|
174 | my $ob = $pl->ob; |
|
|
175 | |
|
|
176 | $ob->goto ($pl->maplevel, $ob->x, $ob->y); |
|
|
177 | |
|
|
178 | while () { |
|
|
179 | $ob->update_stats; |
|
|
180 | $pl->save_stats; |
|
|
181 | |
|
|
182 | my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
|
|
183 | "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?"; |
|
|
184 | |
|
|
185 | if ($res =~ /^[Nn]/) { |
|
|
186 | last; |
|
|
187 | } elsif ($res > 0 && $res <= 7) { |
|
|
188 | my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?"; |
|
|
189 | |
|
|
190 | if ($swap > 0 && $swap <= 7) { |
|
|
191 | $ob->swap_stats ($res - 1, $swap - 1); |
|
|
192 | } |
|
|
193 | } else { |
|
|
194 | $ob->roll_stats; |
|
|
195 | } |
|
|
196 | |
|
|
197 | Coro::Timer::sleep 0.05; |
|
|
198 | } |
|
|
199 | |
|
|
200 | $ob->set_animation (2); |
|
|
201 | $ob->add_statbonus; |
|
|
202 | |
|
|
203 | while () { |
|
|
204 | $ns->send_msg ("chargen-race-title", ucfirst $pl->title, -1); |
|
|
205 | my $msg = $ob->msg; |
|
|
206 | $msg =~ s/(?<=\S)\n(?=\S)/ /g; |
|
|
207 | $ns->send_msg ("chargen-race-description", $msg, cf::NDI_BLUE); |
|
|
208 | |
|
|
209 | my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
|
|
210 | "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"; |
|
|
211 | |
|
|
212 | last if $res =~ /[dD]/; |
|
|
213 | |
|
|
214 | $pl->chargen_race_next; |
|
|
215 | Coro::Timer::sleep 0.05; |
|
|
216 | } |
|
|
217 | |
|
|
218 | # create the playerdir, if necessary, as chargen_race_done did it before |
|
|
219 | # presumably because of unique maps |
|
|
220 | aio_mkdir playerdir $pl, 0770; |
|
|
221 | $pl->chargen_race_done; |
|
|
222 | |
|
|
223 | while () { |
|
|
224 | my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
|
|
225 | "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n"; |
|
|
226 | |
|
|
227 | if ($res =~ /^[fF]/) { |
|
|
228 | $pl->gender (1); |
|
|
229 | last; |
|
|
230 | } elsif ($res =~ /^[mM]/) { |
|
|
231 | $pl->gender (0); |
|
|
232 | last; |
|
|
233 | } |
|
|
234 | Coro::Timer::sleep 0.05; |
|
|
235 | } |
|
|
236 | |
|
|
237 | $ob->reply (undef, "Welcome to Deliantra!"); |
|
|
238 | |
|
|
239 | # XXX: Workaround for delayed client ext protocol handshake |
|
|
240 | $pl->esrv_new_player; |
|
|
241 | |
|
|
242 | delete $pl->{deny_save}; |
|
|
243 | } |
|
|
244 | |
163 | cf::client->attach (on_addme => sub { |
245 | cf::client->attach (on_addme => sub { |
164 | my ($ns) = @_; |
246 | my ($ns) = @_; |
165 | |
247 | |
166 | $ns->pl and return $ns->destroy; |
248 | $ns->{addme}++ and return $ns->destroy; |
167 | |
249 | |
168 | $ns->async (sub { |
250 | $ns->async (sub { |
169 | $Coro::current->{desc} = "addme init"; |
251 | $Coro::current->{desc} = "addme init"; |
170 | |
252 | |
171 | my ($user, $pass); |
253 | my ($user, $pass); |
… | |
… | |
246 | aio_stat $pl->path and next; |
328 | aio_stat $pl->path and next; |
247 | my $mtime = (stat _)[9]; |
329 | my $mtime = (stat _)[9]; |
248 | my $token = $pl->password; |
330 | my $token = $pl->password; |
249 | |
331 | |
250 | if ($cf::CFG{ext_login_nocheck} or compare_password $pass, $token) { |
332 | if ($cf::CFG{ext_login_nocheck} or compare_password $pass, $token) { |
251 | $pl->password (encode_password $pass); # make sure we store the new encoding #d# |
333 | # player exists and passwords match - we can proceed |
252 | nuke_str $pass; |
334 | |
253 | # password matches, wonderful |
335 | # password matches, wonderful |
254 | my $pl = cf::player::find $user or next; |
336 | my $pl = cf::player::find $user or next; |
255 | $pl->connect ($ns); |
337 | $pl->connect ($ns); |
256 | enter_map $pl; |
338 | enter_map $pl; |
|
|
339 | login_done $pl; |
257 | last; |
340 | return; |
258 | } elsif (can_cleanup $pl, $mtime) { |
341 | } elsif (can_cleanup $pl, $mtime) { |
259 | Coro::Timer::sleep 1; |
342 | Coro::Timer::sleep 1; |
260 | |
343 | |
261 | $ns->send_drawinfo ( |
344 | $ns->send_drawinfo ( |
262 | "Player exists, but password does not match. If this is your account, " |
345 | "Player exists, but password does not match. If this is your account, " |
263 | . "please try again. If not, you can now decide to take over this account " |
346 | . "please try again. If not, you can now decide to take over this account " |
264 | . "because it has not been in-use for some time.", |
347 | . "because it has not been in-use for some time.", |
265 | cf::NDI_RED |
348 | cf::NDI_RED |
266 | ); |
349 | ); |
267 | |
350 | |
268 | #TODO: nuke_str |
|
|
269 | (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ |
351 | (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ |
270 | or next; |
352 | or next; |
271 | |
353 | |
272 | # check if the file hasn't changed |
354 | # check if the file hasn't changed |
273 | aio_stat cf::player::path $user and next; |
355 | aio_stat cf::player::path $user and next; |
… | |
… | |
275 | |
357 | |
276 | $pl->quit_character; |
358 | $pl->quit_character; |
277 | |
359 | |
278 | # fall through to creation |
360 | # fall through to creation |
279 | } else { |
361 | } else { |
280 | nuke_str $pass; |
|
|
281 | |
|
|
282 | Coro::Timer::sleep 1; |
362 | Coro::Timer::sleep 1; |
283 | |
363 | |
284 | $ns->send_drawinfo ( |
364 | $ns->send_drawinfo ( |
285 | "Wrong username or password. Please try again " |
365 | "Wrong username or password. Please try again " |
286 | . "(check for Numlock and other semi-obvious error sources).", |
366 | . "(check for Numlock and other semi-obvious error sources).", |
… | |
… | |
301 | ); |
381 | ); |
302 | next; |
382 | next; |
303 | } |
383 | } |
304 | } |
384 | } |
305 | |
385 | |
306 | # the rest of this function is character creation |
|
|
307 | $Coro::current->{desc} = "addme($user) chargen"; |
|
|
308 | |
|
|
309 | # just to make sure nothing is left over |
|
|
310 | # normally, nothing is there. |
|
|
311 | nuke_playerdir $user; |
|
|
312 | |
|
|
313 | my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; |
386 | my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; |
314 | |
387 | |
315 | if ($pass2 ne $pass) { |
388 | if ($pass2 ne $pass) { |
316 | nuke_str $pass; |
|
|
317 | nuke_str $pass2; |
|
|
318 | $ns->send_drawinfo ( |
389 | $ns->send_drawinfo ( |
319 | "The passwords do not match, please try again.", |
390 | "The passwords do not match, please try again.", |
320 | cf::NDI_RED |
391 | cf::NDI_RED |
321 | ); |
392 | ); |
322 | Coro::Timer::sleep 0.5; |
393 | Coro::Timer::sleep 0.5; |
323 | next; |
394 | next; |
324 | } |
395 | } |
325 | |
396 | |
326 | nuke_str $pass2; |
|
|
327 | |
|
|
328 | my $pl = cf::player::new $user; |
|
|
329 | $pl->password (encode_password $pass); |
|
|
330 | nuke_str $pass; |
|
|
331 | $pl->connect ($ns); |
|
|
332 | my $ob = $pl->ob; |
|
|
333 | |
|
|
334 | $ob->goto ($pl->maplevel, $ob->x, $ob->y); |
|
|
335 | |
|
|
336 | while () { |
|
|
337 | $ob->update_stats; |
|
|
338 | $pl->save_stats; |
|
|
339 | |
|
|
340 | my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
|
|
341 | "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?"; |
|
|
342 | |
|
|
343 | if ($res =~ /^[Nn]/) { |
|
|
344 | last; |
|
|
345 | } elsif ($res > 0 && $res <= 7) { |
|
|
346 | my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?"; |
|
|
347 | |
|
|
348 | if ($swap > 0 && $swap <= 7) { |
|
|
349 | $ob->swap_stats ($res - 1, $swap - 1); |
|
|
350 | } |
|
|
351 | } else { |
|
|
352 | $ob->roll_stats; |
|
|
353 | } |
|
|
354 | |
|
|
355 | Coro::Timer::sleep 0.05; |
|
|
356 | } |
|
|
357 | |
|
|
358 | $ob->set_animation (2); |
|
|
359 | $ob->add_statbonus; |
|
|
360 | |
|
|
361 | while () { |
|
|
362 | $ns->send_msg ("chargen-race-title", ucfirst $pl->title, -1); |
|
|
363 | my $msg = $ob->msg; |
|
|
364 | $msg =~ s/(?<=\S)\n(?=\S)/ /g; |
|
|
365 | $ns->send_msg ("chargen-race-description", $msg, cf::NDI_BLUE); |
|
|
366 | |
|
|
367 | my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
|
|
368 | "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"; |
|
|
369 | |
|
|
370 | last if $res =~ /[dD]/; |
|
|
371 | |
|
|
372 | $pl->chargen_race_next; |
|
|
373 | Coro::Timer::sleep 0.05; |
|
|
374 | } |
|
|
375 | |
|
|
376 | # create the playerdir, if necessary, as chargen_race_done did it before |
|
|
377 | # presumably because of unique maps |
|
|
378 | aio_mkdir playerdir $pl, 0770; |
|
|
379 | $pl->chargen_race_done; |
|
|
380 | |
|
|
381 | while () { |
|
|
382 | my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
|
|
383 | "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n"; |
|
|
384 | |
|
|
385 | if ($res =~ /^[fF]/) { |
|
|
386 | $pl->gender (1); |
|
|
387 | last; |
|
|
388 | } elsif ($res =~ /^[mM]/) { |
|
|
389 | $pl->gender (0); |
|
|
390 | last; |
|
|
391 | } |
|
|
392 | Coro::Timer::sleep 0.05; |
|
|
393 | } |
|
|
394 | |
|
|
395 | $ob->reply (undef, "Welcome to Deliantra!"); |
|
|
396 | |
|
|
397 | # XXX: Workaround for delayed client ext protocol handshake |
|
|
398 | $pl->esrv_new_player; |
|
|
399 | |
|
|
400 | delete $pl->{deny_save}; |
|
|
401 | |
|
|
402 | last; |
397 | last; |
403 | } |
398 | } |
404 | |
399 | |
405 | if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) { |
400 | # lock again, too layz to make this nicer |
406 | $ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); |
401 | local $cf::LOGIN_LOCK{$user} = 1; |
407 | } |
402 | |
|
|
403 | chargen $ns, $user, $pass; |
|
|
404 | login_done $ns->pl; |
408 | }); |
405 | }); |
409 | }); |
406 | }); |
|
|
407 | |
|
|
408 | cf::client->attach ( |
|
|
409 | on_version => sub { |
|
|
410 | my ($ns, $arg) = @_; |
|
|
411 | |
|
|
412 | srand 1;#d# |
|
|
413 | $ns->{nonces} = [map { join "", map { chr rand 256 } 0..63 } 1..2]; |
|
|
414 | $ns->ext_msg (nonces => @{ $ns->{nonces} }); |
|
|
415 | }, |
|
|
416 | ); |
|
|
417 | |
|
|
418 | #cf::register_async_exticmd create_login => sub { |
|
|
419 | # my ($ns, $reply, $user, $pass) = @_; |
|
|
420 | # |
|
|
421 | # $ns->{addme}++ and return $ns->destroy; |
|
|
422 | #}; |
|
|
423 | |
|
|
424 | cf::register_async_exticmd login => sub { |
|
|
425 | my ($ns, $reply, $user, $hash) = @_; |
|
|
426 | |
|
|
427 | $ns->{addme}++ and return $ns->destroy; |
|
|
428 | |
|
|
429 | $ns->async (sub { |
|
|
430 | local $cf::LOGIN_LOCK{$user} = 1; |
|
|
431 | |
|
|
432 | $Coro::current->{desc} = "login($user) check"; |
|
|
433 | |
|
|
434 | my $fail = sub { |
|
|
435 | $reply->(0, $_[0]); |
|
|
436 | $ns->flush; # does not ensure that the data reaches the client - TODO |
|
|
437 | # need to do this in another thread, as this one gets canceled |
|
|
438 | Coro::async_pool { |
|
|
439 | Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack |
|
|
440 | $ns->destroy; |
|
|
441 | }; |
|
|
442 | Coro::schedule; # do the destroy, should not return |
|
|
443 | }; |
|
|
444 | |
|
|
445 | # try to read the user file and check the password |
|
|
446 | my $pl = cf::player::find $user |
|
|
447 | or return $fail->("User '$user' does not exist - wrong spelling?"); |
|
|
448 | |
|
|
449 | aio_stat $pl->path |
|
|
450 | and return $ns->destroy; |
|
|
451 | |
|
|
452 | my $mtime = (stat _)[9]; |
|
|
453 | my $token = $pl->password; |
|
|
454 | |
|
|
455 | $token = $token =~ /^!/ |
|
|
456 | ? Deliantra::Util::hash_pw pack "H*", substr $token, 1 |
|
|
457 | : pack "H*", $token; |
|
|
458 | |
|
|
459 | $token = Deliantra::Util::auth_pw $token, $ns->{nonces}[0], $ns->{nonces}[1]; |
|
|
460 | |
|
|
461 | $token eq $hash |
|
|
462 | or $cf::CFG{ext_login_nocheck} |
|
|
463 | or return $fail->("User exists, but the password doesn't match - check your spelling, NumLock/CapsLock etc."); |
|
|
464 | |
|
|
465 | # player exists and passwords match - we can proceed |
|
|
466 | |
|
|
467 | $pl->connect ($ns); |
|
|
468 | enter_map $pl; |
|
|
469 | login_done $pl; |
|
|
470 | |
|
|
471 | $reply->(1, "Success"); |
|
|
472 | }); |
|
|
473 | }; |
410 | |
474 | |
411 | cf::register_command password => sub { |
475 | cf::register_command password => sub { |
412 | my ($pl, $arg) = @_; |
476 | my ($pl, $arg) = @_; |
413 | |
477 | |
414 | unless ($pl->flag (cf::FLAG_WIZ)) { |
478 | unless ($pl->flag (cf::FLAG_WIZ)) { |