LCOV - code coverage report
Current view: top level - pkg/lib - pam_user_parser.ts Coverage Total Hit
Test: cockpit Lines: 100.0 % 62 62
Test Date: 2026-07-02 14:11:36

            Line data    Source code
       1              : /*
       2              :  * Copyright (C) 2013 Red Hat, Inc.
       3              :  * SPDX-License-Identifier: LGPL-2.1-or-later
       4              :  */
       5              : 
       6              : export interface PamCommon {
       7              :     name: string,
       8              :     password: string,
       9              :     gid: number,
      10              : }
      11              : 
      12              : export interface PasswdUserInfo extends PamCommon {
      13              :     uid: number,
      14              :     gecos: string,
      15              :     home: string,
      16              :     shell: string,
      17              : }
      18              : 
      19              : export interface EtcGroupInfo extends PamCommon {
      20              :     userlist: string[],
      21              : }
      22              : 
      23           15 : function parse_passwd_content(content: string): PasswdUserInfo[] {
      24            3 :     if (!content) {
      25            3 :         console.warn("Couldn't read /etc/passwd");
      26            3 :         return [];
      27            3 :     }
      28              : 
      29           15 :     const ret = [];
      30           15 :     const lines = content.split('\n');
      31              : 
      32           15 :     for (let i = 0; i < lines.length; i++) {
      33           15 :         if (!lines[i])
      34           15 :             continue;
      35           15 :         const column = lines[i].split(':');
      36           15 :         ret.push({
      37           15 :             name: column[0],
      38           15 :             password: column[1],
      39           15 :             uid: parseInt(column[2], 10),
      40           15 :             gid: parseInt(column[3], 10),
      41           15 :             gecos: (column[4] || '').replace(/,*$/, ''),
      42            3 :             home: column[5] || '',
      43            3 :             shell: column[6] || '',
      44           15 :         });
      45           15 :     }
      46              : 
      47           15 :     return ret;
      48           15 : }
      49              : 
      50           15 : export const etc_passwd_syntax = {
      51           15 :     parse: parse_passwd_content
      52           15 : };
      53              : 
      54           15 : function parse_group_content(content: string): EtcGroupInfo[] {
      55              :     // /etc/group file is used to set only secondary groups of users. The primary group is saved in /etc/passwd-
      56            3 :     content = (content || "").trim();
      57            3 :     if (!content) {
      58            3 :         console.warn("Couldn't read /etc/group");
      59            3 :         return [];
      60            3 :     }
      61              : 
      62           15 :     const ret = [];
      63           15 :     const lines = content.split('\n');
      64              : 
      65           15 :     for (let i = 0; i < lines.length; i++) {
      66           15 :         if (!lines[i])
      67           15 :             continue;
      68           15 :         const column = lines[i].split(':');
      69           15 :         ret.push({
      70           15 :             name: column[0],
      71           15 :             password: column[1],
      72           15 :             gid: parseInt(column[2], 10),
      73           15 :             userlist: column[3].split(','),
      74           15 :         });
      75           15 :     }
      76              : 
      77           15 :     return ret;
      78           15 : }
      79              : 
      80           15 : export const etc_group_syntax = {
      81           15 :     parse: parse_group_content
      82           15 : };
      83              : 
      84           15 : function parse_shells_content(content: string) {
      85            3 :     content = (content || "").trim();
      86            3 :     if (!content) {
      87            3 :         console.warn("Couldn't read /etc/shells");
      88            3 :         return [];
      89            3 :     }
      90              : 
      91           15 :     const lines = content.split('\n');
      92              : 
      93           15 :     return lines.filter(line => !line.includes("#") && line.trim());
      94           15 : }
      95              : 
      96           15 : export const etc_shells_syntax = {
      97           15 :     parse: parse_shells_content
      98           15 : };
        

Generated by: LCOV version 2.0-1