Exemplo n.º 1
0
def get_domain_details(domain, analyst):
    """
    Generate the data to render the Domain details template.

    :param domain: The name of the Domain to get details for.
    :type domain: str
    :param analyst: The user requesting this information.
    :type analyst: str
    :returns: template (str), arguments (dict)
    """

    template = None
    allowed_sources = user_sources(analyst)
    dmain = Domain.objects(domain=domain,
                           source__name__in=allowed_sources).first()
    if not dmain:
        error = ("Either no data exists for this domain"
                 " or you do not have permission to view it.")
        template = "error.html"
        args = {'error': error}
        return template, args

    forms = {}
    #populate whois data into whois form
    # and create data object (keyed on date) for updating form on date select
    whois_data = {'': ''}  #blank info for "Add New" option
    initial_data = {'data': ' '}
    raw_data = {}
    whois = getattr(dmain, 'whois', None)
    if whois:
        for w in whois:
            #build data as a display-friendly string
            w.date = datetime.datetime.strftime(w.date,
                                                settings.PY_DATETIME_FORMAT)
            from whois_parser import WhoisEntry
            #prettify the whois data
            w.data = unicode(WhoisEntry.from_dict(w.data))
            if 'text' not in w:  #whois data was added with old data format
                w.text = w.data
            #also save our text blob for easy viewing of the original data
            whois_data[w.date] = (w.data, w.text)
        #show most recent entry first
        initial_data = {'data': whois[-1].data, 'date': whois[-1].date}
        raw_data = {'data': whois[-1].text, 'date': whois[-1].date}

    whois_len = len(
        whois_data) - 1  #subtract one to account for blank "Add New" entry
    whois_data = json.dumps(whois_data)

    dmain.sanitize_sources(username="******" % analyst, sources=allowed_sources)

    forms['whois'] = UpdateWhoisForm(initial_data, domain=domain)
    forms['raw_whois'] = UpdateWhoisForm(raw_data,
                                         domain=domain,
                                         allow_adding=False)
    forms['diff_whois'] = DiffWhoisForm(domain=domain)

    # remove pending notifications for user
    remove_user_from_notification("%s" % analyst, dmain.id, 'Domain')

    # subscription
    subscription = {
        'type': 'Domain',
        'id': dmain.id,
        'subscribed': is_user_subscribed("%s" % analyst, 'Domain', dmain.id),
    }

    #objects
    objects = dmain.sort_objects()

    #relationships
    relationships = dmain.sort_relationships("%s" % analyst, meta=True)

    # relationship
    relationship = {'type': 'Domain', 'value': dmain.id}

    #comments
    comments = {'comments': dmain.get_comments(), 'url_key': dmain.domain}

    #screenshots
    screenshots = dmain.get_screenshots(analyst)

    # favorites
    favorite = is_user_favorite("%s" % analyst, 'Domain', dmain.id)

    # services
    manager = crits.service_env.manager
    service_list = manager.get_supported_services('Domain', True)

    args = {
        'objects': objects,
        'relationships': relationships,
        'comments': comments,
        'favorite': favorite,
        'relationship': relationship,
        'subscription': subscription,
        'screenshots': screenshots,
        'domain': dmain,
        'forms': forms,
        'whois_data': whois_data,
        'service_list': service_list,
        'whois_len': whois_len
    }

    return template, args
Exemplo n.º 2
0
def update_whois(request, domain, editable=True):
    """
    Edit whois data for a domain. Should be an AJAX POST.

    :param request: Django request.
    :type request: :class:`django.http.HttpRequest`
    :param domain: The domain to query for.
    :type domain: str
    :param editable: We are editing.
    :type editable: boolean
    :returns: :class:`django.http.HttpResponse`
    """

    if request.method == 'POST' and request.is_ajax():
        form = UpdateWhoisForm(request.POST, domain=domain)
        date = request.POST.get('date')
        analyst = request.user.username
        if date:
            # kind of hackish, but lets us ensure a newly added date
            # doesn't get flagged as a non-available option
            # during validation
            if date not in form.fields['date'].choices:
                form.fields['date'].choices.append((date, date))
        if form.is_valid():
            whois_data = form.cleaned_data['data']
            result = None
            if date:
                date = datetime.datetime.strptime(date,
                                                  settings.PY_DATETIME_FORMAT)
                result = edit_whois(domain,
                                    whois_data,
                                    date,
                                    analyst)
            else: #adding a new entry
                date = datetime.datetime.now()
                result = add_whois(domain,
                                    whois_data,
                                    date,
                                    analyst,
                                    editable)
            if result['success']:
                date = datetime.datetime.strftime(date,
                                                  settings.PY_DATETIME_FORMAT)
                return HttpResponse(json.dumps({'success': True,
                                                'data': str(result['whois']),
                                                'date': date,
                                                'analyst': analyst}),
                                    mimetype="application/json")
            else:
                return HttpResponse(json.dumps({'success':False,
                                                'message':result['message']}),
                                    mimetype="application/json")
        else:
            #TODO: return and repopulate form with error messages
            return HttpResponse(json.dumps({'success': False,
                                            'form': form.as_table()}),
                                mimetype="application/json")
    else:
        return render_to_response("error.html",
                                  {"error" : 'Expected AJAX POST' },
                                  RequestContext(request))