Loading...
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | --- utmpx.c.orig 2004-07-13 13:02:37.000000000 -0700 +++ utmpx.c 2004-08-05 15:25:10.000000000 -0700 @@ -50,7 +50,6 @@ #include <sys/wait.h> #include <assert.h> -#include <db.h> #include <errno.h> #include <fcntl.h> #include <stdio.h> @@ -63,6 +62,13 @@ #include <utmpx.h> #include <vis.h> +#ifdef UTMP_COMPAT +#include <utmp.h> +#include <ttyent.h> + +static void _utmp_compat(const struct utmpx *); +#endif /* UTMP_COMPAT */ + __warn_references(getlastlogx, "warning: reference to compatibility getlastlogx(); include <utmpx.h> for correct reference") __warn_references(lastlogxname, @@ -72,7 +78,6 @@ static int readonly = 0; static struct utmpx ut; static char utfile[MAXPATHLEN] = _PATH_UTMPX; -static char llfile[MAXPATHLEN] = _PATH_LASTLOGX; static struct utmpx *utmp_update(const struct utmpx *); @@ -270,6 +275,9 @@ goto fail; u = memcpy(&ut, &temp, sizeof(ut)); +#ifdef UTMP_COMPAT + _utmp_compat(u); +#endif /* UTMP_COMPAT */ fail: if (gotlock) { if (lockf(fileno(fp), F_ULOCK, (off_t)0) == -1) @@ -308,185 +316,50 @@ } -/* - * The following are extensions and not part of the X/Open spec. - */ -int -updwtmpx(const char *file, const struct utmpx *utx) -{ - int fd; - int saved_errno; - - _DIAGASSERT(file != NULL); - _DIAGASSERT(utx != NULL); - - fd = open(file, O_WRONLY|O_APPEND|O_SHLOCK); - - if (fd == -1) { - if ((fd = open(file, O_CREAT|O_WRONLY|O_EXLOCK, 0644)) == -1) - return -1; - (void)memset(&ut, 0, sizeof(ut)); - ut.ut_type = SIGNATURE; - (void)memcpy(ut.ut_user, vers, sizeof(vers)); - if (write(fd, &ut, sizeof(ut)) == -1) - goto failed; +#ifdef UTMP_COMPAT +static void +_utmp_compat(const struct utmpx *ux) +{ + struct utmp u; + int fd, slot; + struct ttyent *ttyp; + + switch (ux->ut_type) { + case INIT_PROCESS: + case LOGIN_PROCESS: + case USER_PROCESS: + break; + case DEAD_PROCESS: + logout(ux->ut_line); + return; + default: + return; } - if (write(fd, utx, sizeof(*utx)) == -1) - goto failed; - if (close(fd) == -1) - return -1; - return 0; - - failed: - saved_errno = errno; - (void) close(fd); - errno = saved_errno; - return -1; -} - - -int -utmpxname(const char *fname) -{ - size_t len; - - _DIAGASSERT(fname != NULL); - - len = strlen(fname); - - if (len >= sizeof(utfile)) - return 0; - - /* must end in x! */ - if (fname[len - 1] != 'x') - return 0; - - (void)strlcpy(utfile, fname, sizeof(utfile)); - endutxent(); - return 1; -} - - -void -getutmp(const struct utmpx *ux, struct utmp *u) -{ - - _DIAGASSERT(ux != NULL); - _DIAGASSERT(u != NULL); - - (void)memcpy(u->ut_name, ux->ut_name, sizeof(u->ut_name)); - (void)memcpy(u->ut_line, ux->ut_line, sizeof(u->ut_line)); - (void)memcpy(u->ut_host, ux->ut_host, sizeof(u->ut_host)); - u->ut_time = ux->ut_tv.tv_sec; -} - -void -getutmpx(const struct utmp *u, struct utmpx *ux) -{ - - _DIAGASSERT(ux != NULL); - _DIAGASSERT(u != NULL); - - (void)memcpy(ux->ut_name, u->ut_name, sizeof(u->ut_name)); - (void)memcpy(ux->ut_line, u->ut_line, sizeof(u->ut_line)); - (void)memcpy(ux->ut_host, u->ut_host, sizeof(u->ut_host)); - ux->ut_tv.tv_sec = u->ut_time; - ux->ut_tv.tv_usec = 0; - (void)memset(&ux->ut_ss, 0, sizeof(ux->ut_ss)); - ux->ut_pid = 0; - ux->ut_type = USER_PROCESS; - ux->ut_session = 0; - ux->ut_exit.e_termination = 0; - ux->ut_exit.e_exit = 0; -} - -int -lastlogxname(const char *fname) -{ - size_t len; - - _DIAGASSERT(fname != NULL); - - len = strlen(fname); - - if (len >= sizeof(llfile)) - return 0; - - /* must end in x! */ - if (fname[len - 1] != 'x') - return 0; - - (void)strlcpy(llfile, fname, sizeof(llfile)); - return 1; -} - -struct lastlogx * -getlastlogx(uid_t uid, struct lastlogx *ll) -{ - - return __getlastlogx13(_PATH_LASTLOGX, uid, ll); -} - -struct lastlogx * -__getlastlogx13(const char *fname, uid_t uid, struct lastlogx *ll) -{ - DBT key, data; - DB *db; - - _DIAGASSERT(fname != NULL); - _DIAGASSERT(ll != NULL); - - db = dbopen(fname, O_RDONLY|O_SHLOCK, 0, DB_HASH, NULL); - - if (db == NULL) - return NULL; - - key.data = &uid; - key.size = sizeof(uid); - - if ((db->get)(db, &key, &data, 0) != 0) - goto error; - - if (data.size != sizeof(*ll)) { - errno = EFTYPE; - goto error; + /* do equivalent of ttyslot(), but using ux->ut_slot */ + setttyent(); + slot = 1; + for(;;) { + if ((ttyp = getttyent()) == NULL) { + endttyent(); + return; + } + if (!strcmp(ttyp->ty_name, ux->ut_line)) { + endttyent(); + break; + } + slot++; } - if (ll == NULL) - if ((ll = malloc(sizeof(*ll))) == NULL) - goto done; - - (void)memcpy(ll, data.data, sizeof(*ll)); - goto done; -error: - ll = NULL; -done: - (db->close)(db); - return ll; -} - -int -updlastlogx(const char *fname, uid_t uid, struct lastlogx *ll) -{ - DBT key, data; - int error = 0; - DB *db; - - _DIAGASSERT(fname != NULL); - _DIAGASSERT(ll != NULL); - - db = dbopen(fname, O_RDWR|O_CREAT|O_EXLOCK, 0, DB_HASH, NULL); - - if (db == NULL) - return -1; - - key.data = &uid; - key.size = sizeof(uid); - data.data = ll; - data.size = sizeof(*ll); - if ((db->put)(db, &key, &data, 0) != 0) - error = -1; - - (db->close)(db); - return error; + /* now write utmp */ + (void)memset(&u, 0, sizeof(u)); + strncpy(u.ut_line, ux->ut_line, UT_LINESIZE); + strncpy(u.ut_name, ux->ut_user, UT_NAMESIZE); + strncpy(u.ut_host, ux->ut_host, UT_HOSTSIZE); + u.ut_time = ux->ut_tv.tv_sec; + if ((fd = open(_PATH_UTMP, O_WRONLY|O_CREAT, 0644)) >= 0) { + (void)lseek(fd, (off_t)(slot * sizeof(struct utmp)), L_SET); + (void)write(fd, &u, sizeof(struct utmp)); + (void)close(fd); + } } +#endif /* UTMP_COMPAT */ |