1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*
* userauth.c - user stuff
*
* $Id: servauth.c,v 1.5 2004/11/14 08:26:40 jon Exp $
*
* Copyright 1999-2004 Jon Trulson under the ARTISTIC LICENSE. (See LICENSE).
*/
#include "c_defs.h"
#include "global.h"
#include "conqdef.h"
#include "conqcom.h"
#include "conqlb.h"
#include "context.h"
#include "conf.h"
#include "color.h"
#include <pwd.h>
#include <sys/types.h>
#include "server.h"
#include "packet.h"
#include "protocol.h"
#include "userauth.h"
#include "servauth.h"
static void expire_users(void);
static int doLogin(char *login, char *pw, char *epw)
{
char salt[3];
int unum;
if (clbGetUserNum(&unum, login, 0))
{ /* user exists */
/* ENCRYPT and compare here... */
salt[0] = (login[0] != EOS) ? login[0] : 'J';
salt[1] = (login[1] != EOS) ? login[1] : 'T';
salt[2] = EOS;
strncpy(epw, (char *)crypt(pw, salt), MAXUSERNAME - 2);
epw[MAXUSERNAME - 1] = EOS;
if (strcmp(epw, Users[unum].pw) != 0)
{ /* invalid pw */
clog("INFO: Invalid password for user '%s'", login);
return PERR_BADPWD;
}
clog("INFO: user '%s' logged in", login);
return PERR_OK; /* it's all good */
}
/* if we're here, it's a new user. encrypt pw for welcome. */
salt[0] = (login[0] != EOS) ? login[0] : 'J';
salt[1] = (login[1] != EOS) ? login[1] : 'T';
salt[2] = EOS;
strncpy(epw, (char *)crypt(pw, salt), MAXUSERNAME - 2);
epw[MAXUSERNAME - 1] = EOS;
clog("INFO: New user '%s' logged in", login);
return PERR_OK;
}
/* here, we wait for auth packets from the client until fatal error,
or successful login */
int Authenticate(char *username, char *password)
{
cpAuthenticate_t *cauth;
int unum;
Unsgn8 buf[PKT_MAXSIZE];
int done, rv;
char epw[MAXUSERNAME];
int logcount = 3;
int sockl[2] = {sInfo.sock, sInfo.usock};
expire_users(); /* now is a good time to expire users */
#if defined(DEBUG_SERVERAUTH)
clog("INFO: Authenticate ENTERING\n");
#endif
done = FALSE;
while (!done)
{
rv = waitForPacket(PKT_FROMCLIENT, sockl, CP_AUTHENTICATE,
buf, PKT_MAXSIZE, (60 * 10), "Waiting for Auth");
if (rv <= 0)
{
clog("conquestd:Authenticate: waitforpacket returned %d", rv);
return FALSE;
}
cauth = (cpAuthenticate_t *)buf;
cauth->login[MAXUSERNAME - 1] = 0;
cauth->pw[MAXUSERNAME - 1] = 0;
if (checkuname(cauth->login) == FALSE)
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_ERROR, PERR_INVUSER,
NULL);
continue;
}
switch (cauth->flag)
{
case CPAUTH_CHECKUSER:
if (clbGetUserNum( &unum, cauth->login, 0 ) == TRUE)
{ /* user exits */
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_OK,
NULL);
}
else
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_ERROR, PERR_NOUSER,
NULL);
}
break;
case CPAUTH_LOGIN:
if (logcount <= 0) /* too many tries, fail */
{
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_FATAL, PERR_BADPWD,
NULL);
return FALSE;
}
if ((rv = doLogin(cauth->login, cauth->pw, epw)) != PERR_OK)
{ /* somethings wrong, bad/inv pw, etc */
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_ERROR, rv,
NULL);
logcount--;
}
else
{ /* login successful */
done = TRUE;
sendAck(sInfo.sock, PKT_TOCLIENT, PSEV_INFO, PERR_OK,
NULL);
}
break;
default:
clog("servauth: invalid auth flag %d\n",
cauth->flag);
break;
}
}
strncpy(username, cauth->login, MAXUSERNAME);
strncpy(password, epw, MAXUSERNAME);
return(TRUE);
}
/* expire any old users (unless they have active ships running...) */
/* (DOES LOCKING) */
void expire_users(void)
{
register int i, j;
time_t difftime = 0;
int hasship = FALSE;
unsigned int expire_secs;
#if defined(DEBUG_SERVERAUTH)
clog("INFO: expire_users(): Expiring users...");
#endif
if (SysConf.UserExpiredays == 0)
{ /* expiration has been disabled */
#if defined(DEBUG_SERVERAUTH)
clog("INFO: expire_users(): SysConf.UserExpiredays == 0, expiration disabled");
#endif
return;
}
expire_secs = (SysConf.UserExpiredays * SECS_PER_DAY);
PVLOCK(&ConqInfo->lockword);
for (i=0; i < MAXUSERS; i++)
{
if (Users[i].live == FALSE || Users[i].robot ||
Users[i].ooptions[OOPT_OPER])
continue; /* only living, human, non godlike users
are subject to expiration. */
difftime = getnow(NULL, 0) - Users[i].lastentry;
if (difftime < 0)
{ /* screen out negative expirations -
only happens when system clock goes
way back */
clog("INFO: expire_users(): difftime (%d) is less than 0, skipping user %s\n",
difftime, Users[i].username);
continue;
}
#if defined(DEBUG_SERVERAUTH)
clog("expire_users(): getnow(NULL, 0) = %d, Users[%d].lastentry = %d",
getnow(NULL, 0),
i, Users[i].lastentry);
#endif
if ((unsigned int)Users[i].lastentry != 0 &&
(unsigned int) difftime > expire_secs)
{ /* we have a candidate... */
/* loop thru the ships, making sure he
doesn't have one active */
#if defined(DEBUG_SERVERAUTH)
clog("expire_users(): have a candidate: user '%s' (%d), difftime = %d > expire_secs = %d, Users[i].lastentry = %d",
Users[i].username,
i,
difftime,
expire_secs,
Users[i].lastentry);
#endif
hasship = FALSE;
for (j=1; j <= MAXSHIPS; j++)
{
if (Ships[j].unum == i && Ships[j].status == SS_LIVE)
{
hasship = TRUE;
break;
}
}
if (hasship)
{ /* we can't waste him */
clog("INFO: expire_users(): Couldn't expire remote user '%s' due to active ship(s)",
Users[i].username);
}
else
{ /* waste him */
/* have to play some trickery here
since resign locks the commonblock */
PVUNLOCK(&ConqInfo->lockword);
#if defined(DEBUG_SERVERAUTH)
clog("expire_users(): calling clbResign(%d, %d)", i, TRUE);
#endif
clbResign(i, TRUE);
clog("INFO: expire_users(): Expired remote user '%s' after %d days of inactivity",
Users[i].username,
difftime / SECS_PER_DAY);
/* re-aquire the lock */
PVLOCK(&ConqInfo->lockword);
}
}
}
/* done */
PVUNLOCK(&ConqInfo->lockword);
#if defined(DEBUG_SERVERAUTH)
clog("expire_users(): ...Done");
#endif
return;
}
|