aboutsummaryrefslogtreecommitdiff
#include "config.h"

#include <iostream>
#include <cstring>

#include "hash.hh"
#include "archive.hh"
#include "util.hh"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


namespace nix {


Hash::Hash()
{
    type = htUnknown;
    hashSize = 0;
    memset(hash, 0, maxHashSize);
}


Hash::Hash(HashType type)
{
    this->type = type;
    hashSize = gcry_md_get_algo_dlen(type);

    if (hashSize == 0) throw Error("unknown hash type");
    assert(hashSize <= maxHashSize);
    memset(hash, 0, maxHashSize);
}


bool Hash::operator == (const Hash & h2) const
{
    if (hashSize != h2.hashSize) return false;
    for (unsigned int i = 0; i < hashSize; i++)
        if (hash[i] != h2.hash[i]) return false;
    return true;
}


bool Hash::operator != (const Hash & h2) const
{
    return !(*this == h2);
}


bool Hash::operator < (const Hash & h) const
{
    for (unsigned int i = 0; i < hashSize; i++) {
        if (hash[i] < h.hash[i]) return true;
        if (hash[i] > h.hash[i]) return false;
    }
    return false;
}


const string base16Chars = "0123456789abcdef";


string printHash(const Hash & hash)
{
    char buf[hash.hashSize * 2];
    for (unsigned int i = 0; i < hash.hashSize; i++) {
        buf[i * 2] = base16Chars[hash.hash[i] >> 4];
        buf[i * 2 + 1] = base16Chars[hash.hash[i] & 0x0f];
    }
    return string(buf, hash.hashSize * 2);
}


Hash parseHash(HashType ht, const string & s)
{
    Hash hash(ht);
    if (s.length() != hash.hashSize * 2) {
	string algo = gcry_md_algo_name(ht);
        throw Error(format("invalid %1% hash '%2%' (%3% bytes but expected %4%)")
		    % algo % s % (s.length() / 2) % hash.hashSize);
    }
    for (unsigned int i = 0; i < hash.hashSize; i++) {
        string s2(s, i * 2, 2);
        if (!isxdigit(s2[0]) || !isxdigit(s2[1]))
            throw Error(format("invalid hash `%1%'") % s);
        std::istringstream str(s2);
        int n;
        str >> std::hex >> n;
        hash.hash[i] = n;
    }
    return hash;
}


unsigned int hashLength32(const Hash & hash)
{
    return (hash.hashSize * 8 - 1) / 5 + 1;
}


// omitted: E O U T
const string base32Chars = "0123456789abcdfghijklmnpqrsvwxyz";


string printHash32(const Hash & hash)
{
    Hash hash2(hash);
    unsigned int len = hashLength32(hash);

    string s;
    s.reserve(len);

    for (int n = len - 1; n >= 0; n--) {
        unsigned int b = n * 5;
        unsigned int i = b / 8;
        unsigned int j = b % 8;
        unsigned char c =
            (hash.hash[i] >> j)
            | (i >= hash.hashSize - 1 ? 0 : hash.hash[i + 1] << (8 - j));
        s.push_back(base32Chars[c & 0x1f]);
    }

    return s;
}


string printHash16or32(const Hash & hash)
{
    return hash.type == htMD5 ? printHash(hash) : printHash32(hash);
}


Hash parseHash32(HashType ht, const string & s)
{
    Hash hash(ht);
    unsigned int len = hashLength32(ht);
    assert(s.size() == len);

    for (unsigned int n = 0; n < len; ++n) {
        char c = s[len - n - 1];
        unsigned char digit;
        for (digit = 0; digit < base32Chars.size(); ++digit) /* !!! slow */
            if (base32Chars[digit] == c) break;
        if (digit >= 32)
            throw Error(format("invalid base-32 hash '%1%'") % s);
        unsigned int b = n * 5;
        unsigned int i = b / 8;
        unsigned int j = b % 8;
        hash.hash[i] |= digit << j;
        if (i < hash.hashSize - 1) hash.hash[i + 1] |= digit >> (8 - j);
    }

    return hash;
}


Hash parseHash16or32(HashType ht, const string & s)
{
    Hash hash(ht);
    if (s.size() == hash.hashSize * 2)
        /* hexadecimal representation */
        hash = parseHash(ht, s);
    else if (s.size() == hashLength32(hash))
        /* base-32 representation */
        hash = parseHash32(ht, s);
    else
        throw Error(format("hash `%1%' has wrong length for hash type `%2%'")
            % s % printHashType(ht));
    return hash;
}


bool isHash(const string & s)
{
    if (s.length() != 32) return false;
    for (int i = 0; i < 32; i++) {
        char c = s[i];
        if (!((c >= '0' && c <= '9') ||
              (c >= 'a' && c <= 'f')))
            return false;
    }
    return true;
}

/* The "hash context".  */
struct Ctx
{
  /* This copy constructor is needed in 'HashSink::currentHash()' where we
     expect the copy of a 'Ctx' object to yield a truly different context.  */
  Ctx(Ctx &ref)
  {
    if (ref.md_handle == NULL)
      md_handle = NULL;
    else
      gcry_md_copy (&md_handle, ref.md_handle);
  }

  /* Make sure 'md_handle' is always initialized.  */
  Ctx(): md_handle (NULL) { };

  gcry_md_hd_t md_handle;
};


static void start(HashType ht, Ctx & ctx)
{
    gcry_error_t err;

    err = gcry_md_open (&ctx.md_handle, ht, 0);
    assert (err == GPG_ERR_NO_ERROR);
}


static void update(HashType ht, Ctx & ctx,
    const unsigned char * bytes, unsigned int len)
{
    gcry_md_write (ctx.md_handle, bytes, len);
}


static void finish(HashType ht, Ctx & ctx, unsigned char * hash)
{
    memcpy (hash, gcry_md_read (ctx.md_handle, ht),
	    gcry_md_get_algo_dlen (ht));
    gcry_md_close (ctx.md_handle);
    ctx.md_handle = NULL;
}


Hash hashString(HashType ht, const string & s)
{
    Ctx ctx;
    Hash hash(ht);
    start(ht, ctx);
    update(ht, ctx, (const unsigned char *) s.data(), s.length());
    finish(ht, ctx, hash.hash);
    return hash;
}


Hash hashFile(HashType ht, const Path & path)
{
    Ctx ctx;
    Hash hash(ht);
    start(ht, ctx);

    AutoCloseFD fd = open(path.c_str(), O_RDONLY);
    if (fd == -1) throw SysError(format("computing hash of file `%1%'") % path);

    unsigned char buf[8192];
    ssize_t n;
    while ((n = read(fd, buf, sizeof(buf)))) {
        checkInterrupt();
        if (n == -1) throw SysError(format("reading file `%1%'") % path);
        update(ht, ctx, buf, n);
    }

    finish(ht, ctx, hash.hash);
    return hash;
}


HashSink::HashSink(HashType ht) : ht(ht)
{
    ctx = new Ctx;
    bytes = 0;
    start(ht, *ctx);
}

HashSink::~HashSink()
{
    bufPos = 0;
    delete ctx;
}

void HashSink::write(const unsigned char * data, size_t len)
{
    bytes += len;
    update(ht, *ctx, data, len);
}

HashResult HashSink::finish()
{
    flush();
    Hash hash(ht);
    nix::finish(ht, *ctx, hash.hash);
    return HashResult(hash, bytes);
}

HashResult HashSink::currentHash()
{
    flush();
    Ctx ctx2 = *ctx;
    Hash hash(ht);
    nix::finish(ht, ctx2, hash.hash);
    return HashResult(hash, bytes);
}


HashResult hashPath(
    HashType ht, const Path & path, PathFilter & filter)
{
    HashSink sink(ht);
    dumpPath(path, sink, filter);
    return sink.finish();
}


Hash compressHash(const Hash & hash, unsigned int newSize)
{
    Hash h;
    h.hashSize = newSize;
    for (unsigned int i = 0; i < hash.hashSize; ++i)
        h.hash[i % newSize] ^= hash.hash[i];
    return h;
}


HashType parseHashType(const string & s)
{
    if (s == "md5") return htMD5;
    else if (s == "sha1") return htSHA1;
    else if (s == "sha256") return htSHA256;
    else if (s == "sha512") return htSHA512;
    else if (s == "sha3-256") return htSHA3_256;
    else if (s == "sha3-512") return htSHA3_512;
    else if (s == "blake2s-256") return htBLAKE2s_256;
    else return htUnknown;
}


string printHashType(HashType ht)
{
    if (ht == htMD5) return "md5";
    else if (ht == htSHA1) return "sha1";
    else if (ht == htSHA256) return "sha256";
    else if (ht == htSHA512) return "sha512";
    else if (ht == htSHA3_256) return "sha3-256";
    else if (ht == htSHA3_512) return "sha3-512";
    else if (ht == htBLAKE2s_256) return "blake2s-256";
    else throw Error("cannot print unknown hash type");
}


}
avail'>...* gnu/services/mail.scm (unix-listener-configuration)[path] (fifo-listener-configuration)[path]: Change type from 'file-name' to 'string'. * doc/guix.texi (Mail Services): Document it. Signed-off-by: Clément Lassieur <clement@lassieur.org> Clément Lassieur 2017-03-10services: Add exim-service-type....* gnu/services/mail.scm (<exim-configuration>): New record type. (exim-computed-config-file, exim-shepherd-service, exim-activation, exim-etc, exim-profile): New procedures. (exim-service-type, %exim-accounts): New variables. * doc/guix.text (Mail Services): Document it. Signed-off-by: Ludovic Courtès <ludo@gnu.org> Carlo Zancanaro 2017-01-25services: Fix 'mkdir-p' in activation scripts....* gnu/services/cups.scm (%cups-activation): Import (guix build utils). * gnu/services/mail.scm (opensmtpd-activation): Idem. * gnu/services/networking.scm (ntp-service-activation): Idem. * gnu/services/spice.scm (spice-vdagent-activation): Idem. * gnu/services/ssh.scm (openssh-activation): Idem. (dropbear-activation): Idem. * gnu/services/vpn.scm (%openvpn-activation): Idem. Signed-off-by: Ludovic Courtès <ludo@gnu.org> Clément Lassieur 2016-11-26services: Factorize configuration abstraction....* gnu/services/mail.scm and gnu/services/cups.scm (&configuration-error) (configuration-error, configuration-field-error) (configuration-missing-field, configuration-field, serialize-configuration) (validate-configuration, define-configuration, uglify-field-name) (serialize-field, serialize-package, serialize-string) (serialize-space-separated-string-list, space-separated-string-list?) (serialize-file-name, file-name?, serialize-field-name) (generate-documentation): Move duplicate code... * gnu/services/configuration.scm: ...to this new file. * gnu/local.mk (GNU_SYSTEM_MODULES): Add configuration.scm. Signed-off-by: Ludovic Courtès <ludo@gnu.org> Julien Lepiller 2016-11-26services: Add opensmtpd service....* gnu/services/mail.scm (<opensmtpd-configuration>): New record type. (%default-opensmtpd-config-file, %opensmtpd-accounts): New variables. (opensmtpd-shepherd-service, opensmtpd-activation): New procedures. (opensmtpd-service-type): New variable. * doc/guix.texi (Mail Services): Document it. 宋文武 2016-09-27gnu: New default Dovecot service postmaster_address...* gnu/services/mail.scm (dovecot-configuration): Change default for postmaster-address, as dovecot is now requiring a non-empty value and will fail to start up otherwise. * doc/guix.texi (Mail Services): Update. Andy Wingo 2016-09-16doc: "filesystem" -> "file system"...* doc/guix.texi: "filesystem" -> "file system" * gnu/packages/admin.scm: "filesystem" -> "file system" * gnu/packages/cdrom.scm: "filesystem" -> "file system" * gnu/packages/compression.scm: "filesystem" -> "file system" * gnu/packages/disk.scm: "filesystem" -> "file system" * gnu/packages/gnome.scm: "filesystem" -> "file system" * gnu/packages/irc.scm: "filesystem" -> "file system" * gnu/packages/linux.scm: "filesystem" -> "file system" * gnu/packages/mail.scm: "filesystem" -> "file system" * gnu/packages/mpd.scm: "filesystem" -> "file system" * gnu/packages/ocaml.scm: "filesystem" -> "file system" * gnu/packages/perl.scm: "filesystem" -> "file system" * gnu/packages/python.scm: "filesystem" -> "file system" * gnu/packages/search.scm: "filesystem" -> "file system" * gnu/packages/tls.scm: "filesystem" -> "file system" * gnu/services/mail.scm: "filesystem" -> "file system" John Darrington 2016-09-10system: Use 'file-append' to denote file names....* gnu/services/avahi.scm, gnu/services/base.scm, gnu/services/databases.scm, gnu/services/dbus.scm, gnu/services/desktop.scm, gnu/services/dict.scm, gnu/services/mail.scm, gnu/services/networking.scm, gnu/services/sddm.scm, gnu/services/spice.scm, gnu/services/ssh.scm, gnu/services/web.scm, gnu/services/xorg.scm, gnu/system.scm: Replace the #~(string-append #$pkg "/bin/foo") idiom with (file-append pkg "/bin/foo"). Ludovic Courtès 2016-07-21services: Export *-service-type and *-configuration....This allows users to use 'modify-services' and similar constructs for all these service types. * gnu/services/avahi.scm: export avahi-configuration. * gnu/services/base.scm: export gpm-configuration and rngd-configuration. * gnu/services/databases.scm: export *-service-type and *-configuration. * gnu/services/dbus.scm: export dbus-configuration. * gnu/services/dict.scm: export dicod-service-type. * gnu/services/lirc.scm: export lirc-configuration and lirc-service-type. * gnu/services/mail.scm: export dovecot-service-type. * gnu/services/web.scm: export nginx-configuration and nginx-service-type. * gnu/services/xorg.scm: export screen-locker and screen-locker?. * gnu/services/ssh.scm: export lsh-configuration and lsh-service-type. * gnu/services/desktop.scm: export *-service, *-service-type and *-configuration. * gnu/services/networking.scm: export *-configuration and *-service-type. Co-authored-by: Ludovic Courtès <ludo@gnu.org> Tomáš Čech 2016-01-29services: Rename 'dmd' services to 'shepherd'....* gnu/services/shepherd.scm (dmd-root-service-type, %dmd-root-service) (dmd-service-type, <dmd-service>, dmd-service, dmd-service?) (make-dmd-service, dmd-service-documentation, dmd-service-provision) (dmd-service-requirement, dmd-service-respawn, dmd-service-start) (dmd-service-stop, dmd-service-auto-start?, dmd-service-modules) (dmd-service-imported-modules, dmd-service-file-name, dmd-service-file) (dmd-service-back-edges): Rename to... (shepherd-root-service-type, %shepherd-root-service, shepherd-service-type) (<shepherd-service>, shepherd-service, shepherd-service?) (make-shepherd-service, shepherd-service-documentation) (shepherd-service-provision, shepherd-service-requirement) (shepherd-service-respawn, shepherd-service-start) (shepherd-service-stop, shepherd-service-auto-start?) (shepherd-service-modules, shepherd-service-imported-modules) (shepherd-service-file-name, shepherd-service-file) (shepherd-service-back-edges): ...this * gnu/services.scm: Adjust comments. * gnu/services/avahi.scm (avahi-dmd-service): Rename to... (avahi-shepherd-service): ... this. * gnu/services/base.scm (%root-file-system-dmd-service) (file-system->dmd-service-name, mapped-device->dmd-service-name) (dependency->dmd-service-name, file-system-dmd-service) (mingetty-dmd-service, nscd-dmd-service, guix-dmd-service) (guix-publish-dmd-service, udev-dmd-service, gpm-dmd-service): Rename to... (%root-file-system-shepherd-service) (file-system->shepherd-service-name, mapped-device->shepherd-service-name) (dependency->shepherd-service-name, file-system-shepherd-service) (mingetty-shepherd-service, nscd-shepherd-service, guix-shepherd-service) (guix-publish-shepherd-service, udev-shepherd-service) (gpm-shepherd-service): ... this. * gnu/services/databases.scm (postgresql-dmd-service): Rename to... (postgresql-shepherd-service): ... this. * gnu/services/desktop.scm (upower-dmd-service, elogind-dmd-service): Rename to... (upower-shepherd-service, elogind-shepherd-service): ... this. * gnu/services/dbus.scm (dbus-dmd-service): Rename to... (dbus-shepherd-service): ... this. * gnu/services/lirc.scm (lirc-dmd-service): Rename to... (lirc-shepherd-service): ... this. * gnu/services/mail.scm (dovecot-dmd-service): Rename to... (dovecot-shepherd-service): ... this. * gnu/services/networking.scm (ntp-dmd-service, tor-dmd-service) (bitlbee-dmd-service, wicd-dmd-service, network-manager-dmd-service): Rename to... (dbus-shepherd-service): ... this. * gnu/services/ssh.scm (lsh-dmd-service): Rename to... (lsh-shepherd-service): ... this. * gnu/services/web.scm (nginx-dmd-service): Rename to... (nginx-shepherd-service): ... this. * gnu/services/xorg.scm (slim-dmd-service): Rename to... (slim-shepherd-service): ... this. * gnu/system.scm (essential-services): Use '%shepherd-root-service'. * gnu/system/install.scm (cow-store-service-type): Adjust accordingly. * guix/scripts/system.scm (dmd-service-node-label, dmd-service-node-type) (export-dmd-graph): Likewise. * tests/guix-system.sh: Likewise. * tests/services.scm ("dmd-service-back-edges"): Rename to... ("shepherd-service-back-edges"): Adjust accordingly. * doc/guix.texi: Likewise. * doc/images/service-graph.dot: Use 'shepherd' service name. Alex Kost 2016-01-29Rename (gnu services dmd) to (gnu services shepherd)....* gnu/services/dmd.scm: Rename to... * gnu/services/shepherd.scm: ... this. * gnu/system.scm: Use it. * gnu/system/install.scm: Likewise. * gnu/services/xorg.scm: Likewise. * gnu/services/web.scm: Likewise. * gnu/services/ssh.scm: Likewise. * gnu/services/networking.scm: Likewise. * gnu/services/mail.scm: Likewise. * gnu/services/lirc.scm: Likewise. * gnu/services/desktop.scm: Likewise. * gnu/services/dbus.scm: Likewise. * gnu/services/databases.scm: Likewise. * gnu/services/base.scm: Likewise. * gnu/services/avahi.scm: Likewise. * guix/scripts/system.scm: Likewise. * tests/services.scm: Likewise. * tests/guix-system.sh: Likewise. * doc/guix.texi (Shepherd Services): Adjust accordingly. * gnu-system.am (GNU_SYSTEM_MODULES): Likewise. * po/guix/POTFILES.in: Likewise. Alex Kost 2015-12-18gnu: Add dovecot service...* gnu/services/mail.scm: New file. (&dovecot-configuration-error, dovecot-configuration-error?) (dovecot-service, dovecot-configuration, dict-configuration) (passdb-configuration, userdb-configuration) (unix-listener-configuration, fifo-listener-configuration) (inet-listener-configuration, service-configuration) (protocol-configuration, plugin-configuration, mailbox-configuration) (namespace-configuration, opaque-dovecot-configuration): New public variables. * gnu-system.am (GNU_SYSTEM_MODULES): Add (gnu services mail). * doc/guix.texi (Mail Services): New node. Andy Wingo