]> git.pond.sub.org Git - empserver/blob - src/client/login.c
Replace missing getpass()
[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
56     printf("Note: your input is echoed to the screen\n");
57     printf("Your name? ");
58     fflush(stdout);
59     p = fgets(buf, sizeof(buf), stdin);
60     if (!p)
61         return NULL;
62     len = strlen(p);
63     if (p[len - 1] == '\n')
64         p[len - 1] = 0;
65     return p;
66 }
67 #endif
68
69 int
70 login(int s, char *uname, char *cname, char *cpass,
71       int kill_proc, int utf8)
72 {
73     char tmp[128];
74     char buf[1024];
75     char *ptr;
76     char *p;
77     int len, code;
78
79     if (!expect(s, C_INIT, buf))
80         return 0;
81     sendcmd(s, "user", uname);
82     if (!expect(s, C_CMDOK, buf)) {
83         fprintf(stderr, "Server rejects your user name\n");
84         return 0;
85     }
86     if (utf8) {
87         sendcmd(s, "options", "utf-8");
88         for (;;) {
89             code = recvline(s, buf);
90             if (code == C_CMDOK)
91                 break;
92             if (code != C_DATA) {
93                 fprintf(stderr, "Server doesn't support UTF-8\n");
94                 return 0;
95             }
96         }
97     }
98     if (cname == NULL) {
99         (void)printf("Country name? ");
100         fflush(stdout);
101         cname = fgets(tmp, sizeof(tmp), stdin);
102         if (cname == NULL || *cname == 0)
103             return 0;
104     }
105     len = strlen(cname);
106     if (cname[len-1] == '\n')
107         cname[len-1] = 0;
108     sendcmd(s, "coun", cname);
109     if (!expect(s, C_CMDOK, buf)) {
110         (void)fprintf(stderr, "No such country\n");
111         return 0;
112     }
113     if (cpass == NULL) {
114         cpass = getpass("Your name? ");
115         if (cpass == NULL || *cpass == 0)
116             return 0;
117     }
118     (void)printf("\n");
119     sendcmd(s, "pass", cpass);
120     memset(cpass, 0, strlen(cpass));    /* for core dumps */
121     if (!expect(s, C_CMDOK, buf)) {
122         (void)fprintf(stderr, "Bad password\n");
123         return 0;
124     }
125     if (kill_proc) {
126         sendcmd(s, "kill", NULL);
127         (void)printf("\n\t-=O=-\n");
128         (void)expect(s, C_EXIT, buf);
129         fprintf(stderr, "%s\n", buf);
130         return 0;
131     }
132     sendcmd(s, "play", NULL);
133     (void)printf("\n\t-=O=-\n");
134     if (!expect(s, C_INIT, buf)) {
135         fprintf(stderr, "%s\n", buf);
136         return 0;
137     }
138     for (ptr = buf; !isspace(*ptr); ptr++) ;
139     ptr++;
140     p = strchr(ptr, '\n');
141     if (p != NULL)
142         *p = 0;
143     if (atoi(ptr) != CLIENTPROTO) {
144         printf("Empire client out of date; get new version!\n");
145         printf("   this version: %d, current version: %d\n",
146                CLIENTPROTO, atoi(ptr));
147     }
148     fflush(stdout);
149     return 1;
150 }