summaryrefslogtreecommitdiff
path: root/php/php_remctl.c
blob: 4aafc50d39c24ba61617967d2d27295aebf63065 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include <config.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <client/remctl.h>

#ifndef IOV_MAX
# define IOV_MAX UIO_MAXIOV
#endif

#include "php.h"
#include "php_remctl.h"

static int			le_remctl_internal;

static zend_function_entry	remctl_functions[] = {
    ZEND_FE( remctl, NULL )
    ZEND_FE( remctl_new, NULL )
    ZEND_FE( remctl_open, NULL )
    ZEND_FE( remctl_close, NULL )
    ZEND_FE( remctl_command, NULL )
    ZEND_FE( remctl_output, NULL )
    ZEND_FE( remctl_error, NULL )
    { NULL, NULL, NULL }
};

zend_module_entry		remctl_module_entry = {
    STANDARD_MODULE_HEADER,
    PHP_REMCTL_EXTNAME,
    remctl_functions,
    PHP_MINIT( remctl ),
    NULL,
    NULL,
    NULL,
    NULL,
    PHP_REMCTL_VERSION,
    STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_REMCTL
ZEND_GET_MODULE( remctl )
#endif /* COMPILE_DL_REMCTL */

    static void
php_remctl_dtor( zend_rsrc_list_entry *rsrc TSRMLS_DC )
{
    struct remctl		*r = ( struct remctl * )rsrc->ptr;

    if ( r ) {
	remctl_close( r );
    }
}

PHP_MINIT_FUNCTION( remctl )
{
    le_remctl_internal = zend_register_list_destructors_ex( php_remctl_dtor,
				NULL, PHP_REMCTL_RES_NAME, module_number );

    return( SUCCESS );
}

ZEND_FUNCTION( remctl )
{
    struct remctl_result	*result = NULL;

    zval			*cmd_array, **data;
    zval			*remres_obj;
    HashTable			*hash;
    HashPosition		pos;

    char			*host;
    char			*principal;
    const char			**command = NULL;
    unsigned short		port;
    int				hlen, plen, count, i;
    int				success = 0;

    if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC,
				"slsa", &host, &hlen, &port,
				&principal, &plen, &cmd_array ) == FAILURE ) {
	zend_error( E_WARNING, "%s: invalid parameters", __FUNCTION__ );
	RETURN_NULL();
    }

    if (( hlen == 0 ) || ( plen == 0 )) {
	zend_error( E_WARNING, "Host and principal must be valid strings\n" );
	RETURN_NULL();
    }

    hash = Z_ARRVAL_P( cmd_array );
    if (( count = zend_hash_num_elements( hash )) < 2 ) {
	zend_error( E_WARNING, "%s: array must contain at least two elements\n",
				__FUNCTION__ );
	RETURN_NULL();
    }

    if (( command = ( const char ** )emalloc(( count + 1 ) *
					sizeof( char * ))) == NULL ) {
	zend_error( E_WARNING, "%s: emalloc failed\n", __FUNCTION__ );
	RETURN_NULL();
    }

    for ( i = 0, zend_hash_internal_pointer_reset_ex( hash, &pos );
		zend_hash_get_current_data_ex( hash, ( void ** )&data,
						   &pos ) == SUCCESS;
		i++, zend_hash_move_forward_ex( hash, &pos )) {
	if ( Z_TYPE_PP( data ) != IS_STRING ) {
	    zend_error( E_WARNING, "%s: array contains non-string value\n",
				__FUNCTION__ );
	    goto remctl_cleanup;
	}

	if ( i >= count ) {
	    zend_error( E_WARNING, "%s: exceeded number of elements!\n",
				__FUNCTION__ );
	    goto remctl_cleanup;
	}

	command[ i ] = estrndup( Z_STRVAL_PP( data ), Z_STRLEN_PP( data ));
	if ( command[ i ] == NULL ) {
	    zend_error( E_WARNING, "%s: estrndup failed\n", __FUNCTION__ );
	    count = i;
	    goto remctl_cleanup;
	}
    }
    /* must NULL-terminate command argv for remctl */
    command[ count ] = NULL;

    if (( result = remctl( host, port, principal, command )) == NULL ) {
	zend_error( E_WARNING, "%s: remctl failed\n", __FUNCTION__ );
	goto remctl_cleanup;
    }

    /*
     * populate an object with remctl results.
     * return_value is defined for us by zend.
     */
    if ( object_init( return_value ) != SUCCESS ) {
	zend_error( E_WARNING, "%s: object_init failed\n", __FUNCTION__ );
	goto remctl_cleanup;
    }
    if ( result->error != NULL ) {
	add_property_string( return_value, "error", result->error, 1 );
    } else {
	add_property_string( return_value, "error", "", 1 );
    }
    add_property_stringl( return_value, "stdout",
			  result->stdout_buf, result->stdout_len, 1 );
    add_property_long( return_value, "stdout_len", result->stdout_len );
    add_property_stringl( return_value, "stderr",
			  result->stderr_buf, result->stderr_len, 1 );
    add_property_long( return_value, "stderr_len", result->stderr_len );
    add_property_long( return_value, "status", result->status );

    success = 1;

remctl_cleanup:
    if ( command != NULL ) {
	for ( i = 0; i < count; i++ ) {
	    efree(( char * )command[ i ] );
	}
	efree( command );
    }
    if ( result != NULL ) {
	remctl_result_free( result );
    }

    if ( !success ) {
	RETURN_NULL();
    }

    /*
     * these functions actually return void. return_value contains
     * the interesting bits. the RETURN_X macros simply change
     * what return_value contains.
     */
}

ZEND_FUNCTION( remctl_new )
{
    struct remctl		*r;

    if (( r = remctl_new()) == NULL ) {
	zend_error( E_WARNING, "%s: remctl_new failed", __FUNCTION__ );
	RETURN_NULL();
    }

    ZEND_REGISTER_RESOURCE( return_value, r, le_remctl_internal );
}

ZEND_FUNCTION( remctl_open )
{
    struct remctl		*r;
    zval			*zrem;
    char			*host, *princ;
    unsigned short		port;
    int				hlen, prlen;

    if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC,
				"rsls", &zrem, &host, &hlen,
				&port, &princ, &prlen ) == FAILURE ) {
	zend_error( E_WARNING, "%s: invalid parameters", __FUNCTION__ );
	RETURN_FALSE;
    }

    ZEND_FETCH_RESOURCE( r, struct remctl *, &zrem, -1,
			 PHP_REMCTL_RES_NAME, le_remctl_internal );

    if ( !remctl_open( r, host, port, princ )) {
	RETURN_FALSE;
    }

    RETURN_TRUE;
}

ZEND_FUNCTION( remctl_command )
{
    struct remctl		*r;
    zval			*zrem, *cmd_array, **data;

    HashTable			*hash;
    HashPosition		pos;

    struct iovec		*cmd_vec = NULL;
    int				i, count;
    int				success = 0;

    if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC,
				"ra", &zrem, &cmd_array ) == FAILURE ) {
	zend_error( E_WARNING, "%s: invalid parameters", __FUNCTION__ );
	RETURN_FALSE;
    }

    ZEND_FETCH_RESOURCE( r, struct remctl *, &zrem, -1,
			 PHP_REMCTL_RES_NAME, le_remctl_internal );

    hash = Z_ARRVAL_P( cmd_array );
    if (( count = zend_hash_num_elements( hash )) < 2 ) {
	zend_error( E_WARNING, "%s: array must contain at least two elements\n",
				__FUNCTION__ );
	RETURN_FALSE;
    }
    if ( count > IOV_MAX ) {
	zend_error( E_WARNING, "%s: array count exceeds IOV_MAX!",
				__FUNCTION__ );
	RETURN_FALSE;
    }

    if (( cmd_vec = ( struct iovec * )emalloc( count *
					sizeof( struct iovec ))) == NULL ) {
	zend_error( E_WARNING, "%s: emalloc failed\n", __FUNCTION__ );
	RETURN_FALSE;
    }

    for ( i = 0, zend_hash_internal_pointer_reset_ex( hash, &pos );
		zend_hash_get_current_data_ex( hash, ( void ** )&data,
						   &pos ) == SUCCESS;
		i++, zend_hash_move_forward_ex( hash, &pos )) {
	if ( Z_TYPE_PP( data ) != IS_STRING ) {
	    zend_error( E_WARNING, "%s: array contains non-string value\n",
				__FUNCTION__ );
	    goto remctl_command_cleanup;
	}

	if ( i >= count ) {
	    zend_error( E_WARNING, "%s: exceeded number of elements!\n",
				__FUNCTION__ );
	    goto remctl_command_cleanup;
	}

	cmd_vec[ i ].iov_base = estrndup( Z_STRVAL_PP( data ),
					  Z_STRLEN_PP( data ));
	if ( cmd_vec[ i ].iov_base == NULL ) {
	    zend_error( E_WARNING, "%s: estrndup failed\n", __FUNCTION__ );
	    count = i;
	    goto remctl_command_cleanup;
	}
	cmd_vec[ i ].iov_len = Z_STRLEN_PP( data );
    }

    if ( !remctl_commandv( r, cmd_vec, count )) {
	/* return false, let script get error from remctl_error */
	goto remctl_command_cleanup;
    }

    success = 1;

remctl_command_cleanup:
    if ( cmd_vec != NULL ) {
	for ( i = 0; i < count; i++ ) {
	    efree(( char * )cmd_vec[ i ].iov_base );
	}
	efree( cmd_vec );
    }

    if ( !success ) {
	RETURN_FALSE;
    }

    RETURN_TRUE;
}

ZEND_FUNCTION( remctl_output )
{
    struct remctl		*r;
    struct remctl_output	*output;
    zval			*zrem;

    int				success = 0;

    if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC,
				"r", &zrem ) == FAILURE ) {
	zend_error( E_WARNING, "%s: invalid parameters", __FUNCTION__ );
	RETURN_NULL();
    }

    ZEND_FETCH_RESOURCE( r, struct remctl *, &zrem, -1,
			 PHP_REMCTL_RES_NAME, le_remctl_internal );

    /*
     * populate an object with remctl results.
     * return_value is defined for us by zend.
     */
    if ( object_init( return_value ) != SUCCESS ) {
	zend_error( E_WARNING, "%s: object_init failed\n", __FUNCTION__ );
	RETURN_NULL();
    }
    
    if (( output = remctl_output( r )) == NULL ) {
	zend_error( E_WARNING, "%s: error reading from server: %s",
				remctl_error( r ));
	RETURN_NULL();
    }

    add_property_bool( return_value, "done", 0 );
    add_property_long( return_value, "stdout_len", 0 );
    add_property_long( return_value, "stderr_len", 0 );

    switch( output->type ) {
    case REMCTL_OUT_OUTPUT:
	add_property_stringl( return_value, "type",
				"output", strlen( "output" ), 1 );
	if ( output->stream == 1 ) {
	    add_property_stringl( return_value, "stdout",
				output->data, output->length, 1 );
	    add_property_long( return_value, "stdout_len", output->length );
	} else if ( output->stream == 2 ) {
	    add_property_stringl( return_value, "stderr",
				output->data, output->length, 1 );
	    add_property_long( return_value, "stderr_len", output->length );
	} else {
	    zend_error( E_WARNING, "%s: unknown output stream %d",
				output->stream );
	    add_property_stringl( return_value, "stderr",
				output->data, output->length, 1 );
	    add_property_long( return_value, "stderr_len", output->length );
	}
	break;

    case REMCTL_OUT_ERROR:
	add_property_stringl( return_value, "type",
				"error", strlen( "error" ), 1 );
	add_property_stringl( return_value, "error",
				output->data, output->length, 1 );
	break;

    case REMCTL_OUT_STATUS:
	add_property_stringl( return_value, "type",
				"status", strlen( "status" ), 1 );
	add_property_long( return_value, "status", output->status );
	break;

    case REMCTL_OUT_DONE:
	add_property_stringl( return_value, "type",
				"done", strlen( "done" ), 1 );
	add_property_bool( return_value, "done", 1 );
	break;
    }

    /* return_value contains something like a remctl_output object */
}

ZEND_FUNCTION( remctl_error )
{
    struct remctl		*r;
    zval			*zrem;

    const char			*error;
    int				success = 0;

    if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC,
				"r", &zrem ) == FAILURE ) {
	zend_error( E_WARNING, "%s: invalid parameters", __FUNCTION__ );
	RETURN_NULL();;
    }

    ZEND_FETCH_RESOURCE( r, struct remctl *, &zrem, -1,
			 PHP_REMCTL_RES_NAME, le_remctl_internal );

    error = remctl_error( r );

    RETURN_STRING(( char * )error, 1 );
}

ZEND_FUNCTION( remctl_close )
{
    struct remctl		*r;
    zval			*zrem;

    if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC,
				"r", &zrem ) == FAILURE ) {
	zend_error( E_WARNING, "%s: invalid parameters", __FUNCTION__ );
	RETURN_FALSE;
    }

    ZEND_FETCH_RESOURCE( r, struct remctl *, &zrem, -1,
			 PHP_REMCTL_RES_NAME, le_remctl_internal );

    /* this delete invokes php_remctl_dtor, which calls remctl_close */
    zend_list_delete( Z_LVAL_P( zrem ));

    RETURN_TRUE;
}