| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
* |
| 4 |
* Copyright (c) 1997-2015 ircd-hybrid development team |
| 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 19 |
* USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/*! \file id.c |
| 23 |
* \brief Contains functions pertaining to SID/UID generation. |
| 24 |
* \version $Id$ |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "stdinc.h" |
| 28 |
#include "id.h" |
| 29 |
#include "irc_string.h" |
| 30 |
#include "client.h" |
| 31 |
|
| 32 |
static char new_uid[TOTALSIDUID + 1]; /* Allow for \0 */ |
| 33 |
|
| 34 |
int |
| 35 |
valid_sid(const char *sid) |
| 36 |
{ |
| 37 |
if (strlen(sid) == IRC_MAXSID) |
| 38 |
if (IsDigit(*sid)) |
| 39 |
if (IsAlNum(*(sid + 1)) && IsAlNum(*(sid + 2))) |
| 40 |
return 1; |
| 41 |
|
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
/* |
| 46 |
* init_uid() |
| 47 |
* |
| 48 |
* inputs - NONE |
| 49 |
* output - NONE |
| 50 |
* side effects - new_uid is filled in with server id portion (sid) |
| 51 |
* (first 3 bytes). Rest is filled in with '9'. |
| 52 |
* |
| 53 |
*/ |
| 54 |
void |
| 55 |
init_uid(void) |
| 56 |
{ |
| 57 |
snprintf(new_uid, sizeof(new_uid), "%s999999", me.id); |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
* add_one_to_uid |
| 62 |
* |
| 63 |
* inputs - index number into new_uid |
| 64 |
* output - NONE |
| 65 |
* side effects - new_uid is incremented by one |
| 66 |
* note this is a recursive function |
| 67 |
*/ |
| 68 |
static void |
| 69 |
add_one_to_uid(unsigned int i) |
| 70 |
{ |
| 71 |
if (i < IRC_MAXSID) |
| 72 |
return; |
| 73 |
|
| 74 |
if (new_uid[i] == 'Z') |
| 75 |
new_uid[i] = '0'; |
| 76 |
else if (new_uid[i] == '9') |
| 77 |
{ |
| 78 |
new_uid[i] = 'A'; |
| 79 |
add_one_to_uid(i - 1); |
| 80 |
} |
| 81 |
else |
| 82 |
++new_uid[i]; |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* uid_get |
| 87 |
* |
| 88 |
* inputs - struct Client * |
| 89 |
* output - new UID is returned to caller |
| 90 |
* side effects - new_uid is incremented by one. |
| 91 |
*/ |
| 92 |
const char * |
| 93 |
uid_get(void) |
| 94 |
{ |
| 95 |
add_one_to_uid(TOTALSIDUID - 1); /* Index from 0 */ |
| 96 |
return new_uid; |
| 97 |
} |
| 98 |
|
| 99 |
void |
| 100 |
generate_sid(void) |
| 101 |
{ |
| 102 |
unsigned int sid = 0; |
| 103 |
const char *p; |
| 104 |
|
| 105 |
for (p = me.name; *p; ++p) |
| 106 |
sid = 5 * sid + *p; |
| 107 |
for (p = me.info; *p; ++p) |
| 108 |
sid = 5 * sid + *p; |
| 109 |
|
| 110 |
snprintf(me.id, IRC_MAXSID + 1, "%03d", sid % 1000); |
| 111 |
} |