/* * Search utmp for the local user */ int find_user(char *name, char *tty, size_t ttyl) { struct utmp ubuf, ubuf1; int status; FILE *fp; char line[UT_LINESIZE+1]; char ftty[MAXPATHLEN]; time_t idle, now; time(&now); idle = INT_MAX; if ((fp = fopen(_PATH_UTMP, "r")) == NULL) { fprintf(stderr, "talkd: can't read %s.\n", _PATH_UTMP); return (FAILED); } #define SCMPN(a, b) strncmp(a, b, sizeof(a)) status = NOT_HERE; (void) strlcpy(ftty, _PATH_DEV, sizeof(ftty)); while (fread((char *) &ubuf, sizeof(ubuf), 1, fp) == 1) if (SCMPN(ubuf.ut_name, name) == 0) { if (*tty == '\0') { /* no particular tty was requested */ struct stat statb; memcpy(line, ubuf.ut_line, UT_LINESIZE); line[sizeof(line)-1] = '\0'; ftty[sizeof(_PATH_DEV)-1] = '\0'; strlcat(ftty, line, sizeof(ftty)); if (stat(ftty, &statb) == 0) { if (!(statb.st_mode & S_IWGRP)) { if (status == NOT_HERE) status = PERMISSION_DENIED; } else if (now - statb.st_atime < idle) { idle = now - statb.st_atime; status = SUCCESS; ubuf1 = ubuf; } } } else if (SCMPN(ubuf.ut_line, tty) == 0) { status = SUCCESS; break; } } fclose(fp); if (*tty == '\0' && status == SUCCESS) { memcpy(line, ubuf1.ut_line, UT_LINESIZE); line[sizeof(line)-1] = '\0'; strlcpy(tty, line, ttyl); } return (status); }
int find_user(char *name, char *tty, char *disp) { struct utmp ubuf; int status; FILE *fd; struct stat statb; char ftty[20]; char ttyFound[UT_LINESIZE] = ""; char dispFound[UT_HOSTSIZE] = ""; if (!(fd = fopen(_PATH_UTMP, "r"))) { fprintf(stderr, "talkd: can't read %s.\n", _PATH_UTMP); return (FAILED); } #define SCMPN(a, b) strncmp(a, b, sizeof (a)) status = NOT_HERE; (void) strcpy(ftty, _PATH_DEV); while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1) if (!SCMPN(ubuf.ut_name, name)) { if (*tty == '\0') { /* no particular tty was requested */ /* if (ttyFound == "") status = PERMISSION_DENIED; */ (void) strcpy(ftty+5, ubuf.ut_line); if (stat(ftty,&statb) == 0) { if (!(statb.st_mode & 020)) /* ?character device? */ continue; (void) strcpy(ttyFound, ubuf.ut_line); #ifdef USE_UT_HOST (void) strcpy(dispFound, ubuf.ut_host); strcat(dispFound, " "); #endif status = SUCCESS; syslog(LOG_DEBUG, "%s", ttyFound); if ((int) ttyFound[3] > (int) 'f') { #ifdef USE_UT_HOST if (Options.debug_mode) { syslog(LOG_DEBUG, "I wanna this:%s", ttyFound); syslog(LOG_DEBUG, "ut_host=%s", ubuf.ut_host); syslog(LOG_DEBUG, "%s", ubuf.ut_line); } #endif break; } } } if (!strcmp(ubuf.ut_line, tty)) { status = SUCCESS; break; } } fclose(fd); if (status == SUCCESS) { (void) strcpy(tty, ttyFound); (void) strcpy(disp, dispFound); } return (status); }
/* * Search utmp for the local user */ int find_user(const char *name, char *tty) { struct utmpentry *ep; int status; struct stat statb; time_t best = 0; char ftty[sizeof(_PATH_DEV) + sizeof(ep->line)]; getutentries(NULL, &ep); #define SCMPN(a, b) strncmp(a, b, sizeof (a)) status = NOT_HERE; (void) strcpy(ftty, _PATH_DEV); for (; ep; ep = ep->next) if (SCMPN(ep->name, name) == 0) { if (*tty == '\0' || best != 0) { if (best == 0) status = PERMISSION_DENIED; /* no particular tty was requested */ (void) strcpy(ftty + sizeof(_PATH_DEV) - 1, ep->line); if (stat(ftty, &statb) == 0) { if (!(statb.st_mode & 020)) continue; if (statb.st_atime > best) { best = statb.st_atime; (void) strcpy(tty, ep->line); status = SUCCESS; continue; } } } if (strcmp(ep->line, tty) == 0) { status = SUCCESS; break; } } return (status); }
int find_user(char *name, char *tty, char *disp) { struct utmp *ubuf; int prio = 0, status = NOT_HERE; struct stat statb; char ftty[20]; char *ntty, *dpy; char ttyFound[UT_LINESIZE] = ""; char dispFound[UT_HOSTSIZE] = ""; strcpy(ftty, _PATH_DEV); ntty = ftty + strlen(ftty); setutent(); while ((ubuf = getutent())) { if ((ubuf->ut_type == USER_PROCESS) && (!SCMPN(ubuf->ut_name, name))) { if (*tty == '\0') { /* no particular tty was requested */ if (Options.XAnnounce && ubuf->ut_line[0] == ':') { /* this is a XDM login (really?). GREAT! */ syslog(LOG_DEBUG, "XDM login: %s at %s", name, ubuf->ut_line); status = SUCCESS; if (prio < PRIO_XDM) { strcpy(dispFound, ubuf->ut_line); strcat(dispFound, " "); prio = PRIO_XDM; } continue; } strcpy(ntty, ubuf->ut_line); if (stat(ftty, &statb) != 0 || (!(statb.st_mode & 020))) { message("Permission denied on %s", ntty); continue; /* not a char dev */ } /* device exists and is a character device */ status = SUCCESS; if (Options.debug_mode) syslog(LOG_DEBUG, "Found %s at %s", name, ubuf->ut_line); if (prio < PRIO_LOGIN) { prio = PRIO_LOGIN; strcpy(ttyFound, ubuf->ut_line); *dispFound = '\0'; } /* the following code is Linux specific... * is there a portable way to * 1) determine if a device is a pseudo terminal and * 2) get environment variables of an arbitrary process? */ if (strncmp("tty", ubuf->ut_line, 3) != 0 || !strchr("pqrstuvwxyzabcde", ubuf->ut_line[3])) continue; /* not a pty */ if (!strchr(ubuf->ut_host, ':')) continue; /* ut_host is not a display (e.g. telnet connection) */ /* device is a pseudo terminal (ex : a xterm) */ if (Options.debug_mode) syslog(LOG_DEBUG, "PTY %s, ut_host=%s", ubuf->ut_line, ubuf->ut_host); if (prio < PRIO_PTY) { prio = PRIO_PTY; strcpy(ttyFound, ubuf->ut_line); strcpy(dispFound, ubuf->ut_host); strcat(dispFound, " "); } dpy = get_display(ubuf->ut_pid); if (!dpy) continue; /* DISPLAY not set or empty */ /* $DISPLAY is set. */ if (Options.debug_mode) syslog(LOG_DEBUG, "Found display %s on %s", dpy, ubuf->ut_line); if (prio < PRIO_DISPLAY) { prio = PRIO_DISPLAY; strcpy(ttyFound, ubuf->ut_line); strcpy(dispFound, dpy+1); /*no space*/ strcat(dispFound, " "); } delete dpy; continue; } if (!strcmp(ubuf->ut_line, tty)) { status = SUCCESS; break; } } } endutent(); message("End of Utmp reading"); #if defined(HAVE_KDE) && defined(ALL_PROCESSES_AND_PROC_FIND_USER) if (Options.XAnnounce && prio < PRIO_DISPLAY) if (find_X_process(name, dispFound)) { message(dispFound); status=SUCCESS; } #endif if (status == SUCCESS) { (void) strcpy(tty, ttyFound); (void) strcpy(disp, dispFound); if (Options.debug_mode) syslog(LOG_DEBUG, "Returning tty '%s', display '%s'", ttyFound, dispFound); } else message("Returning status %d",status); return (status); }