summaryrefslogtreecommitdiffstats
path: root/src/core/transport/http/sender/ntlm/test/mock_client.c
blob: d44ce80e228ef009df6a43bb38c5d072878863a1 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* tcpclient.c */

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <axutil_base64.h>
#include <axutil_string.h>
#include <axis2_util.h>
#include <axis2_ntlm.h>


/*axis2_char_t *hostname = "172.16.176.132";*/
axis2_char_t *hostname = "localhost";
int port = 8080;
static char *
create_type1_header(const axutil_env_t *env, axis2_char_t *encoded)
{
    char *type1_header = NULL;
    char *header1 = NULL;

    type1_header = axutil_strcat(env, "Authorization: NTLM " ,encoded, NULL);

    header1 = "HEAD /myservice/Service1.asmx HTTP/1.1\n"\
            "Host: 172.16.176.132:8080\n"\
            "User-Agent: Axis2C/1.7.0\n";
    header1 = axutil_strcat(env, header1, type1_header, "\n\n", NULL);
    AXIS2_FREE(env->allocator, type1_header);

    return header1;
}

static axis2_char_t *
create_type3_header(const axutil_env_t *env, 
        char *encoded)
{
    char *header3 = NULL;

    char *temp_header = strdup("GET /myservice/Service1.asmx?WSDL HTTP/1.1\n"\
            "Host: 172.16.176.132:8080\n"\
            "User-Agent: Axis2C/1.7.0\n"\
            "Authorization: NTLM ");

    int len1 = axutil_strlen(encoded);
    int len2 = axutil_strlen(temp_header);
    header3 = AXIS2_MALLOC(env->allocator, (len1 + len2 + 5));
    memset(header3, 0, sizeof(header3));
    strcat(header3, temp_header);
    strcat(header3, encoded);
    strcat(header3, "\n\n");

    return header3;

}

int main()
{
    int sock, bytes_recieved;  
    char recv_data[10024];
    struct hostent *host;
    struct sockaddr_in server_addr;
    const axis2_char_t *header = NULL;
    axis2_char_t *header_value = NULL;
    char *type3_header = NULL;
    char *type1_header = NULL;
    const char *user = "nandika",
	            *domain = "mydomain",
	            *password = "nandika",
	            *workstation = "workstation";
    axis2_status_t status = AXIS2_FAILURE;
    const axutil_env_t *env = NULL;
    env = axutil_env_create_all("mock_client.log", AXIS2_LOG_LEVEL_TRACE);

    host = gethostbyname(hostname);

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("Socket");
        exit(1);
    }

    server_addr.sin_family = AF_INET;     
    server_addr.sin_port = htons(port);   
    server_addr.sin_addr = *((struct in_addr *)host->h_addr);
    bzero(&(server_addr.sin_zero),8); 
  
    /* Create a normal message */
    char *init_send = axutil_strdup(env, "HEAD /myservice/Service1.asmx HTTP/1.1\n"\
                        "Host: 172.16.176.132:8080\n"\
                        "User-Agent: Axis2C/1.7.0\n\n");

    if (connect(sock, (struct sockaddr *)&server_addr,
                sizeof(struct sockaddr)) == -1) 
    {
        perror("Connect");
        exit(1);
    }
    /* Send a normal message */
    send(sock, init_send, strlen(init_send), 0); 

    while(1)
    {
        bytes_recieved=recv(sock,recv_data,10024,0);
        recv_data[bytes_recieved] = '\0';
        header = axutil_strstr(recv_data, "WWW-Authenticate: NTLM ");
        
        /* Process the challange */
        if(header)
        {
            int i = 22;
            while(header[i] && isspace((unsigned char) header[i]))
            {
                i++;
            }
            if (header[i] != '\0') 
            {
                int len = axutil_strlen(&header[i]);
                if (len == 0)
                {
                    printf("invalid Negotiate token\n");
                }
                else
                {
                    header_value = axutil_strdup(env, &header[i]);
                }
            }
            if(header_value)
            {
                axis2_char_t *temp = strstr(header_value, "==");
                temp = temp + 3;
                *temp = '\0';
                header_value = axutil_strdup(env, header_value);
            }
        }
        if(!header_value) /* printf("unauth_header:\n%s\n", unauth_header); */
        {
            axis2_char_t *encoded = NULL;
            int elen = 0;
            int flags = 0;
            axis2_ntlm_t *ntlm = NULL;
            ntlm = axis2_ntlm_create(env);
            status = axis2_ntlm_auth_create_type1_message(ntlm, env, &encoded, &elen, user, 
                    password, flags, domain);
            if(AXIS2_SUCCESS != status)
            {
                AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, 
                        "axis2_ntlm_auth_create_type1_message call failed");
                printf("axis2_ntlm_auth_create_type1_message call failed\n");
                return 1;
            }

            /* Ceate type 1(negotiation) header  message from the recieved header */
            type1_header = create_type1_header(env, encoded);
            /* Send netotiation message */
            send(sock, type1_header,strlen(type1_header), 0);
            printf("sent:\n%s\n", type1_header);
            free(type1_header);
            continue;
        }
        if(header_value)
        {
            /*printf("header_value:\n***%s***\n", header_value);*/
            int elen = 0;
            axis2_char_t *encoded = NULL;
            axis2_char_t *header_value = NULL;
            axis2_ntlm_t *ntlm = NULL;
            ntlm = axis2_ntlm_create(env);
            status = axis2_ntlm_auth_create_type3_message(ntlm, env, header_value, &encoded, 
                    &elen, user, password, domain, workstation);
            if(AXIS2_SUCCESS != status)
            {
                AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, 
                        "axis2_ntlm_auth_create_type3_message call failed");
                printf("axis2_ntlm_auth_create_type3_message call failed\n");
                return 1;
            }

            /* Create Type3 (authentication) header */
            type3_header = create_type3_header(env, encoded);
            break;
        }
    }
    /* Send Type3(authentication) message */
    send(sock, type3_header, strlen(type3_header), 0); 
    bytes_recieved=recv(sock,recv_data,10024,0);
    recv_data[bytes_recieved] = '\0';
    printf("\nRecieved data:\n%s \n" , recv_data);
    close(sock);
    
    return 0;
}