1 |
michael |
6161 |
/* |
2 |
|
|
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
|
|
* |
4 |
michael |
7006 |
* Copyright (c) 1997-2016 ircd-hybrid development team |
5 |
michael |
6161 |
* |
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 |
michael |
6743 |
int |
46 |
|
|
valid_uid(const char *uid) |
47 |
|
|
{ |
48 |
|
|
if (strlen(uid) != TOTALSIDUID) |
49 |
|
|
return 0; |
50 |
|
|
|
51 |
|
|
if (!IsDigit(*uid)) |
52 |
|
|
return 0; |
53 |
|
|
|
54 |
|
|
for (unsigned int i = 1; i < TOTALSIDUID; ++i) |
55 |
|
|
if (!IsAlNum(*(uid + i))) |
56 |
|
|
return 0; |
57 |
|
|
|
58 |
|
|
return 1; |
59 |
|
|
} |
60 |
|
|
|
61 |
michael |
6161 |
/* |
62 |
|
|
* init_uid() |
63 |
|
|
* |
64 |
|
|
* inputs - NONE |
65 |
|
|
* output - NONE |
66 |
|
|
* side effects - new_uid is filled in with server id portion (sid) |
67 |
|
|
* (first 3 bytes). Rest is filled in with '9'. |
68 |
|
|
* |
69 |
|
|
*/ |
70 |
|
|
void |
71 |
|
|
init_uid(void) |
72 |
|
|
{ |
73 |
|
|
snprintf(new_uid, sizeof(new_uid), "%s999999", me.id); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
/* |
77 |
|
|
* add_one_to_uid |
78 |
|
|
* |
79 |
|
|
* inputs - index number into new_uid |
80 |
|
|
* output - NONE |
81 |
|
|
* side effects - new_uid is incremented by one |
82 |
|
|
* note this is a recursive function |
83 |
|
|
*/ |
84 |
|
|
static void |
85 |
|
|
add_one_to_uid(unsigned int i) |
86 |
|
|
{ |
87 |
|
|
if (i < IRC_MAXSID) |
88 |
|
|
return; |
89 |
|
|
|
90 |
|
|
if (new_uid[i] == 'Z') |
91 |
|
|
new_uid[i] = '0'; |
92 |
|
|
else if (new_uid[i] == '9') |
93 |
|
|
{ |
94 |
|
|
new_uid[i] = 'A'; |
95 |
|
|
add_one_to_uid(i - 1); |
96 |
|
|
} |
97 |
|
|
else |
98 |
|
|
++new_uid[i]; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
/* |
102 |
|
|
* uid_get |
103 |
|
|
* |
104 |
|
|
* inputs - struct Client * |
105 |
|
|
* output - new UID is returned to caller |
106 |
|
|
* side effects - new_uid is incremented by one. |
107 |
|
|
*/ |
108 |
|
|
const char * |
109 |
|
|
uid_get(void) |
110 |
|
|
{ |
111 |
|
|
add_one_to_uid(TOTALSIDUID - 1); /* Index from 0 */ |
112 |
|
|
return new_uid; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
void |
116 |
|
|
generate_sid(void) |
117 |
|
|
{ |
118 |
|
|
unsigned int sid = 0; |
119 |
|
|
const char *p; |
120 |
|
|
|
121 |
|
|
for (p = me.name; *p; ++p) |
122 |
|
|
sid = 5 * sid + *p; |
123 |
|
|
for (p = me.info; *p; ++p) |
124 |
|
|
sid = 5 * sid + *p; |
125 |
|
|
|
126 |
|
|
snprintf(me.id, IRC_MAXSID + 1, "%03d", sid % 1000); |
127 |
|
|
} |