]> git.pond.sub.org Git - empserver/blob - src/client/login.c
Make Windows client read password without echo again
[empserver] / src / client / login.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  login.c: Log into an empire server
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Steve McClure, 1998
33  */
34
35 #include <config.h>
36
37 #include <ctype.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #if !defined(_WIN32)
42 #include <unistd.h>
43 #endif
44 #include "misc.h"
45 #include "proto.h"
46
47 #ifndef HAVE_GETPASS
48 #define getpass ersatz_getpass
49 static char *
50 ersatz_getpass(char *prompt)
51 {
52     static char buf[128];
53     char *p;
54     size_t len;
55 #ifdef _WIN32
56     DWORD mode;
57     HANDLE input_handle = GetStdHandle(STD_INPUT_HANDLE);
58
59     if (GetConsoleMode(input_handle, &mode))
60         SetConsoleMode(input_handle, mode & ~ENABLE_ECHO_INPUT);
61     else
62 #endif
63         printf("Note: your input is echoed to the screen\n");
64     printf("Your name? ");
65     fflush(stdout);
66     p = fgets(buf, sizeof(buf), stdin);
67 #ifdef _WIN32
68     if (GetConsoleMode(input_handle, &mode))
69         SetConsoleMode(input_handle, mode | ENABLE_ECHO_INPUT);
70 #endif
71     if (!p)
72         return NULL;
73     len = strlen(p);
74     if (p[len - 1] == '\n')
75         p[len - 1] = 0;
76     return p;
77 }
78 #endif
79
80 int
81 login(int s, char *uname, char *cname, char *cpass,
82       int kill_proc, int utf8)
83 {
84     char tmp[128];
85     char buf[1024];
86     char *ptr;
87     char *p;
88     int len, code;
89
90     if (!expect(s, C_INIT, buf))
91         return 0;
92     sendcmd(s, "user", uname);
93     if (!expect(s, C_CMDOK, buf)) {
94         fprintf(stderr, "Server rejects your user name\n");
95         return 0;
96     }
97     if (utf8) {
98         sendcmd(s, "options", "utf-8");
99         for (;;) {
100             code = recvline(s, buf);
101             if (code == C_CMDOK)
102                 break;
103             if (code != C_DATA) {
104                 fprintf(stderr, "Server doesn't support UTF-8\n");
105                 return 0;
106             }
107         }
108     }
109     if (cname == NULL) {
110         (void)printf("Country name? ");
111         fflush(stdout);
112         cname = fgets(tmp, sizeof(tmp), stdin);
113         if (cname == NULL || *cname == 0)
114             return 0;
115     }
116     len = strlen(cname);
117     if (cname[len-1] == '\n')
118         cname[len-1] = 0;
119     sendcmd(s, "coun", cname);
120     if (!expect(s, C_CMDOK, buf)) {
121         (void)fprintf(stderr, "No such country\n");
122         return 0;
123     }
124     if (cpass == NULL) {
125         cpass = getpass("Your name? ");
126         if (cpass == NULL || *cpass == 0)
127             return 0;
128     }
129     (void)printf("\n");
130     sendcmd(s, "pass", cpass);
131     memset(cpass, 0, strlen(cpass));    /* for core dumps */
132     if (!expect(s, C_CMDOK, buf)) {
133         (void)fprintf(stderr, "Bad password\n");
134         return 0;
135     }
136     if (kill_proc) {
137         sendcmd(s, "kill", NULL);
138         (void)printf("\n\t-=O=-\n");
139         (void)expect(s, C_EXIT, buf);
140         fprintf(stderr, "%s\n", buf);
141         return 0;
142     }
143     sendcmd(s, "play", NULL);
144     (void)printf("\n\t-=O=-\n");
145     if (!expect(s, C_INIT, buf)) {
146         fprintf(stderr, "%s\n", buf);
147         return 0;
148     }
149     for (ptr = buf; !isspace(*ptr); ptr++) ;
150     ptr++;
151     p = strchr(ptr, '\n');
152     if (p != NULL)
153         *p = 0;
154     if (atoi(ptr) != CLIENTPROTO) {
155         printf("Empire client out of date; get new version!\n");
156         printf("   this version: %d, current version: %d\n",
157                CLIENTPROTO, atoi(ptr));
158     }
159     fflush(stdout);
160     return 1;
161 }