void respond(int fd, int code, int protocol, const char *res, int reslen, const char *headers) { char buffer[BUFFER_SIZE], buffer2[BUFFER_SIZE]; int len; respond2(fd, code, protocol); if (code != 100) { if (reslen > 0) { len = snprintf(buffer, BUFFER_SIZE, "Content-Length: %d\r\n", reslen); writeall(fd, buffer, len); } else if (code != 100 && code != 200) { len = snprintf(buffer, BUFFER_SIZE, "Content-Type: text/plain\r\n"); writeall(fd, buffer, len); reslen = responseline(code, protocol, buffer2, BUFFER_SIZE); res = buffer2 + 9; reslen -= 9; } if (headers) writeall(fd, headers, strlen(headers)); } len = snprintf(buffer, BUFFER_SIZE, "\r\n"); writeall(fd, buffer, len); if (res) writeall(fd, res, reslen); }
void respond2(int fd, int code, int protocol) { char buffer[BUFFER_SIZE]; int len; time_t now; len = responseline(code, protocol, buffer, BUFFER_SIZE); writeall(fd, buffer, len); if (code != 100) { if (protocol == HTTP_1_1) { len = snprintf(buffer, BUFFER_SIZE, "Connection: close\r\n"); writeall(fd, buffer, len); } now = time(NULL); len = strftime(buffer, BUFFER_SIZE, "Date: %a, %d %b %Y %H:%M:%S GMT\r\n", gmtime(&now)); writeall(fd, buffer, len); len = snprintf(buffer, BUFFER_SIZE, "Server: %s\r\n", SERVER_NAME); writeall(fd, buffer, len); } }
static void read_server_setup_reply_cb(EV_P_ ev_io *w, int revents) { struct connstate *connstate = (struct connstate *)w->data; xcb_setup_failed_t setup_failed; must_read(readall_into(&setup_failed, sizeof(setup_failed), w->fd)); switch (setup_failed.status) { case 0: errx(EXIT_FAILURE, "error authenticating at the X11 server"); case 2: errx(EXIT_FAILURE, "two-factor auth not implemented"); case 1: must_write(writeall(connstate->clientw->fd, &setup_failed, sizeof(xcb_setup_failed_t))); const size_t len = (setup_failed.length * 4); void *buf = smalloc(len); must_read(readall_into(buf, len, w->fd)); must_write(writeall(connstate->clientw->fd, buf, len)); free(buf); ev_set_cb(connstate->clientw, read_client_x11_packet_cb); ev_set_cb(connstate->serverw, read_server_x11_packet_cb); ev_io_start(EV_A_ connstate->clientw); break; default: errx(EXIT_FAILURE, "X11 protocol error: expected setup_failed.status in [0..2], got %d", setup_failed.status); } }
/* * write an reop encrypted message header, followed by base64 data */ static void writeencfile(const char *filename, const void *hdr, size_t hdrlen, const char *ident, uint8_t *msg, unsigned long long msglen) { char header[1024]; char b64[1024]; char *b64data; size_t b64len; int fd; b64len = (msglen + 2) / 3 * 4 + 1; b64data = xmalloc(b64len); if (b64_ntop(msg, msglen, b64data, b64len) == -1) errx(1, "b64 encode failed"); fd = xopen(filename, O_CREAT|O_TRUNC|O_NOFOLLOW|O_WRONLY, 0666); snprintf(header, sizeof(header), "-----BEGIN REOP ENCRYPTED MESSAGE-----\n"); writeall(fd, header, strlen(header), filename); snprintf(header, sizeof(header), "ident:%s\n", ident); writeall(fd, header, strlen(header), filename); if (b64_ntop(hdr, hdrlen, b64, sizeof(b64)) == -1) errx(1, "b64 encode failed"); writeb64data(fd, filename, b64); explicit_bzero(b64, sizeof(b64)); snprintf(header, sizeof(header), "-----BEGIN REOP ENCRYPTED MESSAGE DATA-----\n"); writeall(fd, header, strlen(header), filename); writeb64data(fd, filename, b64data); xfree(b64data, b64len); snprintf(header, sizeof(header), "-----END REOP ENCRYPTED MESSAGE-----\n"); writeall(fd, header, strlen(header), filename); close(fd); }
int main(int argc, char **argv) { unsigned char *x; unsigned long long xlen; if (argv[0]) if (argv[1]) { if (str_equal(argv[1], "-h")) die_usage(0); } /* get password */ x = (unsigned char *)env_get("PASSWORD"); if (!x) { errno = 0; die_usage("$PASSWORD not set"); } xlen = str_len((char *)x); /* create salt */ randombytes(s, sizeof s); /* derive key */ if (sha512hmacpbkdf2(h, sizeof h, x, xlen, s, sizeof s, ROUNDS) == -1) die_fatal("unable to derive keys", 0); byte_zero(x, xlen); /* create nonce */ randombytes(n, sizeof n); uint64_pack(n, nanoseconds()); sha512(nk, (unsigned char *)MAGIC, MAGICBYTES); crypto_block_aes256vulnerable(n, n, nk); /* initialize */ crypto_init(&ctx, n, h, MAGIC); randombytes(h, sizeof h); sha512_init(&shactx); /* write header */ if (writeall(1, MAGIC, MAGICBYTES) == -1) die_fatal("unable to write output", 0); if (writeall(1, s, sizeof s) == -1) die_fatal("unable to write output", 0); randombytes(s, sizeof s); if (writeall(1, n, sizeof n) == -1) die_fatal("unable to write output", 0); for (;;) { inlen = readblock(in, BLOCK); if (inlen != BLOCK) break; if (sha512_block(&shactx, in, inlen) != 0) die_fatal("unable to compute hash", 0); if (crypto_block(&ctx, in, inlen) != 0) die_fatal("unable to encrypt stream", 0); if (writeall(1, in, inlen) == -1) die_fatal("unable to write output", 0); } if (sha512_last(&shactx, h, in, inlen) != 0) die_fatal("unable to compute hash", 0); byte_copy(in + inlen, CHECKSUMBYTES, h); inlen += CHECKSUMBYTES; if (crypto_last(&ctx, in, inlen) != 0) die_fatal("unable to encrypt stream", 0); if (writeall(1, in, inlen) == -1) die_fatal("unable to write output", 0); if (fsyncfd(1) == -1) die_fatal("unable to write output", 0); cleanup(); _exit(0); }
int main(int argc, char *argv[]) { if (argc == 1) { writeall(0); } for (int i = 1; i < argc; ++i) { int fd = open(argv[i], O_RDONLY); writeall(fd); close(fd); } return 0; }
static int put(struct store *st, const void *buf, size_t len, struct addr *at) { struct fstore *fst; struct addr pa; idx_t i, pi; struct idxent ie; loff_t leoff; int c; struct logent le; if(len > STORE_MAXBLSZ) { errno = E2BIG; return(-1); } fst = st->pdata; hash(buf, len, &pa); if(at != NULL) memcpy(at->hash, pa.hash, 32); if(lookup(fst, &pa, &pi) != -1) return(0); memcpy(le.magic, LOGENTMAGIC, 4); le.name = pa; le.len = len; le.fl = 0; /* XXX: Thread safety { */ leoff = fst->logsize; fst->logsize += sizeof(le) + len; /* } */ /* XXX: Handle data with embedded LOGENTMAGIC */ writeall(fst->logfd, &le, sizeof(le), leoff); writeall(fst->logfd, buf, len, leoff + sizeof(le)); i = newindex(fst); assert(!getidx(fst, i, &ie)); ie.addr = pa; ie.off = leoff; assert(!putidx(fst, i, &ie)); if(pi != -1) { assert(!getidx(fst, pi, &ie)); c = addrcmp(&pa, &ie.addr); if(c < 0) ie.l = i; else ie.r = i; assert(!putidx(fst, pi, &ie)); } return(0); }
// https://www.x.org/releases/current/doc/xproto/x11protocol.html#Encoding::Connection_Setup static void read_client_setup_request_cb(EV_P_ ev_io *w, int revents) { ev_io_stop(EV_A_ w); struct connstate *connstate = (struct connstate *)w->data; /* Read X11 setup request in its entirety. */ xcb_setup_request_t setup_request; must_read(readall_into(&setup_request, sizeof(setup_request), w->fd)); /* Establish a connection to X11. */ int fd = socket(AF_LOCAL, SOCK_STREAM, 0); if (fd == -1) { err(EXIT_FAILURE, "socket()"); } char *host; int displayp; if (xcb_parse_display(getenv("DISPLAY"), &host, &displayp, NULL) == 0) { errx(EXIT_FAILURE, "Could not parse DISPLAY=%s", getenv("DISPLAY")); } free(host); struct sockaddr_un addr; memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_LOCAL; snprintf(addr.sun_path, sizeof(addr.sun_path), "/tmp/.X11-unix/X%d", displayp); if (connect(fd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) { err(EXIT_FAILURE, "connect(%s)", addr.sun_path); } /* Relay setup request. */ must_write(writeall(fd, &setup_request, sizeof(setup_request))); if (setup_request.authorization_protocol_name_len > 0 || setup_request.authorization_protocol_data_len > 0) { const size_t authlen = setup_request.authorization_protocol_name_len + XCB_PAD(setup_request.authorization_protocol_name_len) + setup_request.authorization_protocol_data_len + XCB_PAD(setup_request.authorization_protocol_data_len); void *buf = smalloc(authlen); must_read(readall_into(buf, authlen, w->fd)); must_write(writeall(fd, buf, authlen)); free(buf); } /* Wait for a response from the X11 server. */ ev_io *serverw = scalloc(1, sizeof(ev_io)); connstate->serverw = serverw; serverw->data = connstate; ev_io_init(serverw, read_server_setup_reply_cb, fd, EV_READ); ev_io_start(EV_A_ serverw); }
static void writeb64data(int fd, const char *filename, char *b64) { size_t amt, pos, rem; rem = strlen(b64); pos = 0; while (rem > 0) { amt = rem > 76 ? 76 : rem; writeall(fd, b64 + pos, amt, filename); writeall(fd, "\n", 1, filename); pos += amt; rem -= amt; } }
int main(int argc, char **argv) { pid_t pid; int tochild[2] = { -1, -1 }; int fromchild[2] = { -1, -1 }; const char *message; long long messagelen; if (argc < 2) _exit(111); if (!argv[0]) _exit(111); if (!argv[1]) _exit(111); if (!argv[2]) _exit(111); ++argv; message = *argv; messagelen = str_len(message); ++argv; if (pipe(tochild) == -1) _exit(111); if (pipe(fromchild) == -1) _exit(111); pid = fork(); if (pid == -1) _exit(111); if (pid == 0) { close(tochild[1]); close(fromchild[0]); if (dup2(tochild[0], 0) == -1) _exit(111); if (dup2(fromchild[1], 1) == -1) _exit(111); execvp(*argv, argv); _exit(111); } close(tochild[0]); close(fromchild[1]); close(0); if (dup2(fromchild[0], 0) == -1) _exit(111); close(1); if (dup2(tochild[1], 1) == -1) _exit(111); signal(SIGPIPE, SIG_IGN); global_init(); if (!packet_hello_receive()) die_fatal("unable to receive hello-string", 0, 0); if (messagelen) { if (writeall(1, message, messagelen) == -1) die_fatal("unable to write hello-string", 0, 0); if (writeall(1, "\r\n", 2) == -1) die_fatal("unable to write hello-string", 0, 0); } _exit(111); }
/* * Release a single client from a barrier at a time. */ static void release_client(int sock, struct in_addr ipaddr, int istcp, int result) { int err; if (istcp) { if ((err = writeall(sock, &result, sizeof(result))) <= 0) { if (err < 0) { errorc("writing to TCP client"); } error("write to TCP client aborted"); } tcpsockets[sock].barrier = NULL; close(sock); } else { struct sockaddr_in client; client.sin_addr = ipaddr; client.sin_family = AF_INET; client.sin_port = htons(portnum); while( ((err = sendto(sock, &result, sizeof(result), 0, (struct sockaddr *)&client, sizeof(client))) < 0) && (errno == EINTR) ) { // Nothing to do... } if (err < 0) errorc("writing to UDP client"); } }
int main (int argc, char **argv) { int copySuccessful, writeallTest; const char* buff; copySuccessful = copy("f","copied_f"); if(copySuccessful < 0) { return errorHandler("copy"); } else if(copySuccessful == 0) { printf("copy unsuccessful"); } else { printf("copy successful"); } writeallTest = creat("writeallTest", S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH); if(writeallTest == -1) { return errorHandler("writeall"); } buff = "siemaaa"; writeall(writeallTest, buff, sizeof(buff)); close(writeallTest); printf("\n"); return(0); }
static inline void log_append(const char *logfile, const char *fmt, ...) { va_list ap; char buf[PATH_MAX]; int fd, l; va_start(ap, fmt); l = vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); #ifdef __USE_LARGEFILE fd=open64(logfile,O_APPEND|O_WRONLY|O_LARGEFILE,0); #else # warning "The wrapper library will not work properly for large logs!" fd=open(logfile,O_APPEND|O_WRONLY,0); #endif if (fd == -1) return; flock(fd, LOCK_EX); lseek(fd, 0, SEEK_END); writeall(fd,buf,l); close(fd); }
int packet_sendall(void) { if (writeall(1, packet.sendbuf.buf, packet.sendbuf.len) == -1) return 0; purge(packet.sendbuf.buf, packet.sendbuf.len); packet.sendbuf.len = 0; return 1; }
int main(int argc, char **argv) { long long r,w; if (argv[0]) if (argv[1]) if (str_equal(argv[1], "-h")) die_usage(); for (;;) { r = readblock(0, buf, BLOCK); if (r == -1) die_fatal("unable to read input", 0); for (;;) { if (r <= 0) goto end; if (buf[r - 1] == '\n') { --r; continue; } if (buf[r - 1] == '\r') { --r; continue; } break; } buf[r] = 0; if (r % 2) { errno = 0; die_fatal("unable to decode from hex", 0); } w = r / 2; if (!hexparse(buf, w, (char *)buf)) { errno = 0; die_fatal("unable to decode from hex", 0); } if (writeall(1, buf, w) == -1) die_fatal("unable to write output", 0); if (r != BLOCK) break; } end: if (fsyncfd(1) == -1) die_fatal("unable to write output", 0); _exit(0); }
int luksmeta_save(struct crypt_device *cd, int slot, const luksmeta_uuid_t uuid, const void *buf, size_t size) { uint32_t length = 0; lm_slot_t *s = NULL; lm_t lm = {}; int fd = -1; int r = 0; off_t off; if (uuid_is_zero(uuid)) return -EKEYREJECTED; fd = read_header(cd, O_RDWR | O_SYNC, &length, &lm); if (fd < 0) return fd; if (slot == CRYPT_ANY_SLOT) slot = find_unused_slot(cd, &lm); r = slot >= 0 && slot < LUKS_NSLOTS ? 0 : -EBADSLT; if (r < 0) goto error; s = &lm.slots[slot]; r = uuid_is_zero(s->uuid) ? 0 : -EALREADY; if (r < 0) goto error; s->offset = find_gap(&lm, length, size); r = s->offset >= ALIGN(sizeof(lm), true) ? 0 : -ENOSPC; if (r < 0) goto error; memcpy(s->uuid, uuid, sizeof(luksmeta_uuid_t)); s->length = size; s->crc32c = crc32c(0, buf, size); off = s->offset - sizeof(lm); r = lseek(fd, off, SEEK_CUR) == -1 ? -errno : 0; if (r < 0) goto error; r = writeall(fd, buf, size); if (r < 0) goto error; off = s->offset + s->length; r = lseek(fd, -off, SEEK_CUR) == -1 ? -errno : 0; if (r < 0) goto error; r = write_header(fd, lm); error: close(fd); return r < 0 ? r : slot; }
static void read_server_x11_packet_cb(EV_P_ ev_io *w, int revents) { struct connstate *connstate = (struct connstate *)w->data; // all packets from the server are at least 32 bytes in length size_t len = 32; void *packet = smalloc(len); must_read(readall_into(packet, len, connstate->serverw->fd)); switch (((generic_x11_reply_t *)packet)->code) { case 0: // error break; case 1: // reply len += ((generic_x11_reply_t *)packet)->length * 4; if (len > 32) { packet = srealloc(packet, len); must_read(readall_into(packet + 32, len - 32, connstate->serverw->fd)); } /* BEGIN RandR 1.5 specific */ const uint16_t sequence = ((generic_x11_reply_t *)packet)->sequence; if (sequence == connstate->getext_randr) { xcb_query_extension_reply_t *reply = packet; connstate->randr_major_opcode = reply->major_opcode; } if (sequence == connstate->getmonitors) { printf("RRGetMonitors reply!\n"); if (injected_reply != NULL) { printf("injecting reply\n"); ((generic_x11_reply_t *)injected_reply)->sequence = sequence; must_write(writeall(connstate->clientw->fd, injected_reply, injected_reply_len)); free(packet); return; } } /* END RandR 1.5 specific */ break; default: // event break; } must_write(writeall(connstate->clientw->fd, packet, len)); free(packet); }
binresult *do_send_command(apisock *sock, const char *command, size_t cmdlen, binparam *params, size_t paramcnt, int64_t datalen, int readres){ size_t i, plen; unsigned char *data; void *sdata; /* 2 byte len (not included), 1 byte cmdlen, 1 byte paramcnt, cmdlen bytes cmd*/ plen=cmdlen+2; if (datalen!=-1) plen+=sizeof(uint64_t); for (i=0; i<paramcnt; i++) if (params[i].paramtype==PARAM_STR) plen+=params[i].paramnamelen+params[i].opts+5; /* 1byte type+paramnamelen, nbytes paramnamelen, 4byte strlen, nbytes str */ else if (params[i].paramtype==PARAM_NUM) plen+=params[i].paramnamelen+1+sizeof(uint64_t); else if (params[i].paramtype==PARAM_BOOL) plen+=params[i].paramnamelen+2; if (plen>0xffff) return NULL; sdata=data=(unsigned char *)malloc(plen+2); memcpy(data, &plen, 2); data+=2; if (datalen!=-1){ *data++=cmdlen|0x80; memcpy(data, &datalen, sizeof(uint64_t)); data+=sizeof(uint64_t); } else *data++=cmdlen; memcpy(data, command, cmdlen); data+=cmdlen; *data++=paramcnt; for (i=0; i<paramcnt; i++){ *data++=(params[i].paramtype<<6)+params[i].paramnamelen; memcpy(data, params[i].paramname, params[i].paramnamelen); data+=params[i].paramnamelen; if (params[i].paramtype==PARAM_STR){ memcpy(data, ¶ms[i].opts, 4); data+=4; memcpy(data, params[i].un.str, params[i].opts); data+=params[i].opts; } else if (params[i].paramtype==PARAM_NUM){ memcpy(data, ¶ms[i].un.num, sizeof(uint64_t)); data+=sizeof(uint64_t); } else if (params[i].paramtype==PARAM_BOOL) *data++=params[i].un.num&1; } if (writeall(sock, sdata, plen+2)){ free(sdata); return NULL; } free(sdata); if (readres) return get_result(sock); else return PTR_OK; }
ssize_t swrite(int fd, const uint8_t* buf, size_t count) { ssize_t n; n = writeall(fd, buf, count); if (n == -1) throw errno_error(auss_t() << "Failed to write " << std::hex << fd); else return n; }
void generic_signal(int sig) { if (toys.signalfd) { char c = sig; writeall(toys.signalfd, &c, 1); } toys.signal = sig; }
static int cgiError(int fd, int status) { char achBuf[ 256 ]; int ret = snprintf(achBuf, sizeof(achBuf) - 1, "Status:%d\n\n%s", status, s_pError); writeall(fd, achBuf, ret); close(fd); return 0; }
static void resp(int ok,int patno,int prevno) { int len; static char buf[LEN_B]; char *f=(char*)(ok?OK:explain?ERROR:ER); len=sprintf(buf,f,patno); assert(len<LEN_B); writeall(1,buf,len); if(!ok) { len=sprintf(buf," %u",lasterr); assert(len<LEN_B); writeall(1,buf,len); if(explain) {buf[0]=' '; writeall(1,buf,1);} } for(;;) { /* read always, write if verbose */ len=read(erp[0],buf,LEN_B); if(len<0) {if(errno==EAGAIN) break; else longjmp(IOER,1);} if(len==0) break; if(!ok&&explain&&prevno!=rn_notAllowed) writeall(1,buf,len); } buf[0]='\0'; writeall(1,buf,1); }
void proxy(int in, int out, int err) { char buf[PATH_MAX]; ssize_t count; fd_set set; int nfds = (out > err ? out : err) + 1; while (1) { FD_ZERO(&set); FD_SET(0, &set); FD_SET(out, &set); if (err != out) FD_SET(err, &set); if (select(nfds, &set, NULL, NULL, NULL) < 0) { if (errno == EINTR) continue; fprintf(stderr, "Error %d on select\n", errno); return; } if (FD_ISSET(0, &set)) { count = read(0, buf, sizeof buf); if (count <= 0) return; writeall(in, buf, count); } if (FD_ISSET(out, &set)) { count = read(out, buf, sizeof buf); if (count <= 0) return; writeall(1, buf, count); } if (err != out && FD_ISSET(err, &set)) { count = read(err, buf, sizeof buf); if (count <= 0) return; writeall(2, buf, count); } if (resize) { resize = 0; struct winsize size; ioctl(0, TIOCGWINSZ, &size); ioctl(in, TIOCSWINSZ, &size); } } }
int mkfstore(char *dir) { char tbuf[1024]; int fd; struct loghdr lh; struct idxhdr ih; if(access(dir, F_OK)) { if(mkdir(dir, 0700)) { flog(LOG_ERR, "could not create %s: %s", dir, strerror(errno)); return(-1); } } snprintf(tbuf, sizeof(tbuf), "%s/log", dir); if((fd = open(tbuf, O_WRONLY | O_CREAT | O_EXCL, 0600)) < 0) { flog(LOG_ERR, "could not create log %s: %s", tbuf, strerror(errno)); return(-1); } memcpy(lh.magic, LOGMAGIC, sizeof(LOGMAGIC)); if(writeall(fd, &lh, sizeof(lh), 0)) { flog(LOG_ERR, "could not write log header: %s", strerror(errno)); close(fd); return(-1); } close(fd); snprintf(tbuf, sizeof(tbuf), "%s/index", dir); if((fd = open(tbuf, O_WRONLY | O_CREAT | O_EXCL, 0600)) < 0) { flog(LOG_ERR, "could not create index %s: %s", tbuf, strerror(errno)); return(-1); } memcpy(ih.magic, IDXMAGIC, sizeof(IDXMAGIC)); ih.size = 0; if(writeall(fd, &ih, sizeof(ih), 0)) { flog(LOG_ERR, "could not write index header: %s", strerror(errno)); close(fd); return(-1); } close(fd); return(0); }
/* * can write a few different file types */ static void writekeyfile(const char *filename, const char *info, const void *key, size_t keylen, const char *ident, int oflags, mode_t mode) { char header[1024]; char b64[1024]; int fd; fd = xopen(filename, O_CREAT|oflags|O_NOFOLLOW|O_WRONLY, mode); snprintf(header, sizeof(header), "-----BEGIN REOP %s-----\nident:%s\n", info, ident); writeall(fd, header, strlen(header), filename); if (b64_ntop(key, keylen, b64, sizeof(b64)) == -1) errx(1, "b64 encode failed"); writeb64data(fd, filename, b64); explicit_bzero(b64, sizeof(b64)); snprintf(header, sizeof(header), "-----END REOP %s-----\n", info); writeall(fd, header, strlen(header), filename); close(fd); }
static int mux_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi){ int i, r; for(i=0;i<Readmax;i++){ if(muxs[fi->fh].wh[i] != 0){ r = writeall(muxs[fi->fh].wh[i], buf, size); if(r<0) return r; } } return size; }
int luksmeta_wipe(struct crypt_device *cd, int slot, const luksmeta_uuid_t uuid) { uint8_t *zero = NULL; uint32_t length = 0; lm_slot_t *s = NULL; lm_t lm = {}; int fd = -1; int r = 0; off_t off; if (slot < 0 || slot >= LUKS_NSLOTS) return -EBADSLT; s = &lm.slots[slot]; fd = read_header(cd, O_RDWR | O_SYNC, &length, &lm); if (fd < 0) return fd; r = uuid_is_zero(s->uuid) ? -EALREADY : 0; if (r < 0) goto error; if (uuid && memcmp(uuid, s->uuid, sizeof(luksmeta_uuid_t)) != 0) { r = -EKEYREJECTED; goto error; } off = s->offset - sizeof(lm_t); r = lseek(fd, off, SEEK_CUR) == -1 ? -errno : 0; if (r < 0) goto error; r = (zero = calloc(1, s->length)) ? 0 : -errno; if (r < 0) goto error; r = writeall(fd, zero, s->length); free(zero); if (r < 0) goto error; off = s->offset + s->length; r = lseek(fd, -off, SEEK_CUR) == -1 ? -errno : 0; if (r < 0) goto error; memset(s, 0, sizeof(lm_slot_t)); r = write_header(fd, lm); error: close(fd); return r < 0 ? r : 0; }
static void flush(void) { if (logflagsyslog) { buf[buflen] = 0; syslog(LOG_INFO, "%s", buf); } else { writeall(2, buf, buflen); } buflen = 0; purge(buf, buflen); }
void die_9(int e ,const char *s0 ,const char *s1 ,const char *s2 ,const char *s3 ,const char *s4 ,const char *s5 ,const char *s6 ,const char *s7 ,const char *s8 ) { const char *s[9]; const char *x; char buf[256]; long long buflen = 0; long long i; s[0] = s0; s[1] = s1; s[2] = s2; s[3] = s3; s[4] = s4; s[5] = s5; s[6] = s6; s[7] = s7; s[8] = s8; for (i = 0;i < 9;++i) { x = s[i]; if (!x) continue; while (*x) { if (buflen == sizeof buf) { writeall(2,buf,buflen); buflen = 0; } buf[buflen++] = *x++; } } writeall(2,buf,buflen); alloc_freeall(); _exit(e); }
static int write_header(int fd, lm_t lm) { for (int slot = 0; slot < LUKS_NSLOTS; slot++) { lm.slots[slot].offset = htobe32(lm.slots[slot].offset); lm.slots[slot].length = htobe32(lm.slots[slot].length); lm.slots[slot].crc32c = htobe32(lm.slots[slot].crc32c); } memcpy(lm.magic, LM_MAGIC, sizeof(LM_MAGIC)); lm.version = htobe32(LM_VERSION); lm.crc32c = htobe32(checksum(lm)); return writeall(fd, &lm, sizeof(lm)); }