void shcert_hex_print(shbuf_t *buff, unsigned char *data, size_t data_len, char *line_prefix) { char text[256]; unsigned int val; int i; if (!data_len) return; for (i = 0; i < data_len; i++) { if (i != 0) { shbuf_catstr(buff, ":"); if (0 == (i % 22)) shbuf_catstr(buff, "\n"); } if (0 == (i % 22)) shbuf_catstr(buff, line_prefix); val = (unsigned int)data[i]; if (val || i < data_len - 3) { sprintf(text, "%-2.2x", val); shbuf_catstr(buff, text); } } shbuf_catstr(buff, "\n"); }
void shlog_write(shbuf_t *buff, int level, int err_code, char *log_str) { static char log_path[PATH_MAX+1]; char line[640]; char *beg_line; size_t mem_size; if (!buff) return; if (!*log_path) { char *label; shpeer_t peer; memcpy(&peer, ashpeer(), sizeof(peer)); label = (!*peer.label ? PACKAGE_NAME : peer.label); sprintf(log_path, "%s%s.log", shlog_path(label), label); } if (*log_path && !_shlog_file) { _shlog_file = fopen(log_path, "ab"); } beg_line = shbuf_data(buff) + shbuf_size(buff); sprintf(line, "%s", shstrtime(shtime(), "[%x %T] ")); shbuf_catstr(buff, line); if (level == SHLOG_ERROR) { shbuf_catstr(buff, "error"); } else if (level == SHLOG_WARNING) { shbuf_catstr(buff, "warning"); } else { shbuf_catstr(buff, "info"); } if (err_code) { memset(line, 0, sizeof(line)); snprintf(line, sizeof(line) - 1, ": %s [code %d]", strerror(-(err_code)), (err_code)); shbuf_catstr(buff, line); } if (log_str) { shbuf_catstr(buff, ": "); shbuf_catstr(buff, log_str); } mem_size = shlog_mem_size(); if (mem_size > 100000) { sprintf(line, " (mem:%dm)", (mem_size / 1000)); shbuf_catstr(buff, line); } shbuf_catstr(buff, "\n"); }
int shfs_file_notify(shfs_ino_t *file) { shfs_t *tree = file->tree; shbuf_t *buff; uint32_t mode; int qid; int err; if (!file || !tree) return (0); /* done */ qid = _shfs_file_qid(); if (qid == -1) return (SHERR_IO); buff = shbuf_init(); mode = TX_FILE; shbuf_cat(buff, &mode, sizeof(mode)); shbuf_cat(buff, &tree->peer, sizeof(shpeer_t)); shbuf_catstr(buff, shfs_inode_path(file)); err = shmsg_write(qid, buff, NULL); shbuf_free(&buff); if (err) return (err); return (0); }
int oauth_response_favicon(shbuf_t *buff) { char text[1024]; sprintf(text, "HTTP/1.1 200 OK\r\n" "Accept-Ranges: bytes\r\n" "Content-Length: %u\r\n" "Content-Type: image/vnd.microsoft.icon\r\n" "\r\n", (unsigned int)FAVICON_ICO_SIZE); shbuf_catstr(buff, text); shbuf_cat(buff, FAVICON_ICO, FAVICON_ICO_SIZE); }
void oauth_admin_redir_login(shd_t *cli, char *client_id) { char *enc_cid = http_token_encode(client_id); char *enc_uri = http_token_encode("/admin"); char buf[1024]; sprintf(buf, "HTTP/1.1 302 Found\r\n" "Location: /auth?response_type=token&client_id=%s&redirect_uri=%s\r\n" "Content-Length: 0\r\n" "\r\n", enc_cid, enc_uri); shbuf_catstr(cli->buff_out, buf); free(enc_cid); free(enc_uri); }
int update_sexe_userdata(sexe_t *S) { SHFL *fl; shjson_t *udata; shfs_t *fs; shbuf_t *buff; shkey_t *k; char path[PATH_MAX+1]; char *str; int err; k = &S->pname; if (shkey_cmp(k, ashkey_blank())) { fprintf(stderr, "DEBUG: update_sexe_userdata: no app key\n"); return (0); /* blank */ } sprintf(path, "/sys/data/sexe/%s", shkey_hex(k)); lua_getglobal(S, "userdata"); udata = sexe_table_get(S); if (!udata) { fprintf(stderr, "DEBUG: update_sexe_userdata: no global 'userdata' variable.\n"); return (SHERR_INVAL); } str = shjson_print(udata); if (!str) { fprintf(stderr, "DEBUG: update_sexe_userdata: error encoding JSON.\n"); return (SHERR_INVAL); } shjson_free(&udata); buff = shbuf_init(); shbuf_catstr(buff, str); free(str); fs = shfs_init(NULL); fl = shfs_file_find(fs, path); err = shfs_write(fl, buff); shbuf_free(&buff); shfs_free(&fs); return (err); }
void shcert_print(shcert_t *cert, shbuf_t *pr_buff) { char tbuf1[256]; char tbuf2[256]; char buf[4096]; if (!cert || !pr_buff) return; shbuf_catstr(pr_buff, "Certificate:\n"); shbuf_catstr(pr_buff, " Data:\n"); sprintf(buf, " Version: %d\n", cert->cert_ver); shbuf_catstr(pr_buff, buf); shbuf_catstr(pr_buff, " Serial Number: "); shcert_hex_print(pr_buff, shcert_sub_ser(cert), sizeof(shcert_sub_ser(cert)), ""); sprintf(buf, " Signature Algorithm: %s\n", shsig_alg_str(shcert_iss_alg(cert) | shcert_sub_alg(cert))); shbuf_catstr(pr_buff, buf); sprintf(buf, " Issuer: %s\n", cert->cert_iss.ent_name); shbuf_catstr(pr_buff, buf); strcpy(tbuf1, shctime(shcert_sub_stamp(cert))+4); strcpy(tbuf2, shctime(shcert_sub_expire(cert))+4); sprintf(buf, " Validity: %-20.20s - %-20.20s\n", tbuf1, tbuf2); shbuf_catstr(pr_buff, buf); sprintf(buf, " Subject: %s\n", cert->cert_sub.ent_name); shbuf_catstr(pr_buff, buf); sprintf(buf, " Public Key Algorithm: (%d bit) %s\n", shcert_sub_len(cert) * 8, shsig_alg_str(shcert_sub_alg(cert))); shbuf_catstr(pr_buff, buf); sprintf(buf, " Checksum: %llu\n", shkey_crc(shcert_sub_sig(cert))); sprintf(buf, " 192-Bit: %s\n", shkey_hex(shcert_sub_sig(cert))); shbuf_catstr(pr_buff, buf); if (shcert_sub_alg(cert) & SHKEY_ALG_RSA) { shbuf_catstr(pr_buff, " Modulus:\n"); shcert_hex_print_reverse(pr_buff, cert->cert_sub.ent_sig.key.rsa.mod, cert->cert_sub.ent_sig.key.rsa.mod_len, " "); } shbuf_catstr(pr_buff, " X509v3 extensions:\n"); sprintf(buf, " Basic Constraints: CA=%s\n", (cert->cert_flag & SHCERT_CERT_CHAIN) ? "false" : "true"); shbuf_catstr(pr_buff, buf); if (!shpeer_localhost(&cert->cert_sub.ent_peer)) { sprintf(buf, " Alternate Subject: %s\n", shpeer_print(&cert->cert_sub.ent_peer)); shbuf_catstr(pr_buff, buf); } sprintf(buf, " Extended Usage: %s\n", shcert_flag_str(cert->cert_flag)); shbuf_catstr(pr_buff, buf); sprintf(buf, " Private Signature: %s (%d bytes)\n", shsig_alg_str(shcert_iss_alg(cert)), shcert_iss_len(cert)); shbuf_catstr(pr_buff, buf); if (shcert_iss_alg(cert) & SHKEY_ALG_MD5) { shcert_hex_print(pr_buff, cert->cert_iss.ent_sig.key.md.md, cert->cert_iss.ent_sig.key.md.md_len, " "); } else if (shcert_iss_alg(cert) & SHKEY_ALG_SHA1) { shcert_hex_print(pr_buff, cert->cert_iss.ent_sig.key.sha.sha, cert->cert_iss.ent_sig.key.sha.sha_len, " "); } else if (shcert_iss_alg(cert) & SHKEY_ALG_SHA256) { shcert_hex_print(pr_buff, cert->cert_iss.ent_sig.key.sha.sha, cert->cert_iss.ent_sig.key.sha.sha_len, " "); } else { sprintf(buf, " Checksum: %llu\n", shkey_crc(shcert_iss_sig(cert))); shbuf_catstr(pr_buff, buf); sprintf(buf, " 192-Bit: %s\n", shkey_hex(shcert_iss_sig(cert))); shbuf_catstr(pr_buff, buf); } }