1 /*        $NetBSD: lprint.c,v 1.23 2013/01/18 22:10:31 christos Exp $ */
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *        The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)lprint.c    8.3 (Berkeley) 4/28/95";
39 #else
40 __RCSID( "$NetBSD: lprint.c,v 1.23 2013/01/18 22:10:31 christos Exp $");
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <fcntl.h>
48 #include <tzfile.h>
49 #include <db.h>
50 #include <err.h>
51 #include <pwd.h>
52 #include <errno.h>
53 #include <unistd.h>
54 #include <stdio.h>
55 #include <string.h>
56 #include <time.h>
57 #include <ctype.h>
58 #include <paths.h>
59 #include <vis.h>
60 
61 #include "utmpentry.h"
62 #include "finger.h"
63 #include "extern.h"
64 
65 #define   LINE_LEN  80
66 #define   TAB_LEN             8                   /* 8 spaces between tabs */
67 #define   _PATH_FORWARD       ".forward"
68 #define   _PATH_PLAN          ".plan"
69 #define   _PATH_PROJECT       ".project"
70 
71 static int          demi_print(const char *, int);
72 static void         lprint(PERSON *);
73 static int          show_text(const char *, const char *, const char *);
74 static void         vputc(int);
75 
76 #ifdef __SVR4
77 #define TIMEZONE(a) tzname[0]
78 #else
79 #define TIMEZONE(a) (a)->tm_zone
80 #endif
81 
82 void
lflag_print(void)83 lflag_print(void)
84 {
85           PERSON *pn;
86           int sflag, r;
87           PERSON *tmp;
88           DBT data, key;
89 
90           if (db == NULL)
91                     return;
92 
93           for (sflag = R_FIRST;; sflag = R_NEXT) {
94                     r = (*db->seq)(db, &key, &data, sflag);
95                     if (r == -1)
96                               err(1, "db seq");
97                     if (r == 1)
98                               break;
99                     memmove(&tmp, data.data, sizeof tmp);
100                     pn = tmp;
101                     if (sflag != R_FIRST)
102                               putchar('\n');
103                     lprint(pn);
104                     if (!pplan) {
105                               (void)show_text(pn->dir,
106                                   _PATH_FORWARD, "Mail forwarded to");
107                               (void)show_text(pn->dir, _PATH_PROJECT, "Project");
108                               if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
109                                         (void)printf("No Plan.\n");
110                     }
111           }
112 }
113 
114 static size_t
visify(char * buf,size_t blen,const char * str)115 visify(char *buf, size_t blen, const char *str)
116 {
117           int len = strnvisx(buf, blen, str, strlen(str), VIS_WHITE|VIS_CSTYLE);
118           if (len == -1) {
119                     buf[0] = '\0';
120                     return 0;
121           }
122           return len;
123 }
124 
125 static void
fmt_time(char * buf,size_t blen,time_t ti,time_t n)126 fmt_time(char *buf, size_t blen, time_t ti, time_t n)
127 {
128           struct tm *tp = localtime(&ti);
129           if (tp != NULL) {
130                     char *t = asctime(tp);
131                     char *tzn = TIMEZONE(tp);
132                     if (n == (time_t)-1 ||
133                         n - ti > SECSPERDAY * DAYSPERNYEAR / 2)
134                               snprintf(buf, blen, "%.16s %.4s (%s)", t, t + 20, tzn);
135                     else
136                               snprintf(buf, blen, "%.16s (%s)", t, tzn);
137           } else
138                     snprintf(buf, blen, "[*bad time 0x%llx*]", (long long)ti);
139 }
140 
141 /*
142  * idle time is tough; if have one, print a comma,
143  * then spaces to pad out the device name, then the
144  * idle time.  Follow with a comma if a remote login.
145  */
146 static int
print_idle(time_t t,int maxlen,size_t hostlen,size_t ttylen)147 print_idle(time_t t, int maxlen, size_t hostlen, size_t ttylen) {
148 
149           int cpr;
150           struct tm *delta = gmtime(&t);
151 
152           if (delta == NULL)
153                     return printf("Bad idle 0x%llx", (long long)t);
154 
155           if (delta->tm_yday == 0 && delta->tm_hour == 0 && delta->tm_min == 0)
156                     return 0;
157 
158           cpr = printf("%-*s idle ", (int)(maxlen - ttylen + 1), ",");
159           if (delta->tm_yday > 0) {
160                     cpr += printf("%d day%s ", delta->tm_yday,
161                        delta->tm_yday == 1 ? "" : "s");
162           }
163           cpr += printf("%d:%02d", delta->tm_hour, delta->tm_min);
164           if (hostlen) {
165                     putchar(',');
166                     ++cpr;
167           }
168           return cpr;
169 }
170 
171 static void
lprint(PERSON * pn)172 lprint(PERSON *pn)
173 {
174           WHERE *w;
175           int cpr, len, maxlen;
176           int oddfield;
177           char timebuf[128], ttybuf[64], hostbuf[512];
178           size_t ttylen, hostlen;
179 
180           cpr = 0;
181           /*
182            * long format --
183            *        login name
184            *        real name
185            *        home directory
186            *        shell
187            *        office, office phone, home phone if available
188            *        mail status
189            */
190           (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
191               pn->name, pn->realname, pn->dir);
192           (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
193 
194           if (gflag)
195                     goto no_gecos;
196           /*
197            * try and print office, office phone, and home phone on one line;
198            * if that fails, do line filling so it looks nice.
199            */
200 #define   OFFICE_TAG                    "Office"
201 #define   OFFICE_PHONE_TAG    "Office Phone"
202           oddfield = 0;
203           if (pn->office && pn->officephone &&
204               strlen(pn->office) + strlen(pn->officephone) +
205               sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
206                     (void)snprintf(timebuf, sizeof(timebuf), "%s: %s, %s",
207                         OFFICE_TAG, pn->office, prphone(pn->officephone));
208                     oddfield = demi_print(timebuf, oddfield);
209           } else {
210                     if (pn->office) {
211                               (void)snprintf(timebuf, sizeof(timebuf), "%s: %s",
212                                   OFFICE_TAG, pn->office);
213                               oddfield = demi_print(timebuf, oddfield);
214                     }
215                     if (pn->officephone) {
216                               (void)snprintf(timebuf, sizeof(timebuf), "%s: %s",
217                                   OFFICE_PHONE_TAG, prphone(pn->officephone));
218                               oddfield = demi_print(timebuf, oddfield);
219                     }
220           }
221           if (pn->homephone) {
222                     (void)snprintf(timebuf, sizeof(timebuf), "%s: %s", "Home Phone",
223                         prphone(pn->homephone));
224                     oddfield = demi_print(timebuf, oddfield);
225           }
226           if (oddfield)
227                     putchar('\n');
228 
229 no_gecos:
230           /*
231            * long format con't:
232            * if logged in
233            *        terminal
234            *        idle time
235            *        if messages allowed
236            *        where logged in from
237            * if not logged in
238            *        when last logged in
239            */
240           /* find out longest device name for this user for formatting */
241           for (w = pn->whead, maxlen = -1; w != NULL; w = w->next) {
242                     visify(ttybuf, sizeof(ttybuf), w->tty);
243                     if ((len = strlen(ttybuf)) > maxlen)
244                               maxlen = len;
245           }
246           /* find rest of entries for user */
247           for (w = pn->whead; w != NULL; w = w->next) {
248                     ttylen = visify(ttybuf, sizeof(ttybuf), w->tty);
249                     hostlen = visify(hostbuf, sizeof(hostbuf), w->host);
250                     switch (w->info) {
251                     case LOGGEDIN:
252                               fmt_time(timebuf, sizeof(timebuf), w->loginat, -1);
253                               cpr = printf("On since %s on %s", timebuf, ttybuf);
254 
255                               cpr += print_idle(w->idletime, maxlen, hostlen,
256                                   ttylen);
257 
258                               if (!w->writable)
259                                         cpr += printf(" (messages off)");
260                               break;
261                     case LASTLOG:
262                               if (w->loginat == 0) {
263                                         (void)printf("Never logged in.");
264                                         break;
265                               }
266                               fmt_time(timebuf, sizeof(timebuf), w->loginat, now);
267                               cpr = printf("Last login %s on %s", timebuf, ttybuf);
268                               break;
269                     }
270                     if (hostlen) {
271                               if (LINE_LEN < (cpr + 6 + hostlen))
272                                         (void)printf("\n   ");
273                               (void)printf(" from %s", hostbuf);
274                     }
275                     putchar('\n');
276           }
277           if (pn->mailrecv == -1)
278                     printf("No Mail.\n");
279           else if (pn->mailrecv > pn->mailread) {
280                     fmt_time(timebuf, sizeof(timebuf), pn->mailrecv, -1);
281                     printf("New mail received %s\n", timebuf);
282                     fmt_time(timebuf, sizeof(timebuf), pn->mailread, -1);
283                     printf("     Unread since %s\n", timebuf);
284           } else {
285                     fmt_time(timebuf, sizeof(timebuf), pn->mailread, -1);
286                     printf("Mail last read %s\n", timebuf);
287           }
288 }
289 
290 static int
demi_print(const char * str,int oddfield)291 demi_print(const char *str, int oddfield)
292 {
293           static int lenlast;
294           int lenthis, maxlen;
295 
296           lenthis = strlen(str);
297           if (oddfield) {
298                     /*
299                      * We left off on an odd number of fields.  If we haven't
300                      * crossed the midpoint of the screen, and we have room for
301                      * the next field, print it on the same line; otherwise,
302                      * print it on a new line.
303                      *
304                      * Note: we insist on having the right hand fields start
305                      * no less than 5 tabs out.
306                      */
307                     maxlen = 5 * TAB_LEN;
308                     if (maxlen < lenlast)
309                               maxlen = lenlast;
310                     if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
311                         lenthis) <= LINE_LEN) {
312                               while(lenlast < (4 * TAB_LEN)) {
313                                         putchar('\t');
314                                         lenlast += TAB_LEN;
315                               }
316                               (void)printf("\t%s\n", str);  /* force one tab */
317                     } else {
318                               (void)printf("\n%s", str);    /* go to next line */
319                               oddfield = !oddfield;         /* this'll be undone below */
320                     }
321           } else
322                     (void)printf("%s", str);
323           oddfield = !oddfield;                             /* toggle odd/even marker */
324           lenlast = lenthis;
325           return(oddfield);
326 }
327 
328 static int
show_text(const char * directory,const char * file_name,const char * header)329 show_text(const char *directory, const char *file_name, const char *header)
330 {
331           struct stat sb;
332           FILE *fp;
333           int ch, cnt, lastc;
334           char *p;
335           int fd, nr;
336 
337           lastc = 0;
338           (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
339           if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
340               sb.st_size == 0)
341                     return(0);
342 
343           /* If short enough, and no newlines, show it on a single line.*/
344           if (sb.st_size <= (off_t)(LINE_LEN - strlen(header) - 5)) {
345                     nr = read(fd, tbuf, sizeof(tbuf));
346                     if (nr <= 0) {
347                               (void)close(fd);
348                               return(0);
349                     }
350                     for (p = tbuf, cnt = nr; cnt--; ++p)
351                               if (*p == '\n')
352                                         break;
353                     if (cnt <= 1) {
354                               (void)printf("%s: ", header);
355                               for (p = tbuf, cnt = nr; cnt--; ++p)
356                                         vputc(lastc = (unsigned char)*p);
357                               if (lastc != '\n')
358                                         (void)putchar('\n');
359                               (void)close(fd);
360                               return(1);
361                     }
362                     else
363                               (void)lseek(fd, 0L, SEEK_SET);
364           }
365           if ((fp = fdopen(fd, "r")) == NULL)
366                     return(0);
367           (void)printf("%s:\n", header);
368           while ((ch = getc(fp)) != EOF)
369                     vputc(lastc = ch);
370           if (lastc != '\n')
371                     (void)putchar('\n');
372           (void)fclose(fp);
373           return(1);
374 }
375 
376 static void
vputc(int ch)377 vputc(int ch)
378 {
379           char visout[5], *s2;
380 
381           if (eightflag || isprint(ch) || isspace(ch)) {
382               (void)putchar(ch);
383               return;
384           }
385           ch = toascii(ch);
386           vis(visout, ch, VIS_SAFE|VIS_NOSLASH, 0);
387           for (s2 = visout; *s2; s2++)
388                     (void)putchar(*s2);
389 }
390