-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyfinger.c
685 lines (622 loc) · 23.1 KB
/
myfinger.c
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
#include "myfinger.h"
// The four possible finger options
bool l_option = false;
bool m_option = false;
bool s_option = false;
bool p_option = false;
// Flag needed to print the header in the short format
bool header = false;
int main(int argc, char** argv) {
int opt;
char** names = malloc(sizeof(char*)); // The array names will contain the names given in input
int names_count = 0; // Index and counter for the array names
while ((opt = getopt(argc, argv, "lmsp")) != -1) {
switch (opt) {
case 'l':
// Handle l option
l_option = true;
break;
case 'm':
// Handle m option
m_option = true;\
break;
case 's':
// Handle s option
s_option = true;
break;
case 'p':
// Handle p option
p_option = true;
break;
case '?':
// Handle unknown options error
fprintf(stderr, "usage: myfinger [-lmps] [login ...]\n");
return 1;
default:
// Handle other errors
abort();
}
}
// Filling the names array
for(int i = optind; i < argc; ++i){
bool try = false;
// Checking if the current name is already present in the array
for(int j = 0; j < names_count; ++j){
if(strcmp(names[j], argv[i]) == 0){
try = true;
}
}
// If the name is already present, skip the cycle
if(try){
continue;
}
// If it is not, the array gets resized and the name gets added
names = (char**)realloc(names, (names_count + 1) * sizeof(char*));
if (names == NULL) {
fprintf(stderr, "An error occurred during the memory allocation for the names array.\n");
return 1;
}
names[names_count++] = argv[i];
}
/* possible cases:
- no names: look for active users
-s default
-m no effect
-l if specified
-p if specified and -l is true
- with names: look for the specified users
-l default
-m if speciified
-s if specified
-p if specified and -s is false (or -l && -s == true)
*/
if (names_count == 0){
handle_active_users();
} else {
handle_specified_users(names, names_count);
}
return 0;
}
/*
* This function look for the current active users,
* then proceeds to call the core function over them
* If no format option is specified, it defaults to short
*/
void handle_active_users(){
s_option = true;
struct utmp* ut;
setutent(); // Set the pointer at the beginning of the utmp file
char encounteredUsers[MAX_USERS][MAX_NAME_LENGTH]; // Array to save the encountered users
int numEncounteredUsers = 0; // Index of the array
while ((ut = getutent()) != NULL) { // Iterating over the active entites
if (ut->ut_type == USER_PROCESS) { // Checking if the entity is an user process
bool userEncountered = false;
char user[UT_NAMESIZE];
strncpy(user, ut->ut_user, UT_NAMESIZE);
for (int i = 0; i < numEncounteredUsers; ++i) { // Checking if the user is already
if (strcmp(encounteredUsers[i], user) == 0) { // present in the array
userEncountered = true;
break;
}
}
if (!userEncountered) { // If the user is a new user, print its finger
strncpy(encounteredUsers[numEncounteredUsers], user, UT_NAMESIZE);
++numEncounteredUsers;
userEncountered = false;
}
}
}
endutent();
for (int i = 0; i < numEncounteredUsers; ++i) {
lookup_user_info(encounteredUsers[i], NULL);
}
}
/*
* This function calls the core function over all the names given by the user prompt
* If no format option is specified, it defaults to long
*/
void handle_specified_users(char** names, int size){
char** copies = malloc(sizeof(char*)*size+2); // Creating an array of copies
if (copies == NULL) {
fprintf(stderr, "Error allocating memory for adding a name\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < size; i++) {
lookup_user_info(names[i], copies);
}
free(names);
free(copies);
}
/*
* This is the core function of the program
* This function looks for the informations about the users firstly in the PASSWD file, then in the
* UTMP file
*/
void lookup_user_info(const char* user, char** copies) {
struct passwd *pw;
setpwent();
// Function-global boolean that is false when no entry
bool user_found = false; // in the passwd is found matching the user's username
// It is used to print the "no such user" label
while((pw = getpwent()) != NULL) { // Iterating over every entry of the passwd file
char* gecos = strdup(pw->pw_gecos);
char* name = strsep(&gecos, ",");
bool passwd_found = false;
if(strcmp(user, pw->pw_name) == 0) { // Check if the current passwd entry perfectly
passwd_found = true; // matches the user's unique username
}
if(!m_option) { // If the m option is false then proceeds to look
char* singleName; // for the user's username in the first gecos field
if(strcasecmp(user, name) == 0) {
passwd_found = true;
}
while((singleName = strsep(&name, " "))) {
if(strcasecmp(user, singleName) == 0) {
passwd_found = true;
}
}
}
// If at least one comparision was successful, then look for the user's utmp/wtmp informations
if(passwd_found) {
passwd_found = false;
user_found = true; // This sets true confirms (out of the while) that the user has
// been found
if(check_presence(pw->pw_name, copies)) { // If the name has already been printed
continue; // skip the cycle
}
if(!s_option || (l_option && s_option)) { // Print the first section of the finger output
print_start_l(pw); // For the long option, it is printed every user
} else {
print_start_s(); // For the short option, it is printed once
}
// Creating and allocating memory for the UserUTMP struct that will contain the information
// needed to print about the user
UserUTMP* userUTMP = malloc(sizeof(UserUTMP));
if (userUTMP == NULL) {
fprintf(stderr, "Failed to allocate memory for userUTMP\n");
exit(EXIT_FAILURE);
}
memset(userUTMP, 0, sizeof(UserUTMP));
// Scanning the utmp file in search of the user
struct utmp* ut;
setutent();
bool utmp_found = false;
bool wtmp_print = true;
while ((ut = getutent()) != NULL) {
if (ut->ut_type == USER_PROCESS && strncmp(ut->ut_user, pw->pw_name, UT_NAMESIZE) == 0) {
// Filling the userUTMP struct with the needed utmp informations
userUTMP->time = ut->ut_tv.tv_sec;
strncpy(userUTMP->tty, ut->ut_line, sizeof(userUTMP->tty));
strncpy(userUTMP->host, ut->ut_host, sizeof(userUTMP->host));
if(!s_option || (l_option && s_option)) {
print_l(userUTMP, !wtmp_print);
} else {
print_s(pw, userUTMP, !wtmp_print);
}
utmp_found = true;
}
}
endutent();
// If the user is not found in the utmp file, look in the wtmp file
if (!utmp_found) {
struct utmp wt;
int wtmp_fd;
time_t last_login_time = 0;
// Trying to open the wtmp file
if ((wtmp_fd = open(WTMP_FILE, O_RDONLY)) == -1) {
perror("Error during the opening of the wtmp file");
exit(EXIT_FAILURE);
}
// Manually scanning the wtmp file (there is no library that automatize such operationn)
while (read(wtmp_fd, &wt, sizeof(struct utmp) ) == sizeof(struct utmp)) {
if (strncmp(wt.ut_name, pw->pw_name, UT_NAMESIZE) == 0 && wt.ut_type == USER_PROCESS) {
if (wt.ut_tv.tv_sec > last_login_time) {
// Filling the userUTMP struct with the needed wtmp informations
last_login_time = wt.ut_tv.tv_sec;
strncpy(userUTMP->tty, wt.ut_line, sizeof(userUTMP->tty));
strncpy(userUTMP->host, wt.ut_host, sizeof(userUTMP->host));
userUTMP->time = wt.ut_tv.tv_sec;
}
}
}
if (last_login_time != 0) {
// If the wtmp entry for the user is found, print it
if(!s_option || (l_option && s_option)) {
print_l(userUTMP, wtmp_print);
} else {
print_s(pw, userUTMP, wtmp_print);
}
// Else print the informations with the "never logged in" label
} else {
if(!s_option || (l_option && s_option)) {
print_l(NULL, false);
} else {
print_s(pw, NULL, false);
}
}
close(wtmp_fd);
}
// In case of the long format, print the end
if(!s_option || (l_option && s_option)) {
print_end_l(pw);
}
free(userUTMP);
}
}
endpwent();
if(!user_found) {
printf("myfinger: %s: no such user.\n", user);
}
}
/*
* This function checks if the current user is already been printed
*/
bool check_presence(const char* name, char** copies) {
if(copies != NULL){
int count = 0;
while(copies[count] != NULL) {
count++;
}
for (int i = 0; i < count; i++) {
if (strcmp(name, copies[i]) == 0) {
return true;
}
}
copies[count] = strdup(name);
if (copies[count] == NULL) {
fprintf(stderr, "Error duplicating the name in the check presence function\n");
exit(EXIT_FAILURE);
}
copies[count + 1] = NULL;
}
return false;
}
/*
* If the long option is enabled, this function prints the common information for the user
*/
void print_start_l(const struct passwd* pw){
// Getting the information from the gecos field
char* login = pw->pw_name;
char* gecos = strdup(pw->pw_gecos);
if(gecos != NULL){
char* name = strsep(&gecos, ",");
if(name == NULL) {
name = strdup("");
}
char* office = strsep(&gecos, ",");
if(office == NULL) {
office = strdup("");
}
char* workNumber = strsep(&gecos, ",");
if(workNumber == NULL) {
workNumber = strdup("");
} else {
workNumber = format_phone_number(workNumber);
}
char* homeNumber = strsep(&gecos, ",");
if(homeNumber == NULL) {
homeNumber = strdup("");
} else {
homeNumber = format_phone_number(homeNumber);
}
// Printing the correctly fomatted information
if(strcmp(name, "") == 0){
printf("Login: %-32s Name:\n", login);
} else {
printf("Login: %-32s Name: %s\n", login, name);
}
printf("Directory: %-28s Shell: %-23s\n", pw->pw_dir, pw->pw_shell);
char* finalString = (char*)malloc(250 * sizeof(char));
if(finalString == NULL){
fprintf(stderr, "Errore durante l'allocazione di memoria per l'utente.\n");
return;
}
if(strcmp(office, "") == 0){
strcpy(finalString, "Office Phone: ");
} else {
strcpy(finalString, "Office: ");
strcat(finalString, office);
}
if(strcmp(workNumber, "") != 0){
if(strcmp(finalString, "Office Phone: ") != 0){
strcat(finalString, ", ");
}
strcat(finalString, workNumber);
free(workNumber);
} else {
if(strcmp(finalString, "Office Phone: ") == 0){
strcpy(finalString, "");
}
}
int count = strlen(finalString);
int maxLenght = 40;
if(count > maxLenght){
strcat(finalString, "\n");
} else {
if(strcmp(finalString, "") != 0){
for(int i = 0; i < maxLenght - count; i++){
strcat(finalString, " ");
}
}
}
if(strcmp(homeNumber, "") != 0){
strcat(finalString, "Home Number: ");
strcat(finalString, homeNumber);
strcat(finalString, "\n");
free(homeNumber);
} else {
if(strcmp(finalString, "") != 0){
strcat(finalString, "\n");
}
}
printf("%s",finalString);
free(finalString);
}
}
/*
* If the long option is enabled, this function prints the information of a single login
* for the current user
*/
void print_l(UserUTMP* user, bool wtmp){
// Printing the current utmp information for the user
if(user != NULL){
time_t login_time = user->time;
char* formatted_login_time = format_time(login_time, true);
if (formatted_login_time == NULL) {
printf("Error during the login time formatting process\n");
strcpy(formatted_login_time, "");
} else {
// If the information are taken from the wtmp file, the label changes
if(!wtmp){
printf("On since %s", formatted_login_time);
} else {
printf("Last login %s", formatted_login_time);
}
free(formatted_login_time);
}
printf(" on %-5s", user->tty);
// If the information are taken from the wtmp file, there is no idle time
if(!wtmp){
char* formatted_idle_time = format_time(login_time, false);
if (formatted_idle_time != NULL) {
const char* teletype = user->tty;
if (check_write_status(teletype)){
if(strcmp(user->host, "") != 0){
printf(" from %s\n", user->host);
}
printf("%s\n", formatted_idle_time);
} else {
printf("%s\n (messages off)\n", formatted_idle_time);
}
free(formatted_idle_time);
} else {
printf("Error during the idle time formatting process\n");
}
} else {
printf("\n");
}
} else {
printf("Never logged in.\n");
}
}
/*
* If the long option is enabled, this function prints the last information for the current user
*/
void print_end_l(const struct passwd* pw){
char plan_path[1024];
snprintf(plan_path, sizeof(plan_path), "%s/.plan", pw->pw_dir);
struct stat st;
if (stat(plan_path, &st) == -1) {
printf("No Plan.\n");
return;
}
FILE *file = fopen(plan_path, "r");
if (file == NULL) {
perror("fopen");
return;
}
printf("Plan:\n");
char buffer[256];
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer);
}
fclose(file);
}
/*
* If the short option is enabled, this function prints only once the header of the myfinger program
*/
void print_start_s(){
if(!header){
printf("Login\t Name\t\t Tty\t Idle Login Time Office\t Office Phone\n");
header = true;
}
}
/*
* If the short option is enabled, this function prints the information of a single login
* for the current user
*/
void print_s(const struct passwd* pw, UserUTMP* userUTMP, bool wtmp){
// Getting the information from the gecos field
char* login = pw->pw_name;
char* gecos = strdup(pw->pw_gecos);
if(gecos != NULL){
char* name = strsep(&gecos, ",");
if(name == NULL) {
name = strdup("");
}
char* office = strsep(&gecos, ",");
if(office == NULL) {
office = strdup("");
}
char* workNumber = strsep(&gecos, ",");
if(workNumber == NULL) {
workNumber = strdup("");
} else {
workNumber = format_phone_number(workNumber);
}
// Printing the correctly fomatted information
printf("%-10.10s", login);
if (strcmp(name,"")!=0){
printf("%-17.16s", name);
} else {
printf(" ");
}
if(userUTMP != NULL){
// If the user is logged in or has logged in in the past, print the information about it
const char* teletype = userUTMP->tty;
if (check_write_status(teletype)){
printf(" %-9.10s", teletype);
} else {
printf("*%-9.9s", teletype);
}
if(!wtmp){
char* idleTime = format_short_time(userUTMP->time, false);
printf("%-6.5s", idleTime);
free(idleTime);
} else {
printf(" * ");
}
char* loginTime = format_short_time(userUTMP->time, true);
printf("%-13.12s", loginTime);
free(loginTime);
if(strcmp(userUTMP->host, "") != 0){
char* copy = strdup(userUTMP->host);
strcat(copy, ")");
printf("(%-10.9s", copy);
} else {
if(strcmp(office, "") != 0){
printf("%-11.10s", office);
} else {
printf(" ");
}
if(strcmp(workNumber, "") != 0){
printf("%-13.12s", workNumber);
}
}
printf("\n");
} else {
// Else, print the No logins label
printf(" *\t * No logins\n");
}
}
}
/*
* This function checks if write permission is denided to the device or not
*/
bool check_write_status(const char* line){
char file_path[256];
strcpy(file_path, "/dev/");
strcat(file_path, line);
struct stat file_stat;
if (stat(file_path, &file_stat) == 0) {
if (file_stat.st_mode & (S_IWOTH | S_IWGRP)) {
return true;
}
}
return false;
}
/*
* This function correctly format the time for the long option
*/
char* format_time(const time_t time_seconds, bool isLogin) {
time_t current_time = time(NULL);
double diff_seconds = difftime(current_time, time_seconds);
struct tm *time_info = localtime(&time_seconds);
int size = 100;
char* time_buffer = malloc(size * sizeof(char));
if (time_buffer == NULL) {
return NULL;
}
if (isLogin) {
if (diff_seconds < 31536000) {
// Less than a year login time string
strftime(time_buffer, 100, "%a %b %d %H:%M (%Z)", time_info);
} else {
// More than a year login time string
strftime(time_buffer, 100, "%d %b %Y", time_info);
}
} else {
if (diff_seconds < 3600) { // Less than an hour
// Prints only the minutes
int minutes = (int)(diff_seconds / 60);
snprintf(time_buffer, size, " %d minutes idle", minutes);
} else if (diff_seconds < 86400) { // Less than a day
// Prints hours and minutes
int hours = (int)(diff_seconds / 3600);
int minutes = (int)(diff_seconds / 60) % 60;
snprintf(time_buffer, size, " %d hour %d minutes idle", hours, minutes);
} else if (diff_seconds < 31536000) { // Less than a year
// Prints the days
int days = (int)(diff_seconds / 86400);
snprintf(time_buffer, size, " %d days idle", days);
} else { // More than a year
// Prints the years
int years = (int)(diff_seconds / 31536000);
snprintf(time_buffer, size, " %d years idle", years);
}
}
return time_buffer;
}
/*
* This function correctly format the time for the short option
*/
char* format_short_time(const time_t time_seconds, bool isLogin) {
time_t current_time = time(NULL);
double diff_seconds = difftime(current_time, time_seconds);
struct tm *time_info = localtime(&time_seconds);
int size = 20;
char* time_buffer = malloc(size * sizeof(char));
if (time_buffer == NULL) {
return NULL;
}
if (isLogin) {
if (diff_seconds < 31536000){
// Less than a year login time string
strftime(time_buffer, size, "%b %d %H:%M", time_info);
} else {
// More than a year login time string
strftime(time_buffer, size, "%b %Y", time_info);
}
} else {
if (diff_seconds < 3600) { // Less than an hour
// Prints only the minutes
int minutes = (int)(diff_seconds / 60);
snprintf(time_buffer, size, "%d", minutes);
} else if (diff_seconds < 86400) { // Less than a day
// Prints hours and minutes
int hours = (int)(diff_seconds / 3600);
int minutes = (int)(diff_seconds / 60) % 60;
snprintf(time_buffer, size, "%d:%02d", hours, minutes);
} else if (diff_seconds < 31536000) { // Less than a year
// Prints the days
int days = (int)(diff_seconds / 86400);
snprintf(time_buffer, size, "%dd", days);
} else { // More than a year
// Prints the years
int years = (int)(diff_seconds / 31536000);
snprintf(time_buffer, size, "%dy", years);
}
}
return time_buffer;
}
/*
* This function correctly forma the phone numbers
*/
char* format_phone_number(const char* phoneNumber){
int len = strlen(phoneNumber);
char* number_buffer = malloc(15 * sizeof(char));
if (number_buffer == NULL) {
printf("Errore nell'allocazione della memoria.\n");
return NULL;
}
if(len<=4){
snprintf(number_buffer, 15, "%s", phoneNumber);
} else if(len>4 && len <= 7){
int i = len - 4;
snprintf(number_buffer, 15, "%.*s-%.*s", i, phoneNumber, 4, phoneNumber+i);
} else if(len>7 && len <= 10){
int j = len - 7;
snprintf(number_buffer, 15, "%.*s-%.*s-%.*s", j , phoneNumber, 3, phoneNumber + j, 4, phoneNumber + j + 3);
}
return number_buffer;
}