My name is Matt Jarjoura and I've been using vpopmail at my company for over a year now and it works great. Also, I have created a few in-house programs using the library.
Anyway, I wanted to get opinions and ideas for creating a unified command line tool for executing vpopmail commands. Possibly in an attempt to move this to /usr/local/sbin on the admin's system in the future?!
Attached to this email is my starting point, let me know what you think.
Thanks, Matt Jarjoura
/* * $Id$ * Copyright (C) 2004 Matt Jarjoura. <[EMAIL PROTECTED]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <getopt.h>
#include <config.h>
#include <vpopmail.h>
#include <vauth.h>
/* Version 5.3.8 */
#define OLD_VPOPMAIL
#define MAX_BUFF 256
//static int mode_flag = 0;
typedef unsigned char BOOL;
#define TRUE 1;
#define FALSE 0;
typedef struct _vpop_info
{
char User[MAX_BUFF];
char Domain[MAX_BUFF];
char Quota[MAX_BUFF];
BOOL Random;
} VpopInfo;
static VpopInfo info;
typedef struct _vpop_modes
{
char *mode;
unsigned char flag;
} VpopModes;
static VpopModes const modes[] = {
{"change-password", 'p'},
{"set-quota", 'q'},
{NULL, 0}
};
static struct option const long_options[] = {
/* Change Password Options */
{"random", no_argument, NULL, 'r'},
/* Change Quota Options */
{"quota", required_argument, NULL, 'q'},
/* For Most Options */
{"password", optional_argument, NULL, 'p'},
{"domain", required_argument, NULL, 'd'},
{"user", required_argument, NULL, 'u'},
/* Generic Options */
{"help", no_argument, NULL, '?'},
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0}
};
int change_user_quota(char *User, char *Domain, char *Quota);
int validate_domain(char *Domain);
int change_password(char *User, char *Domain, BOOL Random);
void dump_help(void);
void dump_version(void);
int main(int argc, char *argv[])
{
int c = 0;
int option_index = 0;
int retValue = 0;
if ( !(argc > 1) )
{
dump_version();
dump_help();
return vexit(0);
}
do {
c = getopt_long (argc, argv, "?d:p:ru:v", long_options, &option_index);
switch (c)
{
//fprintf(stdout, "Changing Password.\n");
//return change_password(info.User, info.Domain, info.Random);
//break;
case 'd':
snprintf(info.Domain, MAX_BUFF, "%s", optarg);
retValue = validate_domain(info.Domain);
if (retValue != 0)
vexit(retValue);
break;
case 'u':
snprintf(info.User, MAX_BUFF, "%s", optarg);
break;
case 'q':
snprintf(info.Quota, MAX_BUFF, "%s", optarg);
break;
case 'r':
info.Random = TRUE;
break;
case 'v':
dump_version();
return vexit(0);
break;
default:
case '?':
dump_version();
dump_help();
return vexit(0);
break;
}
} while (c != -1);
return vexit(0);
}
int validate_domain(char *Domain)
{
if ( vget_assign(Domain, NULL, 0, NULL, NULL) == NULL )
{
fprintf(stderr, "Error: %s\n", verror(VA_DOMAIN_DOES_NOT_EXIST));
return VA_DOMAIN_DOES_NOT_EXIST;
}
return 0;
}
int change_user_quota(char *User, char *Domain, char *Quota)
{
int ret;
if ((ret = vsetuserquota( User, Domain, Quota )) != VA_SUCCESS)
{
printf("Error: %s\n", verror(ret));
return ret;
}
return 0;
}
int change_password(char *User, char *Domain, BOOL Random)
{
char Email[MAX_BUFF];
char Passwd[MAX_BUFF];
int i;
/* create email for old functions */
snprintf(Email, sizeof(Email), "[EMAIL PROTECTED]", User, Domain);
if ( Random )
#ifdef OLD_VPOPMAIL
snprintf( Passwd, MAX_BUFF, "%s", vgen_pass(8) );
#else
vrandom_pass (Passwd, 8);
#endif
else
snprintf(Passwd, sizeof(Passwd), "%s", vgetpasswd(Email));
if ( (i=vpasswd( User, Domain, Passwd, USE_POP )) != 0 )
{
fprintf(stderr, "Error: %s\n", verror(i));
return i;
}
if ( Random )
fprintf(stdout, "Random password: %s\n", Passwd);
return 0;
}
void dump_version(void)
{
fprintf(stdout, "vpopmail version %s\n", VERSION);
fprintf(stdout, "Copyright (C) 1999-2003 Inter7 Internet Technologies, Inc.\n");
}
void dump_help(void)
{
fprintf(stdout, "Usage: vpopmail-cmd MODE [OPTIONS]...\n");
fprintf(stdout, " -?, --help Display this help and exit\n");
fprintf(stdout, " --version Display the version and exit\n");
fprintf(stdout, " -u, --user=USER The USER in the domain\n");
fprintf(stdout, " -d, --domain=DOMAIN Select the DOMAIN\n");
fprintf(stdout, " -p, --password[=PASS] Password to use when accessing account.
If password is");
fprintf(stdout, " not given it's asked from the tty.");
fprintf(stdout, "\nAvailable Modes: \n");
fprintf(stdout, " change-password Change users password\n");
fprintf(stdout, " -r, --random Generate a random password\n\n");
fprintf(stdout, " set-quota Change users quota\n");
fprintf(stdout, " -q, --quota=SIZE Set quota to SIZE\n");
}
