summaryrefslogtreecommitdiffstats
path: root/ldap/libraries/liblitekey/keycheck.c
blob: 16fed8991643196582525cdc918375c528d7503b (plain)
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
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/
/*
 * keycheck.c
 */


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <litekey.h>

#define DS_NORMAL_MAGIC_KEY	119
#define	DS_LITE_MAGIC_KEY	326
#define FILE_PATHSEP '/'
#define BUFSIZE 800

/*
 * is_directory_lite
 *
 *	Checks if the directory server installation is a normal or a
 * 	lite.  The decision is made based on the key in the key file.
 *
 *   Input:
 *	char	*root;		Pathname to install root
 *   Returns:
 *		1  - yes, it's LITE server
 *		0  - No; it's fully paid (normal)  server.
 *
 */
int is_directory_lite( char *root)
{

	char	buf[40];
	char	*bufp = buf;
	FILE	*fp = NULL;
	int	key =0;
	char	*nsroot;
	char	pathname[BUFSIZE];

	return DS_NORMAL_TYPE; /* richm: no more lite mode in DS 5.0 */
#if 0 /* no more lite mode */
	/* There are 3 ways to determine if the server is FULL or LITE.
	 * 1) Use NETSITE_ROOT variable
	 * 2) Use the root path provided 
	 * 3) Look at the current directory
	 * 
	 * If all of them fails, then it's LITE.
	*/
	nsroot = getenv("NETSITE_ROOT");

	if ( (NULL == root) && (NULL == nsroot)) {
		/* case 3 */
		sprintf ( pathname, "slapd.key" );
	} else if (NULL == nsroot) {
		/* case 2 */
		sprintf ( pathname, "%s%cbin%cslapd%cserver%cslapd.key",
				root, FILE_PATHSEP,FILE_PATHSEP,
				FILE_PATHSEP, FILE_PATHSEP);
	} else {
		/* case 1 */
		sprintf ( pathname, "%s%cbin%cslapd%cserver%cslapd.key",
				nsroot, FILE_PATHSEP,FILE_PATHSEP,
				FILE_PATHSEP, FILE_PATHSEP);
	}


	/* First read from the key file */
	if ((fp = fopen ( pathname, "r")) == NULL )
		return DS_LITE_TYPE;

	if ( fgets(buf, 40, fp) == NULL)
		return DS_LITE_TYPE;

	fclose (fp );

	/* The key is in the format: "key:123456" */
	bufp +=4;
	key = atoi (  (const char *) bufp );

	/* Now we have the key. Determine which one it is */
	if (  0 == (key % DS_NORMAL_MAGIC_KEY))
		return DS_NORMAL_TYPE;
	else if ( 0 == (key % DS_LITE_MAGIC_KEY) )
		return DS_LITE_TYPE;

	/* By defualt, it's lite */
	return DS_LITE_TYPE;
#endif /* no more lite mode */
}

/*
 * generate_lite_key
 * 	Generate a key for the product that is being used.
 *
 *   Input:
 *	type  DS_NORMAL_TYPE	- Normal
 *	      DS_LITE_TYPE	- Lite
 *   Returns:
 *	a int key.
 *
 */
int generate_directory_key( int type)
{

	int key = 0;
	int val;

	val = rand();

	if (type == DS_NORMAL_TYPE )
		key = val * DS_NORMAL_MAGIC_KEY;
	else if (type == DS_LITE_TYPE )
		key = val * DS_LITE_MAGIC_KEY;

	return key;
}

/*
 * is_key_validNormalKey
 * 
 * Check if the key ia a valid normal key or not.
 */
int 
is_key_validNormalKey ( int key )
{

	if (key <= 0 ) return 0;

	if (0 == ( key % DS_NORMAL_MAGIC_KEY ))
		return 1;

	return 0;
}