00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "includes.h"
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 #ifndef WITH_UTMP
00112
00113
00114
00115
00116 void sys_utmp_claim(const char *username, const char *hostname,
00117 struct in_addr *ipaddr,
00118 const char *id_str, int id_num)
00119 {}
00120
00121 void sys_utmp_yield(const char *username, const char *hostname,
00122 struct in_addr *ipaddr,
00123 const char *id_str, int id_num)
00124 {}
00125
00126 #else
00127
00128 #include <utmp.h>
00129
00130 #ifdef HAVE_UTMPX_H
00131 #include <utmpx.h>
00132 #endif
00133
00134
00135
00136 #ifdef HAVE_LASTLOG_H
00137 #include <lastlog.h>
00138 #endif
00139
00140
00141
00142
00143
00144 #ifdef HAVE_UTMPX_H
00145
00146 static const char *ux_pathname =
00147 # if defined (UTMPX_FILE)
00148 UTMPX_FILE ;
00149 # elif defined (_UTMPX_FILE)
00150 _UTMPX_FILE ;
00151 # elif defined (_PATH_UTMPX)
00152 _PATH_UTMPX ;
00153 # else
00154 "" ;
00155 # endif
00156
00157 static const char *wx_pathname =
00158 # if defined (WTMPX_FILE)
00159 WTMPX_FILE ;
00160 # elif defined (_WTMPX_FILE)
00161 _WTMPX_FILE ;
00162 # elif defined (_PATH_WTMPX)
00163 _PATH_WTMPX ;
00164 # else
00165 "" ;
00166 # endif
00167
00168 #endif
00169
00170 static const char *ut_pathname =
00171 # if defined (UTMP_FILE)
00172 UTMP_FILE ;
00173 # elif defined (_UTMP_FILE)
00174 _UTMP_FILE ;
00175 # elif defined (_PATH_UTMP)
00176 _PATH_UTMP ;
00177 # else
00178 "" ;
00179 # endif
00180
00181 static const char *wt_pathname =
00182 # if defined (WTMP_FILE)
00183 WTMP_FILE ;
00184 # elif defined (_WTMP_FILE)
00185 _WTMP_FILE ;
00186 # elif defined (_PATH_WTMP)
00187 _PATH_WTMP ;
00188 # else
00189 "" ;
00190 # endif
00191
00192
00193
00194 #ifndef HAVE_PUTUTLINE
00195 static const char *ll_pathname =
00196 # if defined (_PATH_LASTLOG)
00197 _PATH_LASTLOG ;
00198 # else
00199 "" ;
00200 # endif
00201 #endif
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 static void uw_pathname(pstring fname, const char *uw_name, const char *uw_default)
00212 {
00213 pstring dirname;
00214
00215 pstrcpy(dirname, "");
00216
00217
00218 if (uw_name[0] == 'w') {
00219 pstrcpy(dirname,lp_wtmpdir());
00220 trim_char(dirname,'\0','/');
00221 }
00222
00223
00224 if (strlen(dirname) == 0) {
00225 pstrcpy(dirname,lp_utmpdir());
00226 trim_char(dirname,'\0','/');
00227 }
00228
00229
00230 if (strlen(dirname) != 0) {
00231 pstrcpy(fname, dirname);
00232 pstrcat(fname, "/");
00233 pstrcat(fname, uw_name);
00234 return;
00235 }
00236
00237
00238 if (strlen(uw_default) == 0) {
00239
00240
00241
00242 DEBUG(2,("uw_pathname: unable to determine pathname\n"));
00243 }
00244 pstrcpy(fname, uw_default);
00245 }
00246
00247 #ifndef HAVE_PUTUTLINE
00248
00249
00250
00251
00252
00253 static void pututline_my(pstring uname, struct utmp *u, BOOL claim)
00254 {
00255 DEBUG(1,("pututline_my: not yet implemented\n"));
00256
00257 }
00258 #endif
00259
00260 #ifndef HAVE_UPDWTMP
00261
00262
00263
00264
00265
00266
00267 static void updwtmp_my(pstring wname, struct utmp *u, BOOL claim)
00268 {
00269 int fd;
00270 struct stat buf;
00271
00272 if (! claim) {
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 #if defined(HAVE_UT_UT_NAME)
00287 memset((char *)&u->ut_name, '\0', sizeof(u->ut_name));
00288 #endif
00289 #if defined(HAVE_UT_UT_HOST)
00290 memset((char *)&u->ut_host, '\0', sizeof(u->ut_host));
00291 #endif
00292 }
00293
00294
00295
00296 if ((fd = open(wname, O_WRONLY|O_APPEND, 0)) < 0)
00297 return;
00298 if (fstat(fd, &buf) == 0) {
00299 if (write(fd, (char *)u, sizeof(struct utmp)) != sizeof(struct utmp))
00300 (void) ftruncate(fd, buf.st_size);
00301 }
00302 (void) close(fd);
00303 }
00304 #endif
00305
00306
00307
00308
00309
00310 static void utmp_nox_update(struct utmp *u, BOOL claim)
00311 {
00312 pstring uname, wname;
00313 #if defined(PUTUTLINE_RETURNS_UTMP)
00314 struct utmp *urc;
00315 #endif
00316
00317 uw_pathname(uname, "utmp", ut_pathname);
00318 DEBUG(2,("utmp_nox_update: uname:%s\n", uname));
00319
00320 #ifdef HAVE_PUTUTLINE
00321 if (strlen(uname) != 0) {
00322 utmpname(uname);
00323 }
00324
00325 # if defined(PUTUTLINE_RETURNS_UTMP)
00326 setutent();
00327 urc = pututline(u);
00328 endutent();
00329 if (urc == NULL) {
00330 DEBUG(2,("utmp_nox_update: pututline() failed\n"));
00331 return;
00332 }
00333 # else
00334 setutent();
00335 pututline(u);
00336 endutent();
00337 # endif
00338
00339 #else
00340 if (strlen(uname) != 0) {
00341 pututline_my(uname, u, claim);
00342 }
00343 #endif
00344
00345 uw_pathname(wname, "wtmp", wt_pathname);
00346 DEBUG(2,("utmp_nox_update: wname:%s\n", wname));
00347 if (strlen(wname) != 0) {
00348 #ifdef HAVE_UPDWTMP
00349 updwtmp(wname, u);
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 #else
00360 updwtmp_my(wname, u, claim);
00361 #endif
00362 }
00363 }
00364
00365
00366
00367
00368
00369 static void utmp_strcpy(char *dest, const char *src, size_t n)
00370 {
00371 size_t len = 0;
00372
00373 memset(dest, '\0', n);
00374 if (src)
00375 len = strlen(src);
00376 if (len >= n) {
00377 memcpy(dest, src, n);
00378 } else {
00379 if (len)
00380 memcpy(dest, src, len);
00381 }
00382 }
00383
00384
00385
00386
00387
00388 static void sys_utmp_update(struct utmp *u, const char *hostname, BOOL claim)
00389 {
00390 #if !defined(HAVE_UTMPX_H)
00391
00392 utmp_nox_update(u, claim);
00393 #elif !defined(HAVE_PUTUTXLINE)
00394
00395 DEBUG(1,("utmp_update: have utmpx.h but no pututxline() function\n"));
00396 utmp_nox_update(u, claim);
00397 #elif !defined(HAVE_GETUTMPX)
00398
00399 DEBUG(1,("utmp_update: have utmpx.h but no getutmpx() function\n"));
00400 utmp_nox_update(u, claim);
00401 #else
00402 pstring uname, wname;
00403 struct utmpx ux, *uxrc;
00404
00405 getutmpx(u, &ux);
00406
00407 #if defined(HAVE_UX_UT_SYSLEN)
00408 if (hostname)
00409 ux.ut_syslen = strlen(hostname) + 1;
00410 else
00411 ux.ut_syslen = 0;
00412 #endif
00413 #if defined(HAVE_UT_UT_HOST)
00414 utmp_strcpy(ux.ut_host, hostname, sizeof(ux.ut_host));
00415 #endif
00416
00417 uw_pathname(uname, "utmpx", ux_pathname);
00418 uw_pathname(wname, "wtmpx", wx_pathname);
00419 DEBUG(2,("utmp_update: uname:%s wname:%s\n", uname, wname));
00420
00421
00422
00423
00424
00425
00426
00427 if ((strlen(uname) == 0) || (strlen(wname) == 0)) {
00428 utmp_nox_update(u, claim);
00429 } else {
00430 utmpxname(uname);
00431 setutxent();
00432 uxrc = pututxline(&ux);
00433 endutxent();
00434 if (uxrc == NULL) {
00435 DEBUG(2,("utmp_update: pututxline() failed\n"));
00436 return;
00437 }
00438 updwtmpx(wname, &ux);
00439 }
00440 #endif
00441 }
00442
00443 #if defined(HAVE_UT_UT_ID)
00444
00445
00446
00447
00448 static int ut_id_encode(int i, char *fourbyte)
00449 {
00450 int nbase;
00451 const char *ut_id_encstr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
00452
00453 fourbyte[0] = 'S';
00454 fourbyte[1] = 'M';
00455
00456
00457
00458
00459
00460
00461 nbase = strlen(ut_id_encstr);
00462
00463 fourbyte[3] = ut_id_encstr[i % nbase];
00464 i /= nbase;
00465 fourbyte[2] = ut_id_encstr[i % nbase];
00466 i /= nbase;
00467
00468 return(i);
00469 }
00470 #endif
00471
00472
00473
00474
00475
00476 static BOOL sys_utmp_fill(struct utmp *u,
00477 const char *username, const char *hostname,
00478 struct in_addr *ipaddr,
00479 const char *id_str, int id_num)
00480 {
00481 struct timeval timeval;
00482
00483
00484
00485
00486
00487
00488
00489 #if defined(HAVE_UT_UT_USER)
00490 utmp_strcpy(u->ut_user, username, sizeof(u->ut_user));
00491 #elif defined(HAVE_UT_UT_NAME)
00492 utmp_strcpy(u->ut_name, username, sizeof(u->ut_name));
00493 #endif
00494
00495
00496
00497
00498
00499 if (strlen(id_str) > sizeof(u->ut_line)) {
00500 DEBUG(1,("id_str [%s] is too long for %lu char utmp field\n",
00501 id_str, (unsigned long)sizeof(u->ut_line)));
00502 return False;
00503 }
00504 utmp_strcpy(u->ut_line, id_str, sizeof(u->ut_line));
00505
00506 #if defined(HAVE_UT_UT_PID)
00507 u->ut_pid = sys_getpid();
00508 #endif
00509
00510
00511
00512
00513
00514
00515
00516 #if defined(HAVE_UT_UT_TIME)
00517 GetTimeOfDay(&timeval);
00518 u->ut_time = timeval.tv_sec;
00519 #elif defined(HAVE_UT_UT_TV)
00520 GetTimeOfDay(&timeval);
00521 u->ut_tv = timeval;
00522 #else
00523 #error "with-utmp must have UT_TIME or UT_TV"
00524 #endif
00525
00526 #if defined(HAVE_UT_UT_HOST)
00527 utmp_strcpy(u->ut_host, hostname, sizeof(u->ut_host));
00528 #endif
00529 #if defined(HAVE_UT_UT_ADDR)
00530 if (ipaddr)
00531 u->ut_addr = ipaddr->s_addr;
00532
00533
00534
00535
00536 #endif
00537
00538 #if defined(HAVE_UT_UT_ID)
00539 if (ut_id_encode(id_num, u->ut_id) != 0) {
00540 DEBUG(1,("utmp_fill: cannot encode id %d\n", id_num));
00541 return False;
00542 }
00543 #endif
00544
00545 return True;
00546 }
00547
00548
00549
00550
00551
00552 void sys_utmp_yield(const char *username, const char *hostname,
00553 struct in_addr *ipaddr,
00554 const char *id_str, int id_num)
00555 {
00556 struct utmp u;
00557
00558 ZERO_STRUCT(u);
00559
00560 #if defined(HAVE_UT_UT_EXIT)
00561 u.ut_exit.e_termination = 0;
00562 u.ut_exit.e_exit = 0;
00563 #endif
00564
00565 #if defined(HAVE_UT_UT_TYPE)
00566 u.ut_type = DEAD_PROCESS;
00567 #endif
00568
00569 if (!sys_utmp_fill(&u, username, hostname, ipaddr, id_str, id_num)) return;
00570
00571 sys_utmp_update(&u, NULL, False);
00572 }
00573
00574
00575
00576
00577
00578 void sys_utmp_claim(const char *username, const char *hostname,
00579 struct in_addr *ipaddr,
00580 const char *id_str, int id_num)
00581 {
00582 struct utmp u;
00583
00584 ZERO_STRUCT(u);
00585
00586 #if defined(HAVE_UT_UT_TYPE)
00587 u.ut_type = USER_PROCESS;
00588 #endif
00589
00590 if (!sys_utmp_fill(&u, username, hostname, ipaddr, id_str, id_num)) return;
00591
00592 sys_utmp_update(&u, hostname, True);
00593 }
00594
00595 #endif