Search in sources :

Example 1 with DomainBy

use of com.zimbra.common.account.Key.DomainBy in project zm-mailbox by Zimbra.

the class SearchAutoProvDirectory method handle.

public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    String query = request.getAttribute(AdminConstants.E_QUERY, null);
    String name = request.getAttribute(AdminConstants.E_NAME, null);
    if ((query != null) && (name != null)) {
        throw ServiceException.INVALID_REQUEST("only one of filter or name can be provided", null);
    }
    int maxResults = (int) request.getAttributeLong(AdminConstants.A_MAX_RESULTS, SearchDirectory.MAX_SEARCH_RESULTS);
    int limit = (int) request.getAttributeLong(AdminConstants.A_LIMIT, Integer.MAX_VALUE);
    if (limit == 0) {
        limit = Integer.MAX_VALUE;
    }
    int offset = (int) request.getAttributeLong(AdminConstants.A_OFFSET, 0);
    boolean refresh = request.getAttributeBool(AdminConstants.A_REFRESH, false);
    String keyAttr = request.getAttribute(AdminConstants.A_KEYATTR);
    String attrsStr = request.getAttribute(AdminConstants.A_ATTRS, null);
    String[] returnAttrs = null;
    if (attrsStr != null) {
        Set<String> attrs = new HashSet<String>();
        for (String attr : Splitter.on(',').trimResults().split(attrsStr)) {
            attrs.add(attr);
        }
        if (!attrs.contains(keyAttr)) {
            attrs.add(keyAttr);
        }
        returnAttrs = attrs.toArray(new String[attrs.size()]);
    }
    Element eDomain = request.getElement(AdminConstants.E_DOMAIN);
    DomainBy domainBy = DomainBy.fromString(eDomain.getAttribute(AdminConstants.A_BY));
    String domainValue = eDomain.getText();
    Domain domain = prov.get(domainBy, domainValue);
    if (domain == null) {
        throw AccountServiceException.NO_SUCH_DOMAIN(domainValue);
    }
    checkRight(zsc, context, domain, Admin.R_autoProvisionAccount);
    AdminSession session = (AdminSession) getSession(zsc, Session.Type.ADMIN);
    List<Entry> entryList = null;
    if (session != null) {
        Cache.Params params = new Cache.Params(domain, query, name, keyAttr, returnAttrs, maxResults);
        if (!refresh) {
            entryList = Cache.getFromCache(session, params);
        }
        if (entryList == null) {
            entryList = search(domain, query, name, keyAttr, returnAttrs, maxResults);
            Cache.putInCache(session, params, entryList);
        }
    } else {
        entryList = search(domain, query, name, keyAttr, returnAttrs, maxResults);
    }
    Element response = zsc.createElement(AdminConstants.SEARCH_AUTO_PROV_DIRECTORY_RESPONSE);
    encodeEntries(response, entryList, keyAttr, offset, limit);
    return response;
}
Also used : Element(com.zimbra.common.soap.Element) Provisioning(com.zimbra.cs.account.Provisioning) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) AdminSession(com.zimbra.cs.session.AdminSession) DomainBy(com.zimbra.common.account.Key.DomainBy) Domain(com.zimbra.cs.account.Domain) HashSet(java.util.HashSet)

Example 2 with DomainBy

use of com.zimbra.common.account.Key.DomainBy in project zm-mailbox by Zimbra.

the class CheckDomainMXRecord method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    CheckDomainMXRecordRequest req = JaxbUtil.elementToJaxb(request);
    DomainBy domainBy = req.getDomain().getBy().toKeyDomainBy();
    String value = req.getDomain().getKey();
    Domain domain = prov.get(domainBy, value);
    checkDomainRight(zsc, domain, Admin.R_checkDomainMXRecord);
    String SMTPHost = domain.getAttr(Provisioning.A_zimbraDNSCheckHostname, true);
    String domainName = domain.getName();
    if (SMTPHost == null || SMTPHost.length() < 1)
        SMTPHost = domain.getAttr(Provisioning.A_zimbraSmtpHostname, false);
    if (SMTPHost == null || SMTPHost.length() < 1)
        SMTPHost = prov.getLocalServer().getAttr(Provisioning.A_zimbraSmtpHostname);
    if (SMTPHost == null || SMTPHost.length() < 1)
        SMTPHost = prov.getConfig().getAttr(Provisioning.A_zimbraSmtpHostname);
    if (SMTPHost == null || SMTPHost.length() < 1)
        SMTPHost = domain.getName();
    String SMTPHostMatch = String.format("^\\d+\\s%s\\.$", SMTPHost);
    ZimbraLog.soap.info("checking domain mx record");
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
    String message = String.format("Domain is configured to use SMTP host: %s. None of the MX records match this name.", SMTPHost);
    // Element response = zsc.createElement(AdminConstants.CHECK_DOMAIN_MX_RECORD_RESPONSE);
    CheckDomainMXRecordResponse resp = new CheckDomainMXRecordResponse();
    boolean found = false;
    try {
        DirContext ictx = new InitialDirContext(env);
        Attributes attrs = ictx.getAttributes(domainName, new String[] { "MX" });
        if (attrs.size() < 1) {
            throw ServiceException.FAILURE("NoMXRecordsForDomain", null);
        }
        for (NamingEnumeration<? extends Attribute> ne = attrs.getAll(); ne.hasMore(); ) {
            Attribute attr = (Attribute) ne.next();
            if (attr.size() == 1) {
                ZimbraLog.soap.info("single attribute");
                Object o = attr.get();
                if (o instanceof String) {
                    String rec = o.toString();
                    ZimbraLog.soap.info("found MX record " + rec);
                    if (rec.matches(SMTPHostMatch)) {
                        found = true;
                        break;
                    }
                    resp.addEntry(rec);
                } else {
                    String rec = new String((byte[]) o);
                    ZimbraLog.soap.info("found MX attribute " + attr.getID() + " = " + rec);
                    if (rec.matches(SMTPHostMatch)) {
                        found = true;
                        break;
                    }
                    resp.addEntry(rec);
                }
            } else {
                ZimbraLog.soap.info("multivalued attribute");
                for (int i = 0; i < attr.size(); i++) {
                    Object o = attr.get(i);
                    if (o instanceof String) {
                        String rec = o.toString();
                        ZimbraLog.soap.info("found MX record " + attr.getID() + "-" + Integer.toString(i) + " = " + rec);
                        if (rec.matches(SMTPHostMatch)) {
                            found = true;
                            break;
                        }
                        resp.addEntry(rec);
                    } else {
                        String rec = new String((byte[]) o);
                        ZimbraLog.soap.info("found MX attribute " + attr.getID() + "-" + Integer.toString(i) + " = " + rec);
                        if (rec.matches(SMTPHostMatch)) {
                            found = true;
                            break;
                        }
                        resp.addEntry(rec);
                    //message = String.format("%s %s", message,rec);
                    }
                }
            }
        }
        if (found)
            resp.setCode("Ok");
        else {
            resp.setCode("Failed");
            resp.setMessage(message);
        }
    } catch (NameNotFoundException e) {
        throw ServiceException.FAILURE("NameNotFoundException", e);
    } catch (NamingException e) {
        throw ServiceException.FAILURE("Failed to verify domain's MX record. " + e.getMessage(), e);
    }
    return zsc.jaxbToElement(resp);
}
Also used : CheckDomainMXRecordResponse(com.zimbra.soap.admin.message.CheckDomainMXRecordResponse) Hashtable(java.util.Hashtable) Provisioning(com.zimbra.cs.account.Provisioning) CheckDomainMXRecordRequest(com.zimbra.soap.admin.message.CheckDomainMXRecordRequest) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) DomainBy(com.zimbra.common.account.Key.DomainBy) Domain(com.zimbra.cs.account.Domain)

Example 3 with DomainBy

use of com.zimbra.common.account.Key.DomainBy in project zm-mailbox by Zimbra.

the class CreateXMPPComponent method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    // <CreateXMPPComponentRequest>
    //    <xmppComponent name="name">
    //       <domain [by="id, name, virtualHostname, krb5Realm"]>domainId</domain>
    //       <server[by="id, name, serviceHostname"]>serviceId</domain>
    //       <a n="zimbraXMPPComponentCategory">category (see XEP-0030)</a>
    //       <a n="zimbraXMPPComponentName">long component name</a>
    //       [<a n="zimbraXMPPComponentType">type from XEP-0030</a>]
    //    </xmppComponent>
    //
    Element cEl = request.getElement(AccountConstants.E_XMPP_COMPONENT);
    Map<String, Object> attrs = AdminService.getAttrs(cEl);
    Element domainElt = cEl.getElement(AdminConstants.E_DOMAIN);
    String byStr = domainElt.getAttribute(AdminConstants.A_BY, "id");
    Key.DomainBy domainby = Key.DomainBy.valueOf(byStr);
    Domain domain = Provisioning.getInstance().get(domainby, domainElt.getText());
    Element serverElt = cEl.getElement(AdminConstants.E_SERVER);
    String serverByStr = serverElt.getAttribute(AdminConstants.A_BY);
    Server server = prov.get(Key.ServerBy.fromString(serverByStr), serverElt.getText());
    String name = cEl.getAttribute(AccountConstants.A_NAME);
    if (!name.endsWith(domain.getName())) {
        throw ServiceException.INVALID_REQUEST("Specified component name must be full name, and must be a subdomain of the specified parent", null);
    }
    checkRight(zsc, context, null, Admin.R_createXMPPComponent);
    checkSetAttrsOnCreate(zsc, TargetType.xmppcomponent, name, attrs);
    XMPPComponent comp = prov.createXMPPComponent(name, domain, server, attrs);
    Element response = zsc.createElement(AdminConstants.CREATE_XMPPCOMPONENT_RESPONSE);
    GetXMPPComponent.encodeXMPPComponent(response, comp);
    return response;
}
Also used : Server(com.zimbra.cs.account.Server) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) Domain(com.zimbra.cs.account.Domain) XMPPComponent(com.zimbra.cs.account.XMPPComponent) Provisioning(com.zimbra.cs.account.Provisioning) DomainBy(com.zimbra.common.account.Key.DomainBy) Key(com.zimbra.common.account.Key)

Example 4 with DomainBy

use of com.zimbra.common.account.Key.DomainBy in project zm-mailbox by Zimbra.

the class AutoProvAccount method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    AutoProvAccountRequest req = JaxbUtil.elementToJaxb(request);
    DomainBy domainBy = req.getDomain().getBy().toKeyDomainBy();
    String domainKey = req.getDomain().getKey();
    Domain domain = prov.get(domainBy, domainKey);
    if (domain == null) {
        throw AccountServiceException.NO_SUCH_DOMAIN(domainKey);
    }
    checkRight(zsc, context, domain, Admin.R_autoProvisionAccount);
    AutoProvPrincipalBy by = req.getPrincipal().getBy();
    String principal = req.getPrincipal().getKey();
    String password = req.getPassword();
    Account acct = prov.autoProvAccountManual(domain, by, principal, password);
    if (acct == null) {
        throw ServiceException.FAILURE("unable to auto provision account: " + principal, null);
    }
    Element response = zsc.createElement(AdminConstants.AUTO_PROV_ACCOUNT_RESPONSE);
    ToXML.encodeAccount(response, acct);
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) DomainBy(com.zimbra.common.account.Key.DomainBy) AutoProvPrincipalBy(com.zimbra.soap.type.AutoProvPrincipalBy) Element(com.zimbra.common.soap.Element) AutoProvAccountRequest(com.zimbra.soap.admin.message.AutoProvAccountRequest) Domain(com.zimbra.cs.account.Domain) Provisioning(com.zimbra.cs.account.Provisioning)

Aggregations

DomainBy (com.zimbra.common.account.Key.DomainBy)4 Domain (com.zimbra.cs.account.Domain)4 Provisioning (com.zimbra.cs.account.Provisioning)4 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)4 Element (com.zimbra.common.soap.Element)3 Key (com.zimbra.common.account.Key)1 Account (com.zimbra.cs.account.Account)1 Server (com.zimbra.cs.account.Server)1 XMPPComponent (com.zimbra.cs.account.XMPPComponent)1 AdminSession (com.zimbra.cs.session.AdminSession)1 AutoProvAccountRequest (com.zimbra.soap.admin.message.AutoProvAccountRequest)1 CheckDomainMXRecordRequest (com.zimbra.soap.admin.message.CheckDomainMXRecordRequest)1 CheckDomainMXRecordResponse (com.zimbra.soap.admin.message.CheckDomainMXRecordResponse)1 AutoProvPrincipalBy (com.zimbra.soap.type.AutoProvPrincipalBy)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1