-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreatecookie.c
More file actions
72 lines (69 loc) · 1.83 KB
/
createcookie.c
File metadata and controls
72 lines (69 loc) · 1.83 KB
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
/*
* createcookie.c
*
* Copyright (C) 2001,2007 Staf Wagemakers Belgie/Belgium
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "createcookie.h"
#define MAX_COOKIE_SIZE 60
/*
* create a valid random char
*/
int gen_random_char(int s)
{
int i;
srand(s);
i=rand();
i=i/(RAND_MAX/63);
if(i==';') i=60;
if(i=='=') i=62;
if(i<0) i=0;
if(i>63) i=63;
if(i<12) return (i+46);
if((i>=12)&&(i<38)) return (i+(65-12));
if(i>=38) return (i+(97-38));
return(i);
}
/*
* create a random cookie string
*/
char *create_cookie() {
char *c,*cookie;
int i;
cookie=(char *)xmalloc(MAX_COOKIE_SIZE);
for (i=0;i<MAX_COOKIE_SIZE;i++) cookie[i]='\0';
srand(getpid());
for (i=0;i<6;i++) {
cookie[i]=gen_random_char(time(0)-rand());
}
cookie[6]='.';
c=getenv("REMOTE_ADDR");
if (c!=NULL) strncat(cookie+7,c,15);
else strncat(cookie+7,"000.000.000.000",15);
cookie[strlen(cookie)]='.';
for(i=0;i<10;i++) {
cookie[strlen(cookie)]=gen_random_char(time(0)-rand());
}
cookie[strlen(cookie)]='.';
for(i=0;i<10;i++) {
srand(rand());
cookie[strlen(cookie)]=gen_random_char(time(0)-rand());
}
cookie[strlen(cookie)]='.';
snprintf(cookie+strlen(cookie),10,"%d",(int) time(0));
return(cookie);
}