ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/login.ext
(Generate patch)

Comparing deliantra/server/ext/login.ext (file contents):
Revision 1.116 by root, Wed May 4 07:36:40 2011 UTC vs.
Revision 1.124 by root, Sat Nov 17 06:41:16 2012 UTC

2 2
3# login handling 3# login handling
4 4
5use Fcntl; 5use Fcntl;
6use Coro::AIO; 6use Coro::AIO;
7use Deliantra::Util ();
7 8
8our $MAX_DISCONNECT_TIME = $cf::CFG{max_disconnect_time} || 3600; 9CONF MAX_DISCONNECT_TIME = 3600;
9
10# paranoia function to overwrite a string-in-place
11sub nuke_str {
12 substr $_[0], 0, (length $_[0]), "x" x length $_[0]
13}
14 10
15sub query { 11sub 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
76sub enter_map { 67sub enter_map {
131 122
132 $ob->goto ($map, $x, $y); 123 $ob->goto ($map, $x, $y);
133} 124}
134 125
135sub encode_password($) { 126sub 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
142sub compare_password($$) { 130sub 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
153sub 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
161sub 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
163cf::client->attach (on_addme => sub { 245cf::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
408cf::client->attach (
409 on_version => sub {
410 my ($ns, $arg) = @_;
411
412 $ns->{nonces} = [map { join "", map { chr rand 256 } 0..63 } 1..2];
413 $ns->ext_msg (nonces => @{ $ns->{nonces} });
414 },
415);
416
417#cf::register_async_exticmd create_login => sub {
418# my ($ns, $reply, $user, $pass) = @_;
419#
420# $ns->{addme}++ and return $ns->destroy;
421#};
422
423cf::register_async_exticmd login => sub {
424 my ($ns, $reply, $user, $hash) = @_;
425
426 $ns->{addme}++ and return $ns->destroy;
427
428 $ns->async (sub {
429 local $cf::LOGIN_LOCK{$user} = 1;
430
431 $Coro::current->{desc} = "login($user) check";
432
433 my $fail = sub {
434 $reply->(0, $_[0]);
435 $ns->flush; # does not ensure that the data reaches the client - TODO
436 # need to do this in another thread, as this one gets canceled
437 Coro::async_pool {
438 Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack
439 $ns->destroy;
440 };
441 Coro::schedule; # do the destroy, should not return
442 };
443
444 # try to read the user file and check the password
445 my $pl = cf::player::find $user
446 or return $fail->("User '$user' does not exist - wrong spelling?");
447
448 aio_stat $pl->path
449 and return $ns->destroy;
450
451 my $mtime = (stat _)[9];
452 my $token = $pl->password;
453
454 $token = $token =~ /^!/
455 ? Deliantra::Util::hash_pw pack "H*", substr $token, 1
456 : pack "H*", $token;
457
458 $token = Deliantra::Util::auth_pw $token, $ns->{nonces}[0], $ns->{nonces}[1];
459
460 $token eq $hash
461 or $cf::CFG{ext_login_nocheck}
462 or return $fail->("User exists, but the password doesn't match - check your spelling, NumLock/CapsLock etc.");
463
464 # player exists and passwords match - we can proceed
465
466 $pl->connect ($ns);
467 enter_map $pl;
468 login_done $pl;
469
470 $reply->(1, "Success");
471 });
472};
410 473
411cf::register_command password => sub { 474cf::register_command password => sub {
412 my ($pl, $arg) = @_; 475 my ($pl, $arg) = @_;
413 476
414 unless ($pl->flag (cf::FLAG_WIZ)) { 477 unless ($pl->flag (cf::FLAG_WIZ)) {
415 $pl->message ( 478 $pl->message (
416 "The password can currently only changed by a DM.", 479 "The password can currently only changed by a DM.",
417 cf::NDI_UNIQUE | cf::NDI_REPLY); 480 cf::NDI_UNIQUE | cf::NDI_REPLY);
418 return; 481 return;
419 } 482 }
483
484 $pl->message (#d#
485 "Passwords cannot currently be changed.",#d#
486 cf::NDI_UNIQUE | cf::NDI_REPLY);#d#
487 return;#d#
420 488
421 my (@args) = split /\s+/, $arg; 489 my (@args) = split /\s+/, $arg;
422 my ($player, $new_pw) = @args; 490 my ($player, $new_pw) = @args;
423 491
424 if ($pl->flag (cf::FLAG_WIZ) && $player eq '') { 492 if ($pl->flag (cf::FLAG_WIZ) && $player eq '') {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines