3 Copyright 1990, 1998 The Open Group
4 Copyright 2005 Oswald Buddenhagen <ossi@kde.org>
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 OTHER DEALINGS IN THE SOFTWARE.
23 Except as contained in this notice, the name of The Open Group shall
24 not be used in advertising or otherwise to promote the sale, use or
25 other dealings in this Software without prior written authorization
32 Author: Keith Packard, MIT X Consortium
33 Lastlog support and dynamic utmp entry allocation
34 by Andreas Stolcke <stolcke@icsi.berkeley.edu>
38 #define _FILE_OFFSET_BITS 64
42 #if defined(__svr4__) || defined(__Lynx__) || defined(__QNX__) || defined(__APPLE__) || defined(_SEQUENT_) /*|| defined(USE_PAM)*/
47 # ifdef HAVE_LASTLOG_H
51 # ifdef _PATH_LASTLOGX
52 # define LLOG_FILE _PATH_LASTLOGX
53 # elif defined(_PATH_LASTLOG)
54 # define LLOG_FILE _PATH_LASTLOG
56 # define LLOG_FILE "/usr/adm/lastlog"
62 # if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
63 /* *BSD doesn't like a ':0' type entry in utmp */
70 # define TTYS_FILE "/etc/ttys"
75 # define UTL_PFX "xdm/"
76 # define UTL_OFF strlen(UTL_PFX)
83 crc32s( const char *str
)
86 unsigned crc
= 0xffffffff, by
;
89 by
= (crc
& 255) ^ *(const unsigned char *)str
;
90 for (b
= 0; b
< 8; b
++)
91 by
= (by
>> 1) ^ (-(by
& 1) & 0xedb88320);
92 crc
= (crc
>> 8) ^ by
;
99 sessreg( struct display
*d
, int pid
, const char *user
, int uid
)
101 const char *dot
, *colon
;
105 int utmp
, slot
, freeslot
;
114 # ifdef HAVE_LASTLOGX
116 # define ll_time ll_tv.tv_sec
127 bzero( &ut_ent
, sizeof(ut_ent
) );
130 strncpy( ut_ent
.ut_user
, user
, sizeof(ut_ent
.ut_user
) );
133 ut_ent
.ut_type
= USER_PROCESS
;
135 ut_ent
.ut_type
= DEAD_PROCESS
;
138 ut_ent
.ut_time
= time( 0 );
140 colon
= strchr( d
->name
, ':' );
141 clen
= strlen( colon
);
142 if (clen
> (int)(sizeof(ut_ent
.ut_line
) - UTL_OFF
) - 2)
143 return; /* uhm, well ... */
144 if (colon
== d
->name
) {
146 strncpy( ut_ent
.ut_id
, d
->name
, sizeof(ut_ent
.ut_id
) );
150 #ifdef HAVE_STRUCT_UTMP_UT_HOST
155 if (colon
- d
->name
> (int)sizeof(ut_ent
.ut_host
)) {
156 ut_ent
.ut_host
[0] = '~';
157 memcpy( ut_ent
.ut_host
+ 1,
158 colon
- (sizeof(ut_ent
.ut_host
) - 1),
159 sizeof(ut_ent
.ut_host
) - 1 );
161 memcpy( ut_ent
.ut_host
, d
->name
, colon
- d
->name
);
165 crc
= crc32s( d
->name
);
166 ut_ent
.ut_id
[0] = crc
% 26 + 'A';
168 for (i
= 1; i
< sizeof(ut_ent
.ut_id
); i
++) {
171 ut_ent
.ut_id
[i
] = c
< 26 ? c
+ 'A' :
172 c
< 52 ? c
- 26 + 'a' : c
- 52 + '0';
175 left
= sizeof(ut_ent
.ut_line
) - UTL_OFF
- clen
;
176 if (colon
- d
->name
<= left
) {
177 clen
+= colon
- d
->name
;
181 dot
= strchr( d
->name
, '.' );
182 if (dot
&& dot
- d
->name
< left
) {
183 memcpy( ut_ent
.ut_line
+ UTL_OFF
, d
->name
, left
- 1 );
184 ut_ent
.ut_line
[UTL_OFF
+ left
- 1] = '~';
186 memcpy( ut_ent
.ut_line
+ UTL_OFF
, d
->name
, left
/2 - 1 );
187 ut_ent
.ut_line
[UTL_OFF
+ left
/2 - 1] = '~';
189 memcpy( ut_ent
.ut_line
+ UTL_OFF
+ left
/2,
190 dot
- (left
- left
/2 - 1),
192 ut_ent
.ut_line
[UTL_OFF
+ left
- 1] = '~';
194 memcpy( ut_ent
.ut_line
+ UTL_OFF
+ left
/2,
195 colon
- (left
- left
/2), left
- left
/2 );
200 memcpy( ut_ent
.ut_line
, UTL_PFX
, UTL_OFF
);
202 memcpy( ut_ent
.ut_line
+ UTL_OFF
+ left
, colon
, clen
);
206 if ((utmp
= open( UTMP_FILE
, O_RDWR
)) < 0)
207 debug( "cannot open utmp file " UTMP_FILE
": %m\n" );
212 if (!(ttys
= fopen( TTYS_FILE
, "r" )))
213 logWarn( "Cannot open tty file " TTYS_FILE
": %m\n" );
216 while ((c
= getc( ttys
)) != EOF
)
228 lseek( utmp
, slot
* sizeof(entry
), SEEK_SET
);
229 while (read( utmp
, (char *)&entry
, sizeof(entry
) ) == sizeof(entry
)) {
230 if (!strncmp( entry
.ut_line
, ut_ent
.ut_line
,
231 sizeof(entry
.ut_line
) ))
232 # ifdef HAVE_STRUCT_UTMP_UT_HOST
233 if (!strncmp( entry
.ut_host
, ut_ent
.ut_host
,
234 sizeof(entry
.ut_host
) ))
237 if (freeslot
< 0 && *entry
.ut_user
== '\0')
242 debug( "utmp entry for display %s vanished\n", d
->name
);
249 # ifdef HAVE_STRUCT_UTMP_UT_HOST
251 bzero( ut_ent
.ut_host
, sizeof(ut_ent
.ut_host
) );
253 lseek( utmp
, slot
* sizeof(ut_ent
), SEEK_SET
);
254 if (write( utmp
, (char *)&ut_ent
, sizeof(ut_ent
) ) != sizeof(ut_ent
))
255 logError( "Cannot write utmp file " UTMP_FILE
": %m\n" );
260 UTMPNAME( UTMP_FILE
);
262 PUTUTLINE( &ut_ent
);
269 updwtmpx( WTMP_FILE
, &ut_ent
);
271 updwtmp( WTMP_FILE
, &ut_ent
);
274 if ((wtmp
= open( WTMP_FILE
, O_WRONLY
|O_APPEND
)) < 0)
275 debug( "cannot open wtmp file " WTMP_FILE
": %m\n" );
277 if (write( wtmp
, (char *)&ut_ent
, sizeof(ut_ent
) ) != sizeof(ut_ent
))
278 logError( "Cannot write wtmp file " WTMP_FILE
": %m\n" );
285 bzero( (char *)&ll
, sizeof(ll
) );
286 ll
.ll_time
= ut_ent
.ut_time
;
287 memcpy( ll
.ll_line
, ut_ent
.ut_line
, sizeof(ll
.ll_line
) );
288 memcpy( ll
.ll_host
, ut_ent
.ut_host
, sizeof(ll
.ll_host
) );
289 # ifdef HAVE_LASTLOGX
290 updlastlogx( LLOG_FILE
, uid
, &ll
);
292 if ((llog
= open( LLOG_FILE
, O_RDWR
)) < 0)
293 debug( "cannot open lastlog file " LLOG_FILE
": %m\n" );
295 lseek( llog
, (off_t
)uid
* sizeof(ll
), SEEK_SET
);
296 if (write( llog
, (char *)&ll
, sizeof(ll
) ) != sizeof(ll
))
297 logError( "Cannot write llog file " WTMP_FILE
": %m\n" );
308 char tmp
[sizeof("/dev/") + sizeof(ut_ent
.ut_line
)];
309 mkdir( "/dev/" UTL_PFX
, 0755 );
310 chmod( "/dev/" UTL_PFX
, 0755 );
311 sprintf( tmp
, "/dev/%.*s", sizeof(ut_ent
.ut_line
), ut_ent
.ut_line
);
313 close( creat( tmp
, 0644 ) );