/*
 *  Login.app
 * 
 *  Copyright (c) 1997-2000 Per Liden
 * 
 *  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 <unistd.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <paths.h>
#include <sys/types.h>
#ifndef __FreeBSD__
#include <shadow.h>
#endif
#include <proplist.h>
#include "util.h"
#include "config.h"

#ifdef __FreeBSD__
extern char **environ;
#endif

static char* baseName(const char* name) {
   const char *base;

   base = name;
   while (*name) {
      if (*name == '/') {
         base = name + 1;
      }
      ++name;
   }
    
   return (char*) base;
}

static void execute(struct passwd *pw, const char* path, const char* script) {
   char* args[4];
   char* shell;
   char* shell_basename;
   char* cmd;

   shell = strdup(pw->pw_shell);
   shell_basename = baseName(shell);
   cmd = stringCombine(path, script);

   args[0] = alloc(strlen(shell_basename) + 2);
   strcpy(args[0], "-");
   strcat(args[0], shell_basename);
   args[1] = "-c";
   args[2] = stringCombine("exec ", cmd);
   args[3] = 0;

   free(cmd);

   execv(shell, args);
}

BOOL isPasswordCorrect(char* username, char* password) {
   char*  encrypted;
   char*  correct;
   struct passwd *pw;
#ifdef SHADOW_PASSWD
   struct spwd* sp;
#endif

   pw = getpwnam(username);
   endpwent();
   if (!pw) {
      return NO;
   }

#ifdef SHADOW_PASSWD
   sp = getspnam(pw->pw_name);    
   endspent();
   if(sp) {
      correct = sp->sp_pwdp;
   }
   else
#endif
      correct = pw->pw_passwd;
    
   if (correct == 0 || correct[0] == '\0') {
      return YES;
   }
    
   encrypted = crypt(password, correct);
   memset(password, 0, strlen(password));
    
   if (!strcmp(encrypted, correct)) {
      return YES;
   } else {
      return NO;
   }
}

void setEnvironment(char* username) {
   struct passwd* pw;
   char*          mail;

   /* Get password struct */
   pw = getpwnam(username);
   endpwent();
   if (!pw) {
      printError("user %s not found in /etc/passwd", username);
      exit(0);
   }

   /* Set environment */
   environ = alloc(sizeof(char*) * 2);
   environ[0] = 0;
   setenv("TERM", "vt100", 0);  // TERM=linux?
   setenv("HOME", pw->pw_dir, 1);
   setenv("SHELL", pw->pw_shell, 1);
   setenv("USER", pw->pw_name, 1);
   setenv("LOGNAME", pw->pw_name, 1);
   setenv("DISPLAY", ":0.0", 1);
   mail = alloc(strlen(_PATH_MAILDIR) + strlen(pw->pw_name) + 2);
   strcpy(mail, _PATH_MAILDIR);
   strcat(mail, "/");
   strcat(mail, pw->pw_name);
   setenv("MAIL", mail, 1);
   chdir(pw->pw_dir);
}

void switchUser(char* username, const char* path, const char* script) {
   struct passwd* pw;

   /* Get password struct */
   pw = getpwnam(username);
   endpwent();
   if (!pw) {
      printError("user %s not found in /etc/passwd", username);
      exit(0);
   }

   /* Set user id */
   if ((initgroups(pw->pw_name, pw->pw_gid) != 0) || 
       (setgid(pw->pw_gid) != 0) || (setuid(pw->pw_uid) != 0)) {
      printError("could not switch user id\n");
      exit(0);
   }

   /* Execute login command */
   execute(pw, path, script);
   printError("could not execute login command");
   exit(0);
}

