static int show_session(struct db_record *rec, void *private_data) { bool *parseable = (bool *)private_data; struct sessionid sessionid; if (rec->value.dsize != sizeof(sessionid)) return 0; memcpy(&sessionid, rec->value.dptr, sizeof(sessionid)); if (!process_exists(sessionid.pid)) { return 0; } if (*parseable) { d_printf("%s\\%s\\%s\\%s\\%s\n", procid_str_static(&sessionid.pid), uidtoname(sessionid.uid), gidtoname(sessionid.gid), sessionid.remote_machine, sessionid.hostname); } else { d_printf("%7s %-12s %-12s %-12s (%s)\n", procid_str_static(&sessionid.pid), uidtoname(sessionid.uid), gidtoname(sessionid.gid), sessionid.remote_machine, sessionid.hostname); } return 0; }
char *alloc_sub_specified(const char *input_string, const char *username, const char *domain, uid_t uid, gid_t gid) { char *a_string, *ret_string; char *b, *p, *s; a_string = SMB_STRDUP(input_string); if (a_string == NULL) { DEBUG(0, ("alloc_sub_specified: Out of memory!\n")); return NULL; } for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) { b = a_string; switch (*(p+1)) { case 'U' : a_string = realloc_string_sub(a_string, "%U", username); break; case 'u' : a_string = realloc_string_sub(a_string, "%u", username); break; case 'G' : if (gid != -1) { a_string = realloc_string_sub(a_string, "%G", gidtoname(gid)); } else { a_string = realloc_string_sub(a_string, "%G", "NO_GROUP"); } break; case 'g' : if (gid != -1) { a_string = realloc_string_sub(a_string, "%g", gidtoname(gid)); } else { a_string = realloc_string_sub(a_string, "%g", "NO_GROUP"); } break; case 'D' : a_string = realloc_string_sub(a_string, "%D", domain); break; case 'N' : a_string = realloc_string_sub(a_string, "%N", automount_server(username)); break; default: break; } p++; if (a_string == NULL) { return NULL; } } ret_string = alloc_sub_basic(username, a_string); SAFE_FREE(a_string); return ret_string; }
static int fsdirstat(char *path, int dev, Dir *d) { int fd; struct stat st; if(stat(path, &st) < 0 && lstat(path, &st) < 0) return -1; d->name = lastelem(path); d->uid = uidtoname(st.st_uid); d->gid = gidtoname(st.st_gid); d->muid = ""; d->qid = fsqid(&st); d->mode = (d->qid.type<<24) | (st.st_mode&0777); d->atime = st.st_atime; d->mtime = st.st_mtime; d->length = st.st_size; if(S_ISBLK(st.st_mode) && (fd = open(path, O_RDONLY)) >= 0){ d->length = disksize(fd, &st); close(fd); } // devmnt leaves 1-9 unused so that we can steal them. // it is easier for all involved if #Z shows M as the file type instead of Z. // dev is c->dev, either 1 (#Z) or 2 (#Zplan9). d->type = 'M'; d->dev = dev; return 0; }
static void print_map_entry (const GROUP_MAP *map, bool long_list) { if (!long_list) d_printf("%s (%s) -> %s\n", map->nt_name, sid_string_tos(&map->sid), gidtoname(map->gid)); else { d_printf("%s\n", map->nt_name); d_printf(_("\tSID : %s\n"), sid_string_tos(&map->sid)); d_printf(_("\tUnix gid : %u\n"), (unsigned int)map->gid); d_printf(_("\tUnix group: %s\n"), gidtoname(map->gid)); d_printf(_("\tGroup type: %s\n"), sid_type_lookup(map->sid_name_use)); d_printf(_("\tComment : %s\n"), map->comment); } }
static int traverse_sessionid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state) { struct sessionid sessionid; fstring uid_str, gid_str; if (dbuf.dsize != sizeof(sessionid)) return 0; memcpy(&sessionid, dbuf.dptr, sizeof(sessionid)); if (!process_exists_by_pid(sessionid.pid) || !Ucrit_checkUid(sessionid.uid)) { return 0; } Ucrit_addPid( sessionid.pid ); fstr_sprintf(uid_str, "%d", sessionid.uid); fstr_sprintf(gid_str, "%d", sessionid.gid); d_printf("%5d %-12s %-12s %-12s (%s)\n", (int)sessionid.pid, numeric_only ? uid_str : uidtoname(sessionid.uid), numeric_only ? gid_str : gidtoname(sessionid.gid), sessionid.remote_machine, sessionid.hostname); return 0; }
static int traverse_sessionid(struct db_record *db, void *state) { struct sessionid sessionid; fstring uid_str, gid_str; if (db->value.dsize != sizeof(sessionid)) return 0; memcpy(&sessionid, db->value.dptr, sizeof(sessionid)); if (!process_exists(sessionid.pid) || !Ucrit_checkUid(sessionid.uid)) { return 0; } Ucrit_addPid( sessionid.pid ); fstr_sprintf(uid_str, "%d", sessionid.uid); fstr_sprintf(gid_str, "%d", sessionid.gid); d_printf("%-7s %-12s %-12s %-12s (%s)\n", procid_str_static(&sessionid.pid), numeric_only ? uid_str : uidtoname(sessionid.uid), numeric_only ? gid_str : gidtoname(sessionid.gid), sessionid.remote_machine, sessionid.hostname); return 0; }
static void standard_sub_advanced(int snum, const char *user, const char *connectpath, gid_t gid, const char *smb_name, char *str, size_t len) { char *p, *s, *home; for (s=str; (p=strchr_m(s, '%'));s=p) { int l = (int)len - (int)(p-str); if (l < 0) l = 0; switch (*(p+1)) { case 'N' : string_sub(p,"%N", automount_server(user),l); break; case 'H': if ((home = get_user_home_dir(user))) string_sub(p,"%H",home, l); else p += 2; break; case 'P': string_sub(p,"%P", connectpath, l); break; case 'S': string_sub(p,"%S", lp_servicename(snum), l); break; case 'g': string_sub(p,"%g", gidtoname(gid), l); break; case 'u': string_sub(p,"%u", user, l); break; /* Patch from [email protected] Left the %N (NIS * server name) in standard_sub_basic as it is * a feature for logon servers, hence uses the * username. The %p (NIS server path) code is * here as it is used instead of the default * "path =" string in [homes] and so needs the * service name, not the username. */ case 'p': string_sub(p,"%p", automount_path(lp_servicename(snum)), l); break; case '\0': p++; break; /* don't run off the end of the string */ default: p+=2; break; } } standard_sub_basic(smb_name, str, len); }
/* traversal fn for showing share connections */ static int traverse_fn3(const struct connections_key *key, const struct connections_data *crec, void *private_data) { if (crec->cnum == -1 || !process_exists(crec->pid)) return 0; printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", crec->servicename, uidtoname(crec->uid), gidtoname(crec->gid),procid_str_static(&crec->pid), crec->machine, tstring(talloc_tos(),crec->start)); return 0; }
void xml_fillinfo(struct _info *ent) { #ifdef __USE_FILE_OFFSET64 if (inodeflag) fprintf(outfile," inode=\"%lld\"",(long long)ent->inode); #else if (inodeflag) fprintf(outfile," inode=\"%ld\"",(long int)ent->inode); #endif if (devflag) fprintf(outfile, " dev=\"%d\"", (int)ent->dev); #ifdef __EMX__ if (pflag) fprintf(outfile, " mode=\"%04o\" prot=\"%s\"",ent->attr, prot(ent->attr)); #else if (pflag) fprintf(outfile, " mode=\"%04o\" prot=\"%s\"", ent->mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX), prot(ent->mode)); #endif if (uflag) fprintf(outfile, " user=\"%s\"", uidtoname(ent->uid)); if (gflag) fprintf(outfile, " group=\"%s\"", gidtoname(ent->gid)); if (sflag) fprintf(outfile, " size=\"%lld\"", (long long int)(ent->size)); if (Dflag) fprintf(outfile, " time=\"%s\"", do_date(cflag? ent->ctime : ent->mtime)); }
/* traversal fn for showing share connections */ static int traverse_fn3(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state) { struct connections_data crec; if (dbuf.dsize != sizeof(crec)) return 0; memcpy(&crec, dbuf.dptr, sizeof(crec)); if (crec.cnum == -1 || !process_exists(crec.pid)) return 0; printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%s</td></tr>\n", crec.name,uidtoname(crec.uid), gidtoname(crec.gid),(int)crec.pid, crec.machine, tstring(crec.start)); return 0; }
static int traverse_processes(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state) { struct sessionid sessionid; struct process_id pid; if (dbuf.dsize != sizeof(sessionid)) return 0; memcpy(&sessionid, dbuf.dptr, sizeof(sessionid)); pid = pid_to_procid((pid_t)sessionid.pid); if (!process_exists(pid)) { return 0; } Ucrit_addPid( sessionid.pid ); d_printf("%5d %-12s %-12s", (int)sessionid.pid, uidtoname(sessionid.uid), gidtoname(sessionid.gid)); do_collect_usrstat(pid); return 0; }
static int traverse_sessionid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state) { struct sessionid sessionid; if (dbuf.dsize != sizeof(sessionid)) return 0; memcpy(&sessionid, dbuf.dptr, sizeof(sessionid)); if (!process_exists(sessionid.pid) || !Ucrit_checkUsername(uidtoname(sessionid.uid))) { return 0; } Ucrit_addPid( sessionid.pid ); d_printf("%5d %-12s %-12s %-12s (%s)\n", (int)sessionid.pid, uidtoname(sessionid.uid), gidtoname(sessionid.gid), sessionid.remote_machine, sessionid.hostname); return 0; }
char *fillinfo(char *buf, struct _info *ent) { int n; buf[n=0] = 0; #ifdef __USE_FILE_OFFSET64 if (inodeflag) n += sprintf(buf," %7lld",(long long)ent->inode); #else if (inodeflag) n += sprintf(buf," %7ld",(long int)ent->inode); #endif if (devflag) n += sprintf(buf+n, " %3d", (int)ent->dev); #ifdef __EMX__ if (pflag) n += sprintf(buf+n, " %s",prot(ent->attr)); #else if (pflag) n += sprintf(buf+n, " %s", prot(ent->mode)); #endif if (uflag) n += sprintf(buf+n, " %-8.32s", uidtoname(ent->uid)); if (gflag) n += sprintf(buf+n, " %-8.32s", gidtoname(ent->gid)); if (sflag) n += psize(buf+n,ent->size); if (Dflag) n += sprintf(buf+n, " %s", do_date(cflag? ent->ctime : ent->mtime)); return buf; }
static int traverse_sessionid(const char *key, struct sessionid *session, void *private_data) { fstring uid_str, gid_str; if (!process_exists(session->pid) || !Ucrit_checkUid(session->uid)) { return 0; } Ucrit_addPid(session->pid); fstr_sprintf(uid_str, "%u", (unsigned int)session->uid); fstr_sprintf(gid_str, "%u", (unsigned int)session->gid); d_printf("%-7s %-12s %-12s %-12s (%s)\n", procid_str_static(&session->pid), numeric_only ? uid_str : uidtoname(session->uid), numeric_only ? gid_str : gidtoname(session->gid), session->remote_machine, session->hostname); return 0; }
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); }
void standard_sub_basic(const char *smb_name, char *str,size_t len) { char *p, *s; fstring pidstr; struct passwd *pass; const char *local_machine_name = get_local_machine_name(); for (s=str; (p=strchr_m(s, '%'));s=p) { fstring tmp_str; int l = (int)len - (int)(p-str); if (l < 0) l = 0; switch (*(p+1)) { case 'U' : fstrcpy(tmp_str, smb_name); strlower_m(tmp_str); string_sub(p,"%U",tmp_str,l); break; case 'G' : fstrcpy(tmp_str, smb_name); if ((pass = Get_Pwnam(tmp_str))!=NULL) { string_sub(p,"%G",gidtoname(pass->pw_gid),l); } else { p += 2; } break; case 'D' : fstrcpy(tmp_str, current_user_info.domain); strupper_m(tmp_str); string_sub(p,"%D", tmp_str,l); break; case 'I' : string_sub(p,"%I", client_addr(),l); break; case 'i' : string_sub(p,"%i", client_socket_addr(),l); break; case 'L' : if (local_machine_name && *local_machine_name) string_sub(p,"%L", local_machine_name,l); else { pstring temp_name; pstrcpy(temp_name, global_myname()); strlower_m(temp_name); string_sub(p,"%L", temp_name,l); } break; case 'M' : string_sub(p,"%M", client_name(),l); break; case 'R' : string_sub(p,"%R", remote_proto,l); break; case 'T' : string_sub(p,"%T", timestring(False),l); break; case 'a' : string_sub(p,"%a", remote_arch,l); break; case 'd' : slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid()); string_sub(p,"%d", pidstr,l); break; case 'h' : string_sub(p,"%h", myhostname(),l); break; case 'm' : string_sub(p,"%m", get_remote_machine_name(),l); break; case 'v' : string_sub(p,"%v", SAMBA_VERSION_STRING,l); break; case '$' : p += expand_env_var(p,l); break; /* Expand environment variables */ case '\0': p++; break; /* don't run off the end of the string */ default: p+=2; break; } } }
static int traverse_sessionid(const char *key, struct sessionid *session, void *private_data) { TALLOC_CTX *mem_ctx = (TALLOC_CTX *)private_data; fstring uid_str, gid_str; struct server_id_buf tmp; char *machine_hostname = NULL; int result = 0; const char *encryption = "-"; const char *signing = "-"; if (do_checks && (!process_exists(session->pid) || !Ucrit_checkUid(session->uid))) { return 0; } Ucrit_addPid(session->pid); fstrcpy(uid_str, "-1"); if (session->uid != -1) { if (numeric_only) { fstr_sprintf(uid_str, "%u", (unsigned int)session->uid); } else { fstrcpy(uid_str, uidtoname(session->uid)); } } fstrcpy(gid_str, "-1"); if (session->gid != -1) { if (numeric_only) { fstr_sprintf(gid_str, "%u", (unsigned int)session->gid); } else { fstrcpy(gid_str, gidtoname(session->gid)); } } machine_hostname = talloc_asprintf(mem_ctx, "%s (%s)", session->remote_machine, session->hostname); if (machine_hostname == NULL) { return -1; } if (smbXsrv_is_encrypted(session->encryption_flags)) { switch (session->cipher) { case SMB2_ENCRYPTION_AES128_CCM: encryption = "AES-128-CCM"; break; case SMB2_ENCRYPTION_AES128_GCM: encryption = "AES-128-GCM"; break; default: encryption = "???"; result = -1; break; } } else if (smbXsrv_is_partially_encrypted(session->encryption_flags)) { switch (session->cipher) { case SMB_ENCRYPTION_GSSAPI: encryption = "partial(GSSAPI)"; break; case SMB2_ENCRYPTION_AES128_CCM: encryption = "partial(AES-128-CCM)"; break; case SMB2_ENCRYPTION_AES128_GCM: encryption = "partial(AES-128-GCM)"; break; default: encryption = "???"; result = -1; break; } } if (smbXsrv_is_signed(session->signing_flags)) { if (session->connection_dialect >= SMB3_DIALECT_REVISION_302) { signing = "AES-128-CMAC"; } else if (session->connection_dialect >= SMB2_DIALECT_REVISION_202) { signing = "HMAC-SHA256"; } else { signing = "HMAC-MD5"; } } else if (smbXsrv_is_partially_signed(session->signing_flags)) { if (session->connection_dialect >= SMB3_DIALECT_REVISION_302) { signing = "partial(AES-128-CMAC)"; } else if (session->connection_dialect >= SMB2_DIALECT_REVISION_202) { signing = "partial(HMAC-SHA256)"; } else { signing = "partial(HMAC-MD5)"; } } d_printf("%-7s %-12s %-12s %-41s %-17s %-20s %-21s\n", server_id_str_buf(session->pid, &tmp), uid_str, gid_str, machine_hostname, session_dialect_str(session->connection_dialect), encryption, signing); TALLOC_FREE(machine_hostname); return result; }
char *alloc_sub_advanced(int snum, const char *user, const char *connectpath, gid_t gid, const char *smb_name, const char *str) { char *a_string, *ret_string; char *b, *p, *s, *t, *h; a_string = strdup(str); if (a_string == NULL) { DEBUG(0, ("alloc_sub_specified: Out of memory!\n")); return NULL; } for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) { b = t = a_string; switch (*(p+1)) { case 'N' : t = realloc_string_sub(t, "%N", automount_server(user)); break; case 'H': if ((h = get_user_home_dir(user))) t = realloc_string_sub(t, "%H", h); break; case 'P': t = realloc_string_sub(t, "%P", connectpath); break; case 'S': t = realloc_string_sub(t, "%S", lp_servicename(snum)); break; case 'g': t = realloc_string_sub(t, "%g", gidtoname(gid)); break; case 'u': t = realloc_string_sub(t, "%u", user); break; /* Patch from [email protected] Left the %N (NIS * server name) in standard_sub_basic as it is * a feature for logon servers, hence uses the * username. The %p (NIS server path) code is * here as it is used instead of the default * "path =" string in [homes] and so needs the * service name, not the username. */ case 'p': t = realloc_string_sub(t, "%p", automount_path(lp_servicename(snum))); break; default: break; } p++; if (t == NULL) { SAFE_FREE(a_string); return NULL; } a_string = t; } ret_string = alloc_sub_basic(smb_name, a_string); SAFE_FREE(a_string); return ret_string; }
char *alloc_sub_basic(const char *smb_name, const char *str) { char *b, *p, *s, *t, *r, *a_string; fstring pidstr; struct passwd *pass; const char *local_machine_name = get_local_machine_name(); /* workaround to prevent a crash while lookinf at bug #687 */ if ( !str ) { DEBUG(0,("alloc_sub_basic: NULL source string! This should not happen\n")); return NULL; } a_string = strdup(str); if (a_string == NULL) { DEBUG(0, ("alloc_sub_specified: Out of memory!\n")); return NULL; } for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) { r = NULL; b = t = a_string; switch (*(p+1)) { case 'U' : r = strdup_lower(smb_name); if (r == NULL) goto error; t = realloc_string_sub(t, "%U", r); break; case 'G' : r = strdup(smb_name); if (r == NULL) goto error; if ((pass = Get_Pwnam(r))!=NULL) { t = realloc_string_sub(t, "%G", gidtoname(pass->pw_gid)); } break; case 'D' : r = strdup_upper(current_user_info.domain); if (r == NULL) goto error; t = realloc_string_sub(t, "%D", r); break; case 'I' : t = realloc_string_sub(t, "%I", client_addr()); break; case 'L' : if (local_machine_name && *local_machine_name) t = realloc_string_sub(t, "%L", local_machine_name); else t = realloc_string_sub(t, "%L", global_myname()); break; case 'N': t = realloc_string_sub(t, "%N", automount_server(smb_name)); break; case 'M' : t = realloc_string_sub(t, "%M", client_name()); break; case 'R' : t = realloc_string_sub(t, "%R", remote_proto); break; case 'T' : t = realloc_string_sub(t, "%T", timestring(False)); break; case 'a' : t = realloc_string_sub(t, "%a", remote_arch); break; case 'd' : slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid()); t = realloc_string_sub(t, "%d", pidstr); break; case 'h' : t = realloc_string_sub(t, "%h", myhostname()); break; case 'm' : t = realloc_string_sub(t, "%m", remote_machine); break; case 'v' : t = realloc_string_sub(t, "%v", SAMBA_VERSION_STRING); break; case '$' : t = realloc_expand_env_var(t, p); /* Expand environment variables */ break; default: break; } p++; SAFE_FREE(r); if (t == NULL) goto error; a_string = t; } return a_string; error: SAFE_FREE(a_string); return NULL; }