static int read_memcmp(const PureDB * const db, const puredb_u32_t offset, const unsigned char *str, const puredb_u32_t len) { unsigned char *mapoffsetbuf; int cmp; #ifdef USE_MAPPED_IO if (db->map != NULL) { return memcmp(db->map + offset, str, (size_t) len) != 0; } #endif if ((mapoffsetbuf = (unsigned char *) ALLOCA(len)) == NULL) { return -2; } if (lseek(db->fd, offset, SEEK_SET) == (off_t) -1) { err: ALLOCA_FREE(mapoffsetbuf); return -2; } if (safe_read(db->fd, mapoffsetbuf, (size_t) len) != (ssize_t) len) { goto err; } cmp = memcmp(mapoffsetbuf, str, (size_t) len) != 0; ALLOCA_FREE(mapoffsetbuf); return cmp; }
static LDAPMessage *pw_ldap_uid_search(LDAP * const ld, const char *uid, char *attrs[]) { char *alloca_filter; size_t uid_size; size_t filter_size; int rc; LDAPMessage *res; if (uid == NULL || *uid == 0) { return NULL; } uid_size = strlen(uid); if (uid_size > MAX_LDAP_UID_LENGTH) { return NULL; } filter_size = strlen(ldap_filter) + uid_size + (size_t) 1U; if ((alloca_filter = ALLOCA(filter_size)) == NULL) { return NULL; } if (SNCHECK(snprintf(alloca_filter, filter_size, ldap_filter, uid), filter_size)) { ALLOCA_FREE(alloca_filter); return NULL; } rc = ldap_search_s(ld, base, LDAP_SCOPE_SUBTREE, alloca_filter, attrs, 0, &res); ALLOCA_FREE(alloca_filter); if (rc != LDAP_SUCCESS) { return NULL; } return res; }
int privsep_recvfd(const int psfd) { char *buf; int *fdptr; struct cmsghdr *cmsg; struct msghdr msg; struct iovec vec; const size_t sizeof_buf = CMSG_SPACE(sizeof *fdptr); size_t sizeof_buf_ = sizeof_buf; PrivSepCmd fodder = 0; ssize_t received; if (sizeof_buf_ < sizeof *cmsg) { sizeof_buf_ = sizeof *cmsg; } if ((buf = ALLOCA(sizeof_buf_)) == NULL) { return -1; } memset(&msg, 0, sizeof msg); vec.iov_base = (void *) &fodder; vec.iov_len = sizeof fodder; msg.msg_name = NULL; msg.msg_namelen = (socklen_t) 0; msg.msg_iov = &vec; msg.msg_iovlen = (size_t) 1U; msg.msg_control = buf; msg.msg_controllen = sizeof_buf; msg.msg_flags = 0; if ((cmsg = CMSG_FIRSTHDR(&msg)) == NULL || (fdptr = (int *) CMSG_DATA(cmsg)) == NULL) { ALLOCA_FREE(buf); return -1; } *fdptr = -1; while ((received = recvmsg(psfd, &msg, 0)) == (ssize_t) -1 && errno == EINTR); # if defined(MSG_TRUNC) && defined(MSG_CTRUNC) if ((msg.msg_flags & MSG_TRUNC) || (msg.msg_flags & MSG_CTRUNC)) { ALLOCA_FREE(buf); return -1; } # endif if (received != (ssize_t) sizeof fodder || fodder != PRIVSEPCMD_ANSWER_FD || (cmsg = CMSG_FIRSTHDR(&msg)) == NULL || (fdptr = (int *) CMSG_DATA(cmsg)) == NULL) { ALLOCA_FREE(buf); return -1; } return *fdptr; }
int privsep_sendfd(const int psfd, const int fd) { char *buf; int *fdptr; struct cmsghdr *cmsg; struct msghdr msg; struct iovec vec; const size_t sizeof_buf = CMSG_SPACE(sizeof *fdptr); size_t sizeof_buf_ = sizeof_buf; PrivSepCmd fodder = PRIVSEPCMD_ANSWER_FD; ssize_t sent; if (sizeof_buf_ < sizeof *cmsg) { sizeof_buf_ = sizeof *cmsg; } if ((buf = ALLOCA(sizeof_buf_)) == NULL) { return -1; } memset(&msg, 0, sizeof msg); vec.iov_base = (void *) &fodder; vec.iov_len = sizeof fodder; msg.msg_name = NULL; msg.msg_namelen = (socklen_t) 0; msg.msg_iov = &vec; msg.msg_iovlen = (size_t) 1U; msg.msg_control = buf; msg.msg_controllen = sizeof_buf; msg.msg_flags = 0; if ((cmsg = CMSG_FIRSTHDR(&msg)) == NULL) { ALLOCA_FREE(buf); return -1; } cmsg->cmsg_len = CMSG_LEN(sizeof fd); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; if ((fdptr = (int *) CMSG_DATA(cmsg)) == NULL) { ALLOCA_FREE(buf); return -1; } *fdptr = fd; msg.msg_controllen = cmsg->cmsg_len; while ((sent = sendmsg(psfd, &msg, 0)) == (ssize_t) -1 && errno == EINTR); ALLOCA_FREE(buf); if (sent != (ssize_t) sizeof fodder) { return -1; } return 0; }
static void do_arcfour_random(struct randomness *r, UINT32 length, UINT8 *dst) { #if ALLOCA_68K_BUG ALLOCA_START(alloca_ref); #endif CAST(arcfour_random, self, r); self->staging_count += RANDOM_POLL_FAST(self->super.poller, self->staging_area); if (self->staging_count > STAGE_THRESHOLD) { /* Pour the collected randomness into the pool */ UINT8 *buf = alloca(self->staging_area->hash_size); verbose("do_arcfour_random: Pouring staging area into pool.\n"); /* Get some data out of the pool, in order to keep any entropy * there. */ arcfour_stream(&self->pool, self->staging_area->hash_size, buf); HASH_UPDATE(self->staging_area, self->staging_area->hash_size, buf); HASH_DIGEST(self->staging_area, buf); arcfour_set_key(&self->pool, self->staging_area->hash_size, buf); self->staging_count = 0; } arcfour_stream(&self->pool, length, dst); #if ALLOCA_68K_BUG ALLOCA_FREE(alloca_ref); #endif }
static int client_process_delete(Client * const client) { char *store_file; char *store_file_pnt; size_t sizeof_store_file; logfile(LOG_DEBUG, _("client_process_delete, fd #%d"), client->client_fd); if (client->offset_read_buf < (size_t) 6U) { return -1; } sizeof_store_file = (sizeof "/") - (size_t) 1U + client->key_len + (size_t) 1U; if ((store_file = ALLOCA(sizeof_store_file)) == NULL) { logfile(LOG_WARNING, _("Out of stack for ALLOCA")); return -1; } store_file_pnt = store_file; *store_file_pnt++ = '/'; if (validate_key(client->read_buf + (size_t) 5U, client->key_len) != 0) { ALLOCA_FREE(store_file); logfile(LOG_WARNING, _("Invalid key name")); return -1; } memcpy(store_file_pnt, client->read_buf + (size_t) 5U, client->key_len); store_file_pnt += client->key_len; *store_file_pnt = 0; logfile(LOG_DEBUG, _("Deleting [%s]"), store_file); if (unlink(store_file) != 0) { logfile(LOG_INFO, _("Unable to delete [%s]: [%s]"), store_file, strerror(errno)); ALLOCA_FREE(store_file); return -1; } if ((client->returncode_bufev = bufferevent_new(client->client_fd, returncode_bufferev_read_cb, returncode_bufferev_write_cb, returncode_bufferev_error_cb, client)) == NULL) { logfile(LOG_WARNING, _("Unable to create a bufferevent for fd #%d"), client->client_fd); } (void) bufferevent_write(client->returncode_bufev, (void *) RETURNCODE_OK, sizeof RETURNCODE_OK - (size_t) 1U); return 0; }
void ftpwho_unlinksbfile(const pid_t pid) { size_t sbfile_size; char *sbfile; sbfile_size = sizeof SCOREBOARD_PATH - 1U + 1U + sizeof SCOREBOARD_PREFIX - 1U + 8U + 1U; if ((sbfile = ALLOCA(sbfile_size)) == NULL) { return; } if (SNCHECK(snprintf(sbfile, sbfile_size, SCOREBOARD_PATH "/" SCOREBOARD_PREFIX "%08lu", (unsigned long) pid), sbfile_size)) { ALLOCA_FREE(sbfile); return; } (void) unlink(sbfile); ALLOCA_FREE(sbfile); }
static int altlog_writexfer_w3c(const int upload, const char * const filename, const off_t size, const double duration) { /* * <date> <time> <ip> "[]sent" <file> "226" <user> * date time c-ip cs-method cs-uri-stem sc-status cs-username */ struct tm *tm; struct tm gmt; const char *host_ = *host != 0 ? host : "-"; const char *account_ = *account != 0 ? account : "-"; char *alloca_line; char *quoted_filename; time_t now; size_t line_size; (void) duration; if ((now = time(NULL)) == (time_t) -1 || (tm = localtime(&now)) == NULL || tm->tm_mon > 11 || tm->tm_mon < 0) { return -1; } gmt = *gmtime(&now); if ((quoted_filename = urlencode(filename)) == NULL) { return -1; } line_size = (sizeof "13-04-1975 12:34:56 " - 1U) + strlen(host_) + 1U + (sizeof "[]created" - 1U) + 1U + strlen(quoted_filename) + 1U + (sizeof "226" - 1U) + strlen(account_) + 1U + 42U + 1U /* \n */ + 1U; if ((alloca_line = ALLOCA(line_size)) == NULL) { return -1; } if (!SNCHECK(snprintf(alloca_line, line_size, "%d-%02d-%02d %02d:%02d:%02d %s []%s %s 226 %s %llu\n", gmt.tm_year + 1900, gmt.tm_mon + 1, gmt.tm_mday, gmt.tm_hour, gmt.tm_min, gmt.tm_sec, host_, (upload != 0 ? "created" : "sent"), quoted_filename, account, (unsigned long long) size), line_size)) { altlog_write(alloca_line); } if (quoted_filename != filename) { free(quoted_filename); } ALLOCA_FREE(alloca_line); return 0; }
static void do_arcfour_random_slow(struct randomness *r, UINT32 length, UINT8 *dst) { #if ALLOCA_68K_BUG ALLOCA_START(alloca_ref); #endif CAST(arcfour_random, self, r); unsigned count = RANDOM_POLL_SLOW(self->super.poller, self->staging_area); debug("arcfour_random: entropy estimate for initialization: %i bits.\n", count); if (count < STAGE_THRESHOLD) { const struct exception low_entropy = STATIC_EXCEPTION(EXC_RANDOMNESS_LOW_ENTROPY, "Could not get enough entropy from the environment."); EXCEPTION_RAISE(self->e, &low_entropy); } else self->super.super.quality = 1; { /* Initialize the pool. */ UINT8 *buf = alloca(self->staging_area->hash_size); verbose("do_arcfour_random_slow: Initalizing randomness pool.\n"); HASH_DIGEST(self->staging_area, buf); arcfour_set_key(&self->pool, self->staging_area->hash_size, buf); self->staging_count = 0; } self->super.super.random = do_arcfour_random; arcfour_stream(&self->pool, length, dst); #if ALLOCA_68K_BUG ALLOCA_FREE(alloca_ref); #endif }
static void pkcs1_encode(mpz_t m, struct rsa_algorithm *params, UINT32 length, UINT32 msg_length, const UINT8 *msg) { #if ALLOCA_68K_BUG ALLOCA_START(alloca_ref); #endif UINT8 *em = alloca(length); unsigned i = length; struct hash_instance *h = MAKE_HASH(params->hash); HASH_UPDATE(h, msg_length, msg); assert(i >= h->hash_size); i -= h->hash_size; HASH_DIGEST(h, em + i); KILL(h); assert(i >= params->prefix_length); i -= params->prefix_length; memcpy(em + i, params->prefix, params->prefix_length); assert(i); em[--i] = 0; assert(i >= 9); em[0] = 1; memset(em + 1, 0xff, i - 1); bignum_parse_u(m, length, em); debug("pkcs1_encode: m = %xn\n", m); #if ALLOCA_68K_BUG ALLOCA_FREE(alloca_ref); #endif }
static int altlog_writexfer_stats(const int upload, const char * const filename, const off_t size, const double duration) { /* * <date> <start.pid> <user> <ip> <u/d> <size> <duration> <file> */ const char *host_ = *host != 0 ? host : "-"; const char *account_ = *account != 0 ? account : "-"; char *alloca_line; size_t line_size; line_size = 16U /* now */ + 1U + 16U /* start */ + 1U /* . */ + 16U /* pid */ + 1U + strlen(account_) + 1U + strlen(host_) + 1U + 1U /* U/D */ + 1U + 20U /* size */ + 1U + 16U /* duration */ + strlen(filename) + 1U /* \n */ + 1U; if ((alloca_line = ALLOCA(line_size)) == NULL) { return -1; } if (!SNCHECK(snprintf(alloca_line, line_size, "%llu %llx.%lx %s %s %c %llu %lu %s\n", (unsigned long long) time(NULL), (unsigned long long) session_start_time, (unsigned long) getpid(), account_, host_, upload != 0 ? 'U' : 'D', (unsigned long long) size, (unsigned long) (duration + 0.5), filename), line_size)) { altlog_write(alloca_line); } ALLOCA_FREE(alloca_line); return 0; }
int altlog_write_w3c_header(void) { time_t now; struct tm *tm; struct tm gmt; char *alloca_line; size_t line_size; if ((now = time(NULL)) == (time_t) -1 || (tm = localtime(&now)) == NULL || tm->tm_mon > 11 || tm->tm_mon < 0) { return -1; } gmt = *gmtime(&now); line_size = sizeof "#Date: 001975-04-13 12:34:56\n"; /* be year-999999 compliant :) */ if ((alloca_line = ALLOCA(line_size)) == NULL) { return -1; } altlog_write("#Software: Pure-FTPd " VERSION "\n"); altlog_write("#Version: 1.0\n"); if (!SNCHECK(snprintf(alloca_line, line_size, "#Date: %04d-%02d-%02d %02d:%02d:%02d\n", gmt.tm_year + 1900, gmt.tm_mon + 1, gmt.tm_mday, gmt.tm_hour, gmt.tm_min, gmt.tm_sec), line_size)) { altlog_write(alloca_line); } altlog_write("#Fields: date time c-ip cs-method cs-uri-stem sc-status cs-username sc-bytes\n"); ALLOCA_FREE(alloca_line); return 0; }
int fill_mac_address(void) { int s; if ((s = socket(HWINFO_DOMAIN, HWINFO_TYPE, 0)) == -1) { logfile(LOG_ERR, _("Unable to open raw device: [%s]"), strerror(errno)); return -1; } #ifdef SIOCGIFHWADDR { struct ifreq ifr; if (strlen(interface) >= sizeof ifr.ifr_name) { logfile(LOG_ERR, _("Interface name too long")); return -1; } strncpy(ifr.ifr_name, interface, sizeof ifr.ifr_name); if (ioctl(s, SIOCGIFHWADDR, &ifr) != 0) { logfile(LOG_ERR, _("Unable to get hardware info about an interface: %s"), strerror(errno)); (void) close(s); return -1; } switch (ifr.ifr_hwaddr.sa_family) { case ARPHRD_ETHER: case ARPHRD_IEEE802: break; default: logfile(LOG_ERR, _("Unknown hardware type [%u]"), (unsigned int) ifr.ifr_hwaddr.sa_family); } memcpy(hwaddr, &ifr.ifr_hwaddr.sa_data, sizeof hwaddr); } #elif defined(HAVE_GETIFADDRS) { struct ifaddrs *ifas; struct ifaddrs *ifa; struct sockaddr_dl *sadl; struct ether_addr *ea; if (getifaddrs(&ifas) != 0) { logfile(LOG_ERR, _("Unable to get interface address: %s"), strerror(errno)); return -1; } ifa = ifas; while (ifa != NULL) { if (strcmp(ifa->ifa_name, interface) == 0 && ifa->ifa_addr->sa_family == AF_LINK) { sadl = (struct sockaddr_dl *) ifa->ifa_addr; if (sadl == NULL || sadl->sdl_type != IFT_ETHER || sadl->sdl_alen <= 0) { logfile(LOG_ERR, _("Invalid media / hardware address for [%s]"), interface); return -1; } ea = (struct ether_addr *) LLADDR(sadl); memcpy(hwaddr, ea, sizeof hwaddr); return 0; } ifa = ifa->ifa_next; } return -1; } #elif defined(SIOCGLIFNUM) { struct lifconf lifc; struct lifnum lifn; struct lifreq *lifr; caddr_t *lifrspace; struct arpreq arpreq; lifn.lifn_flags = 0; lifn.lifn_family = AF_INET; if (ioctl(s, SIOCGLIFNUM, &lifn) < 0) { logfile(LOG_ERR, _("ioctl SIOCGLIFNUM error")); return -1; } if (lifn.lifn_count <= 0) { logfile(LOG_ERR, _("No interface found")); return -1; } lifc.lifc_family = lifn.lifn_family; lifc.lifc_len = lifn.lifn_count * sizeof *lifr; lifrspace = ALLOCA(lifc.lifc_len); lifc.lifc_buf = (caddr_t) lifrspace; if (ioctl(s, SIOCGLIFCONF, &lifc) < 0) { logfile(LOG_ERR, _("ioctl SIOCGLIFCONF error")); ALLOCA_FREE(lifrspace); return -1; } lifr = lifc.lifc_req; for(;;) { if (lifn.lifn_count <= 0) { logfile(LOG_ERR, _("Interface [%s] not found"), interface); ALLOCA_FREE(lifrspace); return -1; } lifn.lifn_count--; if (strcmp(lifr->lifr_name, interface) == 0) { break; } lifr++; } memcpy(&arpreq.arp_pa, &lifr->lifr_addr, sizeof arpreq.arp_pa); ALLOCA_FREE(lifrspace); if (ioctl(s, SIOCGARP, &arpreq) != 0) { logfile(LOG_ERR, _("Unable to get hardware info about [%s]"), interface); return -1; } memcpy(hwaddr, &arpreq.arp_ha.sa_data, sizeof hwaddr); } #endif (void) close(s); return 0; }
static int client_process_store(Client * const client) { char *store_file; char *store_file_tmp; char *store_file_pnt; char *store_file_tmp_pnt; size_t sizeof_store_file; size_t sizeof_store_file_tmp; logfile(LOG_DEBUG, _("client_process_store, fd #%d"), client->client_fd); if (client->offset_read_buf < (size_t) 10U) { return -1; } sizeof_store_file = (sizeof "/") - (size_t) 1U + client->key_len + (size_t) 1U; if ((store_file = ALLOCA(sizeof_store_file)) == NULL) { logfile(LOG_ERR, _("Out of stack for ALLOCA")); return -1; } store_file_pnt = store_file; *store_file_pnt++ = '/'; if (validate_key(client->read_buf + (size_t) 9U, client->key_len) != 0) { ALLOCA_FREE(store_file); logfile(LOG_WARNING, _("Invalid key name")); return -1; } memcpy(store_file_pnt, client->read_buf + (size_t) 9U, client->key_len); store_file_pnt += client->key_len; *store_file_pnt = 0; sizeof_store_file_tmp = (sizeof "/") - (size_t) 1U + sizeof STOREFILE_TMP_PREFIX - (size_t) 1U + client->key_len + (size_t) 1U; if ((store_file_tmp = ALLOCA(sizeof_store_file_tmp)) == NULL) { logfile(LOG_ERR, _("Out of stack for ALLOCA")); ALLOCA_FREE(store_file); return -1; } store_file_tmp_pnt = store_file_tmp; *store_file_tmp_pnt++ = '/'; memcpy(store_file_tmp_pnt, STOREFILE_TMP_PREFIX, sizeof STOREFILE_TMP_PREFIX - (size_t) 1U); store_file_tmp_pnt += sizeof STOREFILE_TMP_PREFIX - (size_t) 1U; memcpy(store_file_tmp_pnt, client->read_buf + (size_t) 9U, client->key_len); store_file_tmp_pnt += client->key_len; *store_file_tmp_pnt = 0; logfile(LOG_DEBUG, _("Creating [%s]"), store_file_tmp); if ((client->write_fd = open(store_file_tmp, O_CREAT | O_NOFOLLOW | O_WRONLY | O_TRUNC, (mode_t) 0600)) == -1) { logfile(LOG_WARNING, _("Unable to create [%s]: [%s]"), store_file, strerror(errno)); ALLOCA_FREE(store_file); ALLOCA_FREE(store_file_tmp); return -1; } logfile(LOG_DEBUG, _("[%s] = fd #%d"), store_file, client->write_fd); if (safe_write(client->write_fd, client->read_buf + (size_t) 9U + client->key_len, client->data_len) < 0) { logfile(LOG_WARNING, _("Write error: [%s]"), strerror(errno)); } (void) close(client->write_fd); client->write_fd = -1; logfile(LOG_DEBUG, _("Renaming [%s] to [%s]"), store_file_tmp, store_file); if (rename(store_file_tmp, store_file) != 0) { logfile(LOG_WARNING, _("Unable to rename [%s] to [%s]: [%s]"), store_file_tmp, store_file, strerror(errno)); (void) unlink(store_file_tmp); ALLOCA_FREE(store_file); ALLOCA_FREE(store_file_tmp); return -1; } ALLOCA_FREE(store_file); ALLOCA_FREE(store_file_tmp); if ((client->returncode_bufev = bufferevent_new(client->client_fd, returncode_bufferev_read_cb, returncode_bufferev_write_cb, returncode_bufferev_error_cb, client)) == NULL) { logfile(LOG_WARNING, _("Unable to create a bufferevent for fd #%d"), client->client_fd); } (void) bufferevent_write(client->returncode_bufev, (void *) RETURNCODE_OK, sizeof RETURNCODE_OK - (size_t) 1U); return 0; }
static int altlog_writexfer_xferlog(const int upload, const char * const filename, const off_t size, const double duration) { char date[sizeof "Mon Apr 13 12:34:56 1975"]; struct tm *tm; char *alloca_line; const char *host_ = *host != 0 ? host : "-"; const char *account_ = *account != 0 ? account : "-"; char *quoted_filename; size_t filename_idx; time_t now; size_t line_size; size_t filename_size; char c; if ((now = time(NULL)) == (time_t) -1 || (tm = localtime(&now)) == NULL || tm->tm_mon > 11 || tm->tm_mon < 0 || tm->tm_wday > 6 || tm->tm_wday < 0) { return -1; } if (SNCHECK(snprintf(date, sizeof date, "%s %s %02d %02d:%02d:%02d %d", week_days[tm->tm_wday], months[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_year + 1900), sizeof date)) { return -1; } if ((filename_idx = strlen(filename)) <= (size_t) 0U) { return -1; } filename_size = filename_idx + (size_t) 1U; if ((quoted_filename = ALLOCA(filename_size)) == NULL) { return -1; } quoted_filename[filename_idx] = 0; do { filename_idx--; c = filename[filename_idx]; if (isspace((unsigned char) c) || ISCTRLCODE(c)) { c = '_'; } quoted_filename[filename_idx] = c; } while (filename_idx > (size_t) 0U); line_size = (sizeof date - 1U) + (sizeof " " - 1U) + (size_t) 16U /* duration */ + (sizeof " " - 1U) + strlen(host_) + (sizeof " " - 1U) + (size_t) 20U /* size */ + (sizeof " " - 1U) + (filename_size - 1U) + (sizeof " " - 1U) + (size_t) 1U /* type */ + (sizeof " _ " - 1U) + (size_t) 1U /* direction */ + (sizeof " " - 1U) + (size_t) 1U /* anonymous */ + (sizeof " " - 1U) + strlen(account_) + (sizeof " ftp 1 * c\n" - 1U) + (size_t) 1U; if ((alloca_line = ALLOCA(line_size)) == NULL) { ALLOCA_FREE(quoted_filename); return -1; } if (!SNCHECK(snprintf(alloca_line, line_size, "%s %lu %s %llu %s %c _ %c %c %s ftp 1 * c\n", date, (unsigned long) (duration + 0.5), host_, (unsigned long long) size, quoted_filename, type == 1 ? 'a' : 'b', upload != 0 ? 'i' : 'o', loggedin != 0 ? 'r' : 'a', account_), line_size)) { altlog_write(alloca_line); } ALLOCA_FREE(quoted_filename); ALLOCA_FREE(alloca_line); return 0; }
static int altlog_writexfer_clf(const int upload, const char * const filename, const off_t size) { char date[sizeof "13/Apr/1975:12:34:56 +0100"]; struct tm *tm; char *alloca_line; const char *host_ = *host != 0 ? host : "-"; const char *account_ = *account != 0 ? account : "-"; char *quoted_filename; time_t now; long diff; int sign; size_t line_size; if ((now = time(NULL)) == (time_t) -1 || (tm = localtime(&now)) == NULL || tm->tm_mon > 11 || tm->tm_mon < 0) { return -1; } # ifdef HAVE_STRUCT_TM_TM_GMTOFF diff = -(tm->tm_gmtoff) / 60L; # elif defined(HAVE_SCALAR_TIMEZONE) diff = -(timezone) / 60L; # else { struct tm gmt; struct tm *t; int days, hours, minutes; gmt = *gmtime(&now); t = localtime(&now); days = t->tm_yday - gmt.tm_yday; hours = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + t->tm_hour - gmt.tm_hour); minutes = hours * 60 + t->tm_min - gmt.tm_min; diff = -minutes; } # endif if (diff > 0L) { sign = '+'; } else { sign = '-'; diff = -diff; } if (SNCHECK(snprintf(date, sizeof date, "%02d/%s/%d:%02d:%02d:%02d %c%02ld%02ld", tm->tm_mday, months[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec, sign, diff / 60L, diff % 60L), sizeof date)) { return -1; } if ((quoted_filename = urlencode(filename)) == NULL) { return -1; } line_size = strlen(host_) + (sizeof " - " - 1U) + strlen(account_) + (sizeof " [" - 1U) + (sizeof date - 1U) + (sizeof "] \"" - 1U) + 3U /* GET / PUT */ + (sizeof " " - 1U) + strlen(quoted_filename) + (sizeof "\" 200 18446744073709551616\n" - 1U) + 1U; if ((alloca_line = ALLOCA(line_size)) == NULL) { return -1; } if (!SNCHECK(snprintf(alloca_line, line_size, "%s - %s [%s] \"%s %s\" 200 %llu\n", host_, account_, date, upload == 0 ? "GET" : "PUT", quoted_filename, (unsigned long long) size), line_size)) { altlog_write(alloca_line); } if (quoted_filename != filename) { free(quoted_filename); } ALLOCA_FREE(alloca_line); return 0; }
static int client_process_fetch(Client * const client) { char *store_file; char *store_file_pnt; size_t sizeof_store_file; struct stat st; logfile(LOG_DEBUG, _("client_process_fetch, fd #%d"), client->client_fd); if (client->offset_read_buf < (size_t) 6U) { return -1; } sizeof_store_file = (sizeof "/") - (size_t) 1U + client->key_len + (size_t) 1U; if ((store_file = ALLOCA(sizeof_store_file)) == NULL) { logfile(LOG_WARNING, _("Out of stack for ALLOCA")); return -1; } store_file_pnt = store_file; *store_file_pnt++ = '/'; if (validate_key(client->read_buf + (size_t) 5U, client->key_len) != 0) { ALLOCA_FREE(store_file); logfile(LOG_WARNING, _("Invalid key name")); return -1; } memcpy(store_file_pnt, client->read_buf + (size_t) 5U, client->key_len); store_file_pnt += client->key_len; *store_file_pnt = 0; logfile(LOG_DEBUG, _("Reading [%s]"), store_file); if ((client->read_fd = open(store_file, O_RDONLY)) == -1) { logfile(LOG_DEBUG, _("Unable to fetch [%s]: [%s]"), store_file, strerror(errno)); ALLOCA_FREE(store_file); return -1; } logfile(LOG_DEBUG, _("[%s] = fd #%d"), store_file, client->read_fd); if (fstat(client->read_fd, &st) != 0 || st.st_size <= (off_t) 0) { logfile(LOG_DEBUG, _("Unable to stat a previous key (%s) : [%s]"), store_file, strerror(errno)); (void) close(client->read_fd); client->read_fd = -1; ALLOCA_FREE(store_file); return -1; } if ((client->read_mapped_zone = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, client->read_fd, (off_t) 0)) == NULL) { logfile(LOG_WARNING, _("Unable to map in memory: [%s]"), strerror(errno)); (void) close(client->read_fd); client->read_fd = -1; ALLOCA_FREE(store_file); return -1; } ALLOCA_FREE(store_file); client->read_mapped_zone_length = st.st_size; if ((client->returncode_bufev = bufferevent_new(client->client_fd, returncode_bufferev_read_cb, returncode_bufferev_write_cb, returncode_bufferev_error_cb, client)) == NULL) { logfile(LOG_WARNING, _("Unable to create a bufferevent for fd #%d"), client->client_fd); } (void) bufferevent_write(client->returncode_bufev, client->read_mapped_zone, client->read_mapped_zone_length); return 0; }
static int do_mkdb(const char *dbfile, const char * const file) { FILE *fp; char *index_dbfile; size_t sizeof_index_dbfile; char *data_dbfile; size_t sizeof_data_dbfile; char *s; PureDBW dbw; int ret = PW_ERROR_UNEXPECTED_ERROR; char line[LINE_MAX]; if (dbfile == NULL || *dbfile == 0) { char *dbfile_; if ((dbfile_ = getenv(ENV_DEFAULT_PW_DB)) != NULL && *dbfile_ != 0) { dbfile = dbfile_; } else { dbfile = DEFAULT_PW_DB; } } if (file == NULL) { fprintf(stderr, "Missing passwd file\n"); return PW_ERROR_MISSING_PASSWD_FILE; } if ((fp = fopen(file, "r")) == NULL) { perror("Unable to open the passwd file"); return PW_ERROR_MISSING_PASSWD_FILE; } sizeof_index_dbfile = strlen(dbfile) + sizeof NEWPASSWD_INDEX_SUFFIX; if ((index_dbfile = ALLOCA(sizeof_index_dbfile)) == NULL) { fclose(fp); no_mem(); } sizeof_data_dbfile = strlen(dbfile) + sizeof NEWPASSWD_DATA_SUFFIX; if ((data_dbfile = ALLOCA(sizeof_data_dbfile)) == NULL) { fclose(fp); ALLOCA_FREE(index_dbfile); no_mem(); } snprintf(index_dbfile, sizeof_index_dbfile, "%s%s", dbfile, NEWPASSWD_INDEX_SUFFIX); snprintf(data_dbfile, sizeof_data_dbfile, "%s%s", dbfile, NEWPASSWD_DATA_SUFFIX); if (puredbw_open(&dbw, index_dbfile, data_dbfile, dbfile) != 0) { perror("Unable to create the database"); goto err; } while (fgets(line, (int) sizeof line - 1U, fp) != NULL) { strip_lf(line); if (*line == PW_LINE_COMMENT) { continue; } if (*line == 0 || (s = strchr(line, *PW_LINE_SEP)) == NULL || s[1] == 0) { continue; } *s++ = 0; if (puredbw_add_s(&dbw, line, s) != 0) { perror("Error while indexing a new entry"); goto err; } } if (puredbw_close(&dbw) != 0) { perror("Unable to close the database"); } else { ret = 0; } err: puredbw_free(&dbw); ALLOCA_FREE(index_dbfile); ALLOCA_FREE(data_dbfile); fclose(fp); return ret; }