~ampelbein/ubuntu/oneiric/heartbeat/lp-770743

« back to all changes in this revision

Viewing changes to lib/plugins/stonith/drac3_hash.c

  • Committer: Bazaar Package Importer
  • Author(s): Ante Karamatic
  • Date: 2010-02-17 21:59:18 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20100217215918-06paxph5do4saw8v
Tags: 3.0.2-0ubuntu1
* New upstream release
* Drop hard dep on pacemaker for heartbet; moved to Recommends
* debian/heartbeat.install:
  - follow upstream changes
* debian/control:
  - added docbook-xsl and xsltproc to build depends

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Stonith module for Dell DRACIII (Dell Remote Access Card)
3
 
 *
4
 
 * Copyright (C) 2003 Alfa21 Outsourcing
5
 
 * Copyright (C) 2003 Roberto Moreda <moreda@alfa21.com>
6
 
 *
7
 
 * This library is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU Lesser General Public
9
 
 * License as published by the Free Software Foundation; either
10
 
 * version 2.1 of the License, or (at your option) any later version.
11
 
 * 
12
 
 * This library is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
 * Lesser General Public License for more details.
16
 
 * 
17
 
 * You should have received a copy of the GNU Lesser General Public
18
 
 * License along with this library; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20
 
 *
21
 
 */
22
 
 
23
 
#include <lha_internal.h>
24
 
 
25
 
#include <string.h>
26
 
#include <stdio.h>
27
 
#include <clplumbing/base64.h>
28
 
#include <clplumbing/md5.h>
29
 
#include <glib.h>
30
 
 
31
 
#include "drac3_hash.h"
32
 
 
33
 
#define BUFLEN        1024    /* buffer */
34
 
#define SBUFLEN        256    /* small buffer */
35
 
#define MD5LEN          16    /* md5 buffer */
36
 
 
37
 
/* Hash functions for DRAC3 authentication */
38
 
 
39
 
guint16 
40
 
drac3Crc16(const char *str, 
41
 
        const int l) {
42
 
 
43
 
    int i,j;
44
 
    guint16 crc = 0;
45
 
    
46
 
    for (i=0; i<l; i++) {
47
 
        crc = crc ^ (str[i] << 8);
48
 
        for (j=0; j<8; j++)  
49
 
            crc = ( (crc & 0x8000) == 32768 ? (crc<<1) ^ 0x1021 : crc<<1);
50
 
    }    
51
 
    crc = crc & 0xFFFF;
52
 
    return crc;
53
 
}
54
 
 
55
 
void 
56
 
drac3AuthHash(const char * chall, 
57
 
        const char * pass, 
58
 
        char * token, 
59
 
        int len) {
60
 
    
61
 
    char * chall_dup;    
62
 
    char challBytes[MD5LEN];
63
 
    char passMD5[MD5LEN];
64
 
    char xorBytes[MD5LEN];
65
 
    char xorBytesMD5[MD5LEN];
66
 
    guint16 crc;
67
 
    char response[MD5LEN+2];
68
 
    char responseb64[SBUFLEN];
69
 
    int i;
70
 
    
71
 
    /* decodes chall -> challBytes */
72
 
    memset(challBytes, 0, MD5LEN);
73
 
    chall_dup = g_strdup(chall);
74
 
    if (chall_dup[strlen(chall_dup) - 1] == '\n' ) {
75
 
        chall_dup[strlen(chall_dup) - 1] = '\0';
76
 
    }
77
 
    base64_to_binary(chall_dup, strlen(chall_dup), challBytes, MD5LEN);
78
 
 
79
 
    /* gets MD5 from pass -> passMD5 */
80
 
    MD5((const unsigned char *)pass, strlen(pass), (unsigned char *)passMD5);
81
 
    
82
 
    /* calculate challBytes and passMD5 xor -> xorBytes */
83
 
    for (i=0; i<MD5LEN; i++) {
84
 
        xorBytes[i] = challBytes[i] ^ passMD5[i];
85
 
    }
86
 
    
87
 
    /* calculate xorBytes MD5 -> xorBytesMD5 */
88
 
    MD5((unsigned char *)xorBytes, MD5LEN, (unsigned char *)xorBytesMD5);
89
 
 
90
 
    /* calculate xorBytesMD5 crc16 */
91
 
    crc = drac3Crc16(xorBytesMD5, MD5LEN);
92
 
    
93
 
    /* joins xorBytesMD5 and crc16 -> response */
94
 
    memcpy(response, xorBytesMD5, MD5LEN);
95
 
    memcpy(response+MD5LEN, &crc, 2);
96
 
    
97
 
    /* calculate response base64 -> responseb64 */
98
 
    memset(responseb64, 0, SBUFLEN);
99
 
    binary_to_base64(response, MD5LEN+2, responseb64, SBUFLEN);
100
 
 
101
 
    /* assuring null-termination */
102
 
    responseb64[SBUFLEN-1]=0x00;
103
 
    
104
 
    snprintf(token, len, "%s", responseb64);
105
 
    token[len-1]=0x00;
106
 
}