2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 #ifdef VERBOSE_REV_INFO
25 static char rcs_id[] = "$TOG: TermPrimSetUtmp.c /main/10 1998/04/03 17:11:42 mgreess $";
26 #endif /* VERBOSE_REV_INFO */
30 * (c) Copyright 1993, 1994, 1996 Hewlett-Packard Company *
31 * (c) Copyright 1993, 1994, 1996 International Business Machines Corp. *
32 * (c) Copyright 1993, 1994, 1996 Sun Microsystems, Inc. *
33 * (c) Copyright 1993, 1994, 1996 Novell, Inc. *
34 * (c) Copyright 1996 Digital Equipment Corporation. *
35 * (c) Copyright 1996 FUJITSU LIMITED. *
36 * (c) Copyright 1996 Hitachi. *
40 #include <TermPrimP.h>
41 #include <TermPrimSetUtmp.h>
42 #include <TermPrimUtil.h>
43 #include "TermPrimDebug.h"
44 #include "TermHeader.h"
46 /* for sigprocmask... */
52 /* for getpw*() and getlogin() calls... */
53 #define X_INCLUDE_PWD_H
54 #define X_INCLUDE_NETDB_H
55 #define X_INCLUDE_UNISTD_H
56 #define XOS_USE_XT_LOCKING
57 #include <X11/Xos_r.h>
60 #define UT_NO_pututline
64 #if OSMAJORVERSION > 8
67 #define UT_HOST ut_host
68 #define UT_NO_pututline
73 #define UT_HOST ut_host
74 #define UT_HOST_LEN ut_syslen
75 #define UT_NO_pututline
79 #define UT_HOST ut_host
80 #define UT_ADDR ut_addr
84 #define UT_HOST ut_host
85 #define UT_NO_pututline
89 /* /etc/utmp include files... */
92 #define getutent getutxent
93 #define getutid getutxid
94 #define getutline getutxline
96 #define pututline(entry) updwtmpx(UTMPX_FILE, entry)
98 #define pututline pututxline
99 #define setutent setutxent
100 #define endutent endutxent
104 #define ut_time ut_tv.tv_sec
107 #endif /* UT_UTMPX */
109 /* gethostbyname include files... */
110 #include <sys/socket.h>
111 #include <netinet/in.h>
113 typedef struct _utmpInfo {
115 struct _utmpInfo *next;
116 struct _utmpInfo *prev;
119 static utmpInfo _utmpInfoHead;
120 static utmpInfo *utmpInfoHead = &_utmpInfoHead;
123 _DtTermPrimUtmpAddEntry
128 utmpInfo *utmpInfoTmp;
132 /* malloc a new entry... */
133 utmpInfoTmp = (utmpInfo *) XtMalloc(sizeof(utmpInfo));
134 (void) memset(utmpInfoTmp, '\0', sizeof(utmpInfo));
136 /* fill in the structure... */
137 utmpInfoTmp->utmpLine = (char *) XtMalloc(strlen(utmpLine) + 1);
138 (void) strcpy(utmpInfoTmp->utmpLine, utmpLine);
140 /* insert it after the head of the list...
142 /* block all signals... */
143 (void) sigfillset(&newSigs);
144 (void) sigemptyset(&oldSigs);
145 (void) sigprocmask(SIG_BLOCK, &newSigs, &oldSigs);
146 /* insert the entry into the list... */
147 _DtTermProcessLock();
148 utmpInfoTmp->prev = utmpInfoHead;
149 utmpInfoTmp->next = utmpInfoHead->next;
150 utmpInfoHead->next = utmpInfoTmp;
151 if (utmpInfoTmp->next) {
152 utmpInfoTmp->next->prev = utmpInfoTmp;
154 _DtTermProcessUnlock();
155 /* restore signals... */
156 (void) sigprocmask(SIG_SETMASK, &oldSigs, (sigset_t *) 0);
165 utmpInfo *utmpInfoTmp;
169 _DtTermProcessLock();
171 /* find the entry... */
172 for (utmpInfoTmp = utmpInfoHead->next; utmpInfoTmp;
173 utmpInfoTmp = utmpInfoTmp->next) {
174 if (!strcmp(utmpInfoTmp->utmpLine, utmpLine)) {
179 /* did we find anything... */
182 _DtTermProcessUnlock();
186 /* delete entry from the list...
188 /* block all signals... */
189 (void) sigfillset(&newSigs);
190 (void) sigemptyset(&oldSigs);
191 (void) sigprocmask(SIG_BLOCK, &newSigs, &oldSigs);
193 utmpInfoTmp->prev->next = utmpInfoTmp->next;
194 if (utmpInfoTmp->next) {
195 utmpInfoTmp->next->prev = utmpInfoTmp->prev;
197 /* restore signals... */
198 (void) sigprocmask(SIG_SETMASK, &oldSigs, (sigset_t *) 0);
200 /* free up the data... */
201 if (utmpInfoTmp->utmpLine) {
202 (void) XtFree(utmpInfoTmp->utmpLine);
203 utmpInfoTmp->utmpLine = (char *) 0;
205 (void) XtFree((char *) utmpInfoTmp);
206 _DtTermProcessUnlock();
209 static char *userName = (char *) 0;
210 Atom _DtTermPrim_XA_UtmpLine = (Atom) 0;
212 static char *localHostname = (char *) 0;
215 #ifdef UT_NO_pututline
217 _pututline(struct utmp *ut)
219 (void) pututline(ut);
222 #endif /* UT_NO_pututline */
224 /* the following things should be done in the parent so that they will
225 * be available to all utmp entry creates which are done in the child.
226 * If we wait until we do the create, the info will not be passed back
227 * to the parent, and we will have to do it each and every time...
230 _DtTermPrimUtmpInit(Widget w)
234 struct passwd * pw_ret;
235 _Xgetpwparams pw_buf;
238 _Xgetloginparams login_buf;
241 struct hostent * name_ret;
242 _Xgethostbynameparams name_buf;
245 _DtTermProcessLock();
248 /* getpw{uid,nam}_r routines fail on IBM when searching passwd info via NIS.
249 The AIX specific code could be removed after IBM fixes the problem. Note
250 that there could still be a failure on IBM platform if "USER" env variable
251 is not defined. In any case, we would really not want to rely on the env
254 #if defined(XTHREADS) && defined(XUSE_MTSAFE_API) && defined(AIXV3)
255 if ((c = getenv("USER")) == NULL) {
257 /* get the user's name from:
258 * -/etc/utmp if possible,
259 * -/etc/passwd if not.
261 if ((((namebuf = _XGetlogin(login_buf))) != NULL)
262 && (((pw_ret = _XGetpwnam(namebuf, pw_buf))) != NULL)
263 && (pw_ret->pw_uid == getuid())) {
264 userName = XtMalloc(strlen(namebuf) + 1);
265 (void) strcpy(userName, namebuf);
266 } else if (((pw_ret = _XGetpwuid(getuid(), pw_buf))) != NULL) {
267 userName = XtMalloc(strlen(pw_ret->pw_name) + 1);
268 (void) strcpy(userName, pw_ret->pw_name);
270 #if defined(XTHREADS) && defined(XUSE_MTSAFE_API) && defined(AIXV3)
273 userName = XtMalloc(strlen(c) + 1);
274 (void) strcpy(userName, c);
279 if (!_DtTermPrim_XA_UtmpLine) {
280 _DtTermPrim_XA_UtmpLine = XInternAtom(XtDisplay(w), TermUtmpIdString, False);
284 if (!localHostname && !gethostname(buffer, sizeof(buffer)) &&
285 (name_ret = _XGethostbyname(buffer, name_buf)) != NULL) {
286 localHostname = XtMalloc(strlen(buffer) + 1);
287 (void) strcpy(localHostname, buffer);
290 _DtTermProcessUnlock();
294 _DtTermPrimUtmpGetUtLine(int pty, char *ptyName)
296 Boolean closePty = False;
300 /* use the same pty name that everyone else will use (the one
301 * returned by ttyname())...
303 _Xttynameparams tty_buf;
305 /* if we weren't passed a pty, let's try opening ptyName. By using
306 * O_NOCTTY we are able to open the pty without accidentally becoming
307 * the session leader for it...
309 if ((pty < 0) && ((pty = open(ptyName, O_RDWR | O_NOCTTY, 0)) >= 0)) {
313 /* if we have a pty, use ttyname to get it's "canonical" name... */
314 if ((pty >= 0) && (c = _XTtyname(pty, tty_buf))) {
318 /* close the pty if we opened it... */
325 if (!strncmp(ptyName, "/dev/", strlen("/dev/"))) {
326 ptyName = ptyName + strlen("/dev/");
329 c = XtMalloc(strlen(ptyName) + 1);
330 (void) strcpy(c, ptyName);
336 UtmpEntryCreate(Widget w, pid_t pid, char *utmpLine)
338 #if !defined(CSRG_BASED) /* XXX */
339 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
340 struct termData *tpd = tw->term.tpd;
346 Boolean closePty = False;
351 struct sockaddr_in from;
355 /* initialize utmp stuff, just incase... */
356 (void) _DtTermPrimUtmpInit(w);
358 /* set up the entry to search for...
361 /* create the ut_line... */
362 (void) strncpy(ut.ut_line, utmpLine, sizeof(ut.ut_line));
364 ut.ut_type = DEAD_PROCESS;
366 /* position to entry in utmp file... */
368 if (NULL == (utPtr = getutline(&ut))) {
369 /* build a base utmp entry... */
372 if (c = strstr(utmpLine, "tty")) {
374 } else if (c = strstr(utmpLine, "pts")) {
378 if (strlen(utmpLine) > sizeof(utPtr->ut_id)) {
379 c += strlen(utmpLine) - sizeof(utPtr->ut_id);
382 (void) strncpy(utPtr->ut_id, c, sizeof(utPtr->ut_id));
385 (void) strncpy(utPtr->ut_id, utmpLine,
386 sizeof(utPtr->ut_id));
388 #if defined(linux) || defined(sun)
389 if (c = strchr(utmpLine, '/')) {
394 (void) strncpy(utPtr->ut_id, c, sizeof(utPtr->ut_id));
395 #else /* linux || sun */
396 error out -- missing code for utPtr->ut_id
402 /* set up the new entry... */
403 utPtr->ut_type = USER_PROCESS;
405 utPtr->ut_exit.e_termination = 0;
406 utPtr->ut_exit.e_exit = 2;
409 snprintf(utPtr->ut_user, sizeof(utPtr->ut_user),
410 "%s", (userName && *userName) ? userName : "????");
411 (void) strncpy(utPtr->ut_line, utmpLine, sizeof(utPtr->ut_line));
414 utPtr->ut_time = now;
416 /* clear and set the host and addr fields... */
418 (void) memset(utPtr->UT_HOST, '\0', sizeof(utPtr->UT_HOST));
419 /* stuff the display name into ut_host. We will stuff as much as
420 * will fit dropping domain chunks as necessary to make it fit. If
421 * we still can't fit with the entire domain removed, we will truncate
422 * the display name...
424 displayName = DisplayString(XtDisplay(w));
425 (void) strncpy(utPtr->UT_HOST, displayName, sizeof(utPtr->UT_HOST));
427 utPtr->UT_HOST_LEN = strlen(displayName + 1);
428 #endif /* UT_HOST_LEN */
429 if (sizeof(utPtr->UT_HOST) < strlen(displayName)) {
430 /* pull off the seat number... */
431 seat = strchr(displayName, ':');
432 /* back up through the display name. Each time we hit a '.' that
433 * signals the start of a new domain chunk, see if we can fit
434 * the display name (less these domain chunks) plus the seat number
435 * into the structure...
437 if (seat - displayName < sizeof(utPtr->UT_HOST)) {
438 c = utPtr->UT_HOST + (seat - displayName) - 1;
440 c = utPtr->UT_HOST + sizeof(utPtr->UT_HOST) - 1;
442 for (; c >= utPtr->UT_HOST; c--) {
444 if ((*c == '.') && (c - utPtr->UT_HOST + strlen(seat) <
445 sizeof(utPtr->UT_HOST))) {
446 /* everything left of the dot plus the seat will fit!... */
450 if (c >= utPtr->UT_HOST) {
451 /* we can perform a fit with some domains stripped... */
452 (void) strncpy(c, seat, sizeof(utPtr->UT_HOST) -
453 (c - utPtr->UT_HOST));
454 if ((c - utPtr->UT_HOST) + strlen(seat) < sizeof(utPtr->UT_HOST)) {
455 /* null out the end of the host name... */
456 utPtr->UT_HOST[c - utPtr->UT_HOST + strlen(seat)] = '\0';
458 utPtr->UT_HOST_LEN = c - utPtr->UT_HOST + strlen(seat) + 1;
459 #endif /* UT_HOST_LEN */
462 /* we can't fit even a single full chunk from the domain.
463 * since it is more important to get the seat number in (the
464 * host can be obtained from the addr), truncate the host.
466 (void) strncpy(utPtr->UT_HOST - strlen(seat), seat,
469 utPtr->UT_HOST_LEN = strlen(seat);
470 #endif /* UT_HOST_LEN */
476 (void) memset(&utPtr->ut_addr, '\0', sizeof(utPtr->ut_addr));
478 /* get the canonical host of the X socket... */
479 fromLen = sizeof(from);
480 if (!getpeername(ConnectionNumber(XtDisplay(w)), &from, &fromLen) &&
481 (from.sin_family == AF_INET)) {
482 utPtr->ut_addr = from.sin_addr.s_addr;
486 /* write it out... */
487 if (_pututline(utPtr)) {
496 #else /* __OpenBSD__ */
501 /* this is a public wrapper around the previous function that runs the
502 * previous function setuid root...
505 _DtTermPrimUtmpEntryCreate(Widget w, pid_t pid, char *utmpLine)
509 /* this function needs to be suid root... */
510 (void) _DtTermPrimToggleSuidRoot(True);
511 retValue = UtmpEntryCreate(w, pid, utmpLine);
512 /* we now need to turn off setuid root... */
513 (void) _DtTermPrimToggleSuidRoot(False);
519 UtmpEntryDestroy(Widget w, char *utmpLine)
521 #if !defined(CSRG_BASED)
526 ut.ut_type = USER_PROCESS;
527 snprintf(ut.ut_line, sizeof(ut.ut_line), "%s", utmpLine);
529 if (utPtr = getutline(&ut)) {
530 utPtr->ut_type = DEAD_PROCESS;
532 utPtr->ut_exit.e_termination = 0;
533 utPtr->ut_exit.e_exit = 0;
536 utPtr->ut_time = now;
538 (void) memset(utPtr->ut_host, '\0', sizeof(utPtr->ut_host));
541 (void) memset(&utPtr->ut_addr, '\0', sizeof(utPtr->ut_addr));
543 (void) pututline(utPtr);
547 (void) DeleteUtmpInfo(utmpLine);
548 #endif /* !__OpenBSD__ */
551 /* this is a public wrapper around the previous function that runs the
552 * previous function setuid root...
555 _DtTermPrimUtmpEntryDestroy(Widget w, char *utmpLine)
557 /* this function needs to be suid root... */
558 (void) _DtTermPrimToggleSuidRoot(True);
559 (void) UtmpEntryDestroy(w, utmpLine);
560 /* we now need to turn off setuid root... */
561 (void) _DtTermPrimToggleSuidRoot(False);
565 _DtTermPrimUtmpCleanup()
567 DebugF('s', 10, fprintf(stderr, ">>_DtTermPrimUtmpCleanup() starting\n"));
568 _DtTermProcessLock();
569 while (utmpInfoHead->next && utmpInfoHead->next->utmpLine) {
570 DebugF('s', 10, fprintf(stderr, ">>resetting utmp for id \"%s\"\n",
571 utmpInfoHead->next->utmpLine));
572 (void) _DtTermPrimUtmpEntryDestroy((Widget) 0,
573 utmpInfoHead->next->utmpLine);
575 _DtTermProcessUnlock();
576 DebugF('s', 10, fprintf(stderr, ">>_DtTermPrimUtmpCleanup() finished\n"));