File: misc.c

package info (click to toggle)
xfingerd 0.6-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 352 kB
  • ctags: 97
  • sloc: sh: 2,679; ansic: 996; makefile: 65
file content (352 lines) | stat: -rw-r--r-- 7,100 bytes parent folder | download | duplicates (4)
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
/* misc.c -- Some tricky functions. */

/* Copyright (C) 1996-1997  Janos Farkas

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   02111-1307, USA.  */


#include "config.h"

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <utmp.h>
#include <sys/stat.h>

#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "misc.h"
#include "finger.h"
#include "util.h"
#include "broken.h"

/* return a fresh string (NULL only if you pass it, but the result
   may be empty), which is P stripped from trailing and leading blanks */

char *
normalize (const char *name)
{
  const char *s;
  char *p, *norm;

  if (!name)
    return NULL;

  /* skip leading blanks */
  for (s = name; *s && isspace (*s); s++)
    /* empty */ ;

  /* strip trailing blanks */
  norm = xstrdup (s);
  for (p = norm + strlen (norm) - 1; p >= norm && isspace (*p); p--)
    *p = 0;

  return norm;
}

char *
sanitize (const char *name)
{
  char *p, *save;

  save = xstrdup (name);
  /* convert any `_' and `,' to dots (maybe more) */
  for (p = save; p && *p; p++)
    if (*p == '_' || *p == ',' || *p == '/' || *p == ' ')
      *p = '.';

   return save;
}

char *
strlwr (char *str)
{
  char *p;

  for (p = str ; *p; p++) {
    if (isupper (*p))
      *p = tolower (*p);
  }
  return str;
}

/* check for illegal characters in NAME
   currently only alphanumerics and `.-_' are allowed
   this may be too strict, but should cause rarely problems
   also note that the NAME should already be sanitized */

int
is_illegal (const char *name)
{
  const char *p;

  for (p = name; *p; p++)
    if (!isalnum (*p) && *p != '.' && *p != '-' && *p != '_')
      return 1;

  return 0;
}

/* check if the given string is empty (or NULL) */

int
is_empty (const char *name)
{
  if (name == NULL || *name == 0)
    return 1;

  return 0;
}

/* print LEVEL number of spaces */

int
blanks (int level)
{
  /* Hmm.. may be ugly, but I like it.. :) */
  if (level > 0)
    return printf ("%*s", level, "");

  return 0;
}

/* just a stub */

int
display_start (void)
{
  return 0;
}

/* finish a display_field() series, move to a new line */

int
display_end (int start_pos)
{
  if (start_pos > 0)
    printf ("\n");

  return 0;
}

/* print the fields on the left or right side of the screen,
   depending on START_POS and return the position we left
   the cursor at */

int
display_field (int start_pos, const char *name, const char *field)
{
  char *p;

  p = xmalloc (strlen (name) + strlen (field) + 1);
  strcpy (p, name);
  strcat (p, field);

  /* devote a full line for too long fields */
  if (strlen (p) >= FIELD_WIDTH)
    start_pos = display_end (start_pos);

  /* if not at the left side, then do some padding until we
     reach the middle */
  if (start_pos > 0 && start_pos < FIELD_WIDTH) {
    start_pos += blanks (FIELD_WIDTH - start_pos);
  }

  start_pos += printf ("%s", p);
  free (p);

  /* if next field wouldn't fit here then CR */
  if (start_pos >= FIELD_WIDTH) {
    printf ("\n");
    start_pos = 0;
  }

  return start_pos;
}

static int newblock = 0;
static int blocklf = 0;

/* start a new `block' (paragraph), but only mark it.
   fill_block () will start a new line if BIF is set. */

void
start_block (int bif)
{
  if (!newblock)
    blocklf = bif;
  newblock = 1;
}

void
fill_block (void)
{
  if (newblock) {
    newblock = 0;
    if (blocklf) {
      printf("\n");
    }
  }
}

void
end_block (void)
{
  newblock = 0;
}

/* format MYTIME in a `nice' way, at least legible;
   return a static buffer */

char *
nicedate (time_t *mytime)
{
  const struct tm *p;
  const char *tz;
  static char wdtab[] = {"SunMonTueWedThuFriSat"};
  static char mytab[] = {"JanFebMarAprMayJunJulAugSepOctNovDec"};
  static char datebuf[40];

  p = localtime (mytime);
  tz = tzname[p->tm_isdst];
  sprintf (datebuf, "%.3s %.3s %2d %2d:%02d:%02d %d (%s)",
           &wdtab[p->tm_wday*3], &mytab[p->tm_mon*3], p->tm_mday,
           p->tm_hour, p->tm_min, p->tm_sec,
           p->tm_year+1900, tz);
  return datebuf;
}

static void
vfputc (int ch, FILE *f)
{
  int ascii;

  ascii = 1;
  if (ch & 0x80) {
    fputc ('M', f); fputc ('-', f);
    ch &= 0x7f;
    ascii = 0;
  }
  if (isprint (ch) || (ascii && (ch == ' ' || ch == '\n' || ch == '\t')))
    fputc (ch, f);
  else {
    fputc ('^', f);
    fputc (ch != 0x7f ? ch^0x40 : '?', f);
  }
}

int
secureopen (const char *fn, uid_t uid)
{
  int fd, ok;
  struct stat sb;

  ok = 0; fd = -1;
  if (lstat(fn, &sb) == 0) {
    /* Not a link */
    if (sb.st_uid == uid && S_ISREG(sb.st_mode) && (sb.st_mode & 004)) {
      /* World readable */

      /* Simpleminded timeout, in case he tries to race into a strange file. */
      alarm (1);
      if ((fd = open (fn, O_RDONLY|O_NONBLOCK|O_NOCTTY)) >= 0) {
        alarm (0);
        /* Now recheck everything */
        if (fstat (fd, &sb) == 0) {
          if (sb.st_uid == uid && S_ISREG(sb.st_mode) && (sb.st_mode & 004)) {
	    ok = 1;
          }
        }
      }
    }
  }
  if (!ok && fd >= 0) {
    close(fd);
    fd = -1;
  }

  return fd;
}

/* display the FILE; optionally removing control characters
   (if UID (the presumed owner of the file) nonzero) */

char *basename (const char *);

int
display_file (const char *header, const char *file, uid_t uid)
{
  int fd;
  FILE *f;
  struct stat sb;
  char buf[256];
  int shown;

  shown = 0;
  if (!stat (file, &sb)) {
    fd = secureopen(file, uid);
    if (fd >= 0) {
      f = fdopen (fd, "r");
      if (f) {
        fill_block ();
        shown = 1;

        if (header && *header)
          printf ("%s", header);

        if (!uid) {
	  /* root/root file */
          while (!feof (f)) {
    	    if (fgets (buf, sizeof (buf), f))
	      fputs (buf, stdout);
          }
        } else {
          while (!feof (f)) {
    	    int c;
	    if ((c = fgetc (f)) != EOF)
	      vfputc (c, stdout);
          }
        }
        fclose (f);
      }
    } else {
      fill_block ();
      printf("Won't show `%s', user is lame.\n", basename((char *)file));
    }
  }
  return shown;
}

/* format HOSTNAME to display a login location;
   return a static buffer that is overwritten on each call */

char *
if_origin (struct fng_info *fi, const char *hostname)
{
  static char conbuf[UT_HOSTSIZE+8];

  if (fi->origin) /* XXX */
    sprintf (conbuf, " from %s", *hostname ? hostname : "console");
  else
    *conbuf = 0;
  return conbuf;
}