File: after_login.c

package info (click to toggle)
francine 0.99.8%2Borig-2
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 608 kB
  • ctags: 262
  • sloc: ansic: 2,375; sh: 330; makefile: 15
file content (213 lines) | stat: -rw-r--r-- 5,707 bytes parent folder | download | duplicates (4)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* $Id: after_login.c,v 1.5 2001/04/22 15:20:38 japh Exp $
 * after_login.c  -- what to do after a login, before executing the shell
 *
 *             fancylogin uses ncurses to display a colorful login-
 *             screen with input-masks.
 *
 * 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, 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.
 *
 * Written by Richard Bergmair and Andreas Krennmair.
 */


#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "after_login.h"
#include "limits.h"
#include "definitions.h"
#include "environment.h"
#include "log_message.h"
#include "emergency.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <utmp.h>
#include <time.h>
#include <malloc.h>





static void print_welcome(struct passwd *userinfo);
static int print_lastlog(struct lastlog *oldlog);
static void print_motd(void);
static void check_mail(struct passwd *userinfo);
void after_login(uid_t uid, char *rmthost, struct lastlog *oldlog);





/*****************************************************************************/
/* print_welcome: prints the "Welcome, username" message after the login.    */
/*****************************************************************************/

static void
print_welcome (struct passwd *userinfo)
{
  char *name;
  if ((userinfo->pw_gecos == NULL) || (userinfo->pw_gecos[0] == 0))
    name=userinfo->pw_name;
  else
    name=strtok(userinfo->pw_gecos,",");

  printf ("Welcome, %s!\n\n", name);
}  



/*****************************************************************************/
/* print_lastlog: prints location and time of the last login.                */
/*****************************************************************************/

static int
print_lastlog (struct lastlog *oldlog)
{
  char *strtime;

  /* convert ll_time to a human-readable string */
  strtime = ctime (&(oldlog->ll_time));

  /* print some information */
  printf (oldlog->ll_time?"Last login was from %s, at %s\n":
                         "This is your first login\n", 
          oldlog->ll_line, strtime);

  return 0;
}



/*****************************************************************************/
/* print_motd: prints the message of the day                                 */
/*****************************************************************************/

static void
print_motd (void)
{
  FILE *motd;
  int c;

  if ((motd = fopen ("/etc/motd", "r")) != NULL)  /* open the file */
    {
      while ((c = fgetc(motd)) != EOF)            /* print the file */
        fputc (c, stdout);
      fclose (motd);                              /* close the file */
    }
}



/*****************************************************************************/
/* check_mail: checks the current state of the user's mailbox                */
/*****************************************************************************/

static void
check_mail(struct passwd *userinfo)
{
  struct stat statbuf;
  char *mailbox;  

  if (get_check_email())
    {
      if ((mailbox = getenv("MAILDIR"))!=NULL) 
        {
          char * newmail;
          newmail = malloc(strlen(mailbox) + 5);

          if (newmail)
            {
              /* What's the mailbox filename? */
              sprintf(newmail, "%s/new", mailbox);
    
              /* Now let's get some information on the file */
              if ((stat(newmail, &statbuf) != -1) && (statbuf.st_size != 0))
              /*
               * is st_mtime (time of last modification) greater than
               * st_atime (time of last access)?
               */
              if (statbuf.st_mtime > statbuf.st_atime)
                {
                  free(newmail);
                  printf(MSG_NEWMAIL);
	          return;
                }
  
              free(newmail);
	    }
	}
    
      mailbox = malloc(strlen(MAILBOX_PATH)+strlen(userinfo->pw_name)+1);

      if (mailbox)
	{

	  strcpy(mailbox,MAILBOX_PATH);
	  strcat(mailbox,userinfo->pw_name);

	  if ((stat(mailbox, &statbuf) == -1) || (statbuf.st_size == 0))
	    printf(MSG_NOMAIL);
	  else 
 
	    if (statbuf.st_atime > statbuf.st_mtime)
	      printf(MSG_SEENMAIL);
	    else
	      printf(MSG_NEWMAIL);
	}
      printf("\n\n");
  }
}
   



/*****************************************************************************/
/* after_login: All the stuff we have to do after a successful login         */
/*****************************************************************************/

void
after_login (uid_t uid, char *rmthost, struct lastlog *oldlog)
{
  struct passwd *userinfo;

  userinfo = getpwuid(uid);
  

  if (userinfo) 
    print_welcome (userinfo);
  print_lastlog (oldlog);
  print_motd ();
  printf ("\n");

  if (userinfo)
    check_mail(userinfo);

  fflush(stdout);
}



/*****************************************************************************/
/* (c) Copyright 1999-2000 Richard Bergmair, 2000-2001 Andreas Krennmair     */
/*****************************************************************************/