Webman-Notes
From Zimbra :: Wiki
Contents |
Sharing
(r)ead - search, view overviews and items
(w)rite - edit drafts/contacts/notes, set flags
action (x) - workflow actions, like accepting appoitnments
(i)nsert - copy/add to directory, create subfolders
(d)elete - delete items and subfolders, set \Deleted flag
(a)dminister - delegate admin and change permissions
C# Preauth app
using System;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Security.Principal;
using System.Security.Cryptography;
using System.IO;
using Ini;
namespace Zimbar{
static class Program{
// File names of INI files
public static String SETTINGS = @"P:\bbzimbra\bbzimbra.ini";
public static String ROOMCTL = @"P:\bbzimbra\roomctl.ini";
static bool CheckPermission() {
string computername = System.Environment.MachineName.ToString().ToLower();
//computername = "studycentre-010";
IniFile roomini = new IniFile(ROOMCTL);
// Find out if the computer name is a controller (a key in the 'controllers' section.
string roomFromCtl = roomini.IniReadValue("controllers", computername, "");
//MessageBox.Show("roomfromctl: " + roomFromCtl);
// Now check if we are a controller machine or not
if (roomFromCtl == "") {
// Computer is not a controller - so we must check if the email for this room is enabled or not
//MessageBox.Show("You are not a controller");
// So first we need to work out which room we are in (first part of machine name before hyphen)
if (computername.IndexOf("-") == -1) {
// There was no hyphen in the name - what's wrong here? Don't know.
return true;
} else {
// Ok - there's a hyphen. We can find out the room name now!
string roomname = computername.Substring(0, computername.IndexOf("-"));
//MessageBox.Show("Based on machine name, you are in room '" + roomname + "'.");
// Now we got room name we need to find out if there is a key in the INI file
// - if yes, then we return true/false depending on room status
// - if no key for room, then we run anyway (will be admin/ereg ...)
string roomStatus = roomini.IniReadValue("status", roomname, "");
//MessageBox.Show("The email status for your room is: " + roomStatus);
switch(roomStatus){
case "": return true;
case "on": return true;
case "off": return false;
default: return true;
}
}
} else {
// Computer is a controller - so we just permit the application to run.
return true;
}
//return true;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(){
Application.SetCompatibleTextRenderingDefault(false);
Process[] pname = Process.GetProcessesByName("prism");
if (pname.Length != 0) {
MessageBox.Show("The Email program is already running. If it has not yet loaded, please be patient.", "Email", MessageBoxButtons.OK, MessageBoxIcon.Warning);
System.Environment.Exit(0);
}
// Check command line for custom settings ini
if (Environment.CommandLine.Contains("debug")) {
SETTINGS = @"P:\bbzimbra\bbzimbra.debug.ini";
} else {
// If no INI file parameter, default settings file is ...
SETTINGS = @"P:\bbzimbra\bbzimbra.ini";
}
// Check for existance of required files
if(!System.IO.File.Exists(SETTINGS)){
MessageBox.Show("Settings file '" + SETTINGS + "' not found.", "Email", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(0);
}
if (!System.IO.File.Exists(ROOMCTL)) {
MessageBox.Show("Control file '" + ROOMCTL + "' not found.", "Email", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(0);
}
// Check if email is permitted at this time based on the INI file values, quit if not.
if (!CheckPermission()) {
MessageBox.Show("You are not permitted to use Email at this time.", "Email", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
System.Environment.Exit(0);
}
// Get settings from INI file
IniFile ini = new IniFile(SETTINGS);
string zimpreauthkey = "MY-PREAUTH-KEY";
string zimdomain = ini.IniReadValue("zimbra", "domain", "");
string zimhost = ini.IniReadValue("zimbra", "host", "");
string prismpath = ini.IniReadValue("prism", "path", "");
string prismapppath = ini.IniReadValue("prism", "apppath", "");
//string prismappname = ini.IniReadValue("prism", "appname", "");
string prismprofile = ini.IniReadValue("prism", "profile", "");
// Some checks
/* if (zimpreauthkey == ""){
MessageBox.Show("No authentication key found.");
System.Environment.Exit(0);
} */
if (zimdomain == ""){
MessageBox.Show("No domain name key found.", "Email", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(0);
}
if (zimhost == ""){
MessageBox.Show("No hostname key found.", "Email", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(0);
}
if (System.IO.Directory.Exists(prismpath) == false){
MessageBox.Show("Prism application path does not exist.", "Email", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(0);
}
if (System.IO.File.Exists(prismpath + @"\prism.exe") == false){
MessageBox.Show("Prism executable does not exist.", "Email", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(0);
}
if (System.IO.Directory.Exists(prismprofile) == false) {
MessageBox.Show("Prism profile path does not exist.", "Email", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(0);
}
// Get username and remove logon domain part
String username = WindowsIdentity.GetCurrent().Name;
username = username.Substring(username.IndexOf('\\')+1);
// Set the email address as the username at the domain
String email = username + "@" + zimdomain;
// Now get date and convert to timestamp
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1);
string timestamp = string.Format(
"{0}",
(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).Ticks / 10000
);
// Expiration is 0
string expires = "0";
// Compose the auth string
string authstr = email + "|name|"+expires+"|" + timestamp;
// HMAC things
// First get the zimbra authkey into the key variable
byte[] key = System.Text.Encoding.UTF8.GetBytes(zimpreauthkey);
// Now put our data string into the data variable
byte[] data = System.Text.Encoding.UTF8.GetBytes(authstr);
HMACSHA1 hmac = new HMACSHA1(key);
byte[] result = hmac.ComputeHash(data);
// Now we got the result in format AA-BB-CC... remove the hyphens and lowercase it
string hex = System.BitConverter.ToString(result);
hex = hex.ToLower();
hex = hex.Replace("-", "");
// Format of URL to log in with
string urlformat = "http://{0}/service/preauth?account={1}&by=name×tamp={2}&expires={3}&preauth={4}";
string url = String.Format(urlformat, zimhost, email, timestamp, expires, hex);
string exec = prismpath + @"\prism.exe";
string args = @"-webapp ""{0}"" -override ""{0}\override.ini"" -profile ""{1}"" -uri ""{2}"" ";
args = string.Format(args, prismapppath, prismprofile, url);
// Configure the process to launch
ProcessStartInfo ps = new ProcessStartInfo();
ps.WindowStyle = ProcessWindowStyle.Maximized;
ps.FileName = exec;
ps.WorkingDirectory = prismpath;
ps.Arguments = args;
// Launch process
System.Diagnostics.Process prism = Process.Start(ps);
}
}
}
Per-user mailbox backup script
#!/bin/bash
#
# Zimbra backup script for individual mailboxes
#
# Variables
TIME=`date +%Y-%m-%d`
DOWN=`date +%u`
DOWD=`date +%A`
NFSHOST="10.0.0.5"
NFSPATH="/mnt/vol1/backups/bbs-zcs-001"
NFSMOUNT="/mnt/bbs-nas-001"
LOG="/var/log/zmbackup-mailbox.log"
EMAIL="emailadmin@example.com"
function out {
echo "["`date +"%Y-%m-%d %T"`"] $1"
}
echo ""
out "Zimbra mailbox backup"
echo ""
mkdir $NFSMOUNT
mount -t nfs $NFSHOST:/$NFSPATH $NFSMOUNT
ACCTS=`su - zimbra -c "/opt/zimbra/bin/zmprov sa '(&(&(!(zimbraCosId=my-zimbra-cos-id))(objectClass=zimbraAccount))(zimbraAccountStatus=*active*))'"`
for user in $ACCTS
do
out "Backing up $user ..."
su - zimbra -c "/opt/zimbra/bin/zmmailbox -z -m $user getRestURL .//?fmt=tgz > /tmp/$user.tgz"
mv /tmp/$user.tgz $NFSMOUNT/mbox-$DOWD/$user.tgz > /dev/null
done
out "Finished backing up users"
ls -lah $NFSMOUNT/mbox-$DOWD
umount $NFSMOUNT
cat $LOG | mail -c '' -s "[Zimbra backup] `hostname --fqdn` $TIME" $EMAIL

