int main(int argc, char *argv[]){ struct sockaddr_in sin; char buf[BUF_SIZE],output_buf[BUF_SIZE]; unsigned short port; int s,len=sizeof sin; // Parse command line arguments if(argc!=2){ fprintf(stderr,"%s:Improper usage!\nUSAGE:%s port\n",argv[0],argv[0]);return-1; } port=atoi(argv[1]); //Set up struct sockaddr_in sin.sin_family=AF_INET; sin.sin_port=htons(port); sin.sin_addr.s_addr=INADDR_ANY; //Create local socket and bind if((s=socket(AF_INET,SOCK_DGRAM,0))<0){ fprintf(stderr,"%s:Can't open socket:%s\n",argv[0],strerror(errno));return -1; } if(bind(s,(struct sockaddr *)&sin,sizeof sin)<0){ fprintf(stderr,"%s:Error binding socket:%s\n",argv[0],strerror(errno)); close(s); return -1; } while(1){// Listen for requests and return information to the requester if(recvfrom(s,buf,BUF_SIZE,0,(struct sockaddr *)&sin,&len)==-1){ fprintf(stderr,"%s:Error receiving request:%s\n",argv[0],strerror(errno));continue; } fprintf(stderr,"Received req from %s:%d...\n",inet_ntoa(sin.sin_addr),ntohs(sin.sin_port)); if(!strcmp(buf,"UPTIME")){ if(get_command_output("uptime",output_buf)==-1){continue;} if(sendto(s,output_buf,strlen(output_buf)+1,0,(struct sockaddr *)&sin,len)==-1){ fprintf(stderr,"%s:Error sending response:%s\n",argv[0],strerror(errno));continue; } } else if(!strcmp(buf,"DATE")){ if(get_command_output("date",output_buf)==-1){continue;} if(sendto(s,output_buf,strlen(output_buf)+1,0,(struct sockaddr *)&sin,len)==-1){ fprintf(stderr,"%s:Error sending response:%s\n",argv[0],strerror(errno));continue; } } else{fprintf(stderr,"%s:Unrecognized message!\n",argv[0]);continue;} fprintf(stderr,"Sent resp to %s:%d...\n\n",inet_ntoa(sin.sin_addr),ntohs(sin.sin_port)); } return 0; }
void test_sha_1(void){ char plaintext[]= "Hello world"; // SHA1 gives a 20 byte hash unsigned char hash[SHA1_HASH_SIZE]; memset(hash, 0, sizeof(hash)); encrypt_digest(plaintext, strlen(plaintext), hash, 0, EVP_sha1()); char buffer[256]; memset(buffer, 0, sizeof(buffer)); sprintf(buffer, "(stdin)= "); int i; for(i = 0; i < SHA1_HASH_SIZE; i++){ sprintf(buffer+9+2*i, "%02x", hash[i]); } sprintf(buffer+9+2*i, "\n"); char *linux_call = get_command_output("echo -n \"Hello world\" | openssl sha1 2>&1", 256); if(strcmp(linux_call, buffer) == 0){ printf(" SHA1 PASSED\n"); } }
gboolean jam_doc_insert_command_output(JamDoc *doc, const char *command, const char *encoding, GError **err, GtkWindow *parent) { /* FIXME: encoding is not currently used, instead we recode from * the current locale. Does anybody knows a program which output * is NOT in the current locale's encoding? If yes, let me know. */ GString * output; const gchar *end; output = get_command_output(command, err, parent); if (output == NULL) return FALSE; if (g_utf8_validate(output->str, output->len, &end)) { gtk_text_buffer_insert_at_cursor(doc->buffer, output->str, output->len); } else { gchar* newtext; newtext = g_locale_to_utf8(output->str, output->len, NULL, NULL, err); if (!newtext) { g_string_free(output, TRUE); return FALSE; } gtk_text_buffer_insert_at_cursor(doc->buffer, newtext, -1); g_free(newtext); } g_string_free(output, TRUE); return TRUE; }
int generate_alps_status( std::vector<std::string> &status, const char *apbasil_path, const char *apbasil_protocol) { int rc; char inventory_command[MAXLINE * 2]; std::string alps_output; status.clear(); alps_nodes.clear(); snprintf(inventory_command, sizeof(inventory_command), APBASIL_QUERY, (apbasil_protocol != NULL) ? apbasil_protocol : DEFAULT_APBASIL_PROTOCOL, (apbasil_path != NULL) ? apbasil_path : DEFAULT_APBASIL_PATH); rc = get_command_output(inventory_command, alps_output); if (rc == PBSE_NONE) { rc = parse_alps_output(alps_output); if (rc == PBSE_NONE) { if (LOGLEVEL >= 7) { /* log the command if output successfully parsed */ snprintf(log_buffer, sizeof(log_buffer), "Successful inventory command is: %s", inventory_command); log_event(PBSEVENT_JOB | PBSEVENT_SYSLOG, PBS_EVENTCLASS_JOB, __func__, log_buffer); } if (!strcmp(apbasil_protocol, "1.7")) rc = get_knl_information(apbasil_path); if (rc == PBSE_NONE) update_status(status); } } return(rc); } /* END generate_alps_status() */
int get_knl_information( const char *apbasil_path) { int rc; std::string knl_output; char system_command[MAXLINE * 2]; snprintf(system_command, sizeof(system_command), APBASIL_SYSTEM, "1.7", (apbasil_path != NULL) ? apbasil_path : DEFAULT_APBASIL_PATH); rc = get_command_output(system_command, knl_output); if (rc == PBSE_NONE) rc = parse_alps_output(knl_output); return(rc); } // END get_knl_information()
void test_md5(void){ char plaintext[]= "Hello world"; // MD5 produces a 16 byte hash value (one way) unsigned char hash[MD5_HASH_SIZE]; memset(hash, 0, sizeof(hash)); encrypt_digest(plaintext, strlen(plaintext), hash, 0, EVP_md5()); // Below output should be the same as echo -n "Hello world" | openssl md5 char buffer[256]; memset(buffer, 0, sizeof(buffer)); sprintf(buffer, "(stdin)= "); int i; for(i = 0; i < MD5_HASH_SIZE; i++){ sprintf(buffer+9+2*i, "%02x", hash[i]); } sprintf(buffer+9+2*i, "\n"); char *linux_call = get_command_output("echo -n \"Hello world\" | openssl md5 2>&1", 256); if(strcmp(linux_call, buffer) == 0){ printf(" MD5 PASSED\n"); } }
static void prefs_custom_header_val_from_file_cb(void) { gchar *filename = NULL; gchar *contents = NULL; const gchar *hdr = gtk_entry_get_text(GTK_ENTRY(customhdr.hdr_entry)); if (!strcmp(hdr, "Face")) filename = filesel_select_file_open(_("Choose a PNG file"), NULL); else if (!strcmp(hdr, "X-Face")) filename = filesel_select_file_open(_("Choose an XBM file"), NULL); else filename = filesel_select_file_open(_("Choose a text file"), NULL); if (!strcmp(hdr, "Face") || !strcmp(hdr, "X-Face")) { if (filename && is_file_exist(filename)) { FILE *fp = NULL; gint len; gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE]; gchar *tmp = NULL; gint w, h; GdkPixbufFormat *format = gdk_pixbuf_get_file_info( filename, &w, &h); if (format == NULL) { alertpanel_error(_("This file isn't an image.")); g_free(filename); return; } if (w != 48 || h != 48) { alertpanel_error(_("The chosen image isn't the correct size (48x48).")); g_free(filename); return; } if (!strcmp(hdr, "Face")) { if (get_file_size(filename) > 725) { alertpanel_error(_("The image is too big; it must be maximum 725 bytes.")); g_free(filename); return; } if (g_ascii_strcasecmp("png", gdk_pixbuf_format_get_name(format))) { alertpanel_error(_("The image isn't in the correct format (PNG).")); g_print("%s\n", gdk_pixbuf_format_get_name(format)); g_free(filename); return; } } else if (!strcmp(hdr, "X-Face")) { gchar *tmp = NULL, *cmd = NULL; int i = 0; if (g_ascii_strcasecmp("xbm", gdk_pixbuf_format_get_name(format))) { alertpanel_error(_("The image isn't in the correct format (XBM).")); g_print("%s\n", gdk_pixbuf_format_get_name(format)); g_free(filename); return; } cmd = g_strdup_printf("compface %s", filename); tmp = get_command_output(cmd); g_free(cmd); if (tmp == NULL || *tmp == '\0') { alertpanel_error(_("Couldn't call `compface`. Make sure it's in your $PATH.")); g_free(filename); g_free(tmp); return; } if (strstr(tmp, "compface:")) { alertpanel_error(_("Compface error: %s"), tmp); g_free(filename); g_free(tmp); return; } while (tmp[i]) { gchar *tmp2 = NULL; if (tmp[i] == ' ') { i++; continue; } if (tmp[i] == '\r' || tmp[i] == '\n') { i++; continue; } tmp2 = contents; contents = g_strdup_printf("%s%c",tmp2?tmp2:"", tmp[i]); g_free(tmp2); i++; } g_free(tmp); goto settext; } fp = g_fopen(filename, "rb"); if (!fp) { g_free(filename); return; } while ((len = fread(inbuf, sizeof(gchar), B64_LINE_SIZE, fp)) == B64_LINE_SIZE) { base64_encode(outbuf, inbuf, B64_LINE_SIZE); tmp = contents; contents = g_strconcat(tmp?tmp:"",outbuf, NULL); g_free(tmp); } if (len > 0 && feof(fp)) { tmp = contents; base64_encode(outbuf, inbuf, len); contents = g_strconcat(tmp?tmp:"",outbuf, NULL); g_free(tmp); } fclose(fp); } } else { if (!filename) return; contents = file_read_to_str(filename); if (strchr(contents, '\n') || strchr(contents,'\r')) { alertpanel_error(_("This file contains newlines.")); g_free(contents); g_free(filename); return; } } settext: if (contents && strlen(contents)) gtk_entry_set_text(GTK_ENTRY(customhdr.val_entry), contents); g_free(contents); g_free(filename); }