use of com.zimbra.client.ZMountpoint in project zm-mailbox by Zimbra.
the class TestMountpoint method testInvalidMountpoint.
/**
* Tests {@link ZMailbox#getValidFolderIds(String)}.
*/
@Test
public void testInvalidMountpoint() throws Exception {
TestUtil.createAccount(USER_NAME);
TestUtil.createAccount(REMOTE_USER_NAME);
ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
ZMailbox remoteMbox = TestUtil.getZMailbox(REMOTE_USER_NAME);
String remoteFolderPath = "/" + NAME_PREFIX + "-testInvalidMountpoint-remote";
ZFolder remoteFolder = TestUtil.createFolder(remoteMbox, remoteFolderPath);
ZMountpoint mountpoint = TestUtil.createMountpoint(remoteMbox, remoteFolderPath, mbox, NAME_PREFIX + "-mountpoint");
// Test valid mountpoint.
Set<String> folderIds = new HashSet<String>();
folderIds.add(mountpoint.getId());
String inboxId = Integer.toString(Mailbox.ID_FOLDER_INBOX);
folderIds.add(inboxId);
String idString = mbox.getValidFolderIds(StringUtil.join(",", folderIds));
List<String> returnedIds = Arrays.asList(idString.split(","));
Assert.assertEquals("Number of return IDs from mbox.getValidFolderIds", 2, returnedIds.size());
Assert.assertTrue("Returned IDs should contain ID of inbox", returnedIds.contains(inboxId));
Assert.assertTrue("Returned IDs should contain ID of mountpoint", returnedIds.contains(mountpoint.getId()));
Assert.assertEquals("Should be 1 comma in string returned by mbox.getValidFolderIds", 1, getNumCommas(idString));
// Delete remote folder and confirm that the id is no longer returned.
remoteMbox.deleteFolder(remoteFolder.getId());
idString = mbox.getValidFolderIds(StringUtil.join(",", folderIds));
returnedIds = Arrays.asList(idString.split(","));
Assert.assertEquals("Number of return IDs from mbox.getValidFolderIds after mountpoint delete", 1, returnedIds.size());
Assert.assertTrue("Returned IDs should contain ID of inbox after mp delete", returnedIds.contains(inboxId));
Assert.assertEquals("Should no commas in string returned by mbox.getValidFolderIds after mp delete", 0, getNumCommas(idString));
}
use of com.zimbra.client.ZMountpoint in project zm-mailbox by Zimbra.
the class ZMailboxUtil method doDumpFolder.
private void doDumpFolder(ZFolder folder, boolean recurse) {
String path;
if (folder instanceof ZSearchFolder) {
path = String.format("%s (%s)", folder.getPath(), ((ZSearchFolder) folder).getQuery());
} else if (folder instanceof ZMountpoint) {
ZMountpoint mp = (ZMountpoint) folder;
path = String.format("%s (%s:%s)", folder.getPath(), mp.getOwnerDisplayName(), mp.getRemoteId());
} else if (folder.getRemoteURL() != null) {
path = String.format("%s (%s)", folder.getPath(), folder.getRemoteURL());
} else {
path = folder.getPath();
}
stdout.format("%10.10s %4.4s %10d %10d %s%n", folder.getId(), folder.getDefaultView().name(), folder.getUnreadCount(), folder.getMessageCount(), path);
if (recurse) {
for (ZFolder child : folder.getSubFolders()) {
doDumpFolder(child, recurse);
}
}
}
use of com.zimbra.client.ZMountpoint in project zm-mailbox by Zimbra.
the class ImapPath method isValidImapPath.
/**
* Mostly checking that the path doesn't clash with any paths we don't want to expose via IMAP.
* Separated out from isVisible() to aid IMAP LSUB command support.
*/
boolean isValidImapPath() throws ServiceException {
if (mCredentials != null) {
if (mCredentials.isHackEnabled(ImapCredentials.EnabledHack.WM5)) {
String lcname = mPath.toLowerCase();
if (lcname.startsWith("sent items") && (lcname.length() == 10 || lcname.charAt(10) == '/'))
return false;
}
}
try {
// you cannot access your own mailbox via the /home/username mechanism
if (mOwner != null && belongsTo(mCredentials))
return false;
getFolder();
if (mFolder instanceof Folder) {
Folder folder = (Folder) mFolder;
// hide all system folders and the user root folder
if (folder.getId() == Mailbox.ID_FOLDER_USER_ROOT && mScope != Scope.REFERENCE) {
return false;
}
// hide spam folder unless anti-spam feature is enabled.
if (folder.getId() == Mailbox.ID_FOLDER_SPAM && !getOwnerAccount().isFeatureAntispamEnabled()) {
return false;
}
boolean isMailFolders = Provisioning.getInstance().getLocalServer().isImapDisplayMailFoldersOnly();
if (!isVisible(folder.getDefaultView(), isMailFolders)) {
return false;
}
// hide subfolders of trashed mountpoints
if (mReferent != this && folder.inTrash() && !((Mountpoint) folder).getTarget().equals(mReferent.asItemId())) {
return false;
}
// hide other users' mountpoints and mountpoints that point to the same mailbox
if (folder instanceof Mountpoint && mReferent == this && mScope != Scope.UNPARSED) {
return false;
}
// search folder visibility depends on an account setting
if (folder instanceof SearchFolder) {
return ((SearchFolder) folder).isImapVisible() && ImapFolder.getTypeConstraint((SearchFolder) folder).size() > 0;
}
} else {
ZFolder zfolder = (ZFolder) mFolder;
int folderId = asItemId().getId();
// the mailbox root folder is not visible
if (folderId == Mailbox.ID_FOLDER_USER_ROOT && mScope != Scope.REFERENCE) {
return false;
}
// hide spam folder unless anti-spam feature is enabled.
if (folderId == Mailbox.ID_FOLDER_SPAM && !getOwnerAccount().isFeatureAntispamEnabled()) {
return false;
}
// calendars, briefcases, etc. are not surfaced in IMAP
ZFolder.View view = zfolder.getDefaultView();
if (view == ZFolder.View.appointment || view == ZFolder.View.task || view == ZFolder.View.wiki || view == ZFolder.View.document) {
return false;
}
// hide other users' mountpoints and mountpoints that point to the same mailbox
if (zfolder instanceof ZMountpoint && mReferent == this && mScope != Scope.UNPARSED) {
return false;
}
// hide all remote searchfolders
if (zfolder instanceof ZSearchFolder) {
return false;
}
}
} catch (NoSuchItemException ignore) {
// 6.3.9. LSUB Command
// The server MUST NOT unilaterally remove an existing mailbox name from the subscription list even if a
// mailbox by that name no longer exists.
} catch (AccountServiceException ase) {
if (!AccountServiceException.NO_SUCH_ACCOUNT.equals(ase.getCode())) {
throw ase;
}
} catch (ServiceException se) {
if (ServiceException.PERM_DENIED.equals(se.getCode())) {
// Path probably OK. For subscriptions, don't disallow path for possibly temporary permissions issue
return true;
}
throw se;
}
return mReferent == this ? true : mReferent.isValidImapPath();
}
use of com.zimbra.client.ZMountpoint in project zm-mailbox by Zimbra.
the class ImapPath method getReferent.
ImapPath getReferent() throws ServiceException {
if (mReferent != null) {
return mReferent;
}
// while calculating, use the base
mReferent = this;
// only follow the authenticated user's own mountpoints
if (mScope == Scope.REFERENCE || mScope == Scope.UNPARSED || !belongsTo(mCredentials)) {
return mReferent;
}
ItemId iidRemote;
String subpathRemote = null;
Object mboxobj = getOwnerMailbox();
if (mboxobj instanceof Mailbox) {
try {
if (mFolder == null) {
Pair<Folder, String> resolved = ((Mailbox) mboxobj).getFolderByPathLongestMatch(getContext(), Mailbox.ID_FOLDER_USER_ROOT, asZimbraPath());
subpathRemote = resolved.getSecond();
boolean isMountpoint = resolved.getFirst() instanceof Mountpoint;
if (isMountpoint || resolved.getSecond() == null) {
mFolder = resolved.getFirst();
mItemId = new ItemId(resolved.getFirst());
}
if (!isMountpoint) {
return mReferent;
}
} else if (!(mFolder instanceof Mountpoint)) {
return mReferent;
}
// somewhere along the specified path is a visible mountpoint owned by the user
iidRemote = ((Mountpoint) mFolder).getTarget();
} catch (ServiceException e) {
return mReferent;
}
} else if (mboxobj instanceof ZMailbox) {
String accountId = mCredentials == null ? null : mCredentials.getAccountId();
if (mFolder == null) {
ZMailbox zmbx = (ZMailbox) mboxobj;
String path = asZimbraPath();
try {
for (int index = path.length(); index != -1; index = path.lastIndexOf('/', index - 1)) {
ZFolder zfolder = zmbx.getFolderByPath(path.substring(0, index));
if (zfolder != null) {
subpathRemote = path.substring(Math.min(path.length(), index + 1));
if (zfolder instanceof ZMountpoint || subpathRemote.isEmpty()) {
mFolder = zfolder;
mItemId = new ItemId(zfolder.getId(), accountId);
}
break;
}
}
} catch (ServiceException e) {
}
}
if (!(mFolder instanceof ZMountpoint)) {
return mReferent;
}
// somewhere along the specified path is a visible mountpoint owned by the user
iidRemote = new ItemId(((ZMountpoint) mFolder).getCanonicalRemoteId(), accountId);
} else {
return mReferent;
}
// don't allow mountpoints that point at the same mailbox (as it can cause infinite loops)
if (belongsTo(iidRemote.getAccountId())) {
return mReferent;
}
Account target = Provisioning.getInstance().get(AccountBy.id, iidRemote.getAccountId());
if (target == null) {
return mReferent;
}
String owner = mCredentials != null && mCredentials.getAccountId().equalsIgnoreCase(target.getId()) ? null : target.getName();
if (Provisioning.onLocalServer(target)) {
try {
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(target);
Folder folder = mbox.getFolderById(getContext(), iidRemote.getId());
if (subpathRemote == null) {
mReferent = new ImapPath(owner, folder, mCredentials);
} else {
(mReferent = new ImapPath(owner, folder.getPath() + (folder.getPath().equals("/") ? "" : "/") + subpathRemote, mCredentials)).mMailbox = mbox;
}
} catch (ServiceException e) {
}
} else {
Account acct = mCredentials == null ? null : Provisioning.getInstance().get(AccountBy.id, mCredentials.getAccountId());
if (acct == null)
return mReferent;
try {
ZMailbox.Options options = new ZMailbox.Options(AuthProvider.getAuthToken(acct).getEncoded(), AccountUtil.getSoapUri(target));
options.setTargetAccount(target.getName());
options.setNoSession(true);
ZMailbox zmbx = ZMailbox.getMailbox(options);
ZFolder zfolder = zmbx.getFolderById(iidRemote.toString(mCredentials.getAccountId()));
if (zfolder == null) {
return mReferent;
}
if (subpathRemote == null) {
mReferent = new ImapPath(owner, zfolder, mCredentials);
} else {
(mReferent = new ImapPath(owner, zfolder.getPath() + (zfolder.getPath().equals("/") ? "" : "/") + subpathRemote, mCredentials)).mMailbox = zmbx;
}
} catch (AuthTokenException ate) {
throw ServiceException.FAILURE("error generating auth token", ate);
} catch (ServiceException e) {
}
}
if (mReferent != this) {
mReferent.mScope = Scope.REFERENCE;
}
return mReferent;
}
use of com.zimbra.client.ZMountpoint in project zm-mailbox by Zimbra.
the class ExternalUserProvServlet method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String param = req.getParameter("p");
if (param == null) {
throw new ServletException("request missing param");
}
Map<Object, Object> tokenMap = validatePrelimToken(param);
Map<String, String> reqHeaders = new HashMap<String, String>();
String ownerId = (String) tokenMap.get("aid");
String folderId = (String) tokenMap.get("fid");
String extUserEmail = (String) tokenMap.get("email");
Provisioning prov = Provisioning.getInstance();
Account grantee;
try {
Account owner = prov.getAccountById(ownerId);
Domain domain = prov.getDomain(owner);
grantee = prov.getAccountByName(mapExtEmailToAcctName(extUserEmail, domain));
if (grantee == null) {
// external virtual account not created yet
if (prov.isOctopus() && DebugConfig.skipVirtualAccountRegistrationPage) {
// provision using 'null' password and display name
// UI will ask the user to set these post provisioning
provisionVirtualAccountAndRedirect(req, resp, null, null, ownerId, extUserEmail);
} else {
resp.addCookie(new Cookie("ZM_PRELIM_AUTH_TOKEN", param));
req.setAttribute("extuseremail", extUserEmail);
if (WebClientServiceUtil.isServerInSplitMode()) {
reqHeaders.put("extuseremail", extUserEmail);
reqHeaders.put("ZM_PRELIM_AUTH_TOKEN", param);
String htmlresp = WebClientServiceUtil.sendServiceRequestToOneRandomUiNode(EXT_USER_PROV_ON_UI_NODE, reqHeaders);
resp.getWriter().print(htmlresp);
} else {
ServletContext context = getServletContext().getContext("/zimbra");
if (context != null) {
RequestDispatcher dispatcher = context.getRequestDispatcher(PUBLIC_EXTUSERPROV_JSP);
dispatcher.forward(req, resp);
} else {
logger.warn("Could not access servlet context url /zimbra");
throw ServiceException.TEMPORARILY_UNAVAILABLE();
}
}
}
} else {
// create a new mountpoint in the external user's mailbox if not already created
String[] sharedItems = owner.getSharedItem();
int sharedFolderId = Integer.valueOf(folderId);
String sharedFolderPath = null;
MailItem.Type sharedFolderView = null;
for (String sharedItem : sharedItems) {
ShareInfoData sid = AclPushSerializer.deserialize(sharedItem);
if (sid.getItemId() == sharedFolderId && extUserEmail.equalsIgnoreCase(sid.getGranteeId())) {
sharedFolderPath = sid.getPath();
sharedFolderView = sid.getFolderDefaultViewCode();
break;
}
}
if (sharedFolderPath == null) {
throw new ServletException("share not found");
}
String mountpointName = getMountpointName(owner, grantee, sharedFolderPath);
ZMailbox.Options options = new ZMailbox.Options();
options.setNoSession(true);
options.setAuthToken(AuthProvider.getAuthToken(grantee).toZAuthToken());
options.setUri(AccountUtil.getSoapUri(grantee));
ZMailbox zMailbox = new ZMailbox(options);
ZMountpoint zMtpt = null;
try {
zMtpt = zMailbox.createMountpoint(String.valueOf(getMptParentFolderId(sharedFolderView, prov)), mountpointName, ZFolder.View.fromString(sharedFolderView.toString()), ZFolder.Color.DEFAULTCOLOR, null, ZMailbox.OwnerBy.BY_ID, ownerId, ZMailbox.SharedItemBy.BY_ID, folderId, false);
} catch (ServiceException e) {
logger.debug("Error in attempting to create mountpoint. Probably it already exists.", e);
}
if (zMtpt != null) {
if (sharedFolderView == MailItem.Type.APPOINTMENT) {
// make sure that the mountpoint is checked in the UI by default
FolderActionSelector actionSelector = new FolderActionSelector(zMtpt.getId(), "check");
FolderActionRequest actionRequest = new FolderActionRequest(actionSelector);
try {
zMailbox.invokeJaxb(actionRequest);
} catch (ServiceException e) {
logger.warn("Error in invoking check action on calendar mountpoint", e);
}
}
HashSet<MailItem.Type> types = new HashSet<MailItem.Type>();
types.add(sharedFolderView);
enableAppFeatures(grantee, types);
}
// check if the external user is already logged-in
String zAuthTokenCookie = null;
javax.servlet.http.Cookie[] cookies = req.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("ZM_AUTH_TOKEN")) {
zAuthTokenCookie = cookie.getValue();
break;
}
}
}
AuthToken zAuthToken = null;
if (zAuthTokenCookie != null) {
try {
zAuthToken = AuthProvider.getAuthToken(zAuthTokenCookie);
} catch (AuthTokenException ignored) {
// auth token is not valid
}
}
if (zAuthToken != null && !zAuthToken.isExpired() && zAuthToken.isRegistered() && grantee.getId().equals(zAuthToken.getAccountId())) {
// external virtual account already logged-in
resp.sendRedirect("/");
} else if (prov.isOctopus() && !grantee.isVirtualAccountInitialPasswordSet() && DebugConfig.skipVirtualAccountRegistrationPage) {
// seems like the virtual user did not set his password during his last visit, after an account was
// provisioned for him
setCookieAndRedirect(req, resp, grantee);
} else {
req.setAttribute("virtualacctdomain", domain.getName());
if (WebClientServiceUtil.isServerInSplitMode()) {
reqHeaders.put("virtualacctdomain", domain.getName());
String htmlresp = WebClientServiceUtil.sendServiceRequestToOneRandomUiNode(PUBLIC_LOGIN_ON_UI_NODE, reqHeaders);
resp.getWriter().print(htmlresp);
} else {
RequestDispatcher dispatcher = getServletContext().getContext("/zimbra").getRequestDispatcher(PUBLIC_LOGIN_JSP);
dispatcher.forward(req, resp);
}
}
}
} catch (ServiceException e) {
throw new ServletException(e);
}
}
Aggregations