File: account.c

package info (click to toggle)
nn 6.7.3-10
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 2,508 kB
  • ctags: 3,198
  • sloc: ansic: 32,035; sh: 1,491; awk: 138; makefile: 98
file content (595 lines) | stat: -rw-r--r-- 12,499 bytes parent folder | download | duplicates (7)
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
/*
 *	(c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
 *      Copyright (c) 1996-2005 Michael T Pins.  All rights reserved.
 *
 *	Accounting for news reading.
 *
 *	The nnacct program is called by nn in three cases:
 *
 *	- on startup (-q) to check for permission to run nn (at this time)
 *	- when the :cost command is executed (-cUSAGE -r) to
 *	  produce a "cost sofar" report, and
 *	- at exit (-uUSAGE -r) to add the USAGE to the user's account
 *	  and print a "cost" report.
 *
 *	It can also be invoked by nnusage to print a usage and cost
 *	report for the current user (default), or by the super user
 *	to produce a usage and cost report for all users.
 */

#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
#include "config.h"
#include "global.h"
#include "account.h"
#include "execute.h"
#include "options.h"
#include "proto.h"

extern char    *lib_directory;
extern int      errno;

struct account {
    off_t           ac_offset;	/* offset in acct file */
    int             ac_found;	/* present in acct file */

    char            ac_user[24];/* user name */

    long            ac_total;	/* total usage */
    time_t          ac_last;	/* last active */
    int             ac_policy;	/* assigned policy */
    int             ac_quota;	/* time quota */
};

/* account.c */

static void     put_entry(FILE * acctf, struct account * ac);
static int      get_entry(FILE * acctf, char *user, struct account * ac);
static void     do_cost(struct account * ac, int ses);
static void     do_report(struct account * ac, int hdr);
static void     do_report_all(FILE * acctf);
static void     do_zero(void);
static int      news_admin(char *caller);

/*
 * 	local authorization policy checking
 *
 *	return/exit values of policy_check (nnacct -P0) are:
 *
 *	0: access granted
 *	1: access granted, but cannot post
 *	2: access denied (not authorized)
 *	3: access denied (not allowed at this time of day)
 *	4: access denied (quota exceeded)
 */

#ifdef AUTHORIZE
#include <time.h>

static int
holiday(struct tm * tm)
{
    /* Kim's birthday - 23 April */
    if (tm->tm_mon == 3 && tm->tm_mday == 23)
	return 1;
    /* another birthday - 25 December :-) */
    if (tm->tm_mon == 11 && tm->tm_mday == 25)
	return 1;
    /* ... */
    return 0;
}

static int
policy_check(int policy)
{
    struct tm      *tm, *localtime();
    time_t          t;
    int             no_post = 0;

    if (policy >= NO_POST) {
	policy -= NO_POST;
	no_post = 1;
    }
    switch (policy % 10) {
	case DENY_ACCESS:
	    return 2;

	case ALL_HOURS:
	case FREE_ACCOUNT:
	    break;

	case OFF_HOURS:	/* adapt this to your local requirements */
	    time(&t);
	    tm = localtime(&t);
	    if (tm->tm_wday == 0)
		break;		/* Sunday */
	    if (tm->tm_wday == 6)
		break;		/* Saturday */
	    if (tm->tm_hour < 9)
		break;		/* morning */
	    if (tm->tm_hour > 16)
		break;		/* evening */
	    if (holiday(tm))
		break;		/* holidays */
	    /* etc. */
	    return 3;

	default:
	    return 2;
    }

    return no_post;
}

#endif

static int      add_usage = -1;
static int      show_cost = -1;
static int      report = 0;
static int      report_all = 0;
static int      quiet = 0;
static char    *acct_file = NULL;
static int      ck_policy = -1;
static int      new_policy = -1;
static int      new_quota = -1;
static int      who_am_caller = I_AM_ACCT;
static char    *zero_accounts = NULL;

Option_Description(acct_options)
{
    'C', Int_Option(show_cost),
	'U', Int_Option(add_usage),
	'W', Int_Option(who_am_caller),
	'P', Int_Option(ck_policy),
	'a', Bool_Option(report_all),
	'f', String_Option(acct_file),
	'p', Int_Option(new_policy),
	'q', Int_Option(new_quota),
	'r', Bool_Option(report),
	'Q', Bool_Option(quiet),
	'Z', String_Option(zero_accounts),
	'\0',
};

/*
 *	Accounting information:
 *
 *	xxxxxxx 00000000 00000000 00 00000\n
 *
 *	login	time used last	 pol quota
 *	name	(minutes) active icy (hours)
 *
 *	See the printf/scanf formats later on.
 */


#define INPUT_FMT	"%s %ld %ld %d %d\n"
#define OUTPUT_FMT	"%s %08ld %08lx %02d %05d\n"

static int
get_entry(FILE * acctf, char *user, struct account * ac)
{
    char            line[100];

    if (acctf != NULL && user != NULL)
	rewind(acctf);

    ac->ac_found = 0;

    for (;;) {
	ac->ac_policy = DEFAULT_POLICY;
	ac->ac_last = 0;
	ac->ac_total = 0;
	ac->ac_quota = DEFAULT_QUOTA;
	ac->ac_user[0] = NUL;

	if (acctf == NULL)
	    break;

	ac->ac_offset = ftell(acctf);

	if (fgets(line, 100, acctf) == NULL)
	    break;

	sscanf(line, INPUT_FMT,
	       ac->ac_user, &ac->ac_total, &ac->ac_last,
	       &ac->ac_policy, &ac->ac_quota);

	if (user == NULL)
	    return 1;

	if (strcmp(user, ac->ac_user) == 0) {
	    ac->ac_found = 1;
	    return 1;
	}
    }

    if (user != NULL)
	strcpy(ac->ac_user, user);
    return 0;
}

static void
put_entry(FILE * acctf, struct account * ac)
{
    if (ac->ac_found)
	fseek(acctf, ac->ac_offset, 0);
    else
	fseek(acctf, (off_t) 0, 2);

    fprintf(acctf, OUTPUT_FMT,
	    ac->ac_user, ac->ac_total, ac->ac_last,
	    ac->ac_policy, ac->ac_quota);
}

static void
do_cost(struct account * ac, int ses)
{
    long            r;

#ifdef COST_PER_MINUTE

#ifndef COST_UNIT
#define COST_UNIT ""
#endif

    if (ses >= 0)
	printf("Cost this session: %ld %s   Period total:",
	       ((long) ses * COST_PER_MINUTE) / 100, COST_UNIT);
    printf("%6ld %s",
	   (ac->ac_total * COST_PER_MINUTE) / 100, COST_UNIT);
#else
    printf("Time used this session: %d:%02d   Period total: %ld:%02ld",
	   ses / 60, ses % 60, ac->ac_total / 60, ac->ac_total % 60);
#endif

    if (ses >= 0 && ac->ac_quota > 0) {
	r = ac->ac_quota - ac->ac_total / 60;
	printf("  Quota: %ld hour%s", r, plural(r));
    }
    fl;
}

static void
do_report(struct account * ac, int hdr)
{
    if (hdr) {
	printf("USER        USAGE  QUOTA  LAST_ACTIVE");

#ifdef COST_PER_MINUTE
	printf("   COST/PERIOD");
#endif

#ifdef AUTHORIZE
	printf("   POLICY");
#endif

	putchar(NL);
    }
    printf("%-8.8s  %4ld.%02ld  %5d  %-12.12s  ",
	   ac->ac_user,
	   ac->ac_total / 60, ac->ac_total % 60,
	   ac->ac_quota,
	   ac->ac_last ? date_time(ac->ac_last) : "");

#ifdef COST_PER_MINUTE
    do_cost(ac, -1);
#endif

#ifdef AUTHORIZE
    printf("     %2d  ", ac->ac_policy);
#endif

    printf("\n");
}

static void
do_report_all(FILE * acctf)
{
    struct account  ac;
    int             first = 1;

    while (get_entry(acctf, (char *) NULL, &ac)) {
	do_report(&ac, first);
	first = 0;
    }
}

static char    *ZERO_STAMP = "(Zeroed)";

static void
do_zero(void)
{
    FILE           *old, *new;
    char           *acct, bak[FILENAME];
    struct account  ac;

    acct = relative(lib_directory, "acct");
    old = open_file(acct, OPEN_READ);
    if (old == NULL)
	goto err;

    sprintf(bak, "%s.old", acct);
    if (unlink(bak) < 0 && errno != ENOENT)
	goto err;
    if (link(acct, bak) < 0)
	goto err;
    if (unlink(acct) < 0) {
	unlink(bak);
	goto err;
    }
    umask(0177);
    new = open_file(acct, OPEN_CREATE);
    if (new == NULL)
	goto err2;
    ac.ac_found = 0;
    strcpy(ac.ac_user, ZERO_STAMP);
    ac.ac_total = ac.ac_policy = ac.ac_quota = 0;
    time(&(ac.ac_last));
    put_entry(new, &ac);

    while (get_entry(old, (char *) NULL, &ac)) {
	if (strcmp(ac.ac_user, ZERO_STAMP) == 0)
	    continue;
	ac.ac_total = 0;
	ac.ac_found = 0;
	put_entry(new, &ac);
    }
    fclose(old);
    if (fclose(new) == EOF)
	goto err2;
    return;

err2:
    unlink(acct);
    if (link(bak, acct) == 0)
	unlink(bak);
err:
    fprintf(stderr, "ZERO of accounts failed -- check permissions etc.\n");
}

static
int
news_admin(char *caller)
{
    FILE           *adm;
    char            line[80];
    int             len, ok;

    adm = open_file(relative(lib_directory, "admins"), OPEN_READ);
    if (adm == NULL)
	return 2;
    len = strlen(caller);
    ok = 0;

    while (fgets(line, 80, adm)) {
	if (line[len] != NL)
	    continue;
	line[len] = NUL;
	if (strcmp(caller, line) == 0) {
	    ok = 1;
	    break;
	}
    }
    fclose(adm);
    return ok;
}

int
main(int argc, char *argv[])
{
    char           *caller;
    FILE           *acctf;
    char           *fname = NULL;
    int             users, i;
    struct account  ac, *actab = NULL;

    who_am_i = I_AM_ACCT;

    init_global();

    users = parse_options(argc, argv, (char *) NULL, acct_options, "");

    if (zero_accounts && strcmp(zero_accounts, "ERO")) {
	fprintf(stderr, "Must specify -ZERO to clear accounts\n");
	exit(1);
    }
    if (user_id != 0) {
	caller = user_name();
	if (news_admin(caller) == 1)
	    goto caller_ok;

	if (report_all) {
	    fprintf(stderr, "Only root can request complete reports\n");
	    exit(9);
	}
	if (new_policy >= 0) {
	    fprintf(stderr, "Only root can change user authorization\n");
	    exit(9);
	}
	if (new_quota >= 0) {
	    fprintf(stderr, "Only root can change user quotas\n");
	    exit(9);
	}
	if (zero_accounts) {
	    fprintf(stderr, "Only root can zero user accounts\n");
	    exit(9);
	}
	if (users > 0) {
	    fprintf(stderr, "Only root can request reports for other users\n");
	    exit(9);
	}
    } else
	caller = "root";

caller_ok:
    if ((new_policy >= 0 || new_quota >= 0) && users == 0) {
	fprintf(stderr, "usage: %s -pPOLICY -qQUOTA user...\n", argv[0]);
	exit(1);
    }
    if (add_usage == 0 && report) {
	show_cost = 0;
	add_usage = -1;
    }
    if (zero_accounts || add_usage > 0 || new_policy >= 0 || new_quota >= 0) {
	if (acct_file) {
	    fprintf(stderr, "Can only update current acct file %s\n", acct_file);
	    exit(2);
	}
	proto_lock(I_AM_ACCT, PL_SET_QUICK);
    }
    if (zero_accounts) {
	do_zero();
	proto_lock(I_AM_ACCT, PL_CLEAR);
	exit(0);
    }
    if (acct_file) {
	if ((acctf = open_file(acct_file, OPEN_READ)) == NULL)
	    acctf = open_file(relative(lib_directory, acct_file), OPEN_READ);
	if (acctf == NULL) {
	    fprintf(stderr, "Accounting file %s not found\n", acct_file);
	    if (add_usage > 0 || new_policy >= 0 || new_quota >= 0)
		proto_lock(I_AM_ACCT, PL_CLEAR);
	    exit(1);
	}
    } else {
	fname = relative(lib_directory, "acct");
	acctf = open_file(fname, OPEN_READ);
    }

    if (report_all) {
	do_report_all(acctf);
	fclose(acctf);
	exit(0);
    }
    if (ck_policy >= 0) {

#ifdef AUTHORIZE
	get_entry(acctf, caller, &ac);

#ifdef ACCOUNTING
	if (ac.ac_quota > 0 && ac.ac_quota < ac.ac_total / 60)
	    exit(4);
#endif

	exit(policy_check(ac.ac_policy));
#else
	exit(0);
#endif
    }
    if (show_cost >= 0) {
	get_entry(acctf, caller, &ac);
	if (ac.ac_policy == FREE_ACCOUNT)
	    exit(0);
	ac.ac_total += show_cost;
	do_cost(&ac, show_cost);
	exit(0);
    }
    if (add_usage > 0) {
	get_entry(acctf, caller, &ac);
	if (ac.ac_policy == FREE_ACCOUNT)
	    goto unlock;
	ac.ac_total += add_usage;
	time(&ac.ac_last);
    } else if (users > 0) {
	actab = newobj(struct account, users + 1);
	for (i = 1; i <= users; i++) {
	    get_entry(acctf, argv[i], &actab[i]);
	    if (new_policy >= 0 || new_quota >= 0) {
		if (new_policy >= 0)
		    actab[i].ac_policy = new_policy;
		if (new_quota >= 0)
		    actab[i].ac_quota = new_quota;
	    } else
		do_report(&actab[i], i == 1);
	}
    } else if (report) {
	if (get_entry(acctf, caller, &ac))
	    do_report(&ac, 1);
	exit(0);
    }
    if (acctf)
	fclose(acctf);

    if (add_usage <= 0 && new_policy < 0 && new_quota < 0)
	exit(0);

    umask(0177);
    acctf = open_file(fname, OPEN_UPDATE | MUST_EXIST);

    if (new_policy >= 0 || new_quota >= 0) {
	for (i = 1; i <= users; i++)
	    put_entry(acctf, &actab[i]);
	fclose(acctf);
	goto unlock;
    }
    if (add_usage > 0) {
	put_entry(acctf, &ac);
	if (report) {
	    do_cost(&ac, add_usage);
	}
	fclose(acctf);

#ifdef ACCTLOG
	fname = relative(lib_directory, "acctlog");
	acctf = open_file(fname, OPEN_APPEND | MUST_EXIST);
	fprintf(acctf, "%s\t%s\t%ld\n",
		caller, date_time(ac.ac_last), (long) add_usage);
	fclose(acctf);
#endif

	goto unlock;
    }
unlock:
    proto_lock(I_AM_ACCT, PL_CLEAR);
    exit(0);
    /* NOTREACHED */
}

void
nn_exit(int n)
{
    exit(n);
}

void
nn_exitmsg(int n, char *fmt,...)
{
    va_list         ap;

    va_start(ap, fmt);
    vprintf(fmt, ap);
    putchar(NL);
    va_end(ap);

    nn_exit(n);
    /* NOTREACHED */
}

/*
 * dummy routines - should never be called by nnacct
 */

int             no_update = 0;

int
set_variable(char *variable, int on, char *val_string)
{
    return 0;
}

void
msg(char *fmt,...)
{
}


#ifdef HAVE_JOBCONTROL
int
suspend_nn(void)
{
    return 0;
}

#endif