~ubuntu-branches/ubuntu/saucy/slurm-llnl/saucy

« back to all changes in this revision

Viewing changes to testsuite/slurm_unit/slurmctld/security_2_8.c

  • Committer: Bazaar Package Importer
  • Author(s): Gennaro Oliva
  • Date: 2008-12-03 11:56:28 UTC
  • mfrom: (1.1.8 upstream) (3.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20081203115628-93t417da6wkazmo5
Tags: 1.3.11-1
New upstream release 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * security_2_8.c
3
 
 * LLNL security test to validate SLURM's wiki interface security
4
 
 *
5
 
 * Compilation line varies by system.
6
 
 * For Peleton:
7
 
 *   cc -L/usr/lib64 -lslurm -osecurity_2_8 security_2_8.c
8
 
 * For most Linux system:
9
 
 *   cc -lslurm -osecurity_2_8 security_2_8.c
10
 
 *
11
 
 * Execute line:
12
 
 *   ./security_2_8
13
 
 *
14
 
 * Expected response:
15
 
 *   Bad checksum reported
16
 
 *   SUCCESS
17
 
 */
18
 
#include <netdb.h>
19
 
#include <stdio.h>
20
 
#include <stdlib.h>
21
 
#include <string.h>
22
 
#include <strings.h>
23
 
#include <time.h>
24
 
#include <slurm/slurm.h>
25
 
#include <sys/types.h>
26
 
#include <sys/socket.h>
27
 
 
28
 
#define _DEBUG 0
29
 
 
30
 
static int _conn_wiki_port(char *host, int port)
31
 
{
32
 
        int sock_fd;
33
 
        struct sockaddr_in wiki_addr;
34
 
        struct hostent *hptr;
35
 
 
36
 
        hptr = gethostbyname(host);
37
 
        if (hptr == NULL) {
38
 
                perror("gethostbyname");
39
 
                exit(1);
40
 
        }
41
 
        if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
42
 
                perror("socket");
43
 
                exit(1);
44
 
        }
45
 
        bzero((char *) &wiki_addr, sizeof(wiki_addr));
46
 
        wiki_addr.sin_family = AF_INET;
47
 
        wiki_addr.sin_port   = htons(port);
48
 
        memcpy(&wiki_addr.sin_addr.s_addr, hptr->h_addr, hptr->h_length);               
49
 
        sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
50
 
        if (connect(sock_fd, (struct sockaddr *) &wiki_addr, 
51
 
                        sizeof(wiki_addr))) {
52
 
                perror("connect");
53
 
                exit(1);
54
 
        }
55
 
        return sock_fd;
56
 
}
57
 
 
58
 
static size_t _read_bytes(int fd, char *buf, const size_t size)
59
 
{
60
 
        size_t bytes_remaining, bytes_read;
61
 
        char *ptr;
62
 
 
63
 
        bytes_remaining = size;
64
 
        ptr = buf;
65
 
        while (bytes_remaining > 0) {
66
 
                bytes_read = read(fd, ptr, bytes_remaining);
67
 
                if (bytes_read <= 0)
68
 
                        return 0;
69
 
                bytes_remaining -= bytes_read;
70
 
                ptr += bytes_read;
71
 
        }
72
 
 
73
 
        return size;
74
 
}
75
 
 
76
 
static size_t _write_bytes(int fd, char *buf, const size_t size)
77
 
{
78
 
        size_t bytes_remaining, bytes_written;
79
 
        char *ptr;
80
 
 
81
 
        bytes_remaining = size;
82
 
        ptr = buf;
83
 
        while (bytes_remaining > 0) {
84
 
                bytes_written = write(fd, ptr, bytes_remaining);
85
 
                if (bytes_written < 0)
86
 
                        return 0;
87
 
                bytes_remaining -= bytes_written;
88
 
                ptr += bytes_written;
89
 
        }
90
 
        return size;
91
 
}
92
 
 
93
 
static size_t _send_msg(int fd, char *buf, size_t size)
94
 
{
95
 
        char header[10];
96
 
        size_t data_sent;
97
 
 
98
 
        (void) sprintf(header, "%08lu\n", (uint32_t) size);
99
 
        if (_write_bytes(fd, header, 9) != 9) {
100
 
                perror("writing message header");
101
 
                exit(1);
102
 
        }
103
 
 
104
 
        data_sent = _write_bytes(fd, buf, size);
105
 
        if (data_sent != size) {
106
 
                perror("writing message");
107
 
                exit(1);
108
 
        }
109
 
 
110
 
        return data_sent;
111
 
}
112
 
 
113
 
static char *_recv_msg(int fd)
114
 
{
115
 
        char header[10];
116
 
        uint32_t size;
117
 
        char *buf;
118
 
 
119
 
        if (_read_bytes(fd, header, 9) != 9) {
120
 
                perror("reading message header");
121
 
                exit(1);
122
 
        }
123
 
        if (sscanf(header, "%ul", &size) != 1) {
124
 
                perror("parsing message header");
125
 
                exit(1);
126
 
        }
127
 
        buf = calloc(1, (size+1));      /* need '\0' on end to print */
128
 
        if (buf == NULL) {
129
 
                perror("malloc");
130
 
                exit(1);
131
 
        }
132
 
        if (_read_bytes(fd, buf, size) != size) {
133
 
                perror("reading message");
134
 
                exit(1);
135
 
        }
136
 
        return buf;
137
 
}
138
 
 
139
 
main(int argc, char **argv)
140
 
{
141
 
        int wiki=0, sched_port=0, wiki_fd;
142
 
        char control_addr[1024];
143
 
        char *in_msg, out_msg[1024], *resp;
144
 
 
145
 
        /*
146
 
         * Get current SLURM configuration
147
 
         */
148
 
        slurm_ctl_conf_t *conf_ptr;
149
 
        slurm_load_ctl_conf((time_t) 0, &conf_ptr);
150
 
        if (strcasecmp(conf_ptr->schedtype, "sched/wiki2") == 0)
151
 
                wiki=1;
152
 
        strncpy(control_addr, conf_ptr->control_addr, sizeof(control_addr));
153
 
        sched_port = conf_ptr->schedport;
154
 
        slurm_free_ctl_conf(conf_ptr);
155
 
 
156
 
        if (wiki == 0) {
157
 
                printf("SLURM's Wiki2 plugin not configured, nothing to test\n");
158
 
                printf("SUCCESS\n");
159
 
                exit(0);
160
 
        }
161
 
#if _DEBUG
162
 
        printf("SLURM's Wiki2 configured on %s:%d\n", control_addr, sched_port);
163
 
#endif
164
 
 
165
 
        /*
166
 
         * Build a Wiki request with arbitrary encryption key
167
 
         */
168
 
        snprintf(out_msg, sizeof(out_msg),
169
 
                "CK=1234567812345678 TS=%u AUTH=root DT=CMD=GETJOBS ARG=0:ALL",
170
 
                (uint32_t) time(NULL));
171
 
 
172
 
        /*
173
 
         * Send the message and get the response from SLURM
174
 
         */
175
 
#if _DEBUG
176
 
        printf("Sending message: %s\n", out_msg);
177
 
#endif
178
 
        wiki_fd = _conn_wiki_port(control_addr, sched_port);
179
 
        _send_msg(wiki_fd, out_msg, strlen(out_msg));
180
 
        in_msg = _recv_msg(wiki_fd);
181
 
#if _DEBUG
182
 
        printf("Received message: %s\n", in_msg);
183
 
#endif
184
 
 
185
 
        /*
186
 
         * Parse the results for desired error
187
 
         */
188
 
        resp = strstr(in_msg, "RESPONSE=bad checksum");
189
 
        if (resp) {
190
 
                printf("Bad checksum reported\n");
191
 
                printf("SUCCESS\n");
192
 
                exit(0);
193
 
        } else {
194
 
                printf("Bad response: %s\n", in_msg);
195
 
                exit(1);
196
 
        }
197
 
}