daemon/listdomain.cc
changeset 0 6f7a81934006
child 2 b3afb9f1e801
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
       
     2 //
       
     3 // This program is free software; you can redistribute it and/or modify
       
     4 // it under the terms of the GNU General Public License as published by
       
     5 // the Free Software Foundation; either version 2 of the License, or
       
     6 // (at your option) any later version.
       
     7 //
       
     8 // This program is distributed in the hope that it will be useful,
       
     9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    11 // GNU General Public License for more details.
       
    12 //
       
    13 // You should have received a copy of the GNU General Public License
       
    14 // along with this program; if not, write to the Free Software
       
    15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    16 
       
    17 #include <config.h>
       
    18 #include "daemon.h"
       
    19 #include "misc/pwentry.h"
       
    20 #include "misc/lookup.h"
       
    21 
       
    22 static bool write_buf(int fd, const char* buf, unsigned length)
       
    23 {
       
    24   mystring str(buf, length);
       
    25   response resp(response::ok, str);
       
    26   return resp.write(fd);
       
    27 }
       
    28 
       
    29 CMD_FD(listdomain)
       
    30   // Usage: listdomain domainname password
       
    31 {
       
    32   mystring domain = args[0];
       
    33   mystring password = args[1];
       
    34   args[1] = LOG_ADMINPASS;
       
    35   logcommand(args);
       
    36   
       
    37   mystring baseuser(find_virtual(args[0]));
       
    38   if(!baseuser)
       
    39     RETURN(err, "Invalid or unknown domain name: " + args[0]);
       
    40   pwentry* pw;
       
    41   mystring v;
       
    42   if(!lookup_baseuser(baseuser, pw, v))
       
    43     RETURN(err, "Invalid or unknown base user name: " + baseuser);
       
    44   if(!pw->authenticate(password))
       
    45     RETURN(err, "Invalid or incorrect password");
       
    46   
       
    47   state = new saved_state(pw);
       
    48   vpwtable* table = state->domain.table();
       
    49   vpwtable_reader* reader = table->start_read();
       
    50   if(!*reader)
       
    51     RETURN(err, "Base user has no virtual password table");
       
    52 
       
    53   if(!write_buf(fd, "", 0))
       
    54     RETURN(err, "Failed while writing initial OK response");
       
    55 
       
    56   vpwentry entry;
       
    57   while(reader->get(entry)) {
       
    58     mystring code = entry.to_record();
       
    59     unsigned length = entry.name.length() + 1 + code.length() + 1;
       
    60     char buf[length];
       
    61     memcpy(buf, entry.name.c_str(), entry.name.length()+1);
       
    62     memcpy(buf+entry.name.length()+1, code.c_str(), code.length()+1);
       
    63     if(!write_buf(fd, buf, length))
       
    64       RETURN(err, "Failed while writing list entry");
       
    65   }
       
    66   
       
    67   delete reader;
       
    68   RETURN(ok, "");
       
    69 }