int main(int argc, char *argv[]) { FILE *f; pstring fname; int uid, c; static pstring servicesf = CONFIGFILE; extern char *optarg; int verbose = 0, brief =0; BOOL processes_only=False; int last_pid=0; struct session_record *ptr; TimeInit(); setup_logging(argv[0],True); charset_initialise(); DEBUGLEVEL = 0; dbf = fopen("/dev/null","w"); if (getuid() != geteuid()) { printf("smbstatus should not be run setuid\n"); return(1); } while ((c = getopt(argc, argv, "pds:u:b")) != EOF) { switch (c) { case 'b': brief = 1; break; case 'd': verbose = 1; break; case 'p': processes_only = 1; break; case 's': pstrcpy(servicesf, optarg); break; case 'u': /* added by OH */ Ucrit_addUsername(optarg); /* added by OH */ break; default: fprintf(stderr, "Usage: %s [-d] [-p] [-s configfile] [-u username]\n", *argv); /* changed by OH */ return (-1); } } get_myname(myhostname, NULL); if (!lp_load(servicesf,False)) { fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf); return (-1); } if (verbose) { printf("using configfile = %s\n", servicesf); printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL"); } pstrcpy(fname,lp_lockdir()); standard_sub_basic(fname); trim_string(fname,"","/"); pstrcat(fname,"/STATUS..LCK"); f = fopen(fname,"r"); if (!f) { printf("Couldn't open status file %s\n",fname); if (!lp_status(-1)) printf("You need to have status=yes in your smb config file\n"); return(0); } else if (verbose) { printf("Opened status file %s\n", fname); } uid = getuid(); if (!processes_only) { printf("\nSamba version %s\n",VERSION); if (brief) { printf("PID Username Machine Time logged in\n"); printf("-------------------------------------------------------------------\n"); } else { printf("Service uid gid pid machine\n"); printf("----------------------------------------------\n"); } } while (!feof(f)) { if (fread(&crec,sizeof(crec),1,f) != 1) break; if ( crec.magic == 0x280267 && process_exists(crec.pid) && Ucrit_checkUsername(uidtoname(crec.uid)) /* added by OH */ ) { if (brief) { ptr=srecs; while (ptr!=NULL) { if ((ptr->pid==crec.pid)&&(strncmp(ptr->machine,crec.machine,30)==0)) { if (ptr->start > crec.start) ptr->start=crec.start; break; } ptr=ptr->next; } if (ptr==NULL) { ptr=(struct session_record *) malloc(sizeof(struct session_record)); ptr->uid=crec.uid; ptr->pid=crec.pid; ptr->start=crec.start; strncpy(ptr->machine,crec.machine,30); ptr->machine[30]='\0'; ptr->next=srecs; srecs=ptr; } } else { Ucrit_addPid(crec.pid); /* added by OH */ if (processes_only) { if (last_pid != crec.pid) printf("%d\n",crec.pid); last_pid = crec.pid; /* XXXX we can still get repeats, have to add a sort at some time */ } else printf("%-10.10s %-8s %-8s %5d %-8s (%s) %s", crec.name,uidtoname(crec.uid),gidtoname(crec.gid),crec.pid, crec.machine,crec.addr, asctime(LocalTime(&crec.start))); } } } fclose(f); if (processes_only) exit(0); if (brief) { ptr=srecs; while (ptr!=NULL) { printf("%-8d%-10.10s%-30.30s%s",ptr->pid,uidtoname(ptr->uid),ptr->machine,asctime(LocalTime(&(ptr->start)))); ptr=ptr->next; } printf("\n"); exit(0); } printf("\n"); locking_init(1); if (share_mode_forall(print_share_mode) <= 0) printf("No locked files\n"); printf("\n"); share_status(stdout); locking_end(); return (0); }
int main(int argc, char *argv[]) { int c; static int profile_only = 0; TDB_CONTEXT *tdb; poptContext pc; struct poptOption long_options[] = { POPT_AUTOHELP {"processes", 'p', POPT_ARG_NONE, &processes_only, 'p', "Show processes only" }, {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" }, {"locks", 'L', POPT_ARG_NONE, &locks_only, 'L', "Show locks only" }, {"shares", 'S', POPT_ARG_NONE, &shares_only, 'S', "Show shares only" }, {"user", 'u', POPT_ARG_STRING, 0, 'u', "Switch to user" }, {"brief", 'b', POPT_ARG_NONE, &brief, 'b', "Be brief" }, #ifdef WITH_PROFILE {"profile", 'P', POPT_ARG_NONE, &profile_only, 'P', "Do profiling" }, #endif /* WITH_PROFILE */ {"byterange", 'B', POPT_ARG_NONE, &show_brl, 'B', "Include byte range locks"}, POPT_COMMON_SAMBA POPT_TABLEEND }; setup_logging(argv[0],True); dbf = x_stderr; if (getuid() != geteuid()) { d_printf("smbstatus should not be run setuid\n"); return(1); } pc = poptGetContext(NULL, argc, (const char **) argv, long_options, POPT_CONTEXT_KEEP_FIRST); while ((c = poptGetNextOpt(pc)) != -1) { switch (c) { case 'u': Ucrit_addUsername(poptGetOptArg(pc)); break; } } if (verbose) { d_printf("using configfile = %s\n", dyn_CONFIGFILE); } if (!lp_load(dyn_CONFIGFILE,False,False,False)) { fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE); return (-1); } if (profile_only) { return profile_dump(); } tdb = tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0); if (!tdb) { d_printf("sessionid.tdb not initialised\n"); } else { if (locks_only) goto locks; d_printf("\nSamba version %s\n",SAMBA_VERSION_STRING); d_printf("PID Username Group Machine \n"); d_printf("-------------------------------------------------------------------\n"); tdb_traverse(tdb, traverse_sessionid, NULL); tdb_close(tdb); } tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0); if (!tdb) { d_printf("%s not initialised\n", lock_path("connections.tdb")); d_printf("This is normal if an SMB client has never connected to your server.\n"); } else { if (verbose) { d_printf("Opened %s\n", lock_path("connections.tdb")); } if (brief) exit(0); d_printf("\nService pid machine Connected at\n"); d_printf("-------------------------------------------------------\n"); tdb_traverse(tdb, traverse_fn1, NULL); tdb_close(tdb); } locks: if (processes_only) exit(0); if (!shares_only) { int ret; if (!locking_init(1)) { d_printf("Can't initialise locking module - exiting\n"); exit(1); } ret = share_mode_forall(print_share_mode); if (ret == 0) { d_printf("No locked files\n"); } else if (ret == -1) { d_printf("locked file list truncated\n"); } d_printf("\n"); if (show_brl) { brl_forall(print_brl); } locking_end(); } return (0); }