Merge branch 'refs/heads/t/duration-keyword' into pu
authorAndre Noll <maan@tuebingen.mpg.de>
Sun, 8 Dec 2019 09:03:58 +0000 (10:03 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Sun, 8 Dec 2019 09:03:58 +0000 (10:03 +0100)
27 files changed:
Makefile.in
Makefile.real
NEWS.md
alsa_write.c
ao_write.c
check_wav.c
command.c
configure.ac
crypt_backend.h
crypt_common.c
dccp_send.c
error.h
gcrypt.c
http_send.c
m4/lls/include/sample-format.m4
m4/lls/play.suite.m4
mixer.c
mood.c
mp3_afh.c
ogg_afh_common.c
openssl.c
para.h
play.c
send.h
send_common.c
vss.c
web/manual.md

index d4a83a7..11fa200 100644 (file)
@@ -67,4 +67,6 @@ curses_ldflags := @curses_ldflags@
 crypto_ldflags := @crypto_ldflags@
 iconv_ldflags := @iconv_ldflags@
 
+ENABLE_UBSAN := @ENABLE_UBSAN@
+
 include Makefile.real
index 7d4eff4..aee9731 100644 (file)
@@ -129,6 +129,11 @@ STRICT_CFLAGS += -Wno-sign-compare -Wno-unknown-pragmas
 STRICT_CFLAGS += -Wdeclaration-after-statement
 STRICT_CFLAGS += -Wformat -Wformat-security -Wmissing-format-attribute
 
+ifeq ($(ENABLE_UBSAN), yes)
+       STRICT_CFLAGS += -fsanitize=undefined
+       LDFLAGS += -lubsan
+endif
+
 ifeq ($(uname_s),Linux)
        # these cause warnings on *BSD
        CPPFLAGS += -Wunused-macros
diff --git a/NEWS.md b/NEWS.md
index bbe2001..fd587fa 100644 (file)
--- a/NEWS.md
+++ b/NEWS.md
@@ -18,6 +18,10 @@ NEWS
 - Cleanup of the audio format handler code.
 - We now build the tree using the .ONESHELL feature of GNU make,
   which results in a significant speedup.
+- Two robustness fixes for FreeBSD.
+- para_client now supports RFC4716 private keys as generated with
+  ssh-keygen -m RFC4716. In fact, this key format has been made the
+  default, and the former PEM keys will be depreciated at some point.
 
 --------------------------------------
 0.6.2 (2018-06-30) "elastic diversity"
index 363e393..14c3555 100644 (file)
@@ -61,6 +61,8 @@ static snd_pcm_format_t get_alsa_pcm_format(enum sample_format sf)
        case SF_S16_BE: return SND_PCM_FORMAT_S16_BE;
        case SF_U16_LE: return SND_PCM_FORMAT_U16_LE;
        case SF_U16_BE: return SND_PCM_FORMAT_U16_BE;
+       case SF_FLOAT_LE: return SND_PCM_FORMAT_FLOAT_LE;
+       case SF_FLOAT_BE: return SND_PCM_FORMAT_FLOAT_BE;
        default: return SND_PCM_FORMAT_S16_LE;
        }
 }
index 447dea8..b4fa454 100644 (file)
@@ -87,6 +87,8 @@ static int aow_set_sample_format(unsigned sample_rate, unsigned channels,
                case SF_U8:
                case SF_U16_LE:
                case SF_U16_BE:
+               case SF_FLOAT_LE:
+               case SF_FLOAT_BE:
                        return -E_BAD_SAMPLE_FORMAT;
                case SF_S8:
                        /* no need to set byte_format */
index 89ebdac..8c2ecee 100644 (file)
@@ -127,7 +127,7 @@ int check_wav_post_select(struct check_wav_context *cwc)
        unsigned char *a;
        size_t sz;
        int ret;
-       uint16_t bps; /* bits per sample */
+       uint16_t format_code, bps; /* bits per sample */
        const char *sample_formats[] = {SAMPLE_FORMATS};
 
        if (!btrn)
@@ -157,13 +157,24 @@ int check_wav_post_select(struct check_wav_context *cwc)
        cwc->state = CWS_HAVE_HEADER;
        /* Only set those values which have not already been set. */
        cwc->channels = a[22];
+       format_code = read_u16(a + 20);
+       if (format_code != 1 && format_code != 3) {
+               cwc->state = CWS_NO_HEADER;
+               goto out;
+       }
        cwc->sample_rate = read_u32(a + 24);
        bps = read_u16(a + 34);
-       if (bps != 8 && bps != 16) {
+       if (bps != 8 && bps != 16 && bps != 32) {
                PARA_WARNING_LOG("%u bps not supported, assuming 16\n",
                        bps);
                bps = 16;
        }
+       if ((bps < 32 && format_code != 1) || (bps == 32 && format_code != 3)) {
+               PARA_WARNING_LOG("invalid bps/format_code (%u/%u)\n",
+                       bps, format_code);
+               cwc->state = CWS_NO_HEADER;
+               goto out;
+       }
        /*
         * 8-bit samples are stored as unsigned bytes, ranging from 0
         * to 255.  16-bit samples are stored as 2's-complement signed
@@ -171,9 +182,12 @@ int check_wav_post_select(struct check_wav_context *cwc)
         */
        if (bps == 8)
                cwc->sample_format = SF_U8;
-       else
+       else if (bps == 16)
                cwc->sample_format = (a[3] == 'F')?
                        SF_S16_LE : SF_S16_BE;
+       else /* 32 bit */
+               cwc->sample_format = (a[3] == 'F')?
+                       SF_FLOAT_LE : SF_FLOAT_BE;
        PARA_NOTICE_LOG("%uHz, %s, %s\n", cwc->sample_rate,
                cwc->channels == 1? "mono" : "stereo",
                sample_formats[cwc->sample_format]);
index 481fd0f..63f0f16 100644 (file)
--- a/command.c
+++ b/command.c
@@ -457,6 +457,7 @@ EXPORT_SERVER_CMD_HANDLER(version);
        ITEM(chunk_time) \
        ITEM(num_chunks) \
        ITEM(amplification) \
+       ITEM(play_time) \
 
 /*
  * Create a set of audio-file related status items with empty values. These are
index 053996d..850e814 100644 (file)
@@ -96,8 +96,33 @@ AC_CHECK_HEADER(openssl/ssl.h, [], [HAVE_OPENSSL=no])
 AC_CHECK_LIB([crypto], [RAND_bytes], [], [HAVE_OPENSSL=no])
 LIB_SUBST_FLAGS(openssl)
 if test $HAVE_OPENSSL = yes; then
-       AC_CHECK_LIB([crypto], [RSA_set0_key],
-               AC_DEFINE([HAVE_RSA_SET0_KEY], [1], [openssl-1.1]))
+       HAVE_RSA_SET0_KEY=yes
+       AC_CHECK_DECL([RSA_set0_key], [], [], [#include <openssl/rsa.h>])
+       AC_CHECK_LIB([crypto], [RSA_set0_key], [], [])
+       if test "$ac_cv_have_decl_RSA_set0_key" != "$ac_cv_lib_crypto_RSA_set0_key"; then
+               AC_MSG_ERROR([openssl header/library mismatch])
+       fi
+       test "$ac_cv_have_decl_RSA_set0_key" = yes &&
+               AC_DEFINE([HAVE_RSA_SET0_KEY], [1], [openssl >= 1.1])
+
+       HAVE_CRYPTO_CLEANUP_ALL_EX_DATA=yes
+       AC_CHECK_DECL([CRYPTO_cleanup_all_ex_data], [],
+               [HAVE_CRYPTO_CLEANUP_ALL_EX_DATA=no],
+               [#include <openssl/rsa.h>])
+       AC_CHECK_LIB([crypto], [CRYPTO_cleanup_all_ex_data], [],
+               [HAVE_CRYPTO_CLEANUP_ALL_EX_DATA=no])
+       test $HAVE_CRYPTO_CLEANUP_ALL_EX_DATA = yes &&
+               AC_DEFINE([HAVE_CRYPTO_CLEANUP_ALL_EX_DATA], [1],
+                       [not available on FreeBSD 12])
+       HAVE_OPENSSL_THREAD_STOP=yes
+       AC_CHECK_DECL([OPENSSL_thread_stop], [],
+               [HAVE_OPENSSL_THREAD_STOP=no],
+               [#include <openssl/crypto.h>])
+       AC_CHECK_LIB([crypto], [OPENSSL_thread_stop], [],
+               [HAVE_OPENSSL_THREAD_STOP=no])
+       test $HAVE_OPENSSL_THREAD_STOP = yes &&
+               AC_DEFINE([HAVE_OPENSSL_THREAD_STOP], [1],
+                       [not available on openssl-1.0])
 fi
 UNSTASH_FLAGS
 ######################################################################### gcrypt
@@ -348,6 +373,11 @@ AC_CHECK_HEADER(samplerate.h, [], HAVE_SAMPLERATE=no)
 AC_CHECK_LIB([samplerate], [src_process], [], HAVE_SAMPLERATE=no)
 LIB_SUBST_FLAGS(samplerate)
 UNSTASH_FLAGS
+######################################################################### ubsan
+AC_ARG_ENABLE([ubsan], [AS_HELP_STRING(--enable-ubsan,
+       [Detect and report undefined behaviour.])],
+       [ENABLE_UBSAN=yes], [ENABLE_UBSAN=no])
+AC_SUBST(ENABLE_UBSAN)
 ######################################################################### server
 if test -n "$CRYPTOLIB" && test $HAVE_OSL = yes && test -n "$BISON" && \
                test -n "$FLEX"; then
index 175a688..b0998d8 100644 (file)
@@ -7,6 +7,13 @@
 /** AES block size in bytes. */
 #define AES_CRT128_BLOCK_SIZE 16
 
-int decode_ssh_key(const char *filename, unsigned char **blob,
+int decode_public_key(const char *filename, unsigned char **blob,
                size_t *decoded_size);
+int decode_private_key(const char *key_file, unsigned char **result,
+               size_t *blob_size);
+/** Legacy PEM keys (openssh-7.7 and earlier, paraslash.0.6.2 and earlier) */
+#define PKT_PEM (0)
+/** OPENSSH keys (since openssh-7.8, paraslash.0.6.3) */
+#define PKT_OPENSSH (1)
 int check_private_key_file(const char *file);
+int find_openssh_bignum_offset(const unsigned char *data, int len);
index 235b8b8..c1e40d9 100644 (file)
@@ -85,7 +85,7 @@ static int check_ssh_key_header(const unsigned char *blob, int blen)
  *
  * \sa \ref uudecode().
  */
-int decode_ssh_key(const char *filename, unsigned char **blob,
+int decode_public_key(const char *filename, unsigned char **blob,
                size_t *decoded_size)
 {
        int ret, ret2;
@@ -159,3 +159,161 @@ int hash_compare(unsigned char *h1, unsigned char *h2)
        }
        return 0;
 }
+
+/**
+ * Check header of an openssh private key and compute bignum offset.
+ *
+ * \param data The base64-decoded key.
+ * \param len The size of the decoded key.
+ *
+ * Several assumptions are made about the key. Most notably, we only support
+ * single unencrypted keys without comments.
+ *
+ * \return The offset at which the first bignum of the private key (the public
+ * exponent n) starts. Negative error code on failure.
+ */
+int find_openssh_bignum_offset(const unsigned char *data, int len)
+{
+       /*
+        * Unencrypted keys without comments always start with the below byte
+        * sequence. See PROTOCOL.key of the openssh package.
+        */
+       static const unsigned char valid_openssh_header[] = {
+               /* string "openssh-key-v1" */
+               0x6f, 0x70, 0x65, 0x6e, 0x73, 0x73, 0x68, 0x2d, 0x6b, 0x65,
+                       0x79, 0x2d, 0x76, 0x31,
+               /* length of the cipher name */
+               0x00, 0x00, 0x00, 0x00, 0x04,
+               /* cipher name: "none" */
+               0x6e, 0x6f, 0x6e, 0x65,
+               /* length of the kdfname (only used for encrypted keys) */
+               0x00, 0x00, 0x00, 0x04,
+               /* kdfname: "none" */
+               0x6e, 0x6f, 0x6e, 0x65,
+               /* length of kdfoptions */
+               0x00, 0x00, 0x00, 0x00,
+               /* number of keys */
+               0x00, 0x00, 0x00, 0x01,
+       };
+       uint32_t val;
+       const unsigned char *p, *end = data + len;
+
+       if (len <= sizeof(valid_openssh_header) + 4)
+               return -E_OPENSSH_PARSE;
+       if (memcmp(data, valid_openssh_header, sizeof(valid_openssh_header)))
+               return -E_OPENSSH_PARSE;
+       p = data + sizeof(valid_openssh_header);
+       /* length of public key */
+       val = read_u32_be(p);
+       if (val > end - p - 4)
+               return -E_OPENSSH_PARSE;
+       p += val + 4;
+       /* length of private key */
+       val = read_u32_be(p);
+       if (val > end - p - 4)
+               return -E_OPENSSH_PARSE;
+       p += 4;
+       /* two equal random integers ("checkint") */
+       if (p + 8 > end)
+               return -E_OPENSSH_PARSE;
+       if (read_u32_be(p) != read_u32_be(p + 4))
+               return -E_OPENSSH_PARSE;
+       p += 8;
+       /* length of name of key type "ssh-rsa" */
+       if (p + 11 > end)
+               return -E_OPENSSH_PARSE;
+       if (read_u32_be(p) != 7)
+               return -E_OPENSSH_PARSE;
+       if (memcmp(p + 4, "ssh-rsa", 7))
+               return -E_OPENSSH_PARSE;
+       p += 11;
+       return p - data;
+}
+
+/** Private PEM keys (legacy format) start with this header. */
+#define PRIVATE_PEM_KEY_HEADER "-----BEGIN RSA PRIVATE KEY-----"
+/** Private OPENSSH keys (RFC4716) start with this header. */
+#define PRIVATE_OPENSSH_KEY_HEADER "-----BEGIN OPENSSH PRIVATE KEY-----"
+/** Private PEM keys (legacy format) end with this footer. */
+#define PRIVATE_PEM_KEY_FOOTER "-----END RSA PRIVATE KEY-----"
+/** Private OPENSSH keys (RFC4716) end with this footer. */
+#define PRIVATE_OPENSSH_KEY_FOOTER "-----END OPENSSH PRIVATE KEY-----"
+
+/**
+ * Decode an openssh-v1 (aka RFC4716) or PEM (aka ASN.1) private key.
+ *
+ * \param key_file The private key file (usually id_rsa).
+ * \param result Pointer to base64-decoded blob is returned here.
+ * \param blob_size The size of the decoded blob.
+ *
+ * This only checks header and footer and base64-decodes the part in between.
+ * No attempt to read the decoded part is made.
+ *
+ * \return Negative on errors, PKT_PEM or PKT_OPENSSH on success, indicating
+ * the type of key.
+ */
+int decode_private_key(const char *key_file, unsigned char **result,
+               size_t *blob_size)
+{
+       int ret, ret2, i, j, key_type;
+       void *map;
+       size_t map_size, key_size;
+       unsigned char *blob = NULL;
+       char *begin, *footer, *key;
+
+       ret = mmap_full_file(key_file, O_RDONLY, &map, &map_size, NULL);
+       if (ret < 0)
+               goto out;
+       ret = -E_KEY_MARKER;
+       if (strncmp(map, PRIVATE_PEM_KEY_HEADER,
+                       strlen(PRIVATE_PEM_KEY_HEADER)) == 0) {
+               key_type = PKT_PEM;
+               begin = map + strlen(PRIVATE_PEM_KEY_HEADER);
+               footer = strstr(map, PRIVATE_PEM_KEY_FOOTER);
+               PARA_INFO_LOG("detected legacy PEM key %s\n", key_file);
+       } else if (strncmp(map, PRIVATE_OPENSSH_KEY_HEADER,
+                       strlen(PRIVATE_OPENSSH_KEY_HEADER)) == 0) {
+               key_type = PKT_OPENSSH;
+               begin = map + strlen(PRIVATE_OPENSSH_KEY_HEADER);
+               footer = strstr(map, PRIVATE_OPENSSH_KEY_FOOTER);
+               PARA_INFO_LOG("detected openssh key %s\n", key_file);
+       } else
+               goto unmap;
+       if (!footer)
+               goto unmap;
+       /* skip whitespace at the beginning */
+       for (; begin < footer; begin++) {
+               if (para_isspace(*begin))
+                       continue;
+               break;
+       }
+       ret = -E_KEY_MARKER;
+       if (begin >= footer)
+               goto unmap;
+
+       key_size = footer - begin;
+       key = para_malloc(key_size + 1);
+       for (i = 0, j = 0; begin + i < footer; i++) {
+               if (para_isspace(begin[i]))
+                       continue;
+               key[j++] = begin[i];
+       }
+       key[j] = '\0';
+       ret = base64_decode(key, j, (char **)&blob, blob_size);
+       free(key);
+       if (ret < 0)
+               goto unmap;
+       ret = key_type;
+unmap:
+       ret2 = para_munmap(map, map_size);
+       if (ret >= 0 && ret2 < 0)
+               ret = ret2;
+       if (ret < 0) {
+               free(blob);
+               blob = NULL;
+       }
+out:
+       *result = blob;
+       return ret;
+}
+
index 496895a..55f189a 100644 (file)
@@ -87,6 +87,7 @@ static void dccp_shutdown(void)
 {
        dccp_shutdown_clients();
        generic_acl_deplete(&dss->acl);
+       free_sender_status(dss);
 }
 
 /** * Obtain current MPS according to RFC 4340, sec. 14. */
diff --git a/error.h b/error.h
index 14e5b07..fe44ff5 100644 (file)
--- a/error.h
+++ b/error.h
@@ -83,6 +83,7 @@
        PARA_ERROR(EMPTY, "file is empty"), \
        PARA_ERROR(ENCRYPT, "encrypt error"), \
        PARA_ERROR(EOF, "end of file"), \
+       PARA_ERROR(EOP, "end of playlist"), \
        PARA_ERROR(FEC_BAD_IDX, "invalid index vector"), \
        PARA_ERROR(FECDEC_EOF, "received eof packet"), \
        PARA_ERROR(FECDEC_OVERRUN, "fecdec output buffer overrun"), \
        PARA_ERROR(OGG_PACKET_IN, "ogg_stream_packetin() failed"), \
        PARA_ERROR(OGG_STREAM_FLUSH, "ogg_stream_flush() failed"), \
        PARA_ERROR(OGG_SYNC, "internal ogg storage overflow"), \
+       PARA_ERROR(OPENSSH_PARSE, "could not parse openssh private key"), \
        PARA_ERROR(OPUS_COMMENT, "invalid or corrupted opus comment"), \
        PARA_ERROR(OPUS_DECODE, "opus decode error"), \
        PARA_ERROR(OPUS_HEADER, "invalid opus header"), \
index 694c0ad..dbe4900 100644 (file)
--- a/gcrypt.c
+++ b/gcrypt.c
@@ -106,66 +106,6 @@ static const char *gcrypt_strerror(gcry_error_t gret)
        return gcry_strerror(gcry_err_code(gret));
 }
 
-static int decode_key(const char *key_file, const char *header_str,
-               const char *footer_str, unsigned char **result)
-{
-       int ret, ret2, i, j;
-       void *map;
-       size_t map_size, key_size, blob_size;
-       unsigned char *blob = NULL;
-       char *begin, *footer, *key;
-
-       ret = mmap_full_file(key_file, O_RDONLY, &map, &map_size, NULL);
-       if (ret < 0)
-               goto out;
-       ret = -E_KEY_MARKER;
-       if (strncmp(map, header_str, strlen(header_str)))
-               goto unmap;
-       footer = strstr(map, footer_str);
-       ret = -E_KEY_MARKER;
-       if (!footer)
-               goto unmap;
-       begin = map + strlen(header_str);
-       /* skip whitespace at the beginning */
-       for (; begin < footer; begin++) {
-               if (para_isspace(*begin))
-                       continue;
-               break;
-       }
-       ret = -E_KEY_MARKER;
-       if (begin >= footer)
-               goto unmap;
-
-       key_size = footer - begin;
-       key = para_malloc(key_size + 1);
-       for (i = 0, j = 0; begin + i < footer; i++) {
-               if (para_isspace(begin[i]))
-                       continue;
-               key[j++] = begin[i];
-       }
-       key[j] = '\0';
-       ret = base64_decode(key, j, (char **)&blob, &blob_size);
-       free(key);
-       if (ret < 0)
-               goto free_unmap;
-       ret = blob_size;
-       goto unmap;
-free_unmap:
-       free(blob);
-       blob = NULL;
-unmap:
-       ret2 = para_munmap(map, map_size);
-       if (ret >= 0 && ret2 < 0)
-               ret = ret2;
-       if (ret < 0) {
-               free(blob);
-               blob = NULL;
-       }
-out:
-       *result = blob;
-       return ret;
-}
-
 /** ASN Types and their code. */
 enum asn1_types {
        /** The next object is an integer. */
@@ -207,11 +147,11 @@ static inline int get_long_form_num_length_bytes(unsigned char c)
 
 /*
  * Returns: Number of bytes scanned. This may differ from the value returned via
- * bn_bytes because the latter does not include the ASN.1 prefix and a leading
- * zero is not considered as an additional byte for bn_bytes.
+ * bitsp because the latter does not include the ASN.1 prefix and a leading
+ * zero is not considered as an additional byte for the number of bits.
  */
-static int read_bignum(unsigned char *start, unsigned char *end, gcry_mpi_t *bn,
-               int *bn_bytes)
+static int read_pem_bignum(unsigned char *start, unsigned char *end, gcry_mpi_t *bn,
+               unsigned *bitsp)
 {
        int i, bn_size;
        gcry_error_t gret;
@@ -249,8 +189,8 @@ static int read_bignum(unsigned char *start, unsigned char *end, gcry_mpi_t *bn,
                cp++;
                bn_size--;
        }
-       if (bn_bytes)
-               *bn_bytes = bn_size;
+       if (bitsp)
+               *bitsp = bn_size * 8;
        cp += bn_size;
 //     unsigned char *buf;
 //     gcry_mpi_aprint(GCRYMPI_FMT_HEX, &buf, NULL, *bn);
@@ -258,7 +198,55 @@ static int read_bignum(unsigned char *start, unsigned char *end, gcry_mpi_t *bn,
        return cp - start;
 }
 
-static int find_privkey_bignum_offset(const unsigned char *data, int len)
+struct rsa_params {
+       gcry_mpi_t n, e, d, p, q, u;
+};
+
+static int read_pem_rsa_params(unsigned char *start, unsigned char *end,
+               struct rsa_params *p)
+{
+       unsigned char *cp = start;
+       unsigned bits;
+       int ret;
+
+       ret = read_pem_bignum(cp, end, &p->n, &bits);
+       if (ret < 0)
+               return ret;
+       cp += ret;
+       ret = read_pem_bignum(cp, end, &p->e, NULL);
+       if (ret < 0)
+               goto release_n;
+       cp += ret;
+       ret = read_pem_bignum(cp, end, &p->d, NULL);
+       if (ret < 0)
+               goto release_e;
+       cp += ret;
+       ret = read_pem_bignum(cp, end, &p->p, NULL);
+       if (ret < 0)
+               goto release_d;
+       cp += ret;
+       ret = read_pem_bignum(cp, end, &p->q, NULL);
+       if (ret < 0)
+               goto release_p;
+       cp += ret;
+       ret = read_pem_bignum(cp, end, &p->u, NULL);
+       if (ret < 0)
+               goto release_q;
+       return bits;
+release_q:
+       gcry_mpi_release(p->q);
+release_p:
+       gcry_mpi_release(p->p);
+release_d:
+       gcry_mpi_release(p->d);
+release_e:
+       gcry_mpi_release(p->e);
+release_n:
+       gcry_mpi_release(p->n);
+       return ret;
+}
+
+static int find_pem_bignum_offset(const unsigned char *data, int len)
 {
        const unsigned char *p = data, *end = data + len;
 
@@ -290,96 +278,131 @@ static int find_privkey_bignum_offset(const unsigned char *data, int len)
        return p - data;
 }
 
-/** Private keys start with this header. */
-#define PRIVATE_KEY_HEADER "-----BEGIN RSA PRIVATE KEY-----"
-/** Private keys end with this footer. */
-#define PRIVATE_KEY_FOOTER "-----END RSA PRIVATE KEY-----"
-
-static int get_private_key(const char *key_file, struct asymmetric_key **result)
+static int read_openssh_bignum(unsigned char *start, unsigned char *end,
+               gcry_mpi_t *bn, unsigned *bitsp)
 {
-       gcry_mpi_t n = NULL, e = NULL, d = NULL, p = NULL, q = NULL,
-               u = NULL;
-       unsigned char *blob, *cp, *end;
-       int blob_size, ret, n_size;
        gcry_error_t gret;
-       size_t erroff;
-       gcry_sexp_t sexp;
-       struct asymmetric_key *key;
+       size_t nscanned;
+       unsigned bits;
 
-       *result = NULL;
-       ret = decode_key(key_file, PRIVATE_KEY_HEADER, PRIVATE_KEY_FOOTER,
-               &blob);
-       if (ret < 0)
-               return ret;
-       blob_size = ret;
-       end = blob + blob_size;
-       ret = find_privkey_bignum_offset(blob, blob_size);
-       if (ret < 0)
-               goto free_blob;
-       PARA_INFO_LOG("reading RSA params at offset %d\n", ret);
-       cp = blob + ret;
+       gret = gcry_mpi_scan(bn, GCRYMPI_FMT_SSH, start, end - start, &nscanned);
+       if (gret) {
+               PARA_ERROR_LOG("gcry_mpi_scan: %s\n",
+                       gcry_strerror(gcry_err_code(gret)));
+               return -E_MPI_SCAN;
+       }
+       bits = (nscanned - 4 - (start[4] == '\0')) * 8;
+       if (bitsp)
+               *bitsp = bits;
+       PARA_DEBUG_LOG("scanned %u-bit bignum\n", bits);
+       return nscanned;
+}
 
-       ret = read_bignum(cp, end, &n, &n_size);
+static int read_openssh_rsa_params(unsigned char *start, unsigned char *end,
+               struct rsa_params *p)
+{
+       unsigned char *cp = start;
+       unsigned bits;
+       int ret;
+
+       ret = read_openssh_bignum(cp, end, &p->n, &bits);
        if (ret < 0)
-               goto free_blob;
+               return ret;
        cp += ret;
-
-       ret = read_bignum(cp, end, &e, NULL);
+       ret = read_openssh_bignum(cp, end, &p->e, NULL);
        if (ret < 0)
                goto release_n;
        cp += ret;
-
-       ret = read_bignum(cp, end, &d, NULL);
+       ret = read_openssh_bignum(cp, end, &p->d, NULL);
        if (ret < 0)
                goto release_e;
        cp += ret;
-
-       ret = read_bignum(cp, end, &p, NULL);
+       ret = read_openssh_bignum(cp, end, &p->u, NULL);
        if (ret < 0)
                goto release_d;
        cp += ret;
-
-       ret = read_bignum(cp, end, &q, NULL);
+       ret = read_openssh_bignum(cp, end, &p->p, NULL);
        if (ret < 0)
-               goto release_p;
+               goto release_u;
        cp += ret;
-       ret = read_bignum(cp, end, &u, NULL);
+       ret = read_openssh_bignum(cp, end, &p->q, NULL);
        if (ret < 0)
-               goto release_q;
+               goto release_p;
+       return bits;
+release_p:
+       gcry_mpi_release(p->p);
+release_u:
+       gcry_mpi_release(p->u);
+release_d:
+       gcry_mpi_release(p->d);
+release_e:
+       gcry_mpi_release(p->e);
+release_n:
+       gcry_mpi_release(p->n);
+       return ret;
+}
+
+static int get_private_key(const char *key_file, struct asymmetric_key **result)
+{
+       struct rsa_params params;
+       unsigned char *blob, *end;
+       unsigned bits;
+       int ret, key_type;
+       gcry_error_t gret;
+       size_t erroff, blob_size;
+       gcry_sexp_t sexp;
+       struct asymmetric_key *key;
+
+       *result = NULL;
+       ret = decode_private_key(key_file, &blob, &blob_size);
+       if (ret < 0)
+               return ret;
+       key_type = ret;
+       end = blob + blob_size;
+       if (key_type == PKT_PEM)
+               ret = find_pem_bignum_offset(blob, blob_size);
+       else
+               ret = find_openssh_bignum_offset(blob, blob_size);
+       if (ret < 0)
+               goto free_blob;
+       PARA_INFO_LOG("reading RSA params at offset %d\n", ret);
+       if (key_type == PKT_PEM)
+               ret = read_pem_rsa_params(blob + ret, end, &params);
+       else
+               ret = read_openssh_rsa_params(blob + ret, end, &params);
+       if (ret < 0)
+               goto free_blob;
+       bits = ret;
        /*
         * OpenSSL uses slightly different parameters than gcrypt. To use these
         * parameters we need to swap the values of p and q and recompute u.
         */
-       if (gcry_mpi_cmp(pq) > 0) {
-               gcry_mpi_swap(pq);
-               gcry_mpi_invm(u, p, q);
+       if (gcry_mpi_cmp(params.p, params.q) > 0) {
+               gcry_mpi_swap(params.p, params.q);
+               gcry_mpi_invm(params.u, params.p, params.q);
        }
-       gret = gcry_sexp_build(&sexp, &erroff, RSA_PRIVKEY_SEXP,
-               n, e, d, p, q, u);
+       gret = gcry_sexp_build(&sexp, &erroff, RSA_PRIVKEY_SEXP, params.n,
+               params.e, params.d, params.p, params.q, params.u);
 
        if (gret) {
                PARA_ERROR_LOG("offset %zu: %s\n", erroff,
                        gcry_strerror(gcry_err_code(gret)));
                ret = -E_SEXP_BUILD;
-               goto release_u;
+               goto free_params;
        }
        key = para_malloc(sizeof(*key));
        key->sexp = sexp;
        *result = key;
-       ret = n_size * 8;
+       ret = bits;
        PARA_INFO_LOG("succesfully read %d bit private key\n", ret);
-release_u:
-       gcry_mpi_release(u);
-release_q:
-       gcry_mpi_release(q);
-release_p:
-       gcry_mpi_release(p);
-release_d:
-       gcry_mpi_release(d);
-release_e:
-       gcry_mpi_release(e);
-release_n:
-       gcry_mpi_release(n);
+free_params:
+       gcry_mpi_release(params.n);
+       gcry_mpi_release(params.e);
+       gcry_mpi_release(params.d);
+       gcry_mpi_release(params.u);
+       gcry_mpi_release(params.p);
+       gcry_mpi_release(params.q);
+
 free_blob:
        free(blob);
        return ret;
@@ -390,34 +413,25 @@ int apc_get_pubkey(const char *key_file, struct asymmetric_key **result)
        unsigned char *blob, *p, *end;
        int ret;
        gcry_error_t gret;
-       size_t nr_scanned, erroff, decoded_size;
+       size_t erroff, decoded_size;
        gcry_mpi_t e, n;
        gcry_sexp_t sexp;
        struct asymmetric_key *key;
+       unsigned bits;
 
-       ret = decode_ssh_key(key_file, &blob, &decoded_size);
+       ret = decode_public_key(key_file, &blob, &decoded_size);
        if (ret < 0)
                return ret;
        p = blob + ret;
        end = blob + decoded_size;
        PARA_DEBUG_LOG("scanning modulus and public exponent\n");
-       gret = gcry_mpi_scan(&e, GCRYMPI_FMT_SSH, p, end - p, &nr_scanned);
-       if (gret) {
-               ret = -E_MPI_SCAN;
-               PARA_CRIT_LOG("%s\n", gcry_strerror(gcry_err_code(gret)));
+       ret = read_openssh_bignum(p, end, &e, NULL);
+       if (ret < 0)
                goto free_blob;
-       }
-       PARA_DEBUG_LOG("scanned e (%zu bytes)\n", nr_scanned);
-       p += nr_scanned;
-       if (p >= end)
-               goto release_e;
-       gret = gcry_mpi_scan(&n, GCRYMPI_FMT_SSH, p, end - p, &nr_scanned);
-       if (gret) {
-               ret = -E_MPI_SCAN;
-               PARA_ERROR_LOG("%s\n", gcry_strerror(gcry_err_code(gret)));
+       p += ret;
+       ret = read_openssh_bignum(p, end, &n, &bits);
+       if (ret < 0)
                goto release_e;
-       }
-       PARA_DEBUG_LOG("scanned n (%zu bytes)\n", nr_scanned);
        gret = gcry_sexp_build(&sexp, &erroff, RSA_PUBKEY_SEXP, n, e);
        if (gret) {
                PARA_ERROR_LOG("offset %zu: %s\n", erroff,
@@ -425,12 +439,12 @@ int apc_get_pubkey(const char *key_file, struct asymmetric_key **result)
                ret = -E_SEXP_BUILD;
                goto release_n;
        }
-       ret = ROUND_DOWN(nr_scanned, 32);
-       PARA_INFO_LOG("successfully read %d bit ssh public key\n", ret * 8);
+       PARA_INFO_LOG("successfully read %u bit ssh public key\n", bits);
        key = para_malloc(sizeof(*key));
        key->num_bytes = ret;
        key->sexp = sexp;
        *result = key;
+       ret = bits;
 release_n:
        gcry_mpi_release(n);
 release_e:
index 330b45a..c6b9dec 100644 (file)
@@ -80,6 +80,7 @@ static void http_shutdown(void)
 {
        http_shutdown_clients();
        generic_acl_deplete(&hss->acl);
+       free_sender_status(hss);
 }
 
 static int queue_chunk_or_shutdown(struct sender_client *sc,
index 0daad19..030ab7d 100644 (file)
@@ -11,7 +11,9 @@
                SAMPLE_FORMAT_S16_LE = "S16_LE",
                SAMPLE_FORMAT_S16_BE = "S16_BE",
                SAMPLE_FORMAT_U16_LE = "U16_LE",
-               SAMPLE_FORMAT_U16_BE = "U16_BE"
+               SAMPLE_FORMAT_U16_BE = "U16_BE",
+               SAMPLE_FORMAT_FLOAT_LE = "FLOAT_LE",
+               SAMPLE_FORMAT_FLOAT_BE = "FLOAT_BE"
        }
        default_val = S16_LE
        [help]
index 57a9377..f2220f1 100644 (file)
@@ -26,6 +26,16 @@ version-string = GIT_VERSION()
        [option randomize]
                short_opt = z
                summary = randomize playlist at startup
+       [option end-of-playlist]
+               summary = what to do after the last file has been played
+               arg_info = required_arg
+               arg_type = string
+               typestr = behaviour
+               values = {
+                       EOP_LOOP = "loop",
+                       EOP_STOP = "stop",
+                       EOP_QUIT = "quit"
+                }
        [option key-map]
                short_opt = k
                summary = map a key to a command
diff --git a/mixer.c b/mixer.c
index efa42b9..eae8929 100644 (file)
--- a/mixer.c
+++ b/mixer.c
@@ -40,7 +40,7 @@ static struct lls_parse_result *lpr, *sub_lpr;
 #define OPT_STRING_VAL(_cmd, _opt) (lls_string_val(0, OPT_RESULT(_cmd, _opt)))
 #define OPT_UINT32_VAL(_cmd, _opt) (lls_uint32_val(0, OPT_RESULT(_cmd, _opt)))
 
-typedef int (*mixer_subcommand_handler_t)(const struct mixer *, struct mixer_handle *);
+typedef int (*mixer_subcommand_handler_t)(const struct mixer *);
 
 #define EXPORT_CMD(_cmd) const mixer_subcommand_handler_t \
        lsg_mixer_com_ ## _cmd ## _user_data = &com_ ## _cmd;
@@ -153,11 +153,37 @@ sleep:
        return ret;
 }
 
-static int com_fade(const struct mixer *m, struct mixer_handle *h)
+static int open_mixer_and_set_channel(const struct mixer *m, struct mixer_handle **h)
+{
+       int ret;
+
+       ret = m->open(OPT_STRING_VAL(PARA_MIXER, MIXER_DEVICE), h);
+       if (ret < 0)
+               return ret;
+       ret = set_channel(m, *h, OPT_STRING_VAL(PARA_MIXER, MIXER_CHANNEL));
+       if (ret == -E_BAD_CHANNEL) {
+               char *channels = m->get_channels(*h);
+               printf("Available channels: %s\n", channels);
+               free(channels);
+       }
+       if (ret < 0)
+               m->close(h);
+       return ret;
+}
+
+static int com_fade(const struct mixer *m)
 {
        uint32_t new_vol = OPT_UINT32_VAL(FADE, FADE_VOL);
        uint32_t fade_time = OPT_UINT32_VAL(FADE, FADE_TIME);
-       return fade(m, h, new_vol, fade_time);
+       struct mixer_handle *h;
+       int ret;
+
+       ret = open_mixer_and_set_channel(m, &h);
+       if (ret < 0)
+               return ret;
+       ret = fade(m, h, new_vol, fade_time);
+       m->close(&h);
+       return ret;
 }
 EXPORT_CMD(fade);
 
@@ -237,7 +263,7 @@ static int set_initial_volume(const struct mixer *m, struct mixer_handle *h)
        return 1;
 }
 
-static int com_sleep(const struct mixer *m, struct mixer_handle *h)
+static int com_sleep(const struct mixer *m)
 {
        time_t t1, wake_time_epoch;
        unsigned int delay;
@@ -252,9 +278,13 @@ static int com_sleep(const struct mixer *m, struct mixer_handle *h)
        int fiv = OPT_UINT32_VAL(SLEEP, FI_VOL);
        int fov = OPT_UINT32_VAL(SLEEP, FO_VOL);
        int32_t hour, min = 0;
-       char *tmp;
-       char *wt = para_strdup(wake_time + (wake_time[0] == '+'));
+       char *tmp, *wt;
+       struct mixer_handle *h;
 
+       ret = open_mixer_and_set_channel(m, &h);
+       if (ret < 0)
+               return ret;
+       wt = para_strdup(wake_time + (wake_time[0] == '+'));
        /* calculate wake time */
        time(&t1);
        tmp = strchr(wt, ':');
@@ -264,13 +294,13 @@ static int com_sleep(const struct mixer *m, struct mixer_handle *h)
                ret = para_atoi32(tmp, &min);
                if (ret < 0) {
                        free(wt);
-                       return ret;
+                       goto close_mixer;
                }
        }
        ret = para_atoi32(wt, &hour);
        free(wt);
        if (ret < 0)
-               return ret;
+               goto close_mixer;
        if (wake_time[0] == '+') { /* relative */
                t1 += hour * 60 * 60 + min * 60;
                tm = localtime(&t1);
@@ -288,30 +318,31 @@ static int com_sleep(const struct mixer *m, struct mixer_handle *h)
        PARA_INFO_LOG("waketime: %d:%02d\n", tm->tm_hour, tm->tm_min);
        client_cmd("stop");
        sleep(1);
-       if (fot && fo_mood) {
+       if (fot && fo_mood && *fo_mood) {
                ret = set_initial_volume(m, h);
                if (ret < 0)
-                       return ret;
+                       goto close_mixer;
                change_afs_mode(fo_mood);
                client_cmd("play");
                ret = set_channel(m, h, OPT_STRING_VAL(PARA_MIXER, MIXER_CHANNEL));
                if (ret < 0)
-                       return ret;
+                       goto close_mixer;
                ret = fade(m, h, fov, fot);
                if (ret < 0)
-                       return ret;
+                       goto close_mixer;
        } else {
                ret = m->set(h, fov);
                if (ret < 0)
-                       return ret;
+                       goto close_mixer;
        }
-       if (sleep_mood) {
+       if (sleep_mood && *sleep_mood) {
                change_afs_mode(sleep_mood);
                if (!fot || !fo_mood) /* currently stopped */
                        client_cmd("play");
-       } else if (fot && fo_mood) /* currently playing */
+       } else if (fot && fo_mood && *fo_mood) /* currently playing */
                client_cmd("stop");
-       if (!fit || !fi_mood) /* nothing to do */
+       m->close(&h);
+       if (!fit || !fi_mood || !*fi_mood) /* nothing to do */
                return 1;
        for (;;) {
                time(&t1);
@@ -324,25 +355,34 @@ static int com_sleep(const struct mixer *m, struct mixer_handle *h)
                sleep(delay);
        }
        change_afs_mode(fi_mood);
-       if (sleep_mood) /* currently playing */
+       if (sleep_mood && *sleep_mood) /* currently playing */
                client_cmd("next");
        else /* currently stopped */
                client_cmd("play");
+       ret = open_mixer_and_set_channel(m, &h);
+       if (ret < 0)
+               return ret;
        ret = fade(m, h, fiv, fit);
-       PARA_INFO_LOG("fade complete, returning\n");
+close_mixer:
+       m->close(&h);
        return ret;
 }
 EXPORT_CMD(sleep);
 
-static int com_snooze(const struct mixer *m, struct mixer_handle *h)
+static int com_snooze(const struct mixer *m)
 {
        int ret, val;
+       struct mixer_handle *h;
 
+       ret = open_mixer_and_set_channel(m, &h);
+       if (ret < 0)
+               return ret;
+       ret = 1;
        if (OPT_UINT32_VAL(SNOOZE, SO_TIME) == 0)
-               return 1;
+               goto close_mixer;
        ret = m->get(h);
        if (ret < 0)
-               return ret;
+               goto close_mixer;
        val = ret;
        if (val < OPT_UINT32_VAL(SNOOZE, SO_VOL))
                ret = m->set(h, OPT_UINT32_VAL(SNOOZE, SO_VOL));
@@ -350,20 +390,35 @@ static int com_snooze(const struct mixer *m, struct mixer_handle *h)
                ret = fade(m, h, OPT_UINT32_VAL(SNOOZE, SO_VOL),
                        OPT_UINT32_VAL(SNOOZE, SO_TIME));
        if (ret < 0)
-               return ret;
+               goto close_mixer;
        client_cmd("pause");
        PARA_NOTICE_LOG("%" PRIu32 " seconds snooze time...\n",
                OPT_UINT32_VAL(SNOOZE, SNOOZE_TIME));
+       m->close(&h);
        sleep(OPT_UINT32_VAL(SNOOZE, SNOOZE_TIME));
        client_cmd("play");
-       return fade(m, h, OPT_UINT32_VAL(SNOOZE, SI_VOL),
+       ret = open_mixer_and_set_channel(m, &h);
+       if (ret < 0)
+               goto close_mixer;
+       ret = fade(m, h, OPT_UINT32_VAL(SNOOZE, SI_VOL),
                OPT_UINT32_VAL(SNOOZE, SI_TIME));
+close_mixer:
+       m->close(&h);
+       return ret;
 }
 EXPORT_CMD(snooze);
 
-static int com_set(const struct mixer *m, struct mixer_handle *h)
+static int com_set(const struct mixer *m)
 {
-       return m->set(h, OPT_UINT32_VAL(SET, VAL));
+       struct mixer_handle *h;
+       int ret;
+
+       ret = open_mixer_and_set_channel(m, &h);
+       if (ret < 0)
+               return ret;
+       ret = m->set(h, OPT_UINT32_VAL(SET, VAL));
+       m->close(&h);
+       return ret;
 }
 EXPORT_CMD(set);
 
@@ -402,9 +457,7 @@ static void show_subcommands(void)
        }
 }
 
-
-static int com_help(__a_unused const struct mixer *m,
-               __a_unused struct mixer_handle *h)
+static int com_help(__a_unused const struct mixer *m)
 {
        const struct lls_command *cmd;
        const struct lls_opt_result *r_l = OPT_RESULT(HELP, LONG);
@@ -487,7 +540,6 @@ int main(int argc, char *argv[])
        char *errctx;
        const char *subcmd;
        const struct mixer *m;
-       struct mixer_handle *h;
        unsigned n;
 
        ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
@@ -515,23 +567,10 @@ int main(int argc, char *argv[])
        if (ret < 0)
                goto free_lpr;
        ret = parse_and_merge_config_file(cmd);
-       if (ret < 0)
-               goto free_lpr;
-       m = get_mixer_or_die();
-       ret = m->open(OPT_STRING_VAL(PARA_MIXER, MIXER_DEVICE), &h);
        if (ret < 0)
                goto free_sub_lpr;
-       ret = set_channel(m, h, OPT_STRING_VAL(PARA_MIXER, MIXER_CHANNEL));
-       if (ret == -E_BAD_CHANNEL) {
-               char *channels = m->get_channels(h);
-               printf("Available channels: %s\n", channels);
-               free(channels);
-       }
-       if (ret < 0)
-               goto close_mixer;
-       ret = (*(mixer_subcommand_handler_t *)(lls_user_data(cmd)))(m, h);
-close_mixer:
-       m->close(&h);
+       m = get_mixer_or_die();
+       ret = (*(mixer_subcommand_handler_t *)(lls_user_data(cmd)))(m);
 free_sub_lpr:
        lls_free_parse_result(sub_lpr, cmd);
 free_lpr:
diff --git a/mood.c b/mood.c
index a63d4d2..919c765 100644 (file)
--- a/mood.c
+++ b/mood.c
@@ -39,10 +39,16 @@ struct afs_statistics {
        int64_t num_played_qd;
        /** Quadratic deviation of last played time. */
        int64_t last_played_qd;
+       /** Correction factor for the num played score. */
+       int64_t num_played_correction;
+       /** Correction factor for the last played score. */
+       int64_t last_played_correction;
+       /** Common divisor of the correction factors. */
+       int64_t normalization_divisor;
        /** Number of admissible files */
        unsigned num;
 };
-static struct afs_statistics statistics;
+static struct afs_statistics statistics = {.normalization_divisor = 1};
 
 /**
  * Each line of the current mood corresponds to a mood_item.
@@ -499,20 +505,78 @@ int mood_check_callback(struct afs_callback_arg *aca)
                check_mood));
 }
 
-static int64_t normalized_value(int64_t x, int64_t n, int64_t sum, int64_t qd)
-{
-       if (!n || !qd)
-               return 0;
-       return 100 * (n * x - sum) / (int64_t)int_sqrt(n) / (int64_t)int_sqrt(qd);
-}
+/*
+ * The normalized num_played and last_played values are defined as
+ *
+ *     nn := -(np - mean_n) / sigma_n and nl := -(lp - mean_l) / sigma_l
+ *
+ *  For a (hypothetical) file with np = 0 and lp = now we thus have
+ *
+ *     nn =  mean_n / sigma_n =: hn > 0
+ *     nl = -(now - mean_l) / sigma_l =: hl < 0
+ *
+ * We design the score function so that both contributions get the same
+ * weight. Define the np and lp score of an arbitrary file as
+ *
+ *     sn := nn * -hl and sl := nl * hn
+ *
+ * Example:
+ *     num_played mean/sigma: 87/14
+ *     last_played mean/sigma: 45/32 days
+ *
+ *     We have hn = 87 / 14 = 6.21 and hl = -45 / 32 = -1.41. Multiplying
+ *     nn of every file with the correction factor 1.41 and nl with
+ *     6.21 makes the weight of the two contributions equal.
+ *
+ * The total score s := sn + sl has the representation
+ *
+ *     s = -cn * (np - mean_n) - cl * (lp - mean_l)
+ *
+ * with positive correction factors
+ *
+ *     cn = (now - mean_l) / (sqrt(ql) * sqrt(qn) / n)
+ *     cl = mean_n / (sqrt(ql) * sqrt(qn) / n)
+ *
+ * where ql and qn are the quadratic deviations stored in the statistics
+ * structure and n is the number of admissible files. To avoid integer
+ * overflows and rounding errors we store the common divisor of the
+ * correction factors separately.
+ */
 
 static long compute_score(struct afs_info *afsi, long mood_score)
 {
-       mood_score -= normalized_value(afsi->num_played, statistics.num,
-               statistics.num_played_sum, statistics.num_played_qd);
-       mood_score -= normalized_value(afsi->last_played, statistics.num,
-               statistics.last_played_sum, statistics.last_played_qd);
-       return mood_score / 3;
+//     int64_t n, sqrt_n, mean_n, mean_l, sigma_n, sigma_l, score_n, score_l;
+       int64_t mean_n, mean_l,score_n, score_l;
+//     struct timeval rnow;
+//     clock_get_realtime(&rnow);
+
+//     n = statistics.num;
+//     sqrt_n = int_sqrt(n);
+       assert(statistics.normalization_divisor > 0);
+       assert(statistics.num > 0);
+       mean_n = statistics.num_played_sum / statistics.num;
+       mean_l = statistics.last_played_sum / statistics.num;
+//     sigma_n = int_sqrt(statistics.num_played_qd) / sqrt_n;
+//     if (sigma_n == 0)
+//             sigma_n = 1;
+//     sigma_l = int_sqrt(statistics.last_played_qd) / sqrt_n;
+//     if (sigma_l == 0)
+//             sigma_l = 1;
+//     score_l = (rnow.tv_sec - mean_l) / sigma_l;
+//     score_n = mean_n / sigma_n;
+
+       score_n = -((int64_t)afsi->num_played - mean_n)
+               * statistics.num_played_correction
+               / statistics.normalization_divisor;
+//     score_n = -(int64_t)100 * (afsi->num_played - mean_n) / sigma_n * (rnow.tv_sec - mean_l) / sigma_l;
+//     PARA_CRIT_LOG("this score nold: %lli\n", score_n);
+       score_l = -((int64_t)afsi->last_played - mean_l)
+               * statistics.last_played_correction
+               / statistics.normalization_divisor;
+//     PARA_CRIT_LOG("this score lnew: %lli\n", score_l);
+//     score_l = -(int64_t)100 * ((int64_t)afsi->last_played - mean_l) / sigma_l * mean_n / sigma_n;
+//     PARA_CRIT_LOG("this score lold: %lli\n", score_l);
+       return (mood_score + score_n + score_l) / 3;
 }
 
 static int add_afs_statistics(const struct osl_row *row)
@@ -556,6 +620,7 @@ static int del_afs_statistics(const struct osl_row *row)
        assert(n);
        if (n == 1) {
                memset(&statistics, 0, sizeof(statistics));
+               statistics.normalization_divisor = 1;
                return 1;
        }
 
@@ -804,15 +869,11 @@ static int mood_update_audio_file(const struct osl_row *aft_row,
        return score_update(aft_row, percent);
 }
 
-static void log_statistics(void)
+/* sse: seconds since epoch. */
+static void log_statistics(int64_t sse)
 {
        unsigned n = statistics.num;
        int mean_days, sigma_days;
-       /*
-        * We can not use the "now" pointer from sched.c here because we are
-        * called before schedule(), which initializes "now".
-        */
-       struct timeval rnow;
 
        assert(current_mood);
        PARA_NOTICE_LOG("loaded mood %s\n", current_mood->name?
@@ -822,13 +883,18 @@ static void log_statistics(void)
                return;
        }
        PARA_NOTICE_LOG("%u admissible files\n", statistics.num);
-       clock_get_realtime(&rnow);
-       mean_days = (rnow.tv_sec - statistics.last_played_sum / n) / 3600 / 24;
+       mean_days = (sse - statistics.last_played_sum / n) / 3600 / 24;
        sigma_days = int_sqrt(statistics.last_played_qd / n) / 3600 / 24;
        PARA_NOTICE_LOG("last_played mean/sigma: %d/%d days\n", mean_days, sigma_days);
-       PARA_NOTICE_LOG("num_played mean/sigma: %llu/%llu\n",
-               (long long unsigned)statistics.num_played_sum / n,
-               (long long unsigned)int_sqrt(statistics.num_played_qd / n));
+       PARA_NOTICE_LOG("num_played mean/sigma: %" PRId64 "/%" PRIu64 "\n",
+               statistics.num_played_sum / n,
+               int_sqrt(statistics.num_played_qd / n));
+       PARA_NOTICE_LOG("num_played correction factor: %" PRId64 "\n",
+               statistics.num_played_correction);
+       PARA_NOTICE_LOG("last_played correction factor: %" PRId64 "\n",
+               statistics.last_played_correction);
+       PARA_NOTICE_LOG("normalization divisor: %" PRId64 "\n",
+               statistics.normalization_divisor);
 }
 
 /**
@@ -841,6 +907,7 @@ void close_current_mood(void)
        destroy_mood(current_mood);
        current_mood = NULL;
        memset(&statistics, 0, sizeof(statistics));
+       statistics.normalization_divisor = 1;
 }
 
 /**
@@ -869,6 +936,11 @@ int change_current_mood(const char *mood_name, char **errmsg)
                .size = 0,
                .array = NULL
        };
+       /*
+        * We can not use the "now" pointer from sched.c here because we are
+        * called before schedule(), which initializes "now".
+        */
+       struct timeval rnow;
 
        if (mood_name) {
                struct mood *m;
@@ -901,6 +973,21 @@ int change_current_mood(const char *mood_name, char **errmsg)
                        *errmsg = make_message("audio file loop failed");
                return ret;
        }
+       /* compute correction factors for score function */
+       statistics.normalization_divisor = int_sqrt(statistics.last_played_qd)
+               * int_sqrt(statistics.num_played_qd) / statistics.num / 100;
+       if (statistics.normalization_divisor == 0)
+               statistics.normalization_divisor = 1;
+       clock_get_realtime(&rnow);
+       statistics.num_played_correction =
+               (int64_t)rnow.tv_sec - statistics.last_played_sum / statistics.num;
+       if (statistics.num_played_correction == 0)
+               statistics.num_played_correction = 1;
+       statistics.last_played_correction =
+               statistics.num_played_sum / statistics.num;
+       if (statistics.last_played_correction == 0)
+               statistics.last_played_correction = 1;
+       log_statistics(rnow.tv_sec);
        for (i = 0; i < statistics.num; i++) {
                struct admissible_file_info *a = aa.array + i;
                ret = add_to_score_table(a->aft_row, a->score);
@@ -911,7 +998,6 @@ int change_current_mood(const char *mood_name, char **errmsg)
                        goto out;
                }
        }
-       log_statistics();
        ret = statistics.num;
 out:
        free(aa.array);
index 6ed73c2..728b25b 100644 (file)
--- a/mp3_afh.c
+++ b/mp3_afh.c
@@ -652,7 +652,7 @@ static int mp3_read_info(unsigned char *map, size_t numbytes, int fd,
        afhi->channels = header_channels(&header);
        afhi->seconds_total = (tv2ms(&total_time) + 500) / 1000;
        tv_divide(afhi->chunks_total, &total_time, &afhi->chunk_tv);
-       PARA_DEBUG_LOG("%" PRIu32 "chunks, each %lums\n", afhi->chunks_total,
+       PARA_DEBUG_LOG("%" PRIu32 " chunks, each %lums\n", afhi->chunks_total,
                tv2ms(&afhi->chunk_tv));
        set_max_chunk_size(afhi);
        ret = mp3_get_id3(map, numbytes, fd, &afhi->tags);
@@ -675,7 +675,7 @@ static int mp3_get_file_info(char *map, size_t numbytes, int fd,
        ret = mp3_read_info((unsigned char *)map, numbytes, fd, afhi);
        if (ret < 0)
                return ret;
-       if (afhi->seconds_total < 2 || !afhi->chunks_total)
+       if (afhi->chunks_total == 0)
                return -E_MP3_INFO;
        return 1;
 }
index 62cde3d..3e36bdd 100644 (file)
@@ -124,8 +124,9 @@ int oac_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
        ogg_sync_state oss;
        ogg_page op;
        char *buf;
-       int ret, i, j, frames_per_chunk, ct_size;
-       long long unsigned num_frames = 0;
+       int ret, i, j, frames_per_chunk, ct_size, prev_pageno = 0;
+       long long unsigned granule_skip = 0, num_frames = 0;
+       int64_t granule = 0, prev_granule = 0;
 
        ogg_sync_init(&oss);
        ret = -E_OGG_SYNC;
@@ -145,8 +146,17 @@ int oac_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
        oss.returned = 0;
        oss.fill = numbytes;
        /* count ogg pages and get duration of the file */
-       for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++)
-               num_frames = ogg_page_granulepos(&op);
+       for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++) {
+               int this_pageno = ogg_page_pageno(&op);
+               int64_t this_granule = ogg_page_granulepos(&op);
+               if (this_granule >= 0)
+                       granule = this_granule;
+               if (i > 0 && this_pageno != prev_pageno + 1) /* hole */
+                       granule_skip += granule - prev_granule;
+               prev_pageno = this_pageno;
+               prev_granule = granule;
+       }
+       num_frames = granule - granule_skip;
        PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
        ret = -E_OGG_EMPTY;
        if (i == 0)
@@ -163,7 +173,7 @@ int oac_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
        oss.returned = afhi->header_len;
        oss.fill = numbytes;
        for (j = 1; ogg_sync_pageseek(&oss, &op) > 0; /* nothing */) {
-               int granule = ogg_page_granulepos(&op);
+               granule = ogg_page_granulepos(&op);
 
                while (granule >= (j + 1) * frames_per_chunk) {
                        j++;
index 4895e17..0ad9d7d 100644 (file)
--- a/openssl.c
+++ b/openssl.c
@@ -57,27 +57,15 @@ void crypt_init(void)
 
 void crypt_shutdown(void)
 {
+#ifdef HAVE_CRYPTO_CLEANUP_ALL_EX_DATA
        CRYPTO_cleanup_all_ex_data();
-}
-
-static int get_private_key(const char *path, RSA **rsa)
-{
-       EVP_PKEY *pkey;
-       BIO *bio = BIO_new(BIO_s_file());
-
-       *rsa = NULL;
-       if (!bio)
-               return -E_PRIVATE_KEY;
-       if (BIO_read_filename(bio, path) <= 0)
-               goto bio_free;
-       pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
-       if (!pkey)
-               goto bio_free;
-       *rsa = EVP_PKEY_get1_RSA(pkey);
-       EVP_PKEY_free(pkey);
-bio_free:
-       BIO_free(bio);
-       return *rsa? RSA_size(*rsa) : -E_PRIVATE_KEY;
+#endif
+#ifdef HAVE_OPENSSL_THREAD_STOP /* openssl-1.1 or later */
+       OPENSSL_thread_stop();
+#else /* openssl-1.0 */
+       ERR_remove_thread_state(NULL);
+#endif
+       EVP_cleanup();
 }
 
 /*
@@ -144,6 +132,152 @@ free_rsa:
        return ret;
 }
 
+static int read_pem_private_key(const char *path, RSA **rsa)
+{
+       EVP_PKEY *pkey;
+       BIO *bio = BIO_new(BIO_s_file());
+
+       *rsa = NULL;
+       if (!bio)
+               return -E_PRIVATE_KEY;
+       if (BIO_read_filename(bio, path) <= 0)
+               goto bio_free;
+       pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
+       if (!pkey)
+               goto bio_free;
+       *rsa = EVP_PKEY_get1_RSA(pkey);
+       EVP_PKEY_free(pkey);
+bio_free:
+       BIO_free(bio);
+       return *rsa? RSA_size(*rsa) : -E_PRIVATE_KEY;
+}
+
+static int read_private_rsa_params(const unsigned char *blob,
+               const unsigned char *end, RSA **result)
+{
+       int ret;
+       RSA *rsa;
+       BN_CTX *ctx;
+       BIGNUM *n, *e, *d, *iqmp, *p, *q; /* stored in the key file */
+       BIGNUM *dmp1, *dmq1; /* these will be computed */
+       BIGNUM *tmp;
+       const unsigned char *cp = blob;
+
+       rsa = RSA_new();
+       if (!rsa)
+               return -E_BIGNUM;
+       ret = -E_BIGNUM;
+       tmp = BN_new();
+       if (!tmp)
+               goto free_rsa;
+       ctx = BN_CTX_new();
+       if (!ctx)
+               goto free_tmp;
+       dmp1 = BN_new();
+       if (!dmp1)
+               goto free_ctx;
+       dmq1 = BN_new();
+       if (!dmq1)
+               goto free_dmp1;
+       ret = read_bignum(cp, end - cp, &n);
+       if (ret < 0)
+               goto free_dmq1;
+       cp += ret;
+       ret = read_bignum(cp, end - cp, &e);
+       if (ret < 0)
+               goto free_n;
+       cp += ret;
+       ret = read_bignum(cp, end - cp, &d);
+       if (ret < 0)
+               goto free_e;
+       cp += ret;
+       ret = read_bignum(cp, end - cp, &iqmp);
+       if (ret < 0)
+               goto free_d;
+       cp += ret;
+       ret = read_bignum(cp, end - cp, &p);
+       if (ret < 0)
+               goto free_iqmp;
+       cp += ret;
+       ret = read_bignum(cp, end - cp, &q);
+       if (ret < 0)
+               goto free_p;
+       ret = -E_BIGNUM;
+       if (!BN_sub(tmp, q, BN_value_one()))
+               goto free_q;
+       if (!BN_mod(dmp1, d, tmp, ctx))
+               goto free_q;
+       if (!BN_sub(tmp, q, BN_value_one()))
+               goto free_q;
+       if (!BN_mod(dmq1, d, tmp, ctx))
+               goto free_q;
+#ifdef HAVE_RSA_SET0_KEY
+       RSA_set0_key(rsa, n, e, d);
+       RSA_set0_factors(rsa, p, q);
+       RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp);
+#else
+       rsa->n = n;
+       rsa->e = e;
+       rsa->d = d;
+       rsa->p = p;
+       rsa->q = q;
+       rsa->dmp1 = dmp1;
+       rsa->dmq1 = dmq1;
+       rsa->iqmp = iqmp;
+#endif
+       *result = rsa;
+       ret = 1;
+       goto free_ctx;
+free_q:
+       BN_clear_free(q);
+free_p:
+       BN_clear_free(p);
+free_iqmp:
+       BN_clear_free(iqmp);
+free_d:
+       BN_clear_free(d);
+free_e:
+       BN_free(e);
+free_n:
+       BN_free(n);
+free_dmq1:
+       BN_clear_free(dmq1);
+free_dmp1:
+       BN_clear_free(dmp1);
+free_ctx:
+       BN_CTX_free(ctx);
+free_tmp:
+       BN_clear_free(tmp);
+free_rsa:
+       if (ret < 0)
+               RSA_free(rsa);
+       return ret;
+}
+
+static int get_private_key(const char *path, RSA **rsa)
+{
+       int ret;
+       unsigned char *blob, *end;
+       size_t blob_size;
+
+       *rsa = NULL;
+       ret = decode_private_key(path, &blob, &blob_size);
+       if (ret < 0)
+               return ret;
+       end = blob + blob_size;
+       if (ret == PKT_OPENSSH) {
+               ret = find_openssh_bignum_offset(blob, blob_size);
+               if (ret < 0)
+                       goto free_blob;
+               PARA_INFO_LOG("reading RSA params at offset %d\n", ret);
+               ret = read_private_rsa_params(blob + ret, end, rsa);
+       } else
+               ret = read_pem_private_key(path, rsa);
+free_blob:
+       free(blob);
+       return ret;
+}
+
 int apc_get_pubkey(const char *key_file, struct asymmetric_key **result)
 {
        unsigned char *blob;
@@ -151,7 +285,7 @@ int apc_get_pubkey(const char *key_file, struct asymmetric_key **result)
        int ret;
        struct asymmetric_key *key = para_malloc(sizeof(*key));
 
-       ret = decode_ssh_key(key_file, &blob, &decoded_size);
+       ret = decode_public_key(key_file, &blob, &decoded_size);
        if (ret < 0)
                goto out;
        ret = read_rsa_bignums(blob + ret, decoded_size - ret, &key->rsa);
diff --git a/para.h b/para.h
index b406818..c745006 100644 (file)
--- a/para.h
+++ b/para.h
@@ -202,6 +202,8 @@ _static_inline_ bool iov_valid(const struct iovec *iov)
        SAMPLE_FORMAT(SF_S16_BE, "16 bit signed, big endian"), \
        SAMPLE_FORMAT(SF_U16_LE, "16 bit unsigned, little endian"), \
        SAMPLE_FORMAT(SF_U16_BE, "16 bit unsigned, big endian"), \
+       SAMPLE_FORMAT(SF_FLOAT_LE, "32 bit float, little endian"), \
+       SAMPLE_FORMAT(SF_FLOAT_BE, "32 bit float, big endian"), \
 
 /** \cond sample_format */
 #define SAMPLE_FORMAT(a, b) a
diff --git a/play.c b/play.c
index 86edc4d..2346c6b 100644 (file)
--- a/play.c
+++ b/play.c
@@ -219,7 +219,6 @@ static long unsigned get_play_time(void)
        return result;
 }
 
-
 static void wipe_receiver_node(void)
 {
        PARA_NOTICE_LOG("cleaning up receiver node\n");
@@ -434,6 +433,15 @@ static int next_valid_file(void)
        int i, j = pt->current_file;
        unsigned num_inputs = lls_num_inputs(play_lpr);
 
+       if (j == num_inputs - 1) {
+               switch (OPT_UINT32_VAL(END_OF_PLAYLIST)) {
+               case EOP_LOOP: break;
+               case EOP_STOP:
+                       pt->playing = false;
+                       return 0;
+               case EOP_QUIT: return -E_EOP;
+               }
+       }
        for (i = 0; i < num_inputs; i++) {
                j = (j + 1) % num_inputs;
                if (!pt->invalid[j])
@@ -1245,7 +1253,6 @@ int main(int argc, char *argv[])
        init_shuffle_map();
        pt->invalid = para_calloc(sizeof(*pt->invalid) * num_inputs);
        pt->rq = CRT_FILE_CHANGE;
-       pt->current_file = num_inputs - 1;
        pt->playing = true;
        pt->task = task_register(&(struct task_info){
                .name = "play",
diff --git a/send.h b/send.h
index 8f7005c..f6aafbb 100644 (file)
--- a/send.h
+++ b/send.h
@@ -212,6 +212,7 @@ void init_sender_status(struct sender_status *ss,
                const struct lls_opt_result *acl_opt_result,
                const struct lls_opt_result *listen_address_opt_result,
                int default_port, int max_clients, int default_deny);
+void free_sender_status(const struct sender_status *ss);
 char *generic_sender_status(struct sender_status *ss, const char *name);
 void generic_com_allow(struct sender_command_data *scd,
                struct sender_status *ss);
index 24b14ab..ea494d9 100644 (file)
@@ -154,6 +154,25 @@ void init_sender_status(struct sender_status *ss,
        ss->default_deny = default_deny;
 }
 
+/**
+ * Deallocate all resources allocated in \ref init_sender_status().
+ *
+ * \param ss The structure whose components should be freed.
+ *
+ * This frees the dynamically allocated parts of the structure which was
+ * initialized by an earlier call to \ref init_sender_status(). It does *not*
+ * call free(ss), though.
+ */
+void free_sender_status(const struct sender_status *ss)
+{
+       int i;
+
+       free(ss->listen_fds);
+       FOR_EACH_LISTEN_FD(i, ss)
+               free(ss->listen_addresses[i]);
+       free(ss->listen_addresses);
+}
+
 /**
  * Return a string containing the current status of a sender.
  *
diff --git a/vss.c b/vss.c
index 73c7231..023b2c1 100644 (file)
--- a/vss.c
+++ b/vss.c
@@ -959,11 +959,12 @@ static void recv_afs_result(struct vss_task *vsst, fd_set *rfds)
        if (ret < 0)
                goto err;
        vsst->afsss = AFS_SOCKET_READY;
-       PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd, afs_code,
-               afs_data);
        ret = -E_NOFD;
-       if (afs_code != NEXT_AUDIO_FILE)
+       if (afs_code != NEXT_AUDIO_FILE) {
+               PARA_ERROR_LOG("afs code: %u, expected: %d\n", afs_code,
+                       NEXT_AUDIO_FILE);
                goto err;
+       }
        if (passed_fd < 0)
                goto err;
        shmid = afs_data;
index f9835a0..56eedff 100644 (file)
@@ -446,7 +446,7 @@ following commands:
 Next, change to the "bar" account on client_host and generate the
 key pair with the commands
 
-       ssh-keygen -q -t rsa -b 2048 -N '' -m PEM
+       ssh-keygen -q -t rsa -b 2048 -N '' -m RFC4716
 
 This generates the two files id_rsa and id_rsa.pub in ~/.ssh.  Note
 that para_server won't accept keys shorter than 2048 bits. Moreover,