Exemplo n.º 1
0
int main(int argc, char *argv[])
{
    Hashmap *packages;

    if (!(packages = readPackages(PACKAGES)))
        return -1;

    //hashmapForEach(packages, dumpPackage, NULL);
    printf("Found packages: %u\n", (int) hashmapSize(packages));

    initUsers();

    if (argc == 2)
    {
        APK *package = (APK *) hashmapGet(packages, argv[1]);
        if (package)
            checkPackage(NULL, package, NULL);
        else
            printf("Package %s not found\n", argv[1]);
    }
    else
        hashmapForEach(packages, checkPackage, NULL);

    hashmapForEach(packages, freePackage, NULL);
    hashmapFree(packages);
    return 0;
}
Exemplo n.º 2
0
/**
 * Called when the peer dies.
 */
static void peerProxyKill(PeerProxy* peerProxy, bool errnoIsSet) {
    if (errnoIsSet) {
        ALOGI("Peer %d died. errno: %s", peerProxy->credentials.pid, 
                strerror(errno));
    } else {
        ALOGI("Peer %d died.", peerProxy->credentials.pid);
    }
    
    // If we lost the master, we're up a creek. We can't let this happen.
    if (peerProxy->master) {    
        LOG_ALWAYS_FATAL("Lost connection to master.");
    }

    Peer* localPeer = peerProxy->peer;
    pid_t pid = peerProxy->credentials.pid;
    
    peerLock(localPeer);
    
    // Remember for awhile that the peer died.
    localPeer->deadPeers[localPeer->deadPeerCursor] 
        = peerProxy->credentials.pid;
    localPeer->deadPeerCursor++;
    if (localPeer->deadPeerCursor == PEER_HISTORY) {
        localPeer->deadPeerCursor = 0;
    }
  
    // Remove from peer map.
    hashmapRemove(localPeer->peerProxies, &pid);
    
    // External threads can no longer get to this peer proxy, so we don't 
    // need the lock anymore.
    peerUnlock(localPeer);
    
    // Remove the fd from the selector.
    if (peerProxy->fd != NULL) {
        peerProxy->fd->remove = true;
    }

    // Clear outgoing packet queue.
    while (peerProxyNextPacket(peerProxy)) {}

    bufferFree(peerProxy->inputBuffer);

    // This only applies to the master.
    if (peerProxy->connections != NULL) {
        // We can't leave these other maps pointing to freed memory.
        hashmapForEach(peerProxy->connections, &peerProxyRemoveConnection, 
                peerProxy);
        hashmapFree(peerProxy->connections);
    }

    // Invoke death listener.
    localPeer->onDeath(pid);

    // Free the peer proxy itself.
    free(peerProxy);
}
Exemplo n.º 3
0
void ttref_print_activity_index(CharBuffer *b, Hashmap *m) {
    struct ctx ctx;
    ctx.b = b;
    ctx.sep = false;
    hashmapForEach(m, append, &ctx);
}
Exemplo n.º 4
0
void imagerenderer_freeImages(struct image_renderers *ir) {
    hashmapForEach(ir->images, callback, (void *) ir);
}
Exemplo n.º 5
0
void massdns_scan(lookup_context_t *context)
{
    memset(&stats, 0, sizeof(dns_stats_t));
    size_t line_buflen = 4096;
    char *line = safe_malloc(line_buflen);
    size_t line_len = 0;
    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = 0;
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    int socketbuf = 1024 * 1024 * 100;
    if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &socketbuf, sizeof(socketbuf)) != 0)
    {
        perror("Failed to adjust socket send buffer size.");
    }
    if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &socketbuf, sizeof(socketbuf)) != 0)
    {
        perror("Failed to adjust socket receive buffer size.");
    }
    bind(sock, (sockaddr_t *) &server_addr, sizeof(server_addr));

    fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);

    if (sock < 0)
    {
        perror("Failed to create socket");
        exit(1);
    }
    FILE *f;
    if(context->cmd_args.show_progress)
    {
        f = fopen(context->cmd_args.domains, "r");
        if (f == NULL)
        {
            perror("Failed to open domain file");
            exit(1);
        }
        while(!feof(f))
        {
            if (0 <= getline(&line, &line_buflen, f))
            {
                trim_end(line);
                strtolower(line);
                if (strcmp(line, "") != 0)
                {
                    context->total_domains++;
                }
            }
        }
        fclose(f);
    }
    f = fopen(context->cmd_args.domains, "r");
    if (f == NULL)
    {
        perror("Failed to open domain file");
        exit(1);
    }
    if (geteuid() == 0)
    {
        fprintf(stderr, "You have started the program with root privileges.\n");
        struct passwd *nobody = getpwnam(UNPRIVILEGED_USER);
        if (!context->cmd_args.root)
        {
            if (nobody && setuid(nobody->pw_uid) == 0)
            {
                fprintf(stderr, "Privileges have been dropped to \"%s\" for security reasons.\n\n", UNPRIVILEGED_USER);
            }
            else if (!context->cmd_args.root)
            {
                fprintf(stderr, "Privileges could not be dropped to \"%s\".\n"
                        "For security reasons, this program will only run as root user when supplied with --root"
                        "which is not recommended.\n"
                        "It is better practice to run this program as a different user.\n", UNPRIVILEGED_USER);
                exit(1);
            }
        }
        else
        {
            fprintf(stderr, "[WARNING] Privileges were not dropped. This is not recommended.\n\n");
        }
    }
    FILE *randomness = fopen("/dev/urandom", "r");
    if (!randomness)
    {
        fprintf(stderr, "Failed to open /dev/urandom.\n");
        exit(1);
    }
    context->current_rate = 0;
    context->sock = sock;
    context->resolvers = massdns_resolvers_from_file(context->cmd_args.resolvers);
    context->map = hashmapCreate(context->cmd_args.hashmap_size, hash_string, cmp_lookup);
    context->initial = true;
    gettimeofday(&context->start_time, NULL);
    context->next_update = context->start_time;
    while (true)
    {
        while (hashmapSize(context->map) < context->cmd_args.hashmap_size && !feof(f))
        {
            if (0 <= getline(&line, &line_buflen, f))
            {
                trim_end(line);
                line_len = strlen(line);
                strtolower(line);
                if(strcmp(line, "") == 0)
                {
                    continue;
                }
                if (line_len > 0 && line[line_len - 1] == '.')
                {
                    // Remove trailing dot from FQDN
                    line[line_len] = 0;
                }
                lookup_t *lookup = hashmapGet(context->map, line);
                if (lookup == NULL)
                {
                    char *value = safe_malloc(line_len + 1);
                    strcpy(value, line);
                    lookup = safe_malloc(sizeof(*lookup));
                    lookup->domain = value;
                    lookup->tries = 0;
                    if (fread(&lookup->transaction, 1, sizeof(lookup->transaction), randomness) !=
                        sizeof(lookup->transaction))
                    {
                        fprintf(stderr, "Failed to get randomness for transaction id.\n");
                        exit(1);
                    }
                    gettimeofday(&lookup->next_lookup, NULL);
                    hashmapPut(context->map, value, lookup);
                }

            }
        }
        if(!context->cooldown && hashmapSize(context->map) < context->cmd_args.hashmap_size)
        {
            context->cooldown = true;
            gettimeofday(&context->cooldown_time, NULL);
        }
        while (massdns_receive_packet(sock, massdns_handle_packet, context));
        if (feof(f) && hashmapSize(context->map) == 0)
        {
            break;
        }
        hashmapForEach(context->map, handle_domain, context);
    }
    for (size_t i = 0; i < context->resolvers.len; i++)
    {
        free(((sockaddr_in_t **) context->resolvers.data)[i]);
        ((sockaddr_in_t **) context->resolvers.data)[i] = NULL;
    }
    free(line);
    free(context->resolvers.data);
    context->resolvers.data = NULL;
    print_stats(context);
    hashmapFree(context->map);
    context->map = NULL;
    fclose(f);
    fclose(randomness);
}