commands/vadduser.cc
changeset 2 b3afb9f1e801
parent 0 6f7a81934006
equal deleted inserted replaced
1:30113bfbe723 2:b3afb9f1e801
    21 #include "misc/passwdfn.h"
    21 #include "misc/passwdfn.h"
    22 #include "mystring/mystring.h"
    22 #include "mystring/mystring.h"
    23 #include "config/configrc.h"
    23 #include "config/configrc.h"
    24 #include "misc/pwcrypt.h"
    24 #include "misc/pwcrypt.h"
    25 #include "vcommand.h"
    25 #include "vcommand.h"
    26 #include "cli/cli.h"
    26 #include "cli++/cli++.h"
    27 
    27 
    28 const char* cli_program = "vadduser";
    28 const char* cli_program = "vadduser";
    29 const char* cli_help_prefix = "Add a user to a virtual domain\n";
    29 const char* cli_help_prefix = "Add a user to a virtual domain\n";
    30 const char* cli_help_suffix = "";
    30 const char* cli_help_suffix = "";
    31 const char* cli_args_usage = "USERNAME [ALIAS1 ...]\n"
    31 const char* cli_args_usage = "USERNAME [ALIAS1 ...]\n"
    32 "or vaddalias [-f ADDRESS] USERNAME [ALIAS1 ...]";
    32 "or vaddalias USERNAME [ALIAS1 ...]";
    33 const int cli_args_min = 1;
    33 const int cli_args_min = 1;
    34 const int cli_args_max = -1;
    34 const int cli_args_max = -1;
    35 
    35 
    36 static const char* o_userdir = 0;
    36 static const char* o_userdir = 0;
    37 static cli_stringlist* o_forwards = 0;
    37 static cli_stringlist* o_forwards = 0;
    40 static int o_hardquota = 0;
    40 static int o_hardquota = 0;
    41 static int o_msgsize = 0;
    41 static int o_msgsize = 0;
    42 static int o_msgcount = 0;
    42 static int o_msgcount = 0;
    43 static int o_expiry = 0;
    43 static int o_expiry = 0;
    44 static int o_password = true;
    44 static int o_password = true;
    45 static int o_domailbox = true;
    45 static int o_hasmailbox = true;
    46 static int o_quiet = false;
    46 static int o_quiet = false;
       
    47 static const char* o_pwcrypt = 0;
    47 
    48 
    48 // This program is used to set up a user within a virtual host.
    49 // This program is used to set up a user within a virtual host.
    49 // If this program is reading from a tty,
    50 // If this program is reading from a tty,
    50 // it will then ask for a password (twice, to make sure you typed it in
    51 // it will then ask for a password (twice, to make sure you typed it in
    51 // correctly), otherwise it will read the password from the input with no
    52 // correctly), otherwise it will read the password from the input with no
    56 // It will also add an entry for each of the named aliases.
    57 // It will also add an entry for each of the named aliases.
    57 
    58 
    58 cli_option cli_options[] = {
    59 cli_option cli_options[] = {
    59   { 'c', "msgcount", cli_option::integer, 0, &o_msgcount,
    60   { 'c', "msgcount", cli_option::integer, 0, &o_msgcount,
    60     "Set the user's message count limit", 0 },
    61     "Set the user's message count limit", 0 },
    61   { 'D', "no-mailbox", cli_option::flag, false, &o_domailbox,
    62   { 'D', "no-mailbox", cli_option::flag, false, &o_hasmailbox,
    62     "Do not create a mailbox for this user", "true for vaddalias" },
    63     "Do not create a mailbox for this user", "true for vaddalias" },
    63   { 'd', "directory", cli_option::string, 0, &o_userdir,
    64   { 'd', "directory", cli_option::string, 0, &o_userdir,
    64     "Set the path to the user's mailbox", 0 },
    65     "Set the path to the user's mailbox", 0 },
    65   // Set the path to the user's mailbox.
    66   // Set the path to the user's mailbox.
    66   // Note that this directory is unconditionally prefixed with "./".
    67   // Note that this directory is unconditionally prefixed with "./".
    67   { 'e', "expiry", cli_option::integer, 0, &o_expiry,
    68   { 'e', "expiry", cli_option::integer, 0, &o_expiry,
    68     "Set the account's expiry time (in seconds)", 0 },
    69     "Set the account's expiry time (in seconds)", 0 },
    69   { 'f', "forward", cli_option::stringlist, 0, &o_forwards,
    70   { 'f', "forward", cli_option::stringlist, 0, &o_forwards,
    70     "Add a forwarding address to this user", 0 },
    71     "Add a forwarding address to this user", 0 },
    71   // Add a forwarding address to this user (this may be used multiple times).
    72   // Add a forwarding address to this user (this may be used multiple times).
       
    73   { 0,   "password",    cli_option::string, 0, &o_pwcrypt,
       
    74     "Encrypted password", "asking for a password" },
    72   { 'P', "no-password", cli_option::flag, false, &o_password,
    75   { 'P', "no-password", cli_option::flag, false, &o_password,
    73     "Do not ask for a password", 0 },
    76     "Do not ask for a password", 0 },
    74   // Do not ask for a password,
    77   // Do not ask for a password,
    75   // and instead set the pass phrase field to an unusable value.
    78   // and instead set the pass phrase field to an unusable value.
    76   { 'p', "personal", cli_option::string, 0, &o_personal,
    79   { 'p', "personal", cli_option::string, 0, &o_personal,
   118 }
   121 }
   119 
   122 
   120 vpwentry* make_user(const mystring& name, const mystring& passcode)
   123 vpwentry* make_user(const mystring& name, const mystring& passcode)
   121 {
   124 {
   122   mystring dir;
   125   mystring dir;
   123   if(o_domailbox) {
   126   if(o_userdir)
   124     if(o_userdir)
   127     dir = o_userdir;
   125       dir = o_userdir;
   128   else
   126     else
   129     dir = domain.userdir(name);
   127       dir = domain.userdir(name);
   130   dir = "./" + dir;
   128     dir = "./" + dir;
       
   129   }
       
   130 
   131 
   131   for(cli_stringlist* node = o_forwards; node; node = node->next) {
   132   for(cli_stringlist* node = o_forwards; node; node = node->next) {
   132     response r = domain.validate_forward(node->string);
   133     response r = domain.validate_forward(node->string);
   133     if(!r) {
   134     if(!r) {
   134       if(!o_quiet)
   135       if(!o_quiet)
   137       exit(1);
   138       exit(1);
   138     }
   139     }
   139   }
   140   }
   140   
   141   
   141   vpwentry* vpw = new vpwentry(name.lower(), passcode, dir,
   142   vpwentry* vpw = new vpwentry(name.lower(), passcode, dir,
   142 			       list2str(o_forwards));
   143 			       list2str(o_forwards), o_hasmailbox);
   143   vpw->set_defaults(true, true);
       
   144   
       
   145   vpw->personal = o_personal;
   144   vpw->personal = o_personal;
   146   vpw->hardquota = o_hardquota;
   145   vpw->hardquota = o_hardquota;
   147   vpw->softquota = o_softquota;
   146   vpw->softquota = o_softquota;
   148   vpw->msgcount = o_msgcount;
   147   vpw->msgcount = o_msgcount;
   149   vpw->msgsize = o_msgsize;
   148   vpw->msgsize = o_msgsize;
   154 }
   153 }
   155 
   154 
   156 void add_user(const mystring& user)
   155 void add_user(const mystring& user)
   157 {
   156 {
   158   if(!domain.exists(user)) {
   157   if(!domain.exists(user)) {
   159     mystring passcode = "*";
   158     mystring passcode;
   160     if(o_password) {
   159     if(o_pwcrypt)
       
   160       passcode = o_pwcrypt;
       
   161     else if(o_password) {
   161       mystring passwd = getpasswd(argv0base);
   162       mystring passwd = getpasswd(argv0base);
   162       if(passwd.length() == 0)
   163       if(passwd.length() == 0)
   163 	exit(1);
   164 	exit(1);
   164       passcode = pwcrypt(passwd);
   165       passcode = pwcrypt(passwd);
   165     }
   166     }
       
   167     else
       
   168       passcode = "*";
   166     vpwentry* vpw = make_user(user, passcode);
   169     vpwentry* vpw = make_user(user, passcode);
   167     response resp = domain.set(vpw, true, vpw->mailbox);
   170     response resp = domain.set(vpw, true);
   168     delete vpw;
   171     delete vpw;
   169     if(!resp) {
   172     if(!resp) {
   170       if(!o_quiet)
   173       if(!o_quiet)
   171 	ferr << argv0base << ": error adding the virtual user:\n  "
   174 	ferr << argv0base << ": error adding the virtual user:\n  "
   172 	     << resp.msg << endl;
   175 	     << resp.msg << endl;
   183 void add_alias(mystring user, mystring alias)
   186 void add_alias(mystring user, mystring alias)
   184 {
   187 {
   185   alias = alias.lower();
   188   alias = alias.lower();
   186   user = user.lower();
   189   user = user.lower();
   187   if(!domain.exists(alias)) {
   190   if(!domain.exists(alias)) {
   188     vpwentry vpw(alias, "*", 0, user);
   191     vpwentry vpw(alias, "*", domain.userdir(alias), user, false);
   189     vpw.set_defaults(true, true);
       
   190     response resp = domain.set(&vpw, true);
   192     response resp = domain.set(&vpw, true);
   191     if(!resp)
   193     if(!resp)
   192       if(!o_quiet)
   194       if(!o_quiet)
   193 	ferr << argv0base << ": warning: adding the alias '"
   195 	ferr << argv0base << ": warning: adding the alias '"
   194 	     << alias
   196 	     << alias
   206 }
   208 }
   207 
   209 
   208 void set_defaults()
   210 void set_defaults()
   209 {
   211 {
   210   if(!strcmp(argv0base, "vaddalias"))
   212   if(!strcmp(argv0base, "vaddalias"))
   211     o_domailbox = false;
   213     o_hasmailbox = false;
   212   if(!o_hardquota)
   214   if(!o_hardquota)
   213     o_hardquota = config->default_hardquota();
   215     o_hardquota = config->default_hardquota();
   214   if(!o_softquota)
   216   if(!o_softquota)
   215     o_softquota = config->default_softquota();
   217     o_softquota = config->default_softquota();
   216   if(!o_msgsize)
   218   if(!o_msgsize)