aboutsummaryrefslogtreecommitdiffstats
path: root/src/login.c
blob: 46bf4e2fe63f33011185ebdbe574896638f21f66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 * cerberus – Minimal login program
 * 
 * Copyright © 2013  Mattias Andrée (maandree@member.fsf.org)
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */
#include <pwd.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include "config.h"

#include "login.h"


/**
 * The environment variables
 */
extern char** environ;


/**
 * Set the user gid and uid
 * 
 * @param  entry  The user entry in the password file
 */
void set_user(struct passwd* entry)
{
  if (setgid(entry->pw_gid) && entry->pw_gid)
    {
      perror("setgid");
      _exit(1);
    }
  
  if (setuid(entry->pw_uid) && entry->pw_uid)
    {
      perror("setuid");
      _exit(1);
    }
}


/**
 * Change directory to the user's home directory
 * 
 * @param  entry  The user entry in the password file
 */
void chdir_home(struct passwd* entry)
{
  if (chdir(entry->pw_dir))
    {
      perror("chdir");
      if (chdir(DEFAULT_HOME))
	{
	  perror("chdir");
	  sleep(ERROR_SLEEP);
	  _exit(1);
	}
      entry->pw_dir = DEFAULT_HOME;
    }
}


/**
 * Make sure the shell to use is definied
 * 
 * @param  entry  The user entry in the password file
 */
void ensure_shell(struct passwd* entry)
{
  if ((entry->pw_shell && *(entry->pw_shell)) == 0)
    entry->pw_shell = DEFAULT_SHELL;
}


/**
 * Set environment variables
 * 
 * @param  entry         The user entry in the password file
 * @param  preserve_env  Whether to preserve the environment
 */
void set_environ(struct passwd* entry, char preserve_env)
{
  char* _term = getenv("TERM");
  char* term = NULL;
  if (_term)
    {
      int n = 0, i;
      while (*(_term + n++))
	;
      term = malloc(n * sizeof(char));
      if (term == NULL)
	{
	  perror("malloc");
	  sleep(ERROR_SLEEP);
	  _exit(1);
	}
      for (i = 0; i < n; i++)
	*(term + i) = *(_term + i);
    }
  
  if (preserve_env == 0)
    {
      environ = malloc(sizeof(char*));
      if (environ == NULL)
	{
	  perror("malloc");
	  sleep(ERROR_SLEEP);
	  _exit(1);
	}
      *environ = NULL;
    }
  
  setenv("HOME", entry->pw_dir, 1);
  setenv("USER", entry->pw_name, 1);
  setenv("LOGUSER", entry->pw_name, 1);
  setenv("SHELL", entry->pw_shell, 1);
  setenv("TERM", term ?: DEFAULT_TERM, 1);
  setenv("PATH", entry->pw_uid ? PATH : PATH_ROOT, 1);
  
  if (term)
    free(term);
}