not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kdm / backend / sessreg.c
blob5b538cb6b2e4b2261c357246ece428f8a23a6f40
1 /*
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
10 documentation.
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
26 from The Open Group.
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
39 #include "dm.h"
40 #include "dm_error.h"
42 #if defined(__svr4__) || defined(__Lynx__) || defined(__QNX__) || defined(__APPLE__) || defined(_SEQUENT_) /*|| defined(USE_PAM)*/
43 # define NO_LASTLOG
44 #endif
46 #ifndef NO_LASTLOG
47 # ifdef HAVE_LASTLOG_H
48 # include <lastlog.h>
49 # endif
50 # ifndef LLOG_FILE
51 # ifdef _PATH_LASTLOGX
52 # define LLOG_FILE _PATH_LASTLOGX
53 # elif defined(_PATH_LASTLOG)
54 # define LLOG_FILE _PATH_LASTLOG
55 # else
56 # define LLOG_FILE "/usr/adm/lastlog"
57 # endif
58 # endif
59 #endif
61 #ifdef BSD
62 # if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
63 /* *BSD doesn't like a ':0' type entry in utmp */
64 # define NO_UTMP
65 # endif
66 #endif
68 #ifdef BSD_UTMP
69 # ifndef TTYS_FILE
70 # define TTYS_FILE "/etc/ttys"
71 # endif
72 #endif
74 #ifdef _AIX
75 # define UTL_PFX "xdm/"
76 # define UTL_OFF strlen(UTL_PFX)
77 #else
78 # define UTL_OFF 0
79 #endif
81 #ifndef BSD_UTMP
82 static unsigned
83 crc32s( const char *str )
85 int b;
86 unsigned crc = 0xffffffff, by;
88 for (; *str; str++) {
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;
94 return crc;
96 #endif
98 void
99 sessreg( struct display *d, int pid, const char *user, int uid )
101 const char *dot, *colon;
102 int left, clen, c;
103 #ifdef BSD_UTMP
104 FILE *ttys;
105 int utmp, slot, freeslot;
106 STRUCTUTMP entry;
107 #else
108 unsigned crc, i;
109 #endif
110 #ifndef HAVE_UPDWTMP
111 int wtmp;
112 #endif
113 #ifndef NO_LASTLOG
114 # ifdef HAVE_LASTLOGX
115 struct lastlogx ll;
116 # define ll_time ll_tv.tv_sec
117 # else
118 int llog;
119 struct lastlog ll;
120 # endif
121 #endif
122 STRUCTUTMP ut_ent;
124 if (!d->useSessReg)
125 return;
127 bzero( &ut_ent, sizeof(ut_ent) );
129 if (pid) {
130 strncpy( ut_ent.ut_user, user, sizeof(ut_ent.ut_user) );
131 #ifndef BSD_UTMP
132 ut_ent.ut_pid = pid;
133 ut_ent.ut_type = USER_PROCESS;
134 } else {
135 ut_ent.ut_type = DEAD_PROCESS;
136 #endif
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) {
145 #ifndef BSD_UTMP
146 strncpy( ut_ent.ut_id, d->name, sizeof(ut_ent.ut_id) );
147 #endif
148 left = 0;
149 } else {
150 #ifdef HAVE_STRUCT_UTMP_UT_HOST
151 # ifndef BSD_UTMP
152 if (pid)
153 # endif
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 );
160 } else
161 memcpy( ut_ent.ut_host, d->name, colon - d->name );
163 #endif
164 #ifndef BSD_UTMP
165 crc = crc32s( d->name );
166 ut_ent.ut_id[0] = crc % 26 + 'A';
167 crc /= 26;
168 for (i = 1; i < sizeof(ut_ent.ut_id); i++) {
169 c = crc % 62;
170 crc /= 62;
171 ut_ent.ut_id[i] = c < 26 ? c + 'A' :
172 c < 52 ? c - 26 + 'a' : c - 52 + '0';
174 #endif
175 left = sizeof(ut_ent.ut_line) - UTL_OFF - clen;
176 if (colon - d->name <= left) {
177 clen += colon - d->name;
178 colon = d->name;
179 left = 0;
180 } else {
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] = '~';
185 } else {
186 memcpy( ut_ent.ut_line + UTL_OFF, d->name, left/2 - 1 );
187 ut_ent.ut_line[UTL_OFF + left/2 - 1] = '~';
188 if (dot) {
189 memcpy( ut_ent.ut_line + UTL_OFF + left/2,
190 dot - (left - left/2 - 1),
191 left - left/2 - 1 );
192 ut_ent.ut_line[UTL_OFF + left - 1] = '~';
193 } else
194 memcpy( ut_ent.ut_line + UTL_OFF + left/2,
195 colon - (left - left/2), left - left/2 );
199 #ifdef UTL_PFX
200 memcpy( ut_ent.ut_line, UTL_PFX, UTL_OFF );
201 #endif
202 memcpy( ut_ent.ut_line + UTL_OFF + left, colon, clen );
204 #ifndef NO_UTMP
205 # ifdef BSD_UTMP
206 if ((utmp = open( UTMP_FILE, O_RDWR )) < 0)
207 debug( "cannot open utmp file " UTMP_FILE ": %m\n" );
208 else {
210 slot = 1;
211 if (pid) {
212 if (!(ttys = fopen( TTYS_FILE, "r" )))
213 logWarn( "Cannot open tty file " TTYS_FILE ": %m\n" );
214 else {
215 int column0 = True;
216 while ((c = getc( ttys )) != EOF)
217 if (c == '\n') {
218 slot++;
219 column0 = True;
220 } else
221 column0 = False;
222 if (!column0)
223 slot++;
224 fclose( ttys );
227 freeslot = -1;
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) ))
235 # endif
236 goto found;
237 if (freeslot < 0 && *entry.ut_user == '\0')
238 freeslot = slot;
239 slot++;
241 if (!pid) {
242 debug( "utmp entry for display %s vanished\n", d->name );
243 goto skip;
245 if (freeslot >= 0)
246 slot = freeslot;
247 found:
249 # ifdef HAVE_STRUCT_UTMP_UT_HOST
250 if (!pid)
251 bzero( ut_ent.ut_host, sizeof(ut_ent.ut_host) );
252 # endif
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" );
256 skip:
257 close( utmp );
259 # else
260 UTMPNAME( UTMP_FILE );
261 SETUTENT();
262 PUTUTLINE( &ut_ent );
263 ENDUTENT();
264 # endif
265 #endif
267 #ifdef HAVE_UPDWTMP
268 # ifdef HAVE_UTMPX
269 updwtmpx( WTMP_FILE, &ut_ent );
270 # else
271 updwtmp( WTMP_FILE, &ut_ent );
272 # endif
273 #else
274 if ((wtmp = open( WTMP_FILE, O_WRONLY|O_APPEND )) < 0)
275 debug( "cannot open wtmp file " WTMP_FILE ": %m\n" );
276 else {
277 if (write( wtmp, (char *)&ut_ent, sizeof(ut_ent) ) != sizeof(ut_ent))
278 logError( "Cannot write wtmp file " WTMP_FILE ": %m\n" );
279 close( wtmp );
281 #endif
283 #ifndef NO_LASTLOG
284 if (pid) {
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 );
291 # else
292 if ((llog = open( LLOG_FILE, O_RDWR )) < 0)
293 debug( "cannot open lastlog file " LLOG_FILE ": %m\n" );
294 else {
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" );
298 close( llog );
300 # endif
302 #else
303 (void)uid;
304 #endif
306 #ifdef UTL_PFX
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 );
312 if (pid)
313 close( creat( tmp, 0644 ) );
314 else
315 unlink( tmp );
317 #endif